GPT+时代来临:OpenAI开放GPT3.5模型,1000token仅1毛钱
GPT3.5 Model API 使用指南
今天OpenAI公司开放了最新的GPT3.5模型:gpt-3.5-turbo,也就是目前网页版的ChatGPT使用的模型。而此前OpenAI开放的最新的模型text-davinci-003则是基于GPT3模型构建的。并且价格十分便宜:1000 token/0.002美元,换算成RMB约1分钱。
请求示例
Curl
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!"}] }
Python
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)
Node.js
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);
接口信息
你也可以创建自己的应用程序进行请求。
Response
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。
这个token可以是一个字符,也可以是一个单词。例如a或者apple。
例如,字符串“ChatGPT is great!”被编码为六个token:[“Chat”, “G”, “PT”, “ is”, “ great”, “!”].
令牌总数必须是低于模型的最大限制,以gpt-3.5-turbo模型为例,token最大为4096。
要查看 API 调用使用了多少令牌,请检查usageAPI 响应中的字段(例如,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模型进行微调。
Reference
[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