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

# Completions

<Note>
  请求参数及响应参数与官方一致，
  具体参考<a href="https://ai.google.dev/gemini-api/docs/text-generation" target="_blank">Google官方文档</a>。
</Note>


## OpenAPI

````yaml openapi/cn/llm.json POST /gemini/chat/completions
openapi: 3.1.0
info:
  title: LLM API DOCS
  version: 1.0.0
  description: TTAPI LLM API 接口服务。
servers:
  - url: https://api.ttapi.io
security: []
paths:
  /gemini/chat/completions:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GeminiRequest'
      responses:
        '200':
          description: 成功响应（stream=false 返回JSON，stream=true 返回Gemini流式）
          content:
            application/json:
              schema:
                type: object
                properties:
                  choices:
                    type: array
                    description: 响应结果列表
                    items:
                      type: object
                      properties:
                        finish_reason:
                          type: string
                          description: 响应停止原因
                          example: stop
                        index:
                          type: integer
                          description: 结果索引
                          example: 0
                        message:
                          type: object
                          description: 响应消息体
                          properties:
                            content:
                              type: string
                              description: Gemini 生成的响应内容
                              example: |-
                                你好！很高兴和你聊天。

                                有什么可以帮你的吗？或者想聊点什么？ 😊
                            role:
                              type: string
                              description: 消息角色
                              example: assistant
                          required:
                            - content
                            - role
                      required:
                        - finish_reason
                        - index
                        - message
                  created:
                    type: integer
                    description: 响应创建时间戳（秒）
                    example: 1757472417
                  id:
                    type: string
                    description: 请求唯一标识
                    example: oebAaMTvMfCUjMcP_prYwAs
                  model:
                    type: string
                    description: 使用的 Gemini 模型版本
                    example: gemini-2.5-pro
                  object:
                    type: string
                    description: 响应对象类型
                    example: chat.completion
                  usage:
                    type: object
                    description: token 使用统计
                    properties:
                      completion_tokens:
                        type: integer
                        description: 生成内容消耗的 token 数
                        example: 21
                      prompt_tokens:
                        type: integer
                        description: 提示词消耗的 token 数
                        example: 3
                      total_tokens:
                        type: integer
                        description: 总消耗 token 数
                        example: 1466
                    required:
                      - completion_tokens
                      - prompt_tokens
                      - total_tokens
                example:
                  choices:
                    - finish_reason: stop
                      index: 0
                      message:
                        content: |-
                          你好！很高兴和你聊天。

                          有什么可以帮你的吗？或者想聊点什么？ 😊
                        role: assistant
                  created: 1757472417
                  id: oebAaMTvMfCUjMcP_prYwAs
                  model: gemini-2.5-pro
                  object: chat.completion
                  usage:
                    completion_tokens: 21
                    prompt_tokens: 3
                    total_tokens: 1466
                required:
                  - choices
                  - created
                  - id
                  - model
                  - object
                  - usage
            text/event-stream:
              schema:
                type: string
                description: Gemini SSE 流式输出，包含 id:null + data:{} + retry:3000 格式
              examples:
                stream-example:
                  summary: Gemini 流式输出 (stream=true)
                  value: >-
                    id:null


                    data:{"choices":[{"delta":{"content":"你好啊！\n\n很高兴和你交流，有什么可以帮助你的吗？无论是解答问题、寻找灵感，还是只想随便","role":"assistant"},"index":0}],"created":1757472443,"id":"sebAaI_bM5qOjMcPmNGeyQs","model":"gemini-2.5-pro","object":"chat.completion.chunk","usage":{"completion_tokens":26,"prompt_tokens":3,"total_tokens":1074}}


                    retry:3000


                    id:null


                    data:{"choices":[{"delta":{"content":"聊聊，我都在这里。😊","role":"assistant"},"finish_reason":"stop","index":0}],"created":1757472443,"id":"sebAaI_bM5qOjMcPmNGeyQs","model":"gemini-2.5-pro","object":"chat.completion.chunk","usage":{"completion_tokens":34,"prompt_tokens":3,"total_tokens":1082}}


                    retry:3000
        '400':
          $ref: '#/components/responses/400Response'
        '401':
          $ref: '#/components/responses/401Response'
      security:
        - CustomApiKey: []
components:
  schemas:
    GeminiRequest:
      type: object
      properties:
        messages:
          type: array
          items:
            type: object
            properties:
              role:
                type: string
              content:
                type: string
          example:
            - role: user
              content: Hello!
        model:
          type: string
          description: TTAPI 支持使用的模型，详见[Gemini支持模型](/grids/cn/start/pricing/llm)
        stream:
          type: boolean
          default: 'false'
          description: 是否使用服务器发送的事件逐步传输响应
      required:
        - messages
        - model
  responses:
    400Response:
      description: 参数错误
      content:
        application/json:
          schema:
            type: object
            properties:
              status:
                type: string
                example: FAILED
              message:
                type: string
                example: '"prompt" cannot be empty.'
              data:
                type: object
            required:
              - status
              - message
    401Response:
      description: 授权失败
      content:
        application/json:
          schema:
            type: object
            properties:
              status:
                type: string
                example: FAILED
              message:
                type: string
                example: Wrong TT-API-KEY or email is not activated.
            required:
              - status
              - message
  securitySchemes:
    CustomApiKey:
      type: apiKey
      in: header
      name: TT-API-KEY
      description: >-
        请前往 <a href='https://dashboard.ttapi.io' target='_blank'>TTAPI 控制台</a>
        获取 API 密钥。

````