如何調用FastGPT的API

fastGPT提供兼容OpenAI格式的接口,但是還是有一些地方需要注意
新建一個應用,可以正常測試通過後。【外部使用】【API訪問】【新建一個KEY】
我們在調用FastGPT API的時候,需要傳遞一個chatId的參數,這個是標識同一個會話的參數。只有傳遞了chatId,才能讓FastGPT知道上下文歷史記錄,否則API調用每次都是一次新的會話
import requests

url = "http://fast.v1kf.com/api/v1/chat/completions"  # 替換爲目標URL
data = {
    "model": "gpt-3.5-turbo",
    "chatId": "2",
    "messages": [
        {
            "role": "user",
            "content": "你叫什麼"
        },
    ]
}
headers = {
    "Authorization": "Bearer fastgpt-key"
}
response = requests.post(url, json=data, headers=headers,timeout=600)
print(response.text)
json_data = response.json()

print(json_data["choices"][0]["message"]["content"])

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章