Messages
http
POST https://gateway.hihapi.com/v1/messagesAnthropic Messages 协议。Claude Code 等 Anthropic 生态的工具使用这个协议,用 Anthropic SDK 也可以直接调用 HiHAi 上的模型。
请求示例
bash
curl https://gateway.hihapi.com/v1/messages \
-H "x-api-key: $HIHAI_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-flash",
"max_tokens": 1024,
"system": "你是一个乐于助人的助手。",
"messages": [
{"role": "user", "content": "用一句话介绍你自己"}
]
}'python
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["HIHAI_API_KEY"],
base_url="https://gateway.hihapi.com",
)
message = client.messages.create(
model="deepseek-flash",
max_tokens=1024,
system="你是一个乐于助人的助手。",
messages=[{"role": "user", "content": "用一句话介绍你自己"}],
)
print(message.content[0].text)js
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.HIHAI_API_KEY,
baseURL: "https://gateway.hihapi.com",
});
const message = await client.messages.create({
model: "deepseek-flash",
max_tokens: 1024,
system: "你是一个乐于助人的助手。",
messages: [{ role: "user", content: "用一句话介绍你自己" }],
});
console.log(message.content[0].text);和 OpenAI 协议的三处不同
- 鉴权用
x-api-key请求头,并且要带anthropic-version。 base_url不带/v1,SDK 会自己拼接路径。- 系统指令放在顶层的
system字段,不放进messages。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型 ID,见模型列表 |
messages | array | 是 | 消息列表,role 只能是 user 或 assistant |
max_tokens | integer | 是 | 本次回复最多生成的 tokens 数 |
system | string | 否 | 系统指令 |
stream | boolean | 否 | 是否流式返回,默认 false |
temperature | number | 否 | 采样温度 |
tools | array | 否 | 可供模型调用的工具,每个工具用 input_schema 描述参数 |
响应
json
{
"id": "msg_...",
"type": "message",
"role": "assistant",
"model": "deepseek-flash",
"content": [
{"type": "text", "text": "我是一个 AI 助手……"}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 24,
"output_tokens": 18
}
}| 字段 | 说明 |
|---|---|
content | 内容块数组。文本在 type 为 text 的块里,工具调用在 type 为 tool_use 的块里 |
stop_reason | 结束原因:end_turn 正常结束,max_tokens 达到长度上限,tool_use 需要调用工具 |
usage | 本次请求的 tokens 用量 |