上期圖文教程,我們介紹了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種語音的識别系統,不僅可以語音轉文字,還可以自動翻譯等。