使用 Anthropic 原生格式分析和提取各种文件类型的信息
POST /v1/messages{
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
}| 参数 | 类型 | 必填 | 默认值 | 范围 | 描述 |
|---|---|---|---|---|---|
model | string | 是 | claude-haiku-4-5-20251001 | - | 模型标识符。此模型使用 claude-haiku-4-5-20251001 |
messages | array | 是 | - | - | 对话的输入消息。每条消息必须包含 role(user/assistant)和 content。 内容可以包含文本块或带有 base64 或 URL 源的文档块。支持的格式:PDF、DOCX、XLSX、TXT、CSV、JSON、XML、HTML。最大文件大小:20MB。详见下方消息结构 |
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 个序列 | 导致模型停止生成的自定义文本序列。每个序列必须包含非空白字符 |
{
"role": "user",
"content": [
{
"type": "text",
"text": "Please analyze this document"
},
{
"type": "document",
"source": {
"type": "url",
"url": "https://example.com/document.pdf"
}
}
]
}{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": "JVBERi0xLjQK..."
}
}messages 数组中的每个消息对象应具有以下结构:| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
role | string | 是 | - | 消息的角色。可以是:user 或 assistant |
content | array/string | 是 | - | 消息的内容。可以是字符串或内容块数组 |
content 为数组时,每个元素可以是以下类型之一:| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 必须是 text |
text | string | 是 | - | 文本内容 |
| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 必须是 document |
source | object | 是 | - | 文档源(base64 或 URL) |
| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 必须是 url |
url | string | 是 | - | 文档文件的 URL |
| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 必须是 base64 |
media_type | string | 是 | - | 文档的 MIME 类型(例如:application/pdf、application/vnd.openxmlformats-officedocument.wordprocessingml.document) |
data | string | 是 | - | Base64 编码的文档数据 |
| 格式 | MIME 类型 | 扩展名 |
|---|---|---|
application/pdf | .pdf | |
| Word | application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docx |
| Excel | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | .xlsx |
| 纯文本 | text/plain | .txt |
| CSV | text/csv | .csv |
| JSON | application/json | .json |
| XML | application/xml 或 text/xml | .xml |
| HTML | text/html | .html |
| 错误码 | 错误名称 | 描述 |
|---|---|---|
| 401 | Unauthorized | API 密钥缺失或无效 |
| 403 | Forbidden | 您的 API 密钥没有访问此资源的权限,或请求操作的余额不足 |
| 429 | Too Many Requests | 您已超出速率限制 |
| 500 | Internal server error | 发生内部服务器错误 |
| 503 | Content policy violation | 内容因安全问题被阻止(实际状态码为 400) |