跳转至

Claude(Anthropic 原生格式)

端点 /v1/messages · 原样转发至 Anthropic 官方

用 Anthropic 原生的 Messages API 格式调用 Claude。请求体、响应体、流式事件、思维链与工具调用都与直连 Anthropic 官方一致——直接用官方 @anthropic-ai SDK,改 base_url 即可。常用模型:claude-opus-5

端点与认证

地址 POST https://6geapi.com/v1/messages
认证 Authorization: Bearer <Key>(六哥统一;x-api-key 多数也接受,优先 Bearer)
必填头 anthropic-version: 2023-06-01 · content-type: application/json

最小请求与响应

curl https://6geapi.com/v1/messages \
  -H "Authorization: Bearer $LIUGE_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "法国的首都是哪里?"}]
  }'
import anthropic
client = anthropic.Anthropic(base_url="https://6geapi.com", api_key="你的Key")
msg = client.messages.create(
    model="claude-opus-5", max_tokens=1024,
    messages=[{"role": "user", "content": "法国的首都是哪里?"}],
)
print(msg.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://6geapi.com", apiKey: process.env.LIUGE_API_KEY });
const msg = await client.messages.create({
  model: "claude-opus-5", max_tokens: 1024,
  messages: [{ role: "user", content: "法国的首都是哪里?" }],
});
console.log(msg.content[0].text);

响应:

{
  "id": "msg_01ABC...",
  "type": "message", "role": "assistant", "model": "claude-opus-5",
  "content": [{ "type": "text", "text": "法国的首都是巴黎。" }],
  "stop_reason": "end_turn",
  "usage": { "input_tokens": 14, "output_tokens": 12 }
}

content块数组text / thinking / tool_use),按 type 判断再取字段。stop_reasonend_turn(结束)、max_tokens(达上限)、tool_use(要调工具)、refusal(安全拒绝)。

请求参数

参数 类型 必填 说明
model string 模型 ID,如 claude-opus-5
messages array {role, content};首条须 user,user/assistant 交替
max_tokens int 输出上限(新模型是「思考 + 正文」总上限)
system string/array 系统提示,顶级字段(不在 messages 里)
stream bool true 开启流式
thinking object 思维链配置,见 思维链
output_config.effort string low/medium/high/xhigh/max,思考深度
tools / tool_choice array/object 工具调用,见 工具调用

这些参数已被新模型移除

temperature / top_p / top_k 在 Claude 4.7+/Opus 5/Fable 5 等新模型上已移除,传了会直接返回 400。需要控制随机性请用 output_config.effort

流式输出

curl https://6geapi.com/v1/messages \
  -H "Authorization: Bearer $LIUGE_API_KEY" \
  -H "anthropic-version: 2023-06-01" -H "content-type: application/json" \
  -d '{"model":"claude-opus-5","max_tokens":1024,"stream":true,
       "messages":[{"role":"user","content":"写一首秋天的俳句"}]}'
with client.messages.stream(
    model="claude-opus-5", max_tokens=1024,
    messages=[{"role":"user","content":"写一首秋天的俳句"}],
) as s:
    for text in s.text_stream:
        print(text, end="", flush=True)

SSE 事件流:message_startcontent_block_startcontent_block_deltatext_delta / thinking_delta)→ content_block_stopmessage_delta(含 stop_reason)→ message_stop

思维链(Thinking)

Claude 4.6+ 推荐自适应思维,模型自决何时思考、思考多少:

{
  "model": "claude-opus-5",
  "max_tokens": 4096,
  "thinking": { "type": "adaptive", "display": "summarized" },
  "output_config": { "effort": "high" },
  "messages": [{ "role": "user", "content": "27 × 453 等于多少?一步步算" }]
}
  • thinking.type"adaptive"(推荐)。新模型不支持 {type:"enabled", budget_tokens:N},传会 400。
  • thinking.display"summarized" 返回思考摘要;"omitted"(默认)思考字段为空。不影响计费
  • output_config.effort:控制深度与 token 消耗。

工具调用(Tool Use)

定义工具 → 模型返回 tool_use 块 → 你执行后用 tool_result 回传 → 循环到 stop_reason == "end_turn"

tools = [{
    "name": "get_weather",
    "description": "获取指定城市的当前天气",
    "input_schema": {
        "type": "object",
        "properties": {"location": {"type": "string", "description": "城市名"}},
        "required": ["location"],
    },
}]
messages = [{"role": "user", "content": "巴黎天气怎么样?"}]

while True:
    resp = client.messages.create(model="claude-opus-5", max_tokens=1024,
                                  tools=tools, messages=messages)
    messages.append({"role": "assistant", "content": resp.content})
    if resp.stop_reason != "tool_use":
        break
    results = []
    for b in resp.content:
        if b.type == "tool_use":
            result = f"{b.input['location']} 晴 22°C"   # 换成你的真实逻辑
            results.append({"type": "tool_result", "tool_use_id": b.id, "content": result})
    messages.append({"role": "user", "content": results})

print(messages[-1]["content"][0].text)

工具输入要解析,不要字符串匹配

工具输入 JSON 可能含 Unicode / 斜杠转义,务必用 json.loads() 解析再取字段。

tool_choiceauto(默认)/ any / none / 指定工具。