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

# OpenAI 格式 - 调用工具对话

> - 适用于所有兼容 OpenAI 格式模型的通用 Chat Completions API（含工具调用）
- 工具调用：支持 Function Calling，定义模型可调用的工具
- 结构化输出：支持 JSON Object / JSON Schema 格式
- 多模态输入：支持文本 + 图像混合输入
- 支持流式和非流式响应




## OpenAPI

````yaml openapi/zh/openai-format-chat-completions.json POST /v1/chat/completions
openapi: 3.1.0
info:
  title: OpenAI Format - Tool Calling Chat
  version: '1.0'
servers:
  - url: https://api.aihubmax.com
security:
  - BearerAuth: []
paths:
  /v1/chat/completions:
    post:
      tags:
        - Text Series > OpenAI Format
      summary: OpenAI Format - Tool Calling Chat
      description: |
        - 适用于所有兼容 OpenAI 格式模型的通用 Chat Completions API（含工具调用）
        - 工具调用：支持 Function Calling，定义模型可调用的工具
        - 结构化输出：支持 JSON Object / JSON Schema 格式
        - 多模态输入：支持文本 + 图像混合输入
        - 支持流式和非流式响应
      operationId: openai-format-chat-completions
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
            examples:
              single-turn:
                summary: 单轮对话
                description: 最基础的单轮问答
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content: 用 Python 写一个快速排序算法
              multi-turn:
                summary: 多轮对话
                description: 包含上下文的多轮对话
                value:
                  model: gpt-5.4
                  messages:
                    - role: system
                      content: 你是一位资深的后端工程师
                    - role: user
                      content: 我的 API 响应时间从 200ms 增长到了 2s，可能是什么原因？
                    - role: assistant
                      content: |-
                        响应时间大幅增长通常有几个常见原因：
                        1. 数据库查询变慢（缺少索引、数据量增长）
                        2. 外部服务调用超时
                        3. 内存泄漏导致 GC 频繁
                        4. 网络延迟增加

                        你能提供更多信息吗？比如是所有接口都变慢还是特定接口？
                    - role: user
                      content: 只有涉及用户列表查询的接口变慢了
              streaming:
                summary: 流式响应
                description: 启用流式输出，实时获取生成内容
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content: 详细解释 TCP 三次握手的过程
                  stream: true
              with-image:
                summary: 带图片
                description: 发送图片让模型分析（视觉理解）
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content:
                        - type: text
                          text: 这个架构图有什么问题？请指出潜在的性能瓶颈
                        - type: image_url
                          image_url:
                            url: >-
                              https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg
              tool-calling-weather:
                summary: 工具调用 - 天气查询
                description: 让模型调用天气查询工具获取实时天气信息
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content: 北京今天天气怎么样？适合户外跑步吗？
                  tools:
                    - type: function
                      function:
                        name: get_weather
                        description: 获取指定城市的当前天气信息
                        parameters:
                          type: object
                          properties:
                            city:
                              type: string
                              description: 城市名称
                            unit:
                              type: string
                              enum:
                                - celsius
                                - fahrenheit
                              description: 温度单位
                          required:
                            - city
              tool-calling-search:
                summary: 工具调用 - 网页搜索
                description: 让模型调用搜索工具查找最新信息
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content: 帮我查一下 GPT-5 最新的发布消息
                  tools:
                    - type: function
                      function:
                        name: web_search
                        description: 在互联网上搜索信息
                        parameters:
                          type: object
                          properties:
                            query:
                              type: string
                              description: 搜索关键词
                            num_results:
                              type: integer
                              description: 返回结果数量
                              default: 5
                          required:
                            - query
              tool-calling-database:
                summary: 工具调用 - 数据查询
                description: 让模型调用数据查询工具获取业务数据
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content: 查一下上个月销售额最高的前5个产品
                  tools:
                    - type: function
                      function:
                        name: query_database
                        description: 查询业务数据库获取数据
                        parameters:
                          type: object
                          properties:
                            query:
                              type: string
                              description: SQL 查询语句
                            database:
                              type: string
                              enum:
                                - sales
                                - inventory
                                - users
                              description: 目标数据库
                          required:
                            - query
                            - database
      responses:
        '200':
          $ref: '#/components/responses/ChatCompletionSuccess'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '402':
          $ref: '#/components/responses/PaymentRequired'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'
components:
  schemas:
    ChatCompletionRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
          description: 模型名称，如 gpt-5.4、deepseek-chat 等
          example: gpt-5.4
        messages:
          type: array
          description: 对话消息列表
          items:
            type: object
            required:
              - role
              - content
            properties:
              role:
                type: string
                description: 消息发送者的角色
                enum:
                  - system
                  - user
                  - assistant
                  - tool
              content:
                type: string
                description: 消息内容
        stream:
          type: boolean
          description: 是否流式返回响应
          default: false
        temperature:
          type: number
          description: 采样温度 (0-2)
          minimum: 0
          maximum: 2
        max_tokens:
          type: integer
          description: 最大生成token数
        top_p:
          type: number
          description: 核采样参数 (0-1)
          minimum: 0
          maximum: 1
        frequency_penalty:
          type: number
          description: 频率惩罚 (-2 到 2)
          minimum: -2
          maximum: 2
        presence_penalty:
          type: number
          description: 存在惩罚 (-2 到 2)
          minimum: -2
          maximum: 2
        tools:
          type: array
          description: 可用工具列表
          items:
            type: object
            properties:
              type:
                type: string
                enum:
                  - function
              function:
                type: object
                properties:
                  name:
                    type: string
                    description: 函数名称
                  description:
                    type: string
                    description: 函数描述
                  parameters:
                    type: object
                    description: 函数参数的 JSON Schema
                required:
                  - name
        tool_choice:
          description: 工具选择策略
          oneOf:
            - type: string
              enum:
                - none
                - auto
                - required
            - type: object
              properties:
                type:
                  type: string
                  enum:
                    - function
                function:
                  type: object
                  properties:
                    name:
                      type: string
                  required:
                    - name
        response_format:
          type: object
          description: 响应格式
          properties:
            type:
              type: string
              description: 响应格式类型
              enum:
                - text
                - json_object
                - json_schema
        stop:
          description: 停止序列
          oneOf:
            - type: string
            - type: array
              items:
                type: string
        'n':
          type: integer
          description: 生成数量
    ChatCompletionResponse:
      type: object
      properties:
        id:
          type: string
          description: 唯一补全ID
          example: chatcmpl-abc123
        object:
          type: string
          description: 对象类型
          enum:
            - chat.completion
        created:
          type: integer
          description: 创建时间戳
          example: 1757165031
        model:
          type: string
          description: 使用的模型
        choices:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
                description: 选项索引
                example: 0
              message:
                type: object
                properties:
                  role:
                    type: string
                    example: assistant
                  content:
                    type: string
                    example: Hello! How can I help you today?
              finish_reason:
                type: string
                description: 生成停止原因
                enum:
                  - stop
                  - length
                  - content_filter
                  - tool_calls
                example: stop
        usage:
          type: object
          properties:
            prompt_tokens:
              type: integer
              description: 提示token数
              example: 20
            completion_tokens:
              type: integer
              description: 补全token数
              example: 50
            total_tokens:
              type: integer
              description: 总token数
              example: 70
    ErrorResponse400:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 请求参数无效
            type:
              type: string
              example: invalid_request_error
    ErrorResponse401:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: API密钥无效
            type:
              type: string
              example: authentication_error
    ErrorResponse403:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 访问被拒绝
            type:
              type: string
              example: permission_error
    ErrorResponse429:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 请求频率超限
            type:
              type: string
              example: rate_limit_error
    ErrorResponse500:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 服务器内部错误
            type:
              type: string
              example: server_error
  responses:
    ChatCompletionSuccess:
      description: 对话补全响应
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ChatCompletionResponse'
    BadRequest:
      description: 请求错误
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse400'
    Unauthorized:
      description: 未授权
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse401'
    PaymentRequired:
      description: 余额不足
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse400'
    Forbidden:
      description: 禁止访问
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse403'
    TooManyRequests:
      description: 请求过多
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse429'
    InternalServerError:
      description: 服务器内部错误
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse500'
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: |
        ## 所有接口均需要使用Bearer Token进行认证 ##

        使用时在请求头中添加：

        `Authorization: Bearer YOUR_API_KEY`

````