Chat Completions API
通过兼容 OpenAI 的 /chat/completions 接口调用聊天模型:请求字段、流式输出、工具调用、图像、JSON 输出、推理和缓存。
Chat
POST /chat/completions 原样接受 OpenAI 请求:messages、max_tokens、temperature、top_p、stop、tools 和 tool_choice、response_format、stream。未知字段会传给模型。每个响应都带有 x-request-id 请求头;联系支持时请提供它。
以下字段适用于所有聊天模型;其他字段会按原样传给模型。
| 字段 | 类型 | |
|---|---|---|
model | string | 必填。聊天模型 id 或其别名之一(GET /models)。 |
messages | array | 必填。对话内容:system、user、assistant 和 tool 消息。content 为字符串或由多个部分(text、image_url、file)组成的列表。 |
max_tokens | integer | 最多生成的输出 token 数,包括推理;max_completion_tokens 视为同一字段。它也决定请求运行期间从余额中预扣的金额。 |
stream | boolean | 为 true 时以 server-sent events 返回回答(见「流式输出」)。 |
temperature, top_p | number | 采样参数。不再接受这些参数的模型(如较新的 Claude 模型)会忽略它们。 |
stop | string | array | 一个字符串或列表;回答会在即将写出其中第一个字符串之前停止。 |
tools, tool_choice | array, string | object | 模型可以调用的函数,采用 OpenAI 格式。tool_choice 为 auto、none、required 或 {"type": "function", "function": {"name": "…"}}。 |
parallel_tool_calls | boolean | 为 false 时,每次回答最多进行一次工具调用。 |
response_format | object | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} 使回答遵循 schema;{"type": "json_object"} 返回任意合法 JSON(GPT、Gemini、DeepSeek)。 |
reasoning_effort | string | none、minimal、low、medium 或 high(Claude 上还有 xhigh 和 max)。会转换为各模型系列自己的设置;没有此类设置的模型会忽略它。 |
user | string | 你为终端用户设定的 id,会传给模型服务商。relay 账户必填。 |
请求与响应
最简单的调用只需发送模型和 messages 列表。回答位于 choices[0].message.content,finish_reason 说明结束原因:stop(模型已答完)、length(达到 max_tokens)、tool_calls 或 content_filter。usage 包含计费的 token 数量。
curl https://api.apistore.cloud/v1/chat/completions \
-H "Authorization: Bearer $APISTORE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-haiku-5-5",
"max_tokens": 1024,
"messages": [
{ "role": "system", "content": "You are a concise technical assistant." },
{ "role": "user", "content": "What is an idempotency key?" }
]
}'{
"id": "chatcmpl-...",
"object": "chat.completion",
"model": "claude-haiku-5-5",
"choices": [{
"index": 0,
"message": { "role": "assistant", "content": "An idempotency key is ..." },
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 27,
"completion_tokens": 112,
"total_tokens": 139,
"prompt_tokens_details": { "cached_tokens": 0 },
"completion_tokens_details": { "reasoning_tokens": 0 }
}
}图像和 PDF 文件
支持读图的模型以 user 消息中的 image_url 部分接收图像:https URL 或 data URL(data:image/png;base64,…)。在支持读取文档的模型上,PDF 文件放在 file 部分中,文档以 data URL 形式发送。图像和文档页面按输入 token 计费。
"messages": [{
"role": "user",
"content": [
{ "type": "text", "text": "List the line items on this invoice." },
{ "type": "image_url", "image_url": { "url": "https://example.com/invoice.png" } },
{ "type": "file", "file": { "filename": "terms.pdf", "file_data": "data:application/pdf;base64,..." } }
]
}]工具调用
在 tools 中描述你的函数,每个函数附带一个描述其参数的 JSON Schema。当模型要调用某个函数时,回答以 finish_reason "tool_calls" 结束,message.tool_calls 列出每次调用:id、函数名以及 JSON 字符串形式的参数。执行该函数,然后把对话发回,附上这条 assistant 消息,并为每次调用附上一条 role 为 "tool" 的消息,包含 tool_call_id 和结果。模型会从这里继续。同一请求适用于 Claude、GPT、Gemini 和 DeepSeek。
// 1. Offer the function
"tools": [{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Look up the shipping status of an order",
"parameters": {
"type": "object",
"properties": { "order_id": { "type": "string" } },
"required": ["order_id"]
}
}
}]
// 2. The model answers with finish_reason "tool_calls"
"tool_calls": [{ "id": "call_1", "type": "function",
"function": { "name": "get_order_status", "arguments": "{\"order_id\":\"A-1042\"}" } }]
// 3. Send back the assistant message, then the result
{ "role": "tool", "tool_call_id": "call_1", "content": "{\"status\":\"shipped\"}" }结构化输出(JSON)
如需程序可解析的 JSON,请发送 type 为 "json_schema" 的 response_format,并附上回答必须遵循的 schema;所有模型系列都支持。type "json_object"(任意合法 JSON,无 schema)适用于 GPT、Gemini 和 DeepSeek。请给 max_tokens 留出足以容纳整个对象的空间:因达到上限而被截断的回答(finish_reason "length")不是合法 JSON。
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "invoice",
"schema": {
"type": "object",
"properties": {
"number": { "type": "string" },
"total": { "type": "number" },
"currency": { "type": "string" }
},
"required": ["number", "total", "currency"]
}
}
}推理
reasoning_effort 设置推理模型在回答前思考的多少。更高的强度有助于数学、代码和多步骤问题,但会消耗更多输出 token 和时间。如果模型会返回推理过程,它将位于 message.reasoning_content(流式输出时为 delta.reasoning_content),与回答分开。推理 token 按输出计费,并计入 usage.completion_tokens_details.reasoning_tokens,因此请在 max_tokens 中为其留出空间。
Streaming
设置 "stream": true 即可接收 server-sent events。加上 "stream_options": {"include_usage": true},可在最后一个 chunk 中获得 token 数量。
{
"model": "claude-haiku-5-5",
"stream": true,
"stream_options": { "include_usage": true },
"messages": [{ "role": "user", "content": "Hello" }]
}提示词缓存
重复的提示词前缀(系统提示词、长文档、对话历史)会由服务商缓存。命中缓存的输入 token 会自动按更便宜的缓存命中价计费。请把提示词中固定不变的部分放在最前面,并保持不变。
GPT、Gemini 和 DeepSeek 模型会自动缓存。Claude 只缓存你标记的内容:在固定前缀(系统提示词、长文档、之前的对话轮次)的最后一条消息或最后一个内容部分上添加 "cache_control": {"type": "ephemeral"}。缓存在最后一次使用后保留约 5 分钟;{"type": "ephemeral", "ttl": "1h"} 可保留一小时。写入缓存比普通输入更贵,因此只有在该时间内再次发送相同前缀时才划算。
"messages": [
{ "role": "system", "content": [
{ "type": "text", "text": "<long instructions or reference document>",
"cache_control": { "type": "ephemeral" } }
] },
{ "role": "user", "content": "First question" }
]usage.prompt_tokens_details.cached_tokens 是从缓存读取的输入 token 数,按缓存命中价计费;在 Claude 上,cache_write_tokens 是写入缓存的 token 数。
