Chat Completions
最常用的端点,与 OpenAI Chat Completions 完全兼容。适用于所有对话模型——Claude、GPT、Gemini、DeepSeek——只需修改 model。
POST
https://apicdn.xyc.ai/v1/chat/completions
请求参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 必填。模型名称,见模型列表 |
messages | array | 必填。消息数组,每条包含 role 和 content |
stream | bool | 是否流式输出响应,默认 false |
temperature | number | 采样温度 |
max_tokens | int | 生成的最大 token 数 |
top_p | number | 核采样 |
tools | array | 工具 / 函数调用定义 |
response_format | object | 结构化输出,如 {"type":"json_object"} |
基础请求
from openai import OpenAI
client = OpenAI(base_url="https://apicdn.xyc.ai/v1", api_key="sk-xxxxxxxx")
resp = client.chat.completions.create(
model="claude-opus-4-8",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what a vector database is."},
],
temperature=0.7,
)
print(resp.choices[0].message.content)
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://apicdn.xyc.ai/v1",
apiKey: "sk-xxxxxxxx",
});
const resp = await client.chat.completions.create({
model: "gpt-5.4",
messages: [
{ role: "system", content: "You are a concise assistant." },
{ role: "user", content: "Explain what a vector database is." },
],
});
console.log(resp.choices[0].message.content);
curl https://apicdn.xyc.ai/v1/chat/completions \
-H "Authorization: Bearer sk-xxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro",
"messages": [
{"role": "user", "content": "Explain what a vector database is."}
]
}'
流式输出
设置 stream: true 后将通过 SSE 接收 chat.completion.chunk 事件,以 data: [DONE] 结束。
stream = client.chat.completions.create(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": "Stream the text: hello"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
多模态(图像输入)
对于支持视觉的模型,content 可以是混合文本和图像的数组:
{
"model": "gemini-3-pro-preview",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/a.jpg"}}
]
}
]
}
工具调用
通过 tools 声明可调用的函数;模型在需要时返回 tool_calls:
{
"model": "gpt-5.4",
"messages": [{"role": "user", "content": "What is the weather in Beijing?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Look up the weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}
]
}
响应结构
{
"id": "chatcmpl-xxxxxxxx",
"object": "chat.completion",
"model": "claude-opus-4-8",
"choices": [
{"index": 0, "message": {"role": "assistant", "content": "..."}, "finish_reason": "stop"}
],
"usage": {"prompt_tokens": 20, "completion_tokens": 120, "total_tokens": 140}
}