今天OpenAI公司开放了最新的GPT3.5模型:gpt-3.5-turbo
,也就是目前网页版的ChatGPT使用的模型。而此前OpenAI开放的最新的模型text-davinci-003
则是基于GPT3模型构建的。并且价格十分便宜:1000 token/0.002美元
,换算成RMB约1分钱。
curl https://api.openai.com/v1/chat/completions \-H 'Content-Type: application/json' \-H 'Authorization: Bearer YOUR_API_KEY' \-d '{"model": "gpt-3.5-turbo","messages": [{"role": "user", "content": "Hello!"}]
}'
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role": "user", "content": "Hello!"}]
)print(completion.choices[0].message)
const { Configuration, OpenAIApi } = require("openai");const configuration = new Configuration({apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);const completion = await openai.createChatCompletion({model: "gpt-3.5-turbo",messages: [{role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);
URL | https://api.openai.com/v1/chat/completions |
Method | POST |
Header Key | Authorization |
Header Value | Bearer YOUR_API_KEY |
你也可以创建自己的应用程序进行请求。
API 响应示例如下所示:
{'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve','object': 'chat.completion','created': 1677649420,'model': 'gpt-3.5-turbo','usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},'choices': [{'message': {'role': 'assistant','content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},'finish_reason': 'stop','index': 0}]
}
在 Python 中,可以使用 提取助手的回复response[‘choices’][0][‘message’][‘content’]
。
模型用到了token的概念,什么是token呢?我们输入的文本经过模型编译后被标记为token。
这个token可以是一个字符,也可以是一个单词。例如a
或者apple
。
例如,字符串“ChatGPT is great!”
被编码为六个token:[“Chat”, “G”, “PT”, “ is”, “ great”, “!”]
.
令牌总数必须是低于模型的最大限制,以gpt-3.5-turbo
模型为例,token最大为4096
。
要查看 API 调用使用了多少令牌,请检查usage
API 响应中的字段(例如,response[‘usage’][‘total_tokens’]
)。
另请注意,很长的对话更有可能收到不完整的回复。例如,一段gpt-3.5-turbo
长度为 4090 个令牌的对话将在仅 6 个令牌后被截断。
输入和输出令牌都计入这些数量。例如,如果API 调用在消息输入中使用了 10 个令牌,而您在消息输出中收到了 20 个令牌,则需要支付 30 个令牌的费用。
而gpt-3.5-turbo模型,它的价格为每1000 token需要支付0.002美元,换算成RMB是 1000 token/1分钱。比目前的GPT-3.5模型便宜10倍。
另外自 2023 年 3 月 1 日起,只能微调基础 GPT-3 模型。不能对gpt-3.5-turbo模型进行微调。
[1] https://platform.openai.com/docs/api-reference/chat/create
[2] https://platform.openai.com/docs/models/gpt-3-5
[3] https://platform.openai.com/docs/guides/chat
[4] https://platform.openai.com/docs/guides/fine-tuning
[5] https://openai.com/blog/introducing-chatgpt-and-whisper-apis
下一篇:数据结构考研习题精选