惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - lodestar

一张图掌握数据存储 使用xtrabackup实现mysql定时热备份 预热篇2:从RNN到Transformmer 预热篇1:大模型训练显卡选型 macbook安装scala、hadoop、saprk环境 centos6.5 squid安装 - lodestar 一次linux服务器黑客入侵后处理 - lodestar linux上搭建svn服务器 - lodestar Windows系统下Oracle每天自动备份 - lodestar android中listView的几点总结 - lodestar 使用template method模式简化android列表页面 - lodestar andorid进度条使用 - lodestar andorid定时器应用 - lodestar Android网络应用(第一部分) - lodestar 错误数据导致java.lang.IllegalArgumentException:Unsupported configuration attributes 使用ACEGI搭建权限系统:第三部分 acegi安全框架使用:第二部分 使用ACEGI实现权限控制,第一部分 ajax实现用户名存在校验
开发篇1:使用原生api和Langchain调用大模型
lodestar · 2024-01-13 · via 博客园 - lodestar

对大模型的调用通常有以下几种方式:方式一、大模型厂商都会定义http风格的请求接口,在代码中可以直接发起http请求调用;方式二、在开发环境中使用大模型厂商提供的api;方式三、使用开发框架Langchain调用,这个就像java对数据库的调用一样,可以直接用jdbc也可以使用第三方框架,第三方框架调用会封装一些共性问题,比如参数配置,多数据库统一调用方式,连接处理,缓存处理等等,使用第三方框架调用往往会大幅提高开发效率。下面逐一说明几种调用方式

方式一: post请求调用,以openai(chatgpt)为例,demo如下,举例代码语言为python,http调用各种语言都有这个能力,个人建议还是python来做大模型相关开发,pandas对数据集合处理已经比较成熟,内存运算性能也很高,下面的例子中requests为python的requests的模块

response = requests.post(

"https://api.openai.com/v1/chat/completions",

headers=headers,

json=json_data,

)

其中head封装了在openai上注册的key

headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + openai.api_key,
}

json是一个python的字典,封装了模型名称和messages(prompt请求)

json_data = {"model": model, "messages": messages}

方式二:python使用使用大模型厂商提供的api(openai为例),首先要在开发环境中pip安装tiktoken和openai模块,openai有两个接口,1个是对话模型,1个语言模型,调用方式分别如下

pip install tiktoken openai

#调用Completion api,openai会以json返回回答

data = openai.Completion.create(

model="text-davinci-003",

prompt="牛肉面故乡在哪里",

max_tokens=1000,

temperature=0

)

#调用 chat Completion api,chat Completion api是GPT3.5开始使用的问答模型,可以使用这个模型实现一问一答

messages=[

{

"role": "user",

"content": "你好"

}

]

data = openai.ChatCompletion.create(

model="gpt-3.5-turbo",

messages = messages

)

方式三:用使用langchain调用,使用langchain自带的OpenAI类,

from langchain.llms import OpenAI

llm = OpenAI(model_name="text-davinci-003")

llm("牛肉面起源于哪个城市")

Langchain有model,Data Connection,chains,Memory,Agents,Callbacks,每个模块的使用会在下一篇说明,OpenAI类属于model模块,如下图所示,Model 模块的主要职责1个是提示词的生成,1个是解决对大模型的调用的封装,这个有点像java里面Springboot template对jdbc的封装,封装后统一了对各类模型的调用