如何调用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"])

 

posted @ 2024-01-02 17:00  唯一客服系统开发笔记  阅读(521)  评论(0编辑  收藏  举报