OneDim 文档中心

Claude Messages API

Anthropic Claude Messages 原生格式接口,调用 Claude 系列模型。

接口详情

  • 接口地址:POST /v1/messages
  • 功能描述:使用 Anthropic Claude Messages 格式创建对话响应。
  • 认证方式:Bearer Token
  • Authorization: Bearer YOUR_API_TOKEN

请求参数

Header 参数

参数名类型必填说明示例
anthropic-versionstringAnthropic API 版本2023-06-01
AuthorizationstringBearer TokenBearer sk-xxx

Body 参数

参数名类型必填说明示例
modelstring模型名称claude-opus-4-7
messagesarray对话消息列表-
systemstring/array系统提示词-
max_tokensinteger最大生成 token 数1024
temperaturenumber温度 (0-1)0.7
top_pnumberNucleus sampling0.9
top_kintegerTop-k sampling40
streamboolean是否流式输出true
stop_sequencesarray停止序列-
toolsarray工具定义-
tool_choiceobject工具选择策略-
thinkingobject思考模式配置-
metadataobject元数据-

Messages 对象

参数名类型必填说明
rolestring角色,可选值:user, assistant
contentstring/array消息内容

Tool 对象

参数名类型必填说明
namestring工具名称
descriptionstring工具描述
input_schemaobject工具输入参数 schema

Thinking 对象

参数名类型必填说明
typestring类型:enabled, disabled
budget_tokensinteger思考预算 token 数

响应格式

成功响应 (200)

参数名类型说明
idstring消息 ID
typestring类型,固定为 message
rolestring角色,固定为 assistant
contentarray内容列表
modelstring模型名称
stop_reasonstring停止原因
usageobjectToken 使用情况

Content 对象

参数名类型说明
typestring内容类型:text, tool_use
textstring文本内容(type 为 text 时)
idstring工具调用 ID(type 为 tool_use 时)
namestring工具名称(type 为 tool_use 时)
inputobject工具输入(type 为 tool_use 时)

Usage 对象

参数名类型说明
input_tokensinteger输入 tokens
output_tokensinteger输出 tokens
cache_creation_input_tokensinteger缓存创建 tokens
cache_read_input_tokensinteger缓存读取 tokens

示例代码

基础对话

curl -X POST https://api.onedim.ai/v1/messages \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -d '{
    "model": "claude-opus-4-7",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello, Claude!"}
    ]
  }'

响应示例:

{
  "id": "msg_01X...",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! How can I help you today?"
    }
  ],
  "model": "claude-opus-4-7",
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 10,
    "output_tokens": 12,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0
  }
}

带系统提示词

curl -X POST https://api.onedim.ai/v1/messages \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -d '{
    "model": "claude-opus-4-7",
    "max_tokens": 1024,
    "system": "You are a professional translator. Translate all user input to Chinese.",
    "messages": [
      {"role": "user", "content": "Hello, how are you today?"}
    ]
  }'

流式响应

import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_TOKEN",
    base_url="https://ezmodel.cloud"
)

with client.messages.stream(
    model="claude-opus-4-7",
    max_tokens=1024,
    messages=[{"role": "user", "content": "讲一个故事"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

工具调用

curl -X POST https://api.onedim.ai/v1/messages \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -d '{
    "model": "claude-opus-4-7",
    "max_tokens": 1024,
    "tools": [
      {
        "name": "get_weather",
        "description": "获取指定城市的天气信息",
        "input_schema": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string",
              "description": "城市名称"
            }
          },
          "required": ["city"]
        }
      }
    ],
    "messages": [
      {"role": "user", "content": "北京今天天气怎么样?"}
    ]
  }'

多模态输入(图像)

import anthropic
import base64

client = anthropic.Anthropic(
    api_key="YOUR_API_TOKEN",
    base_url="https://ezmodel.cloud"
)

# 使用 URL
message = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "url",
                        "url": "https://example.com/image.jpg"
                    }
                },
                {
                    "type": "text",
                    "text": "描述这张图片"
                }
            ]
        }
    ]
)

# 或使用 Base64
with open("image.jpg", "rb") as f:
    image_data = base64.standard_b64encode(f.read()).decode("utf-8")

message = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/jpeg",
                        "data": image_data
                    }
                },
                {
                    "type": "text",
                    "text": "描述这张图片"
                }
            ]
        }
    ]
)

支持的模型

模型上下文窗口说明
claude-opus-4-7200K / 1M最强能力,适合复杂任务
claude-sonnet-4-6200K / 1M速度与能力平衡,日常首选

与 OpenAI 格式对比

特性Claude MessagesOpenAI Chat
系统消息独立 system 字段messages 数组中
响应格式content 数组message.content 字符串
必填参数max_tokens 必填max_tokens 可选
停止原因stop_reasonfinish_reason

提示

如果你习惯使用 OpenAI 格式,也可以使用 /v1/chat/completions 接口调用 Claude 模型,系统会自动转换格式。

On this page