> For the complete documentation index, see [llms.txt](https://nsclouds.gitbook.io/aillm.nscloud.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nsclouds.gitbook.io/aillm.nscloud.ai/api-reference-cn/dui-hua-jie-kou/anthropic/chat.md).

# Chat Completions

### 1. 概述

Anthropic 在当前环境中提供的对话生成能力。

{% hint style="success" %}
本接口提供与 OpenAI Chat Completions 兼容的请求路径。不同厂家和模型的实际参数支持范围可能不同。
{% endhint %}

**模型列表：**

* `claude-haiku-4-5-20251001`
* `claude-haiku-4.5`
* `claude-opus-4.5`
* `claude-opus-4.6`
* `claude-opus-4.7`
* `claude-opus-4.8`
* `claude-sonnet-4.5`
* `claude-sonnet-4.6`

### 2. 接口详情

## 创建聊天补全

> 提供与 OpenAI Chat Completions 兼容的对话接口。具体支持的参数和行为因模型而异。

```json
{"openapi":"3.1.0","info":{"title":"NSCloud AI API 文档","version":"1.11.0"},"tags":[{"name":"chat_completions_anthropic","description":"Anthropic 的对话生成 API"}],"servers":[{"url":"https://aillm.nscloud.ai","description":"国际服务器"}],"security":[{"APIKeyHeader":[]}],"components":{"securitySchemes":{"APIKeyHeader":{"type":"http","scheme":"bearer","description":"使用 Bearer 令牌进行认证，格式：'Bearer YOUR_SECRET_TOKEN'"}},"schemas":{"CreateChatCompletionRequest":{"type":"object","description":"Chat Completions 请求体。","properties":{"model":{"type":"string","description":"要调用的模型 ID。"},"messages":{"type":"array","description":"对话历史消息列表，按顺序传入。","items":{"$ref":"#/components/schemas/ChatMessage"}},"temperature":{"type":"number","description":"采样温度，值越高结果越随机。","default":1},"top_p":{"type":"number","description":"核采样参数。"},"presence_penalty":{"type":"number","description":"根据 token 是否已出现施加惩罚。"},"frequency_penalty":{"type":"number","description":"根据 token 出现频率施加惩罚。"},"stop":{"description":"最多四个停止序列。","oneOf":[{"type":"string"},{"type":"array","items":{"type":"string"}}]},"max_completion_tokens":{"type":"integer","description":"允许生成的最大输出 token 数。"},"n":{"type":"integer","description":"为同一请求生成的候选回复数量。","default":1},"seed":{"type":"integer","description":"尽力保证可复现的采样随机种子。"},"reasoning_effort":{"type":"string","description":"推理模型支持的 reasoning 强度等级。"},"response_format":{"$ref":"#/components/schemas/ChatResponseFormat"},"modalities":{"type":"array","description":"期望返回的输出模态，例如 text 或 audio。","items":{"type":"string"}},"audio":{"$ref":"#/components/schemas/ChatAudioRequestConfig"},"store":{"type":"boolean","description":"是否存储本次响应以供后续检索或蒸馏使用。"},"metadata":{"type":"object","description":"开发者自定义元数据。","additionalProperties":true},"service_tier":{"type":"string","description":"支持时请求的服务层级。"},"user":{"type":"string","description":"终端用户标识。"},"tools":{"type":"array","description":"提供给模型可调用的工具声明。","items":{"$ref":"#/components/schemas/ChatToolDefinition"}},"tool_choice":{"$ref":"#/components/schemas/ChatToolChoice"},"parallel_tool_calls":{"type":"boolean","description":"是否允许模型并行返回多个工具调用。"},"stream":{"type":"boolean","description":"是否以流式方式返回响应。","default":false},"stream_options":{"$ref":"#/components/schemas/ChatStreamOptions"}},"required":["model","messages"]},"ChatMessage":{"description":"聊天消息对象。","oneOf":[{"$ref":"#/components/schemas/ChatDeveloperMessage"},{"$ref":"#/components/schemas/ChatSystemMessage"},{"$ref":"#/components/schemas/ChatUserMessage"},{"$ref":"#/components/schemas/ChatAssistantMessage"},{"$ref":"#/components/schemas/ChatToolMessage"},{"$ref":"#/components/schemas/ChatFunctionMessage"}]},"ChatDeveloperMessage":{"type":"object","description":"开发者消息。仅支持文本内容。","properties":{"role":{"type":"string","enum":["developer"]},"name":{"type":"string","description":"可选的参与者名称。"},"content":{"$ref":"#/components/schemas/ChatTextOnlyContent"}},"required":["role","content"],"additionalProperties":true},"ChatTextOnlyContent":{"description":"纯文本聊天消息内容。","oneOf":[{"type":"string","description":"纯文本内容。"},{"type":"array","description":"文本内容片段数组。","items":{"$ref":"#/components/schemas/ChatMessageTextPart"}}]},"ChatMessageTextPart":{"type":"object","description":"文本内容片段。","properties":{"type":{"type":"string","description":"内容片段类型。","enum":["text"]},"text":{"type":"string","description":"文本内容。"}},"required":["type","text"]},"ChatSystemMessage":{"type":"object","description":"系统消息。仅支持文本内容。","properties":{"role":{"type":"string","enum":["system"]},"name":{"type":"string","description":"可选的参与者名称。"},"content":{"$ref":"#/components/schemas/ChatTextOnlyContent"}},"required":["role","content"],"additionalProperties":true},"ChatUserMessage":{"type":"object","description":"用户消息。支持文本、图片、音频和文件输入片段。","properties":{"role":{"type":"string","enum":["user"]},"name":{"type":"string","description":"可选的参与者名称。"},"content":{"description":"用户消息内容。","oneOf":[{"type":"string","description":"纯文本内容。"},{"type":"array","description":"多段结构化多模态内容数组。","items":{"oneOf":[{"$ref":"#/components/schemas/ChatMessageTextPart"},{"$ref":"#/components/schemas/ChatMessageImagePart"},{"$ref":"#/components/schemas/ChatMessageInputAudioPart"},{"$ref":"#/components/schemas/ChatMessageFilePart"}]}}]}},"required":["role","content"],"additionalProperties":true},"ChatMessageImagePart":{"type":"object","description":"图片输入内容片段。可添加多个 image_url 内容片段来发送多张图片。","properties":{"type":{"type":"string","description":"内容片段类型。","enum":["image_url"]},"image_url":{"$ref":"#/components/schemas/ChatMessageImageUrl"}},"required":["type","image_url"]},"ChatMessageImageUrl":{"type":"object","description":"图片 URL 对象。","properties":{"url":{"type":"string","description":"可公开访问的图片 URL 或图片 data URL。支持常见图片格式，示例中可使用 PNG。"},"detail":{"type":"string","description":"可选的图片理解细节等级。","enum":["auto","low","high"]}},"required":["url"]},"ChatMessageInputAudioPart":{"type":"object","description":"音频输入内容片段。","properties":{"type":{"type":"string","description":"内容片段类型。","enum":["input_audio"]},"input_audio":{"$ref":"#/components/schemas/ChatMessageInputAudio"}},"required":["type","input_audio"]},"ChatMessageInputAudio":{"type":"object","description":"音频输入结构。","properties":{"data":{"type":"string","description":"Base64 编码的音频数据。"},"format":{"type":"string","description":"音频格式。","enum":["wav","mp3"]}},"required":["data","format"]},"ChatMessageFilePart":{"type":"object","description":"Chat Completions 的文件输入内容片段。支持 file_data，也兼容 file_id；不支持外部文件 URL。如需传入文件 URL，请使用 Responses 的 input_file.file_url。","properties":{"type":{"type":"string","description":"内容片段类型。","enum":["file"]},"file":{"$ref":"#/components/schemas/ChatMessageFileObject"}},"required":["type","file"]},"ChatMessageFileObject":{"type":"object","description":"Chat Completions 内容片段中的文件结构。Chat 的 file 片段支持内联 file_data，也兼容 OpenAI 的 file_id；当前推荐优先使用 file_data。不支持 file_url；外部文件 URL 请使用 Responses 的 input_file.file_url。","properties":{"file_id":{"type":"string","description":"OpenAI 兼容字段。预先上传后的文件 ID，来源于 Files API；当前平台该能力建设中/适配中，实际可用性取决于文件托管和模型侧支持。"},"file_data":{"type":"string","description":"内联传递的 base64 文件内容，当前推荐优先使用。"},"filename":{"type":"string","description":"与内联文件内容配套的文件名。"}},"anyOf":[{"required":["file_id"]},{"required":["file_data"]}],"additionalProperties":false},"ChatAssistantMessage":{"type":"object","description":"assistant 消息，可包含工具调用。","properties":{"role":{"type":"string","enum":["assistant"]},"name":{"type":"string","description":"可选的参与者名称。"},"content":{"description":"assistant 消息内容。","oneOf":[{"$ref":"#/components/schemas/ChatTextOnlyContent"},{"type":"null","description":"空内容，常见于 assistant 发起工具调用时。"}]},"tool_calls":{"type":"array","description":"assistant 发出的工具调用列表。","items":{"$ref":"#/components/schemas/ChatCompletionToolCall"}},"function_call":{"oneOf":[{"$ref":"#/components/schemas/ChatCompletionFunctionCall"},{"type":"null"}],"description":"已废弃的单函数调用结构。"},"refusal":{"type":["string","null"],"description":"模型拒答时返回的拒绝内容。"},"audio":{"oneOf":[{"$ref":"#/components/schemas/ChatAudioResponseMetadata"},{"type":"null"}],"description":"与消息关联的音频信息。"}},"required":["role"],"additionalProperties":true},"ChatCompletionToolCall":{"description":"assistant 返回的工具调用结构。","oneOf":[{"$ref":"#/components/schemas/ChatCompletionFunctionToolCall"},{"$ref":"#/components/schemas/ChatCompletionCustomToolCall"}]},"ChatCompletionFunctionToolCall":{"type":"object","description":"assistant 返回的函数工具调用结构。","properties":{"id":{"type":"string","description":"工具调用 ID。"},"type":{"type":"string","description":"工具调用类型。","enum":["function"]},"function":{"$ref":"#/components/schemas/ChatCompletionFunctionCall"}},"required":["id","type","function"],"additionalProperties":true},"ChatCompletionFunctionCall":{"type":"object","description":"某些模型仍可能返回的已废弃 function_call 结构。","properties":{"name":{"type":"string","description":"函数名。"},"arguments":{"type":"string","description":"JSON 字符串形式的函数参数。"}},"additionalProperties":true},"ChatCompletionCustomToolCall":{"type":"object","description":"assistant 返回的自定义工具调用结构。","properties":{"id":{"type":"string","description":"工具调用 ID。"},"type":{"type":"string","description":"工具调用类型。","enum":["custom"]},"custom":{"type":"object","description":"自定义工具调用负载。","additionalProperties":true}},"required":["id","type","custom"],"additionalProperties":true},"ChatAudioResponseMetadata":{"type":"object","description":"聊天消息返回的音频元数据。","properties":{"id":{"type":"string","description":"音频对象 ID。"},"expires_at":{"type":"integer","description":"过期时间戳（如有）。"},"transcript":{"type":"string","description":"与音频对应的转写文本。"}},"additionalProperties":true},"ChatToolMessage":{"type":"object","description":"工具结果消息。仅支持文本内容，并必须包含关联的 tool call ID。","properties":{"role":{"type":"string","enum":["tool"]},"content":{"$ref":"#/components/schemas/ChatTextOnlyContent"},"tool_call_id":{"type":"string","description":"此消息响应的工具调用 ID。"}},"required":["role","content","tool_call_id"],"additionalProperties":true},"ChatFunctionMessage":{"type":"object","description":"已废弃的函数结果消息。新集成应使用 tool 消息。","properties":{"role":{"type":"string","enum":["function"]},"name":{"type":"string","description":"函数名。"},"content":{"type":["string","null"],"description":"函数结果内容。"}},"required":["role","name","content"],"additionalProperties":true},"ChatResponseFormat":{"type":"object","description":"结构化输出格式配置。","properties":{"type":{"type":"string","description":"返回格式类型，例如 text、json_object、json_schema。"},"json_schema":{"type":"object","description":"当 type=json_schema 时使用的 JSON Schema 配置。","additionalProperties":true}},"additionalProperties":true},"ChatAudioRequestConfig":{"type":"object","description":"音频输出请求配置。","properties":{"voice":{"type":"string","description":"音频输出使用的声音名称。"},"format":{"type":"string","description":"音频格式。"}},"additionalProperties":true},"ChatToolDefinition":{"description":"Chat Completions 中的工具声明。","oneOf":[{"$ref":"#/components/schemas/ChatFunctionToolDefinition"},{"$ref":"#/components/schemas/ChatCustomToolDefinition"}]},"ChatFunctionToolDefinition":{"type":"object","description":"Chat Completions 中的函数工具声明。","properties":{"type":{"type":"string","description":"工具类型。","enum":["function"]},"function":{"$ref":"#/components/schemas/ChatToolFunction"}},"required":["type","function"],"additionalProperties":true},"ChatToolFunction":{"type":"object","description":"工具声明中的函数定义。","properties":{"name":{"type":"string","description":"函数名。"},"description":{"type":"string","description":"函数说明。"},"parameters":{"type":"object","description":"描述函数参数的 JSON Schema。","additionalProperties":true},"strict":{"type":"boolean","description":"是否严格遵循 schema。"}},"required":["name"],"additionalProperties":true},"ChatCustomToolDefinition":{"type":"object","description":"Chat Completions 中的自定义工具声明。","properties":{"type":{"type":"string","description":"工具类型。","enum":["custom"]},"custom":{"$ref":"#/components/schemas/ChatCustomTool"}},"required":["type","custom"],"additionalProperties":true},"ChatCustomTool":{"type":"object","description":"工具声明中的自定义工具定义。","properties":{"name":{"type":"string","description":"自定义工具名称。"},"description":{"type":"string","description":"自定义工具说明。"},"format":{"type":"object","description":"可选的自定义工具输入格式定义。","additionalProperties":true}},"required":["name"],"additionalProperties":true},"ChatToolChoice":{"description":"工具选择策略。","anyOf":[{"type":"string","description":"简单策略，例如 none、auto、required。","enum":["none","auto","required"]},{"$ref":"#/components/schemas/ChatNamedFunctionToolChoice"},{"$ref":"#/components/schemas/ChatNamedCustomToolChoice"},{"$ref":"#/components/schemas/ChatAllowedToolsChoice"},{"type":"object","description":"厂家扩展的工具选择对象结构。","additionalProperties":true}]},"ChatNamedFunctionToolChoice":{"type":"object","description":"显式选择指定函数工具。","properties":{"type":{"type":"string","enum":["function"]},"function":{"type":"object","properties":{"name":{"type":"string","description":"函数工具名称。"}},"required":["name"],"additionalProperties":true}},"required":["type","function"],"additionalProperties":true},"ChatNamedCustomToolChoice":{"type":"object","description":"显式选择指定自定义工具。","properties":{"type":{"type":"string","enum":["custom"]},"custom":{"type":"object","properties":{"name":{"type":"string","description":"自定义工具名称。"}},"required":["name"],"additionalProperties":true}},"required":["type","custom"],"additionalProperties":true},"ChatAllowedToolsChoice":{"type":"object","description":"将工具选择限制到指定工具子集。","properties":{"type":{"type":"string","enum":["allowed_tools"]},"allowed_tools":{"type":"object","description":"允许调用的工具选择配置。","properties":{"mode":{"type":"string","description":"限定工具集合内的选择模式。"},"tools":{"type":"array","description":"允许调用的工具声明。","items":{"$ref":"#/components/schemas/ChatToolDefinition"}}},"required":["mode","tools"],"additionalProperties":true}},"required":["type","allowed_tools"],"additionalProperties":true},"ChatStreamOptions":{"type":"object","description":"Chat Completions 的流式选项。","properties":{"include_usage":{"type":"boolean","description":"在流式返回末尾附带 usage 信息。"},"include_obfuscation":{"type":"boolean","description":"在支持时附带 obfuscation 元数据。"}},"additionalProperties":true},"ChatCompletionResponse":{"type":"object","description":"Chat Completions 成功响应。","properties":{"id":{"type":"string","description":"本次响应的唯一 ID。"},"object":{"type":"string","description":"对象类型，通常为 chat.completion。"},"created":{"type":"integer","description":"响应创建时间戳（Unix 秒）。"},"model":{"type":"string","description":"实际使用的模型 ID。"},"service_tier":{"type":"string","description":"实际使用的服务层级（如有）。"},"system_fingerprint":{"type":"string","description":"后端配置指纹（如有）。"},"request_id":{"type":"string","description":"服务端暴露的请求 ID（如有）。"},"choices":{"type":"array","description":"模型返回的候选结果列表。","items":{"$ref":"#/components/schemas/ChatCompletionChoice"}},"usage":{"$ref":"#/components/schemas/Usage"}}},"ChatCompletionChoice":{"type":"object","description":"单个候选回复。","properties":{"index":{"type":"integer","description":"候选结果在返回数组中的序号。"},"message":{"$ref":"#/components/schemas/ChatCompletionResponseMessage"},"finish_reason":{"description":"生成结束原因，例如 stop、length、tool_calls。","type":["string","null"]},"logprobs":{"type":["object","null"],"description":"可选的 token 级 log probability 细节。","additionalProperties":true}}},"ChatCompletionResponseMessage":{"type":"object","description":"聊天补全中的单条消息。","properties":{"role":{"type":"string","description":"生成消息的角色，通常为 assistant。"},"content":{"description":"模型生成的消息文本，部分场景下可能为 null。","type":["string","null"]},"refusal":{"type":["string","null"],"description":"assistant 拒答时返回的拒绝内容。"},"annotations":{"type":"array","description":"可选标注信息，例如引用或 URL 标记。","items":{"$ref":"#/components/schemas/ChatCompletionAnnotation"}},"tool_calls":{"type":"array","description":"assistant 返回的工具调用列表。","items":{"$ref":"#/components/schemas/ChatCompletionToolCall"}},"function_call":{"oneOf":[{"$ref":"#/components/schemas/ChatCompletionFunctionCall"},{"type":"null"}],"description":"已废弃的单函数调用结构。"},"audio":{"oneOf":[{"$ref":"#/components/schemas/ChatAudioResponseMetadata"},{"type":"null"}],"description":"与消息一起返回的音频信息。"}}},"ChatCompletionAnnotation":{"type":"object","description":"响应消息上的附加标注信息。","additionalProperties":true},"Usage":{"type":"object","description":"Token 使用量统计。","properties":{"prompt_tokens":{"type":"integer","description":"输入消耗的 token 数。"},"completion_tokens":{"type":"integer","description":"输出消耗的 token 数。"},"total_tokens":{"type":"integer","description":"总 token 数。"},"input_tokens":{"type":"integer","description":"输入消耗的 token 数，部分协议使用该字段。"},"output_tokens":{"type":"integer","description":"输出消耗的 token 数，部分协议使用该字段。"},"completion_tokens_details":{"type":"object","description":"输出 token 使用明细。","properties":{"reasoning_tokens":{"type":"integer","description":"推理类输出消耗的 token 数。"}},"additionalProperties":true},"input_tokens_details":{"type":"object","description":"输入 token 使用明细。","properties":{"text_tokens":{"type":"integer","description":"文本输入消耗的 token 数。"},"image_tokens":{"type":"integer","description":"图片输入消耗的 token 数。"}},"additionalProperties":true}}},"ErrorResponse":{"type":"object","description":"错误响应。","properties":{"error":{"$ref":"#/components/schemas/ErrorDetail"}}},"ErrorDetail":{"type":"object","description":"错误详情对象。","properties":{"message":{"type":"string","description":"错误说明。"},"type":{"type":"string","description":"错误类型。"},"param":{"description":"与错误相关的参数名。","type":["string","null"]},"code":{"description":"错误码。","type":["string","null"]}}}},"responses":{"ErrorResponse":{"description":"错误响应","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}},"paths":{"/v1/chat/completions":{"post":{"tags":["chat_completions_anthropic"],"summary":"创建聊天补全","description":"提供与 OpenAI Chat Completions 兼容的对话接口。具体支持的参数和行为因模型而异。","operationId":"createChatCompletion_anthropic","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/CreateChatCompletionRequest"}}}},"responses":{"200":{"description":"成功响应","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ChatCompletionResponse"}},"text/event-stream":{"schema":{"type":"string","description":"流式响应以 Server-Sent Events 格式返回，每个事件是一行以 'data: ' 开头的 JSON 数据块，最后以 'data: [DONE]' 结束。"}}}},"400":{"$ref":"#/components/responses/ErrorResponse"},"401":{"$ref":"#/components/responses/ErrorResponse"},"429":{"$ref":"#/components/responses/ErrorResponse"}}}}}}
```
