会话补全
本文提供调用「会话补全」接口的操作示例。
提示
此 API 仅支持 AI 功能的白名单用户可用,点此申请
示例:与 AI 助手 对话
假设你有一个 AI 助手,你想通过 API 与其进行对话。
你的操作步骤如下:
获取你的 API Token。(获取方法)
获取你的 Bot ID。(打开 AI 助手 的设置面板,可以在面板上方找到 Bot ID)
打开你电脑上的终端,执行下列代码,向服务器发送查询请求(假设 Bot ID 为
ai_zxLeHGV3ac32YYC
):
- cURL
- Javascript SDK
- Python SDK
curl -X POST \
"https://vika.cn/fusion/ai/ai_zxLeHGV3ac32YYC/chat/completions" \
-H "Authorization: Bearer {你的 API Token}" \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
]
}'
注意:需要先下载和初始化 JavaScript SDK,再执行如下命令。
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "_你的_API_Token_",
basePath: "https://vika.cn/fusion/v1/ai/ai_zxLeHGV3ac32YYC"
});
const openai = new OpenAIApi(configuration);
const chatCompletion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: "Hello world"}],
});
注意:需要先下载和初始化 Python SDK,再执行如下命令
import openai
openai.api_key = "_你的_API_Token_"
openai.api_base = "https://vika.cn/nest/v1/ai/ai_zxLeHGV3ac32YYC"
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
服务器会返回下列 JSON 数据包
有关返回结果中各参数的含义,可查看 API 手册。
{
"id": "aitable_ai_CkZH2zQokhry31j_1693452659",
"conversation id": "CS-0253eb8d-d6c6-4543-88d4-fcb555f52982",
"actions": null,
"object": "chat.completion",
"created": 1693452659,
"model":"gpt-3.5-turbo",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today",
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21,
"total cost": 7.900000000000001e-05,
"result": "I am an AI, specifically a language model developed by OpenAI."
}
}