有关可用参数的完整文档,请参阅 API 参考

基本请求和响应

#!/bin/sh
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-opus-20240229",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "你好,Claude"}
    ]
}'
JSON
{
  "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "你好!"
    }
  ],
  "model": "claude-3-opus-20240229",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 6
  }
}

多轮对话

Messages API 是无状态的,这意味着您总是需要将完整的对话历史发送给 API。您可以使用此模式随时间构建对话。早期的对话轮次不一定要实际来自 Claude - 您可以使用合成的 assistant 消息。

Shell
#!/bin/sh
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-opus-20240229",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "你好,Claude"},
        {"role": "assistant", "content": "你好!"},
        {"role": "user", "content": "你能向我描述一下 LLM 吗?"}
        
    ]
}'
Python
import anthropic

message = anthropic.Anthropic().messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "你好,Claude"},
        {"role": "assistant", "content": "你好!"},
        {"role": "user", "content": "你能向我描述一下 LLM 吗?"}
    ],
)
print(message)

TypeScript
import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic();

await anthropic.messages.create({
  model: 'claude-3-opus-20240229',
  max_tokens: 1024,
  messages: [
    {"role": "user", "content": "你好,Claude"},
    {"role": "assistant", "content": "你好!"},
    {"role": "user", "content": "你能向我描述一下 LLM 吗?"}
  ]
});
JSON
{
    "id": "msg_018gCsTGsXkYJVqYPxTgDHBU",
    "type": "message",
    "role": "assistant",
    "content": [
        {
            "type": "text",
            "text": "当然,我很乐意提供..."
        }
    ],
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 30,
      "output_tokens": 309
    }
}

预填 Claude 的回答

您可以在输入消息列表的最后一个位置预填 Claude 的部分回答。这可用于塑造 Claude 的回答。下面的示例使用 "max_tokens": 1 从 Claude 获得单个多选答案。

#!/bin/sh
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-opus-20240229",
    "max_tokens": 1,
    "messages": [
        {"role": "user", "content": "蚂蚁的拉丁文是什么?(A) Apoidea, (B) Rhopalocera, (C) Formicidae"},
        {"role": "assistant", "content": "答案是 ("}
    ]
}'
JSON
{
  "id": "msg_01Q8Faay6S7QPTvEUUQARt7h",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "C"
    }
  ],
  "model": "claude-3-opus-20240229",
  "stop_reason": "max_tokens",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 42,
    "output_tokens": 1
  }
}

视觉

Claude 可以读取请求中的文本和图像。目前,我们支持图像的 base64 源类型,以及 image/jpegimage/pngimage/gifimage/webp 媒体类型。有关更多详细信息,请参阅我们的视觉指南

#!/bin/sh

IMAGE_URL="https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
IMAGE_MEDIA_TYPE="image/jpeg"
IMAGE_BASE64=$(curl "$IMAGE_URL" | base64)

curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-opus-20240229",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": [
            {"type": "image", "source": {
                "type": "base64",
                "media_type": "'$IMAGE_MEDIA_TYPE'",
                "data": "'$IMAGE_BASE64'"
            }},
            {"type": "text", "text": "上面的图像中有什么?"}
        ]}
    ]
}'
JSON
{
  "id": "msg_01EcyWo6m4hyW8KHs2y2pei5",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "这张图片展示了一只蚂蚁,特别是蚂蚁的特写视图。蚂蚁的细节清晰可见,包括它独特的头部、触角和腿。图像专注于捕捉蚂蚁的复杂细节和特征,可能是用微距镜头拍摄的,以获得极端特写的视角。"
    }
  ],
  "model": "claude-3-opus-20240229",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 1551,
    "output_tokens": 71
  }
}

[beta] 工具使用和 JSON 模式

有关如何将工具与 Messages API 一起使用的示例,请参阅我们的 beta 指南