文档 / API 参考 / Chat Completions
POST
/v1/chat/completionsChat Completions
兼容 OpenAI 的大模型对话端点。支持流式输出和工具调用。可无缝替换:只需修改 base URL 和 API 密钥。
#请求体
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 你的密钥模型列表中的模型 ID |
messages | array | 是 | 由 {role, content} 对象组成的数组。角色:system | user | assistant |
temperature | number | 否 | 采样温度,取值 0 到 2。默认值:1 |
max_tokens | integer | 否 | 生成的最大 token 数。省略时采用模型上限 |
stream | boolean | 否 | 若为 true,则通过 SSE 流式返回 token。默认值:false |
top_p | number | 否 | 核采样,可作为 temperature 的替代 |
stop | string | array | 否 | 最多 4 个停止序列 |
tools | array | 否 | 用于函数调用的 OpenAI tool-use schema |
#请求示例
terminalbash
curl https://relayrouter.io/v1/chat/completions \
-H "Authorization: Bearer $RELAYROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-flash",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in 2 sentences."}
],
"temperature": 0.7,
"max_tokens": 256
}'#响应(非流式)
response.jsonjson
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1749300000,
"model": "deepseek-v4-flash",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "Quantum computing uses quantum bits..." },
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 32,
"completion_tokens": 48,
"total_tokens": 80
}
}#流式输出
设置 stream: true 即可以 Server-Sent Events 的形式接收 token:
stream_response.txttext
data: {"id":"chatcmpl-abc123","choices":[{"delta":{"content":"Quantum"},"index":0}]}
data: {"id":"chatcmpl-abc123","choices":[{"delta":{"content":" computing"},"index":0}]}
...
data: [DONE]#工具调用
tools.jsonjson
{
"model": "deepseek-v4-flash",
"messages": [{"role": "user", "content": "What's the weather in Tokyo?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
}
]
}不同模型对工具调用和参数的支持有所不同。上游会忽略不支持的参数。