> ## 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.

# Gemini 格式 - 音乐生成

> - 使用 Gemini 原生格式的 generateContent 接口，通过 Lyria 3 模型生成音乐
- 通过 `generationConfig.responseModalities` 包含 `AUDIO` 启用音频输出；若同时包含 `TEXT`，响应会额外返回文本描述（歌词/结构）
- 支持文本 prompt 和图片输入（最多 10 张），图片用于激发视觉灵感驱动的音乐创作
- 时长、结构（主歌/副歌/桥段）、风格等主要通过文本 prompt 控制
- `lyria-3-clip-preview`：固定生成 30 秒短片段，默认返回 MP3（`audio/mpeg`）
- `lyria-3-pro-preview`：生成完整歌曲；可通过 `responseMimeType` 请求 `audio/mpeg` 或 `audio/wav`，但实际输出格式请以响应中的 `inlineData.mimeType` 为准
- 如需 SSE 流式输出，请使用 `/v1beta/models/{model}:streamGenerateContent?alt=sse`
- 音乐生成为单轮过程，不支持多轮迭代编辑




## OpenAPI

````yaml openapi/zh/gemini-format-music-generation.json POST /v1beta/models/{model}:generateContent
openapi: 3.1.0
info:
  title: Gemini 格式 - 音乐生成
  version: '1.0'
servers:
  - url: https://api.aihubmax.com
security:
  - BearerAuth: []
paths:
  /v1beta/models/{model}:generateContent:
    post:
      tags:
        - Text Series > Gemini Format
      summary: Gemini 格式 - 音乐生成
      description: >
        - 使用 Gemini 原生格式的 generateContent 接口，通过 Lyria 3 模型生成音乐

        - 通过 `generationConfig.responseModalities` 包含 `AUDIO` 启用音频输出；若同时包含
        `TEXT`，响应会额外返回文本描述（歌词/结构）

        - 支持文本 prompt 和图片输入（最多 10 张），图片用于激发视觉灵感驱动的音乐创作

        - 时长、结构（主歌/副歌/桥段）、风格等主要通过文本 prompt 控制

        - `lyria-3-clip-preview`：固定生成 30 秒短片段，默认返回 MP3（`audio/mpeg`）

        - `lyria-3-pro-preview`：生成完整歌曲；可通过 `responseMimeType` 请求 `audio/mpeg` 或
        `audio/wav`，但实际输出格式请以响应中的 `inlineData.mimeType` 为准

        - 如需 SSE 流式输出，请使用 `/v1beta/models/{model}:streamGenerateContent?alt=sse`

        - 音乐生成为单轮过程，不支持多轮迭代编辑
      operationId: gemini-format-music-generation
      parameters:
        - name: model
          in: path
          required: true
          schema:
            type: string
            example: lyria-3-clip-preview
            enum:
              - lyria-3-clip-preview
              - lyria-3-pro-preview
          description: >-
            模型名称。`lyria-3-clip-preview` 生成 30 秒短片段（默认 MP3 /
            `audio/mpeg`）。`lyria-3-pro-preview` 生成完整歌曲；可请求 `audio/mpeg` 或
            `audio/wav`，实际输出格式以返回的 `inlineData.mimeType` 为准
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MusicGenerationRequest'
            examples:
              text-to-music:
                summary: 文生音乐（基础）
                description: 通过文本 prompt 生成音乐片段，返回音频 + 文本（歌词/结构描述）
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 创建一首 30 秒的欢快民谣，使用吉他和口琴。
                  generationConfig:
                    responseModalities:
                      - AUDIO
                      - TEXT
              full-song:
                summary: 完整歌曲（带结构）
                description: >-
                  使用 Lyria 3 Pro 生成带主歌/副歌结构的完整歌曲（需将路径中的 model 改为
                  lyria-3-pro-preview）
                value:
                  contents:
                    - role: user
                      parts:
                        - text: |-
                            创建一首 3 分钟的独立流行歌曲，C 大调，120 BPM。

                            [前奏] 柔和的钢琴琶音
                            [主歌1] 轻柔的人声配合原声吉他
                            [副歌] 完整乐队编制，鼓和贝斯加入，旋律积极向上
                            [主歌2] 加入微妙的合成器层次
                            [副歌] 重复，加入和声
                            [尾奏] 钢琴渐隐
                  generationConfig:
                    responseModalities:
                      - AUDIO
                      - TEXT
              wav-output:
                summary: WAV 格式输出（仅 Pro）
                description: >-
                  使用 Lyria 3 Pro 生成 WAV 格式音乐（需将路径中的 model 改为
                  lyria-3-pro-preview）
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 一首氛围感的环境音乐，带有空灵的合成器铺底和轻柔的雨声。
                  generationConfig:
                    responseModalities:
                      - AUDIO
                      - TEXT
                    responseMimeType: audio/wav
              image-to-music:
                summary: 图片驱动音乐（图生音乐）
                description: 上传图片 + 文本描述，模型根据图片的色彩、情绪和氛围生成音乐
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 根据这张图片的色彩和氛围，创作一首大气的环境音乐。
                        - inlineData:
                            mimeType: image/jpeg
                            data: BASE64_ENCODED_IMAGE_DATA
                  generationConfig:
                    responseModalities:
                      - AUDIO
                      - TEXT
      responses:
        '200':
          $ref: '#/components/responses/MusicGenerationSuccess'
        '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:
    MusicGenerationRequest:
      type: object
      required:
        - contents
        - generationConfig
      properties:
        contents:
          type: array
          description: 内容列表。音乐生成为单轮过程，不支持多轮迭代编辑
          items:
            type: object
            required:
              - role
              - parts
            properties:
              role:
                type: string
                description: 消息发送者的角色
                enum:
                  - user
                  - model
              parts:
                type: array
                description: 内容部分列表。支持文本和图片（最多 10 张），图片用于视觉灵感驱动的音乐创作
                items:
                  type: object
                  properties:
                    text:
                      type: string
                      description: 文本内容（音乐生成 prompt，描述风格、乐器、情绪、BPM、结构、歌词等）
                    inlineData:
                      type: object
                      description: 图片内联数据（base64 编码）。模型根据图片的色彩、情绪和氛围生成音乐，最多支持 10 张图片。
                      required:
                        - mimeType
                        - data
                      properties:
                        mimeType:
                          type: string
                          description: 图片 MIME 类型
                          enum:
                            - image/jpeg
                            - image/png
                        data:
                          type: string
                          description: Base64 编码的图片数据
        systemInstruction:
          type: object
          description: 系统指令。Lyria 3 模型对该字段的支持未经官方文档确认，效果可能不生效
          properties:
            parts:
              type: array
              description: 系统指令内容部分
              items:
                type: object
                properties:
                  text:
                    type: string
                    description: 系统指令文本
        generationConfig:
          type: object
          description: 生成配置；音乐生成请求的 `responseModalities` 必须包含 `AUDIO`
          required:
            - responseModalities
          properties:
            responseModalities:
              type: array
              description: 响应模态。音乐生成请求必须包含 `AUDIO`；若同时包含 `TEXT`，会额外返回文本描述
              items:
                type: string
                enum:
                  - AUDIO
                  - TEXT
              example:
                - AUDIO
                - TEXT
            responseMimeType:
              type: string
              description: >-
                请求输出音频格式。推荐 `audio/mpeg` 或 `audio/wav`；当前已知即使请求
                `audio/wav`，实际返回也可能是 `audio/mpeg`，请以响应中的 `inlineData.mimeType`
                为准
              enum:
                - audio/mpeg
                - audio/mp3
                - audio/wav
            temperature:
              type: number
              description: 采样温度。Lyria 3 官方示例中未使用此参数，效果未确认
              minimum: 0
              maximum: 2
        safetySettings:
          type: array
          description: 内容安全过滤设置
          items:
            type: object
            properties:
              category:
                type: string
                description: 危害类别
                enum:
                  - HARM_CATEGORY_HARASSMENT
                  - HARM_CATEGORY_HATE_SPEECH
                  - HARM_CATEGORY_SEXUALLY_EXPLICIT
                  - HARM_CATEGORY_DANGEROUS_CONTENT
              threshold:
                type: string
                description: 阻止阈值
                enum:
                  - BLOCK_NONE
                  - BLOCK_LOW_AND_ABOVE
                  - BLOCK_MEDIUM_AND_ABOVE
                  - BLOCK_ONLY_HIGH
    MusicGenerationResponse:
      type: object
      properties:
        candidates:
          type: array
          description: 生成结果候选列表
          items:
            type: object
            properties:
              content:
                type: object
                description: 生成的内容
                properties:
                  parts:
                    type: array
                    description: >-
                      内容部分，可能包含文本（歌词/结构描述）和音频；实际音频 MIME type 以
                      `inlineData.mimeType` 为准
                    items:
                      type: object
                      properties:
                        text:
                          type: string
                          description: 生成的文本内容（歌词、歌曲结构描述）
                          example: 这是一首欢快的民谣，使用了吉他和口琴。
                        inlineData:
                          type: object
                          description: 生成的音频数据
                          properties:
                            mimeType:
                              type: string
                              description: 音频 MIME 类型；实际返回通常为 `audio/mpeg` 或 `audio/wav`
                              example: audio/mpeg
                            data:
                              type: string
                              description: Base64 编码的音频数据
                              example: SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4...
                  role:
                    type: string
                    description: 响应的角色
                    enum:
                      - model
              finishReason:
                type: string
                description: 生成停止的原因
                enum:
                  - STOP
                  - MAX_TOKENS
                  - SAFETY
                  - RECITATION
                  - OTHER
                example: STOP
              safetyRatings:
                type: array
                description: 生成内容的安全评级
                items:
                  type: object
                  properties:
                    category:
                      type: string
                      description: 危害类别
                    probability:
                      type: string
                      description: 概率等级
        usageMetadata:
          type: object
          description: Token 使用统计
          properties:
            promptTokenCount:
              type: integer
              description: 提示词 token 数
              example: 15
            candidatesTokenCount:
              type: integer
              description: 候选结果 token 数
              example: 200
            totalTokenCount:
              type: integer
              description: 总 token 数
              example: 215
    ErrorResponse400:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 请求参数无效
            type:
              type: string
              description: 错误类型；并非所有错误都会返回该字段
              example: http_error
            code:
              description: 错误码；可能是 HTTP 数字码，也可能是字符串错误码
              oneOf:
                - type: integer
                  example: 400
                - type: string
                  example: http_400
            status:
              type: string
              description: Gemini 风格状态码；并非所有错误都会返回该字段
              example: INVALID_ARGUMENT
    ErrorResponse401:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: API Key 无效
            type:
              type: string
              description: 错误类型；并非所有错误都会返回该字段
              example: authentication_error
            code:
              description: 错误码；可能是 HTTP 数字码，也可能是字符串错误码
              oneOf:
                - type: integer
                  example: 401
                - type: string
                  example: http_401
            status:
              type: string
              description: Gemini 风格状态码；并非所有错误都会返回该字段
              example: UNAUTHENTICATED
    ErrorResponse403:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 访问被拒绝
            type:
              type: string
              description: 错误类型；并非所有错误都会返回该字段
              example: permission_error
            code:
              description: 错误码；可能是 HTTP 数字码，也可能是字符串错误码
              oneOf:
                - type: integer
                  example: 403
                - type: string
                  example: http_403
            status:
              type: string
              description: Gemini 风格状态码；并非所有错误都会返回该字段
              example: PERMISSION_DENIED
    ErrorResponse429:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 请求频率超限
            type:
              type: string
              description: 错误类型；并非所有错误都会返回该字段
              example: rate_limit_error
            code:
              description: 错误码；可能是 HTTP 数字码，也可能是字符串错误码
              oneOf:
                - type: integer
                  example: 429
                - type: string
                  example: http_429
            status:
              type: string
              description: Gemini 风格状态码；并非所有错误都会返回该字段
              example: RESOURCE_EXHAUSTED
    ErrorResponse500:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 内部服务器错误
            type:
              type: string
              description: 错误类型；并非所有错误都会返回该字段
              example: server_error
            code:
              description: 错误码；可能是 HTTP 数字码，也可能是字符串错误码
              oneOf:
                - type: integer
                  example: 500
                - type: string
                  example: http_500
            status:
              type: string
              description: Gemini 风格状态码；并非所有错误都会返回该字段
              example: UNKNOWN
  responses:
    MusicGenerationSuccess:
      description: 音乐生成响应
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/MusicGenerationResponse'
    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`

````