Anthropic 接口规范
兼容 Anthropic Messages 协议,适合使用 anthropic 官方 SDK 或已有 Anthropic 集成的项目。
| 项目 | 值 |
|---|---|
| Base URL | https://api.taiha.cn/v1 |
| API Key | 在控制台创建 |
| Model ID | 在模型广场查看 |
本接口仅支持标注了
Anthropic协议的模型,具体可在模型详情页确认。
基础对话
POST /v1/messages
- cURL
- Python
cURL
curl --location --request POST 'https://api.taiha.cn/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"messages": [
{
"role": "user",
"content": "hello"
}
]
}'
Python
import requests
url = "https://api.taiha.cn/v1/messages"
headers = {
"Authorization": "<API Key>",
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
}
payload = {
"model": "<Model ID>",
"messages": [{
"role": "user",
"content": "hello"
}],
"max_tokens": 1024
}
resp = requests.post(url, headers=headers, json=payload, timeout=30)
resp.raise_for_status()
data = resp.json()
reply = "".join(
item["text"]
for item in data.get("content", [])
if item.get("type") == "text"
)
print("Assistant reply:")
print(reply)
常用参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 必填。Model ID,见模型广场 |
messages | array | 必填。对话历史(角色只有 user / assistant) |
max_tokens | integer | 必填。最大输出长度 |
system | string | 系统指令,顶级字段(不放在 messages 内) |
stream | boolean | 流式输出,默认 false |
temperature | number | 随机性 [0, 1] |
与 OpenAI 的主要区别
| 项目 | OpenAI | Anthropic |
|---|---|---|
| 系统指令 | messages 内 role=system | 顶级 system 字段 |
| max_tokens | 可选 | 必填 |
| 请求头 | 无额外要求 | 需带 anthropic-version: 2023-06-01 |
| temperature 范围 | [0, 2] | [0, 1] |