사용 가능한 매개변수에 대한 전체 문서는 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/jpeg, image/png, image/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
  }
}

도구 사용 및 JSON 모드

Messages API와 함께 도구를 사용하는 방법에 대한 예제는 가이드를 참조하세요.