Create chat completions
This article provides an example of calling the Create chat completions API.
This API is only available to whitelisted users. Click here to apply
Example:
Assuming you have an AI agent and you want to chat with it.
Follow these steps:
Get your API Token. (How to get it)
Get your AI agent ID. (Open the setting panel of the AI agent, you can see the Bot ID on the top)
Open the terminal on your computer and execute the following code to send an API request to the server (assuming the Bot ID is
ai_zxLeHGV3ac32YYC
):- cURL
- Javascript SDK
- Python SDK
curl -X POST \
"https://aitable.ai/fusion/ai/ai_zxLeHGV3ac32YYC/chat/completions" \
-H "Authorization: Bearer {Your 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!"
}
]
}'Note: You need to download and initialize Javascript SDK first, and then execute the following command.
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "_Paste_Your_API_Token_",
basePath: "https://aitable.ai/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"}],
});Note: You need to download and initialize the Python SDK first, and then execute the following command.
import openai
openai.api_key = "_Paste_Your_API_Token_"
openai.api_base = "https://aitable.ai/nest/v1/ai/ai_zxLeHGV3ac32YYC"
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])The server will return the following JSON data
For the meaning of each parameter in the response, please check the API Reference
{
"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."
}
}