

openapi: 3.0.3
info:
  title: Teacher Helper Chat Server API
  description: OpenAPI格式的LLM聊天服务器，支持流式对话
  version: 1.0.0
  contact:
    name: Teacher Helper Team
    email: support@teacherhelper.com

servers:
  - url: http://localhost:8080
    description: 开发服务器
  - url: https://api.teacherhelper.com
    description: 生产服务器

paths:
  /v1/chat/completions:
    post:
      summary: 创建聊天完成
      description: 发送消息到LLM并获取响应，支持流式和非流式模式
      operationId: createChatCompletion
      tags:
        - Chat
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
            examples:
              simple_chat:
                summary: 简单对话
                value:
                  model: 'gpt-3.5-turbo'
                  messages:
                    - role: 'user'
                      content: '你好，请介绍一下自己'
                  max_tokens: 150
                  temperature: 0.7
              streaming_chat:
                summary: 流式对话
                value:
                  model: 'gpt-3.5-turbo'
                  messages:
                    - role: 'system'
                      content: '你是一个有用的AI助手'
                    - role: 'user'
                      content: '请解释什么是机器学习'
                  stream: true
                  max_tokens: 500
                  temperature: 0.8
              tool_call_chat:
                summary: 工具调用对话
                value:
                  model: 'gpt-3.5-turbo'
                  messages:
                    - role: 'user'
                      content: '请帮我计算 25 + 15 * 3 的结果，并查询北京的天气'
                  tools: true
                  tool_choice: 'auto'
                  max_tokens: 400
              streaming_tool_call:
                summary: 流式工具调用对话
                value:
                  model: 'gpt-3.5-turbo'
                  messages:
                    - role: 'user'
                      content: '请计算圆周率乘以5，并获取当前时间'
                  tools: true
                  tool_choice: 'auto'
                  stream: true
                  max_tokens: 500
      responses:
        '200':
          description: 成功响应
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
            text/event-stream:
              schema:
                type: string
                description: 服务器发送事件流
              examples:
                streaming_response:
                  summary: 流式响应示例
                  value: |
                    data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"delta":{"content":"你好"},"index":0,"finish_reason":null}]}

                    data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"delta":{"content":"！"},"index":0,"finish_reason":null}]}

                    data: [DONE]
        '400':
          description: 请求参数错误
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: 认证失败
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '429':
          description: 请求过于频繁
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: 服务器内部错误
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

  /v1/models:
    get:
      summary: 获取可用模型列表
      description: 返回所有可用的LLM模型
      operationId: listModels
      tags:
        - Models
      responses:
        '200':
          description: 成功响应
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ModelList'

  /v1/tools:
    get:
      summary: 获取可用工具列表
      description: 返回所有可用的工具及其描述
      operationId: listTools
      tags:
        - Tools
      responses:
        '200':
          description: 成功响应
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ToolList'

components:
  schemas:
    ChatCompletionRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
          description: 要使用的模型ID
          example: 'gpt-3.5-turbo'
        messages:
          type: array
          description: 对话消息列表
          items:
            $ref: '#/components/schemas/Message'
        max_tokens:
          type: integer
          description: 生成的最大token数
          minimum: 1
          maximum: 4096
          default: 150
        temperature:
          type: number
          description: 控制随机性的参数
          minimum: 0
          maximum: 2
          default: 1
        top_p:
          type: number
          description: 核采样参数
          minimum: 0
          maximum: 1
          default: 1
        stream:
          type: boolean
          description: 是否启用流式响应
          default: false
        stop:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
          description: 停止生成的标记
        presence_penalty:
          type: number
          description: 存在惩罚参数
          minimum: -2
          maximum: 2
          default: 0
        frequency_penalty:
          type: number
          description: 频率惩罚参数
          minimum: -2
          maximum: 2
          default: 0
        user:
          type: string
          description: 用户标识符
        tools:
          type: boolean
          description: 是否启用工具调用
          default: false
        tool_choice:
          oneOf:
            - type: string
              enum: ['none', 'auto']
            - type: object
              properties:
                type:
                  type: string
                  enum: ['function']
                function:
                  type: object
                  properties:
                    name:
                      type: string
          description: 工具选择策略
          default: 'auto'

    Message:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum: ['system', 'user', 'assistant']
          description: 消息角色
        content:
          type: string
          description: 消息内容
        name:
          type: string
          description: 消息发送者名称

    ChatCompletionResponse:
      type: object
      properties:
        id:
          type: string
          description: 完成ID
        object:
          type: string
          enum: ['chat.completion']
          description: 对象类型
        created:
          type: integer
          description: 创建时间戳
        model:
          type: string
          description: 使用的模型
        choices:
          type: array
          items:
            $ref: '#/components/schemas/Choice'
        usage:
          $ref: '#/components/schemas/Usage'

    Choice:
      type: object
      properties:
        index:
          type: integer
          description: 选择索引
        message:
          $ref: '#/components/schemas/Message'
        finish_reason:
          type: string
          enum: ['stop', 'length', 'content_filter']
          description: 完成原因

    Usage:
      type: object
      properties:
        prompt_tokens:
          type: integer
          description: 输入token数
        completion_tokens:
          type: integer
          description: 输出token数
        total_tokens:
          type: integer
          description: 总token数

    ModelList:
      type: object
      properties:
        object:
          type: string
          enum: ['list']
        data:
          type: array
          items:
            $ref: '#/components/schemas/Model'

    Model:
      type: object
      properties:
        id:
          type: string
          description: 模型ID
        object:
          type: string
          enum: ['model']
        created:
          type: integer
          description: 创建时间戳
        owned_by:
          type: string
          description: 模型所有者

    ToolList:
      type: object
      properties:
        object:
          type: string
          enum: ['list']
        data:
          type: array
          items:
            $ref: '#/components/schemas/Tool'
        stats:
          $ref: '#/components/schemas/ToolStats'

    Tool:
      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格式）

    ToolStats:
      type: object
      properties:
        total_tools:
          type: integer
          description: 工具总数
        tools:
          type: object
          description: 各工具的详细统计信息

    ToolCall:
      type: object
      properties:
        id:
          type: string
          description: 工具调用ID
        type:
          type: string
          enum: ['function']
        function:
          type: object
          properties:
            name:
              type: string
              description: 工具名称
            arguments:
              type: string
              description: 工具参数（JSON字符串）

    ToolMessage:
      type: object
      properties:
        role:
          type: string
          enum: ['tool']
        tool_call_id:
          type: string
          description: 对应的工具调用ID
        name:
          type: string
          description: 工具名称
        content:
          type: string
          description: 工具执行结果

    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              description: 错误消息
            type:
              type: string
              description: 错误类型
            param:
              type: string
              description: 相关参数
            code:
              type: string
              description: 错误代码

  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

security:
  - BearerAuth: []

tags:
  - name: Chat
    description: 聊天对话相关接口
  - name: Models
    description: 模型管理相关接口
  - name: Tools
    description: 工具调用相关接口
