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

推荐订阅源

Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园_首页
U
Unit 42
T
Tailwind CSS Blog
G
GRAHAM CLULEY
F
Full Disclosure
V
Vulnerabilities – Threatpost
T
Tenable Blog
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
K
Kaspersky official blog
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LINUX DO - 最新话题
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
罗磊的独立博客
S
Security Affairs
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
美团技术团队
Simon Willison's Weblog
Simon Willison's Weblog
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
V
Visual Studio Blog

博客园 - lodestar

一张图掌握数据存储 使用xtrabackup实现mysql定时热备份 预热篇2:从RNN到Transformmer 预热篇1:大模型训练显卡选型 macbook安装scala、hadoop、saprk环境 centos6.5 squid安装 一次linux服务器黑客入侵后处理 linux上搭建svn服务器 Windows系统下Oracle每天自动备份 android中listView的几点总结 使用template method模式简化android列表页面 andorid进度条使用 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的封装,封装后统一了对各类模型的调用