Chat Completions(OpenAI 兼容)
POST /v1/chat/completions — OpenAI Chat Completions,逐参数详解
完全兼容 OpenAI Chat Completions。把 base_url 指向 ModelSite,即可用同一套代码调用 Claude、GPT、DeepSeek、GLM、Qwen、MiniMax 等所有模型,无需改业务代码。平台自动处理鉴权、计费与多 vendor failover。
调用示例
from openai import OpenAI
client = OpenAI(
base_url="https://api.modelsite.ai/v1",
api_key="ms_live_sk_xxxxxxxx", # 环境变量 MODELSITE_API_KEY
)
resp = client.chat.completions.create(
model="claude-sonnet-4.6",
messages=[{"role": "user", "content": "用一句话解释量子纠缠"}],
)
print(resp.choices[0].message.content)model 字段必填(计费与路由都依赖它)。下面的接口卡片由 OpenAPI 规范自动渲染:HTTP 方法与路径、可交互的 Request body 参数表、Response schema。
Authorization
BearerAuth Authorization: Bearer $MODELSITE_API_KEY
In: header
Request Body
application/json
TypeScript Definitions
Use the request body type in TypeScript.
Response Body
application/json
curl -X POST "https://example.com/chat/completions" \ -H "Content-Type: application/json" \ -d '{ "model": "string", "messages": [ { "role": "system" } ] }'{ "id": "string", "object": "string", "created": 0, "model": "string", "choices": [ { "index": 0, "message": { "role": "system", "content": "string", "name": "string", "tool_calls": [ { "id": "string", "type": "function", "function": { "name": "string", "arguments": "string" } } ], "tool_call_id": "string", "refusal": "string" }, "finish_reason": "stop", "logprobs": {} } ], "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0, "prompt_tokens_details": { "cached_tokens": 0, "text_tokens": 0, "audio_tokens": 0 }, "completion_tokens_details": { "reasoning_tokens": 0, "audio_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 } }}多模态输入
视觉模型在 content 里用对象数组传图:
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "这张图里有什么?"},
{"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}}
]
}]流式输出
stream: true 返回 Server-Sent Events,逐 token 推送:
for chunk in client.chat.completions.create(
model="claude-sonnet-4.6",
messages=[{"role": "user", "content": "讲个故事"}],
stream=True,
stream_options={"include_usage": True},
):
delta = chunk.choices[0].delta.content if chunk.choices else None
if delta:
print(delta, end="")工具调用(Function Calling)
声明 tools,模型返回 tool_calls;你回填 tool 角色消息继续对话:
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取某城市天气",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}}
}
}]vendor 专属参数透传:作为聚合代理,平台把请求体转发给底层模型厂商。厂商特有参数(如 Qwen 的 enable_thinking、thinking_budget、top_k、enable_search,DeepSeek 的 reasoning_effort 等)会原样透传——在 Python SDK 里放 extra_body,例如 extra_body={"enable_thinking": True}。是否生效取决于该模型厂商是否支持。
平台对每个请求自动做多 vendor failover,上游故障透明切换。详见 Failover。