> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenlab.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Tạo phản hồi

> Tạo một phản hồi theo định dạng Responses API của OpenAI

The Responses API là API hội thoại có trạng thái mới hơn của OpenAI. TokenLab hỗ trợ định dạng này như một đường dẫn tùy chọn nâng cao cho các mô hình tương thích; sử dụng `POST /v1/chat/completions` làm đường dẫn mặc định tương thích với OpenAI trừ khi bạn rõ ràng cần các hành vi đặc thù của Responses.

## Nội dung yêu cầu

<ParamField body="model" type="string" required>
  ID của mô hình để sử dụng. Xem [Models](https://tokenlab.sh/vi/models) để biết các lựa chọn có sẵn.
</ParamField>

<ParamField body="input" type="array" required>
  Một danh sách các mục đầu vào tạo thành cuộc hội thoại.

  Mỗi mục có thể là:

  * `message`: Một tin nhắn hội thoại với role và content
  * `function_call`: Một yêu cầu gọi hàm
  * `function_call_output`: Kết quả từ một cuộc gọi hàm

  Đối với đầu vào đa phương thức, `message.content` có thể là một chuỗi văn bản thuần hoặc một mảng các khối nội dung. Đối với các mô hình có khả năng xử lý hình ảnh như các biến thể GPT-5.4, truyền hình ảnh dưới dạng các khối `input_image` thay vì nhúng URL hoặc chuỗi Base64 trực tiếp vào văn bản thuần.

  Ví dụ các khối nội dung:

  * `{ "type": "input_text", "text": "Describe this image" }`
  * `{ "type": "input_image", "image_url": "https://example.com/image.jpg" }`
  * `{ "type": "input_image", "image_url": "data:image/png;base64,..." }`
</ParamField>

<ParamField body="instructions" type="string">
  Hướng dẫn hệ thống cho mô hình (tương đương với system message).
</ParamField>

<ParamField body="max_output_tokens" type="integer">
  Số token tối đa để sinh.
</ParamField>

<ParamField body="temperature" type="number" default="1">
  Nhiệt độ sampling trong khoảng 0 đến 2.
</ParamField>

<ParamField body="tools" type="array">
  Danh sách công cụ mà mô hình có thể gọi.

  Với tool hosted `image_generation` dùng model tool ảnh mặc định hoặc đặt rõ `model: "gpt-image-2"`, TokenLab sẽ xóa `input_fidelity` không được hỗ trợ trước khi chuyển tiếp request, vì GPT Image 2 đã xử lý input ảnh ở high fidelity. Đừng gửi `background: "transparent"` cho tool này; TokenLab không âm thầm xóa trường đó vì nó làm thay đổi ngữ nghĩa output.
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  Nếu true, trả về một luồng sự kiện.
</ParamField>

<ParamField body="previous_response_id" type="string">
  ID của một phản hồi trước để tiếp tục cuộc hội thoại từ đó.
</ParamField>

<ParamField body="store" type="boolean" default="true">
  Có lưu phản hồi để truy xuất sau hay không.
</ParamField>

<ParamField body="metadata" type="object">
  Metadata để đính kèm vào phản hồi cho mục đích theo dõi.
</ParamField>

<ParamField body="text" type="object">
  Các tuỳ chọn cấu hình sinh văn bản. Hành vi cho `text.format` phụ thuộc vào mô hình được chọn và đường dẫn được định tuyến; không được đảm bảo đồng nhất trên mọi mô hình.
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean" default="true">
  Cho phép gọi nhiều công cụ song song hay không.
</ParamField>

<ParamField body="top_p" type="number">
  Tham số lấy mẫu Nucleus (0-1).
</ParamField>

<ParamField body="reasoning" type="object">
  Cấu hình reasoning cho các mô hình có hỗ trợ reasoning như các biến thể thuộc họ GPT-5.

  * `effort` (string): Mức độ nỗ lực reasoning (`low`, `medium`, `high`)
</ParamField>

## Phản hồi

<ResponseField name="id" type="string">
  Định danh duy nhất cho phản hồi.
</ResponseField>

<ResponseField name="object" type="string">
  Luôn là `response`.
</ResponseField>

<ResponseField name="created" type="integer">
  Dấu thời Unix khi phản hồi được tạo.
</ResponseField>

<ResponseField name="output" type="array">
  Danh sách các mục đầu ra do mô hình sinh ra.
</ResponseField>

<ResponseField name="usage" type="object">
  Thống kê sử dụng token.
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.tokenlab.sh/v1/responses" \
    -H "Authorization: Bearer sk-your-api-key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o",
      "input": [
        {"type": "message", "role": "user", "content": "Hello!"}
      ],
      "max_output_tokens": 1000
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="sk-your-api-key",
      base_url="https://api.tokenlab.sh/v1"
  )

  response = client.responses.create(
      model="gpt-4o",
      input=[
          {"type": "message", "role": "user", "content": "Hello!"}
      ],
      max_output_tokens=1000
  )

  print(response.output)
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from 'openai';

  const client = new OpenAI({
    apiKey: 'sk-your-api-key',
    baseURL: 'https://api.tokenlab.sh/v1'
  });

  const response = await client.responses.create({
    model: 'gpt-4o',
    input: [
      { type: 'message', role: 'user', content: 'Hello!' }
    ],
    max_output_tokens: 1000
  });

  console.log(response.output);
  ```

  ```go Go theme={null}
  package main

  import (
      "bytes"
      "encoding/json"
      "fmt"
      "net/http"
  )

  func main() {
      payload := map[string]interface{}{
          "model": "gpt-4o",
          "input": []map[string]interface{}{
              {"type": "message", "role": "user", "content": "Hello!"},
          },
          "max_output_tokens": 1000,
      }
      body, _ := json.Marshal(payload)

      req, _ := http.NewRequest("POST", "https://api.tokenlab.sh/v1/responses", bytes.NewBuffer(body))
      req.Header.Set("Authorization", "Bearer sk-your-api-key")
      req.Header.Set("Content-Type", "application/json")

      client := &http.Client{}
      resp, _ := client.Do(req)
      defer resp.Body.Close()

      var result map[string]interface{}
      json.NewDecoder(resp.Body).Decode(&result)
      fmt.Println(result["output"])
  }
  ```

  ```php PHP theme={null}
  <?php
  $ch = curl_init('https://api.tokenlab.sh/v1/responses');

  curl_setopt_array($ch, [
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POST => true,
      CURLOPT_HTTPHEADER => [
          'Content-Type: application/json',
          'Authorization: Bearer sk-your-api-key'
      ],
      CURLOPT_POSTFIELDS => json_encode([
          'model' => 'gpt-4o',
          'input' => [
              ['type' => 'message', 'role' => 'user', 'content' => 'Hello!']
          ],
          'max_output_tokens' => 1000
      ])
  ]);

  $response = curl_exec($ch);
  curl_close($ch);

  $data = json_decode($response, true);
  print_r($data['output']);
  ```

  ## Ví dụ Đầu vào Thị giác

  Sử dụng các mô hình có khả năng xử lý hình ảnh bằng cách đặt hình ảnh bên trong `message.content` dưới dạng các khối `input_image`. Giá trị `image_url` có thể là một URL công khai hoặc một data URL Base64.

  ```json theme={null}
  {
    "model": "gpt-5.4",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": [
          {
            "type": "input_text",
            "text": "Please describe this image."
          },
          {
            "type": "input_image",
            "image_url": "https://example.com/demo.jpg"
          }
        ]
      }
    ]
  }
  ```

  ```json theme={null}
  {
    "model": "gpt-5.4",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": [
          {
            "type": "input_text",
            "text": "Please describe this image."
          },
          {
            "type": "input_image",
            "image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..."
          }
        ]
      }
    ]
  }
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "id": "resp_abc123",
    "object": "response",
    "created": 1706000000,
    "model": "gpt-4o",
    "output": [
      {
        "type": "message",
        "role": "assistant",
        "content": [
          {"type": "text", "text": "Hello! How can I help you today?"}
        ]
      }
    ],
    "usage": {
      "input_tokens": 10,
      "output_tokens": 12,
      "total_tokens": 22
    }
  }
  ```
</ResponseExample>
