Tạo một chat completion
Sử dụng CometAPI POST /v1/chat/completions để gửi các cuộc hội thoại gồm nhiều message đến các chat model với Streaming, đầu ra có cấu trúc và tool calling.
base_url="https://api.cometapi.com/v1" và API key CometAPI của bạn.
Các ví dụ cơ bản, hình ảnh, Streaming và đầu ra có cấu trúc sử dụng gpt-6-astra. Ví dụ Function Calling sử dụng gpt-5.6-sol, còn ví dụ log-probability sử dụng gpt-4.1.
Vai trò message
Gửi đầu vào Multimodal
Sử dụng một mảng các phần nội dung để gửi hình ảnh kèm văn bản:image_url.detail để chọn mức độ chi tiết khi xử lý hình ảnh. Ví dụ này sử dụng high.
Truyền phát phản hồi
Để nhận đầu ra tăng dần, hãy đặtstream thành true. Phản hồi được gửi dưới dạng Server-Sent Events (SSE). Văn bản đến trong các đối tượng chat.completion.chunk; ví dụ rút gọn này bao gồm chunk sử dụng cuối cùng:
Yêu cầu đầu ra có cấu trúc
Sử dụngresponse_format để yêu cầu đầu ra có cấu trúc:
Gọi công cụ và hàm
Sử dụnggpt-5.6-sol với định nghĩa hàm:
finish_reason: "tool_calls" và một mảng message.tool_calls. Phân tích cú pháp arguments được mã hóa JSON của hàm, thực thi hàm của bạn, rồi thêm thông báo trợ lý và thông báo kết quả tool có tool_call_id tương ứng.
Chọn tham số yêu cầu
Tham số dành riêng cho model
Tham số dành riêng cho model
reasoning_effort và max_completion_tokens với Astra GPT-6. Các ví dụ về Functions và Logprobs lần lượt cho thấy các tùy chọn yêu cầu dành cho gpt-5.6-sol và gpt-4.1.Để biết định dạng yêu cầu dành riêng cho Claude hoặc Gemini, xem Anthropic Messages và Gemini Generate content.max_tokens và max_completion_tokens
max_tokens và max_completion_tokens
max_completion_tokens để giới hạn Token được tạo cho các ví dụ Astra GPT-6. Giá trị này bao gồm cả phần suy luận lẫn đầu ra hiển thị, vì vậy hãy dành chỗ cho cả hai. max_tokens là một tham số cũ.Vai trò hướng dẫn
Vai trò hướng dẫn
developer cho hướng dẫn ứng dụng trong các ví dụ Astra GPT-6. Giữ nội dung của người dùng cuối trong các thông báo user và giữ lại các phản hồi trước đó của trợ lý khi tiếp tục cuộc trò chuyện.Câu hỏi thường gặp
Cách xử lý giới hạn tốc độ?
Khi gặp429 Too Many Requests, hãy triển khai cơ chế trì hoãn theo cấp số nhân:
Cách duy trì ngữ cảnh hội thoại?
Bao gồm toàn bộ lịch sử hội thoại trong mảngmessages:
finish_reason có nghĩa là gì?
Cách kiểm soát chi phí?
- Sử dụng
max_completion_tokensđể giới hạn độ dài đầu ra. - So sánh giá của các mô hình và chọn mô hình đáp ứng yêu cầu khối lượng công việc của bạn.
- Giữ Prompt ngắn gọn — tránh ngữ cảnh dư thừa.
- Theo dõi mức sử dụng token trong trường phản hồi
usage.
Ủy quyền
Bearer token authentication. Use your CometAPI key.
Nội dung
Model ID to use for this request. See the Models page for current options.
"gpt-6-astra"
Conversation messages, including instructions, user input, assistant replies, and tool results.
If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.
Sampling temperature. Omit this field for GPT-6 Astra.
0 <= x <= 2Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.
0 <= x <= 1Number of completion choices to generate for each input message. Defaults to 1.
Stop string or list of up to four strings, for models that support stop sequences.
Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.
Number between -2.0 and 2.0. Positive values penalize tokens based on whether they have already appeared, encouraging the model to explore new topics.
-2 <= x <= 2Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.
-2 <= x <= 2A JSON object mapping token IDs to bias values from -100 to 100. The bias is added to the model's logits before sampling. Values between -1 and 1 subtly adjust likelihood; -100 or 100 effectively ban or force selection of a token.
A unique identifier for your end-user. Helps with abuse detection and monitoring.
Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.
Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.
Function definitions for a model that supports tool calling on Chat Completions. The function example uses GPT-5.6 Sol; use Responses for GPT-6 Astra tool calls.
Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.
Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.
Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.
0 <= x <= 20Reasoning effort supported by the selected model. The GPT-6 Astra examples use low; the GPT-5.6 Sol function example uses none. See the model reference for other supported levels.
Options for streaming. Only valid when stream is true.
Specifies the processing tier.
auto, default, flex, priority Phản hồi
Successful chat completion response.
Unique completion identifier.
"chatcmpl_example"
Object type. Non-streaming responses use chat.completion.
chat.completion "chat.completion"
Unix timestamp of creation.
1788763703
The model used (may include version suffix).
"gpt-6-astra"
Array of completion choices.
Token accounting for this request. Billing uses these counts.
Service tier that processed the request, when returned.
Model configuration fingerprint, when returned.