Recently, OpenAI
released a new model of ChatGPT
. According to OpenAI
‘s official news, this time the model API
of ChatGPT
and Whisper
have been opened, which can be used by users and integrated into their applications. ChatGPT
model released this time is GPT-3.5-turbo
. From the official website of OpenAI
, you can already find examples of the use of this model. The model released this time is much more capable than the previous open model, and the price is even lower at $0.002 per 1000 tokens
, which is 10 times cheaper than the previous model!
Without further ado, this article will simply talk about how to use the latest GPT-3.5-turbo
model. Check out the sample code from the official OpenAI
documentation. The official Python
sample code is provided, from which we can extract the content of the message to be sent.
# Note: you need to be using OpenAI Python v0.27.0 for the code below to work import openai openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"}, {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}, {"role": "user", "content": "Where was it played?"} ]
It can be found that the message body that GPT-3.5-turbo
API
needs to send has two parts: model
and messages
, and messages
can send multiple records. In the example text of message
, there are three roles. Through actual measurement, these three roles represent:
- system: It can be understood as the human setting of
AI
- user: information sent by the user
- assistant: information returned to you by the model
In this way, we know that through the above information, we know the format of the message that http
needs to send, which is a JSON
data format
{ "model":"gpt-3.5-turbo", "messages":[ {"role": "system", "content": "你是一个编程助手,能够帮我编写C#代码,并且给我提示"}, {"role": "user", "content": "你好,你叫什么名字"}, {"role": "assistant", "content": "你好,我是一个语言模型AI,没有实体名字,你可以随意称呼我。"} ] }
The interface accessed is
https://api.openai.com/v1/chat/completions
Among them, Header
part information:
- ContentType: application/json
- Authorization: Bearer your
apikey
Use API
debugging assistant postman
to test it, write your api key
in Header
, select json
for the raw
information, and send the JSON
message according to your needs. The data format returned OpenAI
is as follows
{ "id": "chatcmpl-6pWU3qzNuTBLU7U0tUw6NqaQhWbHF", "object": "chat.completion", "created": 1677737615, "model": "gpt-3.5-turbo-0301", "usage": { "prompt_tokens": 39, "completion_tokens": 35, "total_tokens": 74 }, "choices": [ { "message": { "role": "assistant", "content": "您好,我是个AI助手,没有具体的名字。您需要我帮助您处理json文件格式吗?" }, "finish_reason": "stop", "index": 0 } ] }
Get the JSON
data returned by the interface, and you can parse it according to your own needs. Here, the text in content
is the answer returned to you by the interface.
This article is transferred from https://xugaoxiang.com/2023/03/08/chatgpt-turbo-api/
This site is only for collection, and the copyright belongs to the original author.