OpenAI官方发布ChatGPTAPI接口gptturbo,python可直接调用
上期图文教程,我们介绍了ChatGPT的注册使用过程,并且介绍了GPT-3代API接口的开发代码实现过程,由于ChatGPT一直使用的是ChatGPT-3.5代接口,且我们在官网上面使用的也是ChatGPT-3.5代接口,大家对3.5代接口都十分认同,上期介绍的ChatGPT-3代API虽然可以实现问答对话,但是大家希望是否可以使用官方的接口来实现强大的ChatGPT功能。关于如何注册,如何获取API key,上期图文我们也进行了详细的分享。
本期,我们介绍一下openai刚刚公布的ChatGPT的API接口gpt-3.5-turbo。首先其gpt-3.5-turbo接口API是官方刚刚发布的gpt-3.5-turbo API接口,针对第三代接口进行了速度与精度方面的优化,其官方也是建议开发者使用gpt-3.5-turbo API接口,且价格是3代API 接口的十分之一的价格。我们首先介绍一下openai发布的gpt-3.5-turbo API如何使用requests库进行调用。
import requests # Your OpenAI API Key api_key = "YOUR KEYS" # The text prompt you want to generate a response input_prompt = input("输入需要跟chat AI的聊天内容:") prompt = input_prompt # The URL for OpenAI"s API url = "https://api.openai.com/v1/chat/completions" # The headers for the API request headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } data = { "model":"gpt-3.5-turbo", "messages":[{"role":"user","content":prompt}], "max_tokens":800, "temperature":0.5, "frequency_penalty":0, "presence_penalty":0} # Make the API request response = requests.post(url, headers=headers, json=data) # Check if the request was successful if response.status_code == 200: # Extract the generated text from the response generated_text = response.json()["choices"][0]["message"]["content"] print(generated_text) else: # Handle the error print(f"Request failed with status code 额{response.status_code}")
首先,跟往期教程类似,我们需要使用requests库,并需要官方账号的api key。然后就可以输入需要的问题了。当然gpt-3.5-turbo的API接口地址如下:url = "https://api.openai.com/v1/chat/completions"
我们可以使用往期的代码,把需要的问题post到上面的API接口接口。这里需要注意的是gpt-3.5-turbo的API接口使用的不再是prompt,而是messages参数,且参数格式如下: 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?"} ]
在messages参数里面,我们需要指定role角色与content我们的问题,然后其他参数跟3代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 } ] }
当然,最后的返回参数也有稍微的区别,我们需要使用如下代码获取返回的参数。response.json()["choices"][0]["message"]["content"]
当然,除了以上直接调用API接口外,openAI也开发了自己的第三方库import openai openai.api_key = "sk-keys" while True: prompt = input("Q:") if prompt == "quit": break else: res = openai.ChatCompletion.create( model = "gpt-3.5-turbo", messages= [{"role":"user","content":prompt}], temperature = 0.5, max_tokens = 500, frequency_penalty=0, presence_penalty=0 ) print("***********GPT-3.5 Open AI**************") print(res["choices"][0]["message"]["content"])
首先需要我们安装openai 的第三方库,这里最好自己的python版本大于3.9,安装完成后,我们便可以使用openai库进行chatGPT的调用工作了。
第二行代码,我们需要提供上一个步骤的api key
然后,我们直接使用openai.ChatCompletion.create函数就可以调用chatGPT了。
同样的道理,我们需要修改一下里面的参数,一个是model参数,另外一个是messages参数。 model = "gpt-3.5-turbo", messages= [{"role":"user","content":prompt}],
以上,便是我们介绍的ChatGPT 官方API接口gpt-3.5-turbo,当然openAI除了ChatGPT这样的NLP领域任务外,还有类似DALL-E的AI绘画模型,且官方开源的whisper,可以识别99种语音的识别系统,不仅可以语音转文字,还可以自动翻译等。