使用 Claude 官方格式进行文本生成
{
"id": "msg_abcd",
"type": "message",
"role": "assistant",
"model": "",
"content": [
{
"type": "text",
"text": "I'm Claude, an AI assistant made by Anthropic. I'm designed to be helpful, harmless, and honest. I can help with a wide range of tasks like answering questions, writing, analysis, brainstorming, coding, and more.\n\nIs there something I can help you with?"
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 11,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"output_tokens": 67,
"cache_creation": {},
"claude_cache_creation_5_m_tokens": 0,
"claude_cache_creation_1_h_tokens": 0
}
}| 参数 | 类型 | 必填 | 默认值 | 范围 | 描述 |
|---|---|---|---|---|---|
model | string | 是 | claude-haiku-4-5-20251001 | - | 模型标识符 |
messages | array | 是 | - | - | 对话的输入消息。每条消息必须包含 role(user/assistant)和 content(字符串或内容块数组) |
max_tokens | integer | 是 | - | 1-64000 | 生成的最大 token 数量。模型可能会在此限制之前停止 |
temperature | number | 否 | 1.0 | 0.0-1.0 | 控制输出的随机性。对于分析任务使用较低的值(接近 0.0),对于创意任务使用较高的值(接近 1.0)。注意:即使在 0.0 时,结果也不是完全确定性的 |
top_p | number | 否 | - | 0.0-1.0 | 核采样阈值。通过仅考虑累积概率达到 top_p 的 token 来控制多样性。建议仅高级用户使用。不要与 temperature 一起使用 |
top_k | integer | 否 | - | >0 | 仅从每个 token 的前 K 个选项中采样。移除低概率响应。建议仅高级用户使用 |
stream | boolean | 否 | false | - | 是否使用服务器发送事件逐步流式传输响应 |
stop_sequences | array | 否 | - | 最多 8191 个序列 | 导致模型停止生成的自定义文本序列。每个序列必须包含非空白字符 |
messages 数组中的每个消息对象应具有以下结构:| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
role | string | 是 | - | 消息的角色。可以是:user 或 assistant |
content | string/array | 是 | - | 消息的内容。对于纯文本消息可以是简单字符串,对于多模态内容可以是内容块数组 |
| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 必须是 text |
text | string | 是 | - | 文本内容 |
| 错误码 | 错误名称 | 描述 |
|---|---|---|
| 401 | Unauthorized | API 密钥缺失或无效 |
| 403 | Forbidden | 您的 API 密钥没有访问此资源的权限,或请求操作的余额不足 |
| 429 | Too Many Requests | 您已超出速率限制 |
| 500 | Internal server error | 发生内部服务器错误 |
| 503 | Content policy violation | 内容因安全问题被阻止(实际状态码为 400) |