模型详情Anthropic
claude-opus-4-7
推理、工具、文件与视觉能力
能力概览
推理与工具
适合复杂分析和带工具的工作流。
图片与文件
可围绕图片或文档内容完成理解任务。
接口
POST
https://www.yunqiai.chat/v1/messages所有请求使用 HTTPS,并通过请求头携带访问密钥。
请求参数
| 参数 | 类型 | 说明 |
|---|---|---|
model必填 | string | 从 GET /v1/models 返回结果中填写完整模型 ID |
messages必填 | array | user / assistant 消息数组;图片作为 image 内容块 |
max_tokens必填 | integer | 最大输出 Token,填写正整数;上限随模型变化 |
system | string | array | 顶层系统提示;Messages API 没有 system 角色 |
stream | boolean | 是否以 SSE 流式返回,默认 false |
temperature | number | 0–1;Claude 4.7 及以后模型建议省略采样参数 |
top_p | number | 0–1;与 temperature 通常只设置一个 |
stop_sequences | string[] | 自定义停止序列 |
tools | array | Anthropic 工具定义 |
请求示例
curl --request POST \
--url https://www.yunqiai.chat/v1/messages \
--header "x-api-key: YOUR_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "Content-Type: application/json" \
--data '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "你好"
}
]
}'Base64 图片输入
图片使用 image 内容块,source.type 设为 base64,并显式提供 media_type。
{
"model": "claude-opus-4-7",
"max_tokens": 256,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "BASE64_IMAGE_DATA"
}
},
{
"type": "text",
"text": "描述这张图片"
}
]
}
]
}PDF 文件输入
把 PDF 放在 document 内容块中,source.type 设为 base64,并在 source.data 中传入裸 Base64。提示词作为同一条消息里的 text 内容块。
import base64
import os
from pathlib import Path
import requests
pdf_path = Path("report.pdf")
pdf_data = base64.b64encode(pdf_path.read_bytes()).decode("ascii")
payload = {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{"type": "text", "text": "提取报告中的结论与关键数字"},
],
}],
}
response = requests.post(
"https://www.yunqiai.chat/v1/messages",
headers={
"x-api-key": os.environ["YUNQI_API_KEY"],
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
},
json=payload,
timeout=(10, 180),
)
response.raise_for_status()
result = response.json()
print("".join(block.get("text", "") for block in result.get("content", []) if block.get("type") == "text"))| 来源 | document.source 写法 | 适用条件 |
|---|---|---|
| 内联 Base64 | type: base64 · media_type · data | 本地文件可直接编码后发送 |
| 公开 URL | type: url · url | 文件地址可由服务端直接访问 |
| 已有文件 ID | type: file · file_id | 仅在业务侧已经取得有效 file_id 时使用 |
流式输出
客户端以 SSE 逐行读取 data: 事件。不要按网络数据块直接解码 JSON;同一事件可能被拆成多个传输片段。
| 阶段 | 事件 | 读取内容 |
|---|---|---|
| 开始 | message_start | 消息元数据 |
| 内容 | content_block_start → content_block_delta → content_block_stop | delta.text |
| 工具调用 | content_block_start / input_json_delta.partial_json | 保存 tool_use.id 与 name,按内容块 index 拼接参数 |
| 完成 | message_delta → message_stop | 停止原因与最终用量 |
| 错误 | error | error.type / error.message / request_id |
import json
import os
import requests
payload = {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "用三句话解释量子计算"}],
"stream": True,
}
with requests.post(
"https://www.yunqiai.chat/v1/messages",
headers={
"x-api-key": os.environ["YUNQI_API_KEY"],
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
},
json=payload,
stream=True,
timeout=(10, 180),
) as response:
response.raise_for_status()
message_stopped = False
tool_calls = {}
for line in response.iter_lines(decode_unicode=True):
if not line or not line.startswith("data: "):
continue
event = json.loads(line[6:])
if event.get("type") == "content_block_start":
block = event.get("content_block") or {}
if block.get("type") == "tool_use":
tool_calls[event.get("index", 0)] = {
"id": block.get("id", ""),
"name": block.get("name", ""),
"input": block.get("input") or {},
"arguments": "",
}
elif event.get("type") == "content_block_delta":
delta = event.get("delta", {})
if delta.get("type") == "text_delta":
print(delta.get("text", ""), end="", flush=True)
elif delta.get("type") == "input_json_delta":
index = event.get("index", 0)
current = tool_calls.setdefault(index, {"id": "", "name": "", "input": {}, "arguments": ""})
current["arguments"] += delta.get("partial_json", "")
elif event.get("type") == "message_stop":
message_stopped = True
elif event.get("type") == "error":
error = event.get("error", {})
raise RuntimeError(f"{error.get('type')}: {error.get('message')}")
if not message_stopped:
raise RuntimeError("stream ended before message_stop")
print()
for call in tool_calls.values():
arguments = json.loads(call["arguments"]) if call["arguments"] else call["input"]
tool_result = {"type": "tool_result", "tool_use_id": call["id"], "content": "工具执行结果"}
print("tool", call["id"], call["name"], arguments, tool_result)错误、重试与超时
| 情况 | 客户端处理 |
|---|---|
| 连接超时 | 单独设置连接超时,例如 10 秒;确认网络与 Base URL |
| 读取超时 | 文本请求可从 180 秒起设置;图像等耗时任务可设为 300 秒 |
| 408、429、5xx | 读取 Retry-After,并使用带随机抖动的指数退避;限制重试次数 |
| 400、401、403、404 | 不要自动重试;先检查错误正文、鉴权头、接口路径、模型与参数 |
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "请求参数无效"
},
"request_id": "req_01JY7X"
}| 协议 | 错误正文中的请求 ID | 响应头中的请求 ID |
|---|---|---|
| Anthropic Messages | request_id | request-id |
import os
import random
import time
import requests
url = "https://www.yunqiai.chat/v1/messages"
headers = {"x-api-key": os.environ["YUNQI_API_KEY"], "anthropic-version": "2023-06-01"}
payload = {"model": "claude-opus-4-7", "max_tokens": 1024, "messages": [{"role": "user", "content": "你好"}]}
for attempt in range(4):
delay = min(2 ** attempt, 8) + random.random()
try:
response = requests.post(url, headers=headers, json=payload, timeout=(10, 180))
except requests.Timeout:
if attempt == 3:
raise
else:
if response.ok:
print(response.json())
break
if response.status_code not in {408, 429, 500, 502, 503, 504}:
response.raise_for_status()
if attempt == 3:
response.raise_for_status()
retry_after = response.headers.get("Retry-After")
if retry_after:
try:
delay = max(delay, float(retry_after))
except ValueError:
pass
time.sleep(delay)响应
{
"id": "msg_01JY7X",
"type": "message",
"role": "assistant",
"model": "claude-opus-4-7",
"content": [
{
"type": "text",
"text": "你好!我是一个 AI 助手。"
}
],
"stop_reason": "end_turn"
}协议参考
本页示例按 YunQi AI 的模型 ID、接口路径和参数格式编写;以下链接提供对应协议的字段说明。