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

# 建立聊天對話

> 根據對話歷史建立模型回應。支援串流與非串流回應。

相容 OpenAI Chat Completions API。



## OpenAPI

````yaml api-reference/zh-tw/openapi.json POST /v1/chat/completions
openapi: 3.0.0
info:
  title: 出海營 API 參考文件
  version: 1.0.0
  description: Public AI Gateway API Reference
servers:
  - url: https://api.aiid.edu.kg
security:
  - BearerAuth: []
tags:
  - name: OpenAI格式（Chat）
  - name: OpenAI格式（Responses）
  - name: 圖片生成 Gemini 格式
  - name: 圖片生成OpenAI DALL-E 格式
  - name: 獲取模型列表
  - name: 影片生成HappyHorse與 Wan
  - name: 影片生成Kling格式
  - name: 影片生成 Omni 與 Veo 格式
  - name: 影片生成 Seedance 格式
  - name: 影片生成Sora相容格式
  - name: 影片生成 Vidu 格式
  - name: 音樂生成任務格式
paths:
  /v1/chat/completions:
    post:
      tags:
        - OpenAI格式（Chat）
      summary: 建立聊天對話
      description: |-
        根據對話歷史建立模型回應。支援串流與非串流回應。

        相容 OpenAI Chat Completions API。
      operationId: createChatCompletion
      parameters: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
        required: true
      responses:
        '200':
          description: 成功建立回應
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
          headers: {}
        '400':
          description: 請求參數錯誤
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          headers: {}
        '429':
          description: 請求頻率限制
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          headers: {}
      deprecated: false
      security:
        - BearerAuth: []
components:
  schemas:
    ChatCompletionRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
          description: 模型 ID
          example: gpt-4
        messages:
          type: array
          items:
            $ref: '#/components/schemas/Message'
          description: 對話訊息列表
        temperature:
          type: number
          minimum: 0
          maximum: 2
          default: 1
          description: 取樣溫度
        top_p:
          type: number
          minimum: 0
          maximum: 1
          default: 1
          description: 核採樣參數
        'n':
          type: integer
          minimum: 1
          default: 1
          description: 生成數量
        stream:
          type: boolean
          default: false
          description: 是否串流回應
        stream_options:
          type: object
          properties:
            include_usage:
              type: boolean
        stop:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
          description: 停止序列
        max_tokens:
          type: integer
          description: 最大生成 Token 數
        max_completion_tokens:
          type: integer
          description: 最大生成 Token 數
        presence_penalty:
          type: number
          minimum: -2
          maximum: 2
          default: 0
        frequency_penalty:
          type: number
          minimum: -2
          maximum: 2
          default: 0
        logit_bias:
          type: object
          additionalProperties:
            type: number
          properties: {}
        user:
          type: string
        tools:
          type: array
          items:
            $ref: '#/components/schemas/Tool'
        tool_choice:
          oneOf:
            - type: string
              enum:
                - none
                - auto
                - required
            - type: object
              properties:
                type:
                  type: string
                function:
                  type: object
                  properties:
                    name:
                      type: string
        response_format:
          $ref: '#/components/schemas/ResponseFormat'
        seed:
          type: integer
        reasoning_effort:
          type: string
          enum:
            - low
            - medium
            - high
          description: 推理強度 (用於支援推理的模型)
        modalities:
          type: array
          items:
            type: string
            enum:
              - text
              - audio
        audio:
          type: object
          properties:
            voice:
              type: string
            format:
              type: string
    ChatCompletionResponse:
      type: object
      properties:
        id:
          type: string
        object:
          type: string
          example: chat.completion
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
              message:
                $ref: '#/components/schemas/Message'
              finish_reason:
                type: string
                enum:
                  - stop
                  - length
                  - tool_calls
                  - content_filter
        usage:
          $ref: '#/components/schemas/Usage'
        system_fingerprint:
          type: string
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              description: 錯誤訊息
            type:
              type: string
              description: 錯誤類型
            param:
              type: string
              description: 相關參數
              nullable: true
            code:
              type: string
              description: 錯誤代碼
              nullable: true
    Message:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - system
            - user
            - assistant
            - tool
            - developer
          description: 訊息角色
        content:
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/MessageContent'
          description: 訊息內容
        name:
          type: string
          description: 發送者名稱
        tool_calls:
          type: array
          items:
            $ref: '#/components/schemas/ToolCall'
        tool_call_id:
          type: string
          description: 工具呼叫 ID（用於 tool 角色訊息）
        reasoning_content:
          type: string
          description: 推理內容
    Tool:
      type: object
      properties:
        type:
          type: string
          example: function
        function:
          type: object
          properties:
            name:
              type: string
            description:
              type: string
            parameters:
              type: object
              description: JSON Schema 格式的參數定義
              properties: {}
    ResponseFormat:
      type: object
      properties:
        type:
          type: string
          enum:
            - text
            - json_object
            - json_schema
        json_schema:
          type: object
          description: JSON Schema 定義
          properties: {}
    Usage:
      type: object
      properties:
        prompt_tokens:
          type: integer
          description: 提示詞 Token 數
        completion_tokens:
          type: integer
          description: 補全 Token 數
        total_tokens:
          type: integer
          description: 總 Token 數
        prompt_tokens_details:
          type: object
          properties:
            cached_tokens:
              type: integer
            text_tokens:
              type: integer
            audio_tokens:
              type: integer
            image_tokens:
              type: integer
        completion_tokens_details:
          type: object
          properties:
            text_tokens:
              type: integer
            audio_tokens:
              type: integer
            reasoning_tokens:
              type: integer
    MessageContent:
      type: object
      properties:
        type:
          type: string
          enum:
            - text
            - image_url
            - input_audio
            - file
            - video_url
        text:
          type: string
        image_url:
          type: object
          properties:
            url:
              type: string
              description: 圖片 URL 或 base64
            detail:
              type: string
              enum:
                - low
                - high
                - auto
        input_audio:
          type: object
          properties:
            data:
              type: string
              description: Base64 編碼的音訊資料
            format:
              type: string
              enum:
                - wav
                - mp3
        file:
          type: object
          properties:
            filename:
              type: string
            file_data:
              type: string
            file_id:
              type: string
        video_url:
          type: object
          properties:
            url:
              type: string
    ToolCall:
      type: object
      properties:
        id:
          type: string
        type:
          type: string
          example: function
        function:
          type: object
          properties:
            name:
              type: string
            arguments:
              type: string
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: |-
        使用 Bearer Token 認證。
        格式: `Authorization: Bearer sk-xxxxxx`

````