OpenAI SDK 호환성
Amazon Bedrock API
Vertex AI
Anthropic API
- 메시지
- 모델
- 메시지 배치
- 텍스트 생성 (기존)
- 관리자 API
메시지 배치
메시지 배치 예제
메시지 배치 API 사용 예제
메시지 배치 API는 메시지 API와 동일한 기능 세트를 지원합니다. 이 페이지는 메시지 배치 API 사용 방법에 중점을 두고 있으며, 메시지 API 기능 세트의 예제는 메시지 API 예제를 참조하세요.
메시지 배치 생성하기
Copy
import anthropic
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
from anthropic.types.messages.batch_create_params import Request
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
requests=[
Request(
custom_id="my-first-request",
params=MessageCreateParamsNonStreaming(
model="claude-3-7-sonnet-20250219",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Hello, world",
}]
)
),
Request(
custom_id="my-second-request",
params=MessageCreateParamsNonStreaming(
model="claude-3-7-sonnet-20250219",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Hi again, friend",
}]
)
)
]
)
print(message_batch)
JSON
Copy
{
"id": "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message_batch",
"processing_status": "in_progress",
"request_counts": {
"processing": 2,
"succeeded": 0,
"errored": 0,
"canceled": 0,
"expired": 0
},
"ended_at": null,
"created_at": "2024-09-24T18:37:24.100435Z",
"expires_at": "2024-09-25T18:37:24.100435Z",
"cancel_initiated_at": null,
"results_url": null
}
메시지 배치 완료 폴링하기
메시지 배치를 폴링하려면 생성 요청 응답이나 목록에서 제공되는 id
가 필요합니다. 예시 id
: msgbatch_013Zva2CMHLNnXjNJJKqJ2EF
.
Copy
import anthropic
client = anthropic.Anthropic()
message_batch = None
while True:
message_batch = client.messages.batches.retrieve(
MESSAGE_BATCH_ID
)
if message_batch.processing_status == "ended":
break
print(f"Batch {MESSAGE_BATCH_ID} is still processing...")
time.sleep(60)
print(message_batch)
워크스페이스의 모든 메시지 배치 나열하기
Copy
import anthropic
client = anthropic.Anthropic()
# Automatically fetches more pages as needed.
for message_batch in client.messages.batches.list(
limit=20
):
print(message_batch)
Output
Copy
{
"id": "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message_batch",
...
}
{
"id": "msgbatch_01HkcTjaV5uDC8jWR4ZsDV8d",
"type": "message_batch",
...
}
메시지 배치 결과 검색하기
메시지 배치 상태가 ended
가 되면 배치의 results_url
을 확인하고 .jsonl
파일 형식으로 결과를 검색할 수 있습니다.
Copy
import anthropic
client = anthropic.Anthropic()
# Stream results file in memory-efficient chunks, processing one at a time
for result in client.messages.batches.results(
MESSAGE_BATCH_ID,
):
print(result)
Output
Copy
{
"id": "my-second-request",
"result": {
"type": "succeeded",
"message": {
"id": "msg_018gCsTGsXkYJVqYPxTgDHBU",
"type": "message",
...
}
}
}
{
"custom_id": "my-first-request",
"result": {
"type": "succeeded",
"message": {
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
...
}
}
}
메시지 배치 취소하기
취소 직후에 배치의 processing_status
는 canceling
이 됩니다. 취소된 배치도 결국 ended
상태가 되고 결과를 포함할 수 있으므로, 배치 완료 폴링 기법을 사용하여 취소가 완료되었는지 폴링할 수 있습니다.
Copy
import anthropic
client = anthropic.Anthropic()
message_batch = client.messages.batches.cancel(
MESSAGE_BATCH_ID,
)
print(message_batch)
JSON
Copy
{
"id": "msgbatch_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message_batch",
"processing_status": "canceling",
"request_counts": {
"processing": 2,
"succeeded": 0,
"errored": 0,
"canceled": 0,
"expired": 0
},
"ended_at": null,
"created_at": "2024-09-24T18:37:24.100435Z",
"expires_at": "2024-09-25T18:37:24.100435Z",
"cancel_initiated_at": "2024-09-24T18:39:03.114875Z",
"results_url": null
}
Was this page helpful?