Chuyển đến nội dung chính
POST
/
v1
/
responses
from openai import OpenAI

client = OpenAI(
    base_url="https://api.cometapi.com/v1",
    api_key="<COMETAPI_KEY>",
)

response = client.responses.create(
    model="gpt-5.4",
    input="Tell me a three sentence bedtime story about a unicorn.",
)

print(response.output_text)
{
  "id": "resp_0a153ae8201f73bc0069a7e8044cc481",
  "object": "response",
  "created_at": 1772611588,
  "status": "completed",
  "background": false,
  "completed_at": 1772611589,
  "error": null,
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "model": "gpt-4.1-nano",
  "output": [
    {
      "id": "msg_0a153ae8201f73bc0069a7e8049a8881",
      "type": "message",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "annotations": [],
          "text": "Four."
        }
      ],
      "role": "assistant"
    }
  ],
  "parallel_tool_calls": true,
  "previous_response_id": null,
  "prompt_cache_key": null,
  "prompt_cache_retention": null,
  "reasoning": {
    "effort": null,
    "summary": null
  },
  "safety_identifier": null,
  "service_tier": "auto",
  "store": true,
  "temperature": 1,
  "text": {
    "format": {
      "type": "text"
    },
    "verbosity": "medium"
  },
  "tool_choice": "auto",
  "tools": [],
  "top_p": 1,
  "truncation": "disabled",
  "usage": {
    "input_tokens": 19,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens": 9,
    "output_tokens_details": {
      "reasoning_tokens": 0
    },
    "total_tokens": 28
  },
  "user": null,
  "metadata": {}
}

Documentation Index

Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt

Use this file to discover all available pages before exploring further.

API Responses mở rộng Chat Completions với các cuộc hội thoại có trạng thái, công cụ tích hợp sẵn, đầu vào tệp multimodal và khả năng kiểm soát reasoning. Đây là endpoint được khuyến nghị cho các mô hình reasoning dòng o-series của OpenAI, dòng GPT-5 và các mô hình Codex.
Các nhà cung cấp mô hình khác nhau hỗ trợ các tham số request khác nhau và trả về các trường response khác nhau. Không phải mọi tham số được liệt kê trong playground phía trên đều hoạt động với mọi mô hình trên CometAPI.

Sử dụng các cuộc hội thoại có trạng thái

Liên kết các response lại với nhau bằng previous_response_id thay vì tự quản lý lịch sử tin nhắn:
from openai import OpenAI

client = OpenAI(
    base_url="https://api.cometapi.com/v1",
    api_key="<COMETAPI_KEY>",
)

# First turn
response = client.responses.create(
    model="gpt-5.4",
    input="What is quantum computing?",
)

# Second turn — previous context is included automatically
follow_up = client.responses.create(
    model="gpt-5.4",
    input="Can you explain that more simply?",
    previous_response_id=response.id,
)

print(follow_up.output_text)

Sử dụng công cụ tích hợp sẵn

API Responses bao gồm các công cụ do nền tảng cung cấp và không cần cấu hình:
Công cụMục đích
web_search_previewTìm kiếm web để lấy thông tin theo thời gian thực
file_searchTìm kiếm trong các tệp đã tải lên
code_interpreterThực thi mã Python trong môi trường sandbox
Để bật một công cụ tích hợp sẵn, hãy thêm nó vào mảng tools:
response = client.responses.create(
    model="gpt-5.4",
    input="Find the current price of Bitcoin",
    tools=[{"type": "web_search_preview"}],
)

print(response.output_text)

Gọi các function tùy chỉnh

Định nghĩa các function mà mô hình có thể gọi bằng đối số có cấu trúc:
response = client.responses.create(
    model="gpt-5.4",
    input="What's the weather in Tokyo?",
    tools=[{
        "type": "function",
        "name": "get_weather",
        "description": "Get current weather for a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }],
)
Khi mô hình gọi một function, mảng output trong response sẽ chứa một mục function_call với tên function và các đối số đã được phân tích. Hãy thực thi function đó và gửi kết quả trở lại trong một request tiếp theo.

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

Để buộc đầu ra JSON khớp với một schema cụ thể, hãy sử dụng tham số text.format:
response = client.responses.create(
    model="gpt-5.4",
    input="List 3 programming languages with their main use cases",
    text={
        "format": {
            "type": "json_schema",
            "name": "languages",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "languages": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string"},
                                "use_case": {"type": "string"}
                            },
                            "required": ["name", "use_case"],
                            "additionalProperties": False
                        }
                    }
                },
                "required": ["languages"],
                "additionalProperties": False
            }
        }
    },
)

Cấu hình reasoning

Đối với các model dòng o-series và GPT-5, hãy kiểm soát độ sâu reasoning bằng reasoning.effort:
response = client.responses.create(
    model="o3",
    input="Solve this step by step: if f(x) = x^3 - 6x^2 + 11x - 6, find all roots.",
    reasoning={"effort": "high"},  # "low", "medium", or "high"
)

print(response.output_text)
Mức reasoning effort cao hơn tạo ra câu trả lời kỹ lưỡng hơn nhưng dùng nhiều tokens hơn. Hãy dùng "low" cho các truy vấn đơn giản và "high" cho các bài toán phức tạp nhiều bước.

Stream phản hồi

Để nhận đầu ra tăng dần, hãy đặt stream thành true. API gửi server-sent events (SSE) theo thứ tự sau:
  1. response.created — Đối tượng phản hồi được khởi tạo
  2. response.in_progress — Bắt đầu sinh nội dung
  3. response.output_item.added — Mục đầu ra mới (message hoặc tool call)
  4. response.content_part.added — Phần nội dung được bắt đầu
  5. response.output_text.delta — Khối văn bản (chứa trường delta)
  6. response.output_text.done — Hoàn tất sinh văn bản cho phần nội dung này
  7. response.content_part.done — Hoàn tất phần nội dung
  8. response.output_item.done — Hoàn tất mục đầu ra
  9. response.completed — Phản hồi đầy đủ với dữ liệu usage
Stream một phản hồi bằng Python SDK:
stream = client.responses.create(
    model="gpt-5.4",
    input="Write a haiku about coding",
    stream=True,
)

for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="")

Để xem các hướng dẫn chuyên sâu cho từng khả năng, hãy tham khảo tài liệu OpenAI: Text · Images · PDF files · Structured Outputs · Function Calling · Conversation State · Built-in Tools · Reasoning

Ủy quyền

Authorization
string
header
bắt buộc

Bearer token authentication. Use your CometAPI key.

Nội dung

application/json
model
string
bắt buộc

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

Ví dụ:

"gpt-5.4"

input
bắt buộc

Text, image, or file inputs to the model, used to generate a response. Can be a simple string for text-only input, or an array of input items for multimodal content (images, files) and multi-turn conversations.

instructions
string

A system (or developer) message inserted into the model's context. When used with previous_response_id, instructions from the previous response are not carried over — this makes it easy to swap system messages between turns.

background
boolean
mặc định:false

Whether to run the model response in the background. Background responses do not return output directly — you retrieve the result later via the response ID.

context_management
object[]

Context management configuration for this request. Controls how the model manages context when the conversation exceeds the context window.

conversation

The conversation this response belongs to. Items from the conversation are prepended to input for context. Input and output items are automatically added to the conversation after the response completes. Cannot be used with previous_response_id.

include
enum<string>[]

Additional output data to include in the response. Use this to request extra information that is not included by default.

Tùy chọn có sẵn:
web_search_call.action.sources,
code_interpreter_call.outputs,
computer_call_output.output.image_url,
file_search_call.results,
message.input_image.image_url,
message.output_text.logprobs,
reasoning.encrypted_content
max_output_tokens
integer

An upper bound for the number of tokens that can be generated for a response, including visible output tokens and reasoning tokens.

max_tool_calls
integer

The maximum number of total calls to built-in tools that can be processed in a response. This limit applies across all built-in tool calls, not per individual tool. Any further tool call attempts by the model will be ignored.

metadata
object

Set of up to 16 key-value pairs that can be attached to the response. Useful for storing additional information in a structured format. Keys have a maximum length of 64 characters; values have a maximum length of 512 characters.

parallel_tool_calls
boolean
mặc định:true

Whether to allow the model to run tool calls in parallel.

previous_response_id
string

The unique ID of a previous response. Use this to create multi-turn conversations without manually managing conversation state. Cannot be used with conversation.

prompt
object

Reference to a prompt template and its variables.

prompt_cache_key
string

A key used to cache responses for similar requests, helping optimize cache hit rates. Replaces the deprecated user field for caching purposes.

prompt_cache_retention
enum<string>

The retention policy for the prompt cache. Set to 24h to keep cached prefixes active for up to 24 hours.

Tùy chọn có sẵn:
in-memory,
24h
reasoning
object

Configuration options for reasoning models (o-series and gpt-5). Controls the depth of reasoning before generating a response.

safety_identifier
string

A stable identifier for your end-users, used to help detect policy violations. Should be a hashed username or email — do not send identifying information directly.

Maximum string length: 64
service_tier
enum<string>

Specifies the processing tier for the request. When set, the response will include the actual service_tier used.

  • auto: Uses the tier configured in project settings (default behavior).
  • default: Standard pricing and performance.
  • flex: Flexible processing with potential cost savings.
  • priority: Priority processing with faster response times.
Tùy chọn có sẵn:
auto,
default,
flex,
priority
store
boolean
mặc định:true

Whether to store the generated response for later retrieval via API.

stream
boolean
mặc định:false

If set to true, the response data will be streamed to the client as it is generated using server-sent events (SSE). Events include response.created, response.output_text.delta, response.completed, and more.

stream_options
object

Options for streaming responses. Only set this when stream is true.

temperature
number
mặc định:1

Sampling temperature between 0 and 2. Higher values (e.g., 0.8) increase randomness; lower values (e.g., 0.2) make output more focused and deterministic. We recommend adjusting either this or top_p, but not both.

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

Configuration for text output. Use this to request structured JSON output via JSON mode or JSON Schema.

tool_choice
mặc định:auto

Controls how the model selects which tool(s) to call.

  • auto (default): The model decides whether and which tools to call.
  • none: The model will not call any tools.
  • required: The model must call at least one tool.
  • An object specifying a particular tool to use.
tools
object[]

An array of tools the model may call while generating a response. CometAPI supports three categories:

  • Built-in tools: Platform-provided tools like web_search_preview and file_search.
  • Function calls: Custom functions you define, enabling the model to call your own code with structured arguments.
  • MCP tools: Integrations with third-party systems via MCP servers.
top_logprobs
integer

Number of most likely tokens to return at each position (0–20), each with an associated log probability. Must include message.output_text.logprobs in the include parameter to receive logprobs.

Phạm vi bắt buộc: 0 <= x <= 20
top_p
number
mặc định:1

Nucleus sampling parameter. The model considers tokens with top_p cumulative probability mass. For example, 0.1 means only the top 10% probability tokens are considered. We recommend adjusting either this or temperature, but not both.

Phạm vi bắt buộc: 0 <= x <= 1
truncation
enum<string>
mặc định:disabled

The truncation strategy for handling inputs that exceed the model's context window.

  • auto: The model truncates the input by dropping items from the beginning of the conversation to fit.
  • disabled (default): The request fails with a 400 error if the input exceeds the context window.
Tùy chọn có sẵn:
auto,
disabled
user
string
không còn sử dụng

Deprecated. Use safety_identifier and prompt_cache_key instead. A stable identifier for your end-user.

Phản hồi

200 - application/json

The generated Response object.

id
string

Unique identifier for the response.

Ví dụ:

"resp_0a153ae8201f73bc0069a7e8044cc481"

object
enum<string>

The object type, always response.

Tùy chọn có sẵn:
response
Ví dụ:

"response"

created_at
integer

Unix timestamp (in seconds) of when the response was created.

Ví dụ:

1772611588

status
enum<string>

The status of the response.

Tùy chọn có sẵn:
completed,
in_progress,
failed,
cancelled,
queued
Ví dụ:

"completed"

background
boolean

Whether the response was run in the background.

Ví dụ:

false

completed_at
integer | null

Unix timestamp of when the response was completed, or null if still in progress.

Ví dụ:

1772611589

error
object

Error information if the response failed, or null on success.

incomplete_details
object

Details about why the response is incomplete, if applicable.

instructions
string | null

The system instructions used for this response.

max_output_tokens
integer | null

The maximum output token limit that was applied.

model
string

The model used for the response.

Ví dụ:

"gpt-4.1-nano"

output
object[]

An array of output items generated by the model. Each item can be a message, function call, or other output type.

output_text
string

A convenience field containing the concatenated text output from all output message items.

parallel_tool_calls
boolean

Whether parallel tool calls were enabled.

previous_response_id
string | null

The ID of the previous response, if this is a multi-turn conversation.

reasoning
object

The reasoning configuration that was used.

service_tier
string

The service tier actually used to process the request.

Ví dụ:

"default"

store
boolean

Whether the response was stored.

temperature
number

The temperature value used.

Ví dụ:

1

text
object

The text configuration used.

tool_choice

The tool choice setting used.

tools
object[]

The tools that were available for this response.

top_p
number

The top_p value used.

Ví dụ:

1

truncation
string

The truncation strategy used.

usage
object

Token usage statistics for this response.

user
string | null

The user identifier, if provided.

metadata
object

The metadata attached to this response.

content_filters
array | null

Content filter results applied to the response, if any.

frequency_penalty
number
mặc định:0

The frequency penalty applied to the request.

max_tool_calls
integer | null

Maximum number of tool calls allowed, if set.

presence_penalty
number
mặc định:0

The presence penalty applied to the request.

prompt_cache_key
string | null

Cache key for prompt caching, if applicable.

prompt_cache_retention
string | null

Prompt cache retention policy, if applicable.

safety_identifier
string | null

Safety system identifier for the response, if applicable.

top_logprobs
integer
mặc định:0

Number of top log probabilities returned per token position.