聊天补全 (Chat Completions)
聊天补全(Chat Completions)是新一代大模型的主流接口,用于对话生成。它支持多模态输入(如图像)、流式输出(Streaming),并且与 OpenAI 的 API 完全兼容。
POST /v1/chat/completions| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
model | string | ✅ | - | 调用的模型 ID(例如 gpt-4o, claude-3-5-sonnet)。 |
messages | array | ✅ | - | 对话历史消息列表。 |
temperature | number | ❌ | 1.0 | 采样温度 (0-2)。值越大越有创意,值越小越精确。 |
top_p | number | ❌ | 1.0 | 核采样参数。与 temperature 选其一调整即可。 |
max_tokens | integer | ❌ | - | 最大生成 token 数。 |
stream | boolean | ❌ | false | 是否开启流式传输。如果为 true,以 text/event-stream 格式逐步返回。 |
tools | array | ❌ | - | 模型可调用的工具(Function Calling)定义列表。 |
messages 对象结构
Section titled “messages 对象结构”[ { "role": "system", "content": "你是一个有用的助手。" }, { "role": "user", "content": "你好,你能帮我写一段 Python 代码吗?" }]cURL 请求
Section titled “cURL 请求”curl https://api.easytakeai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "model": "gpt-4o", "messages": [ {"role": "user", "content": "你好!"} ], "temperature": 0.7 }'JSON 响应示例
Section titled “JSON 响应示例”{ "id": "chatcmpl-123456", "object": "chat.completion", "created": 1716298500, "model": "gpt-4o", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "你好!很高兴为您服务。今天有什么我可以帮您的吗?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 9, "completion_tokens": 26, "total_tokens": 35 }}