Skip to main content
POST
Gửi các cuộc hội thoại qua Chat Completions API tương thích với OpenAI. Cấu hình SDK của bạn bằng 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.
Sử dụng tài liệu tham khảo OpenAI Chat Completions để xem định nghĩa tham số. Đối với tool calling Astra của GPT-6, hãy sử dụng Responses, theo hướng dẫn mô hình OpenAI.

Vai trò message

Đặt hướng dẫn ứng dụng trong một message developer trước các message của người dùng khi sử dụng các ví dụ Astra của GPT-6.

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:
Sử dụng 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 đặt stream 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:
Để bao gồm thống kê mức sử dụng Token trong các phản hồi Streaming, hãy đặt stream_options.include_usage thành true. Dữ liệu mức sử dụng xuất hiện trong một chunk cuối trước [DONE]. Các chunk có thể có mảng choices rỗng; hãy kiểm tra mảng này trước khi truy cập choices[0].

Yêu cầu đầu ra có cấu trúc

Sử dụng response_format để yêu cầu đầu ra có cấu trúc:
Chế độ JSON Schema chỉ định cấu trúc bắt buộc. Chế độ JSON Object yêu cầu JSON hợp lệ mà không áp dụng schema của bạn. Hãy kiểm tra việc từ chối hoặc phản hồi bị giới hạn độ dài trước khi sử dụng kết quả.

Gọi công cụ và hàm

Sử dụng gpt-5.6-sol với định nghĩa hàm:
Một lệnh gọi hàm trả về 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ả tooltool_call_id tương ứng.

Chọn tham số yêu cầu

Sử dụng reasoning_effortmax_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-solgpt-4.1.Để biết định dạng yêu cầu dành riêng cho Claude hoặc Gemini, xem Anthropic MessagesGemini Generate content.
Sử dụng 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ũ.
Sử dụng 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ặp 429 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ảng messages:

finish_reason có nghĩa là gì?

Cách kiểm soát chi phí?

  1. Sử dụng max_completion_tokens để giới hạn độ dài đầu ra.
  2. 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.
  3. Giữ Prompt ngắn gọn — tránh ngữ cảnh dư thừa.
  4. Theo dõi mức sử dụng token trong trường phản hồi usage.

Ủy quyền

Authorization
string
header
bắt buộc

Bearer token authentication. Use your CometAPI key.

Nội dung

application/json
model
string
mặc định:gpt-6-astra
bắt buộc

Model ID to use for this request. See the Models page for current options.

Ví dụ:

"gpt-6-astra"

messages
object[]
bắt buộc

Conversation messages, including instructions, user input, assistant replies, and tool results.

stream
boolean

If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.

temperature
number

Sampling temperature. Omit this field for GPT-6 Astra.

Phạm vi bắt buộc: 0 <= x <= 2
top_p
number

Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.

Phạm vi bắt buộc: 0 <= x <= 1
n
integer

Number of completion choices to generate for each input message. Defaults to 1.

stop

Stop string or list of up to four strings, for models that support stop sequences.

max_tokens
integer
không còn sử dụng

Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.

presence_penalty
number

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.

Phạm vi bắt buộc: -2 <= x <= 2
frequency_penalty
number

Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.

Phạm vi bắt buộc: -2 <= x <= 2
logit_bias
object

A 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.

user
string

A unique identifier for your end-user. Helps with abuse detection and monitoring.

max_completion_tokens
integer

Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.

response_format
object

Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.

tools
object[]

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.

tool_choice

Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.

logprobs
boolean

Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.

top_logprobs
integer

Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.

Phạm vi bắt buộc: 0 <= x <= 20
reasoning_effort
string

Reasoning 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.

stream_options
object

Options for streaming. Only valid when stream is true.

service_tier
enum<string>

Specifies the processing tier.

Tùy chọn có sẵn:
auto,
default,
flex,
priority

Phản hồi

Successful chat completion response.

id
string

Unique completion identifier.

Ví dụ:

"chatcmpl_example"

object
enum<string>

Object type. Non-streaming responses use chat.completion.

Tùy chọn có sẵn:
chat.completion
Ví dụ:

"chat.completion"

created
integer

Unix timestamp of creation.

Ví dụ:

1788763703

model
string

The model used (may include version suffix).

Ví dụ:

"gpt-6-astra"

choices
object[]

Array of completion choices.

usage
object

Token accounting for this request. Billing uses these counts.

service_tier
string

Service tier that processed the request, when returned.

system_fingerprint
string | null

Model configuration fingerprint, when returned.

Lần sửa đổi cuối 8 tháng 9, 2026