Tạo một Message
Gọi Claude thông qua endpoint Messages của CometAPI với dữ liệu đầu vào văn bản và hình ảnh, adaptive thinking, Prompt caching, Streaming và công cụ.
POST /v1/messages để gửi yêu cầu Claude theo định dạng Anthropic Messages.
Các ví dụ cấu hình Anthropic SDK chính thức với base URL của CometAPI và
đọc API key của bạn từ $COMETAPI_KEY.
x-api-key hoặc Authorization: Bearer. Anthropic SDK sử dụng
x-api-key. Các ví dụ HTTP bao gồm anthropic-version: 2023-06-01.Bắt đầu nhanh
Các ví dụ sau yêu cầu ba truy vấn tìm kiếm. Đặt$COMETAPI_KEY trước khi
chạy chúng. Cài đặt anthropic cho Python hoặc @anthropic-ai/sdk cho JavaScript:
content. Đọc các khối có type là text;
các khối thinking và tool có thể xuất hiện trước văn bản.
Kiểm soát suy luận thích ứng
Đặtthinking.type thành adaptive và chọn một giá trị output_config.effort.
Ví dụ sau sử dụng xhigh và đọc thông báo đã hoàn tất từ một luồng:
max_tokens. Hãy chừa chỗ cho câu trả lời cuối cùng
cũng như quá trình suy luận. Một phản hồi có thể chứa văn bản mà không có khối suy luận hiển thị.
Giữ nguyên mọi khối suy luận được trả về trong
lịch sử cuộc trò chuyện.
Để biết cấu hình suy luận dành riêng cho từng model, hãy xem
Suy luận.
Các ví dụ bỏ qua temperature, top_p và top_k; hãy tham khảo tài liệu về tham số của
model đã chọn trước khi thêm các điều khiển lấy mẫu.
Lưu đệm Prompt
Đặt một điểm ngắt bộ nhớ đệm trên tài liệu tham chiếu mà bạn tái sử dụng giữa các yêu cầu. Lưu tài liệu tham chiếu của bạn vào một tệpreference.txt mã hóa UTF-8 trước khi chạy ví dụ
này. Hãy sử dụng một tiền tố đáp ứng yêu cầu của model đã chọn về
độ dài tối thiểu có thể lưu vào bộ nhớ đệm:
cache_creation_input_tokensđếm số Token được ghi vào bộ nhớ đệm.cache_read_input_tokensđếm số Token được đọc từ bộ nhớ đệm.input_tokensđếm số Token đầu vào được xử lý ngoài các bộ đếm bộ nhớ đệm đó.
cache_read_input_tokens báo cáo số Token đầu vào
được đọc từ bộ nhớ đệm. Ví dụ OpenAPI Prompt Cache
chứa một tài liệu tham chiếu hư cấu hoàn chỉnh mà bạn có thể lưu dưới dạng
reference.txt để thử ví dụ mẫu.
Truyền phản hồi theo luồng
Đặtstream: true để dùng Server-Sent Events. SDK hiển thị các đoạn văn bản khi chúng
đến:
message_start, các sự kiện khối nội dung, message_delta,
và message_stop. Các khối nội dung có thể chứa văn bản, quá trình suy luận hoặc hoạt động của công cụ.
Đối với khối văn bản, content_block_delta chứa một text_delta. Đọc thông tin sử dụng cuối cùng
và lý do dừng từ message_delta.
Kiểm soát mức độ nỗ lực
Đặtoutput_config.effort để định hướng mức độ suy luận. Ví dụ này sử dụng
low cho phần giải thích ngắn và chờ thông điệp được truyền theo luồng hoàn tất:
max_tokens để giới hạn độ dài đầu ra.
Sử dụng công cụ máy chủ
Các công cụ máy chủ thực thi trong quá trình gửi yêu cầu API và trả về các khối kết quả cùng với câu trả lời của Claude.- Web Fetch
- Web Search
web_fetch_tool_result khối:server_tool_use với một khối web_fetch_tool_result
chứa tài liệu đã tìm nạp hoặc lỗi công cụ.Trả về kết quả công cụ phía máy khách
Đối với công cụ phía máy khách, Claude trả về một khốitool_use. Hãy chạy hàm của ứng dụng bạn
và gửi kết quả của hàm đó trong một khối tool_result có tool_use_id tương ứng.
Giữ nguyên toàn bộ nội dung assistant giữa hai yêu cầu.
Ví dụ này cung cấp một kết quả đơn hàng hư cấu và yêu cầu Claude sử dụng kết quả đó:
Ví dụ phản hồi
Một yêu cầu không dùng Streaming trả về một đối tượng message. Ví dụ sau hiển thị các trường text và usage cùng một mã định danh message minh họa:end_turn hoàn tất câu trả lời,
max_tokens có nghĩa là đầu ra đã đạt giới hạn, còn tool_use yêu cầu kết quả từ một công cụ phía máy khách
trả về. Với một lượt công cụ phía máy chủ trả về pause_turn, hãy tiếp tục với
nội dung assistant được trả về mà không thay đổi.Ủy quyền
Your CometAPI key passed via the x-api-key header. Authorization: Bearer $COMETAPI_KEY is also supported.
Tiêu đề
The Anthropic API version to use. Defaults to 2023-06-01.
"2023-06-01"
Comma-separated feature identifiers required by a specific beta API feature. Omit this header for the examples on this page.
Nội dung
The Claude model to use. See the Models page for available Claude model IDs.
"claude-opus-5"
Conversation history. Use user and assistant messages with text strings or content-block arrays. Return complete assistant content blocks when continuing a tool call.
The maximum number of tokens to generate. The model may stop before reaching this limit. When using thinking, the thinking tokens count towards this limit.
x >= 11024
System prompt providing context and instructions to Claude. Can be a plain string or an array of content blocks (useful for prompt caching).
Sampling temperature. The examples omit sampling overrides.
0 <= x <= 1Nucleus sampling threshold. The examples omit sampling overrides.
0 <= x <= 1Limits sampling to the k most likely tokens. The examples omit sampling overrides.
x >= 0If true, stream the response incrementally using Server-Sent Events (SSE). Events include message_start, content_block_start, content_block_delta, content_block_stop, message_delta, and message_stop.
Custom strings that cause the model to stop generating when encountered. The stop sequence is not included in the response.
Thinking configuration. The adaptive example uses type adaptive and sets output_config.effort separately.
Client tools define a name and input_schema. Server tools use a versioned type and name, such as the web_fetch and web_search examples on this page.
Controls how the model uses tools.
Request metadata for tracking and analytics.
Configuration for reasoning effort and structured output.
The service tier to use. auto tries priority capacity first, standard_only uses only standard capacity.
auto, standard_only Phản hồi
Successful response. When stream is true, the response is a stream of SSE events.
Message identifier returned by the API.
Always message.
message Always assistant.
assistant The response content blocks. May include text, thinking, tool_use, and other block types.
Model ID reported by the response.
Why the model stopped generating. refusal can be returned as a successful HTTP response when the model declines a request.
end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal The stop sequence that caused the model to stop, if applicable.
Token usage statistics.