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

# Gemini 媒體辨識

> Gemini-native generateContent interface for text chat, multimodal media recognition (images, audio, video), speech synthesis, and image generation with structured parts. Use `generationConfig` to request specific response modalities such as speech (`speechConfig`) or images (`imageConfig`).

本頁使用與 [生成內容（Gemini）](/zh-Hant/api-reference/chat/generate-content) 相同的 `generateContent` 操作，上方 playground 已預先填入純文字對話範例。以下說明可加入 `contents[].parts` 的 Gemini 原生多模態欄位，讓你在單一請求中分析圖片、音訊、影片或混合媒體。

<Note>
  每個 part 都可以同時攜帶內嵌資料（base64 編碼位元組加上 MIME 類型）與文字指令，讓模型能在一次呼叫中跨模態推理。
</Note>

### Gemini 原生請求欄位

playground 中顯示的通用 `contents` 與 `generationConfig` 欄位，可接受以下多模態辨識的巢狀結構：

| 欄位                                       | 類型     | 必填 | 說明                                                     |
| ---------------------------------------- | ------ | -- | ------------------------------------------------------ |
| `contents[].role`                        | string | 否  | 該輪對話的角色，例如 `user`。                                     |
| `contents[].parts`                       | array  | 是  | 有順序的內容 parts 清單（文字與／或內嵌媒體）。                            |
| `contents[].parts[].text`                | string | 否  | 提供給模型的文字指令或問題。                                         |
| `contents[].parts[].inlineData`          | object | 否  | 用於圖片、音訊或影片理解的內嵌媒體負載。                                   |
| `contents[].parts[].inlineData.mimeType` | string | 否  | 內嵌資料的 MIME 類型，例如 `image/jpeg`、`audio/mp3`、`video/mp4`。 |
| `contents[].parts[].inlineData.data`     | string | 否  | 以 base64 編碼的媒體位元組。                                     |

<Tip>
  你可以在單一輪次中混合多個 parts，例如先放一個 `text` 指令，再放一個或多個包含待分析媒體的 `inlineData` parts。
</Tip>

### 範例：分析圖片

```json theme={null}
{
  "contents": [
    {
      "role": "user",
      "parts": [
        { "text": "Describe what is happening in this image." },
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": "<base64-encoded-image-bytes>"
          }
        }
      ]
    }
  ]
}
```

### 回應欄位

回應遵循標準 `generateContent` 結構。與媒體辨識最相關的欄位如下：

<ResponseField name="candidates" type="array">
  模型回傳的候選回應。

  <Expandable title="candidate properties">
    <ResponseField name="content" type="object">
      生成內容物件。

      <Expandable title="content properties">
        <ResponseField name="role" type="string">
          生成內容區塊中回傳的角色，通常為 `model`。
        </ResponseField>

        <ResponseField name="parts" type="array">
          回傳的內容 parts，通常是描述分析媒體結果的文字。
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="finishReason" type="string">
      完成原因字串，例如 `STOP`。
    </ResponseField>

    <ResponseField name="safetyRatings" type="array">
      安全評估結果。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usageMetadata" type="object">
  Token 計量中繼資料，包括 `promptTokenCount`、`candidatesTokenCount` 與 `totalTokenCount`。內嵌媒體（圖片、音訊、影片）除了文字 parts 外，也會消耗 prompt tokens。
</ResponseField>

### 回應範例

```json 200 theme={null}
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          { "text": "The image shows a golden retriever sitting on a grassy lawn." }
        ]
      },
      "finishReason": "STOP",
      "safetyRatings": []
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 264,
    "candidatesTokenCount": 18,
    "totalTokenCount": 282
  }
}
```


## OpenAPI

````yaml api-reference/openapi.json POST /v1/models/{model}:generateContent
openapi: 3.1.0
info:
  title: DGrid AI Gateway API
  description: >-
    A single, unified API to access 200+ leading AI models. OpenAI-, Claude-,
    and Gemini-compatible endpoints for chat, completions, embeddings, images,
    audio, and moderations.
  version: 1.0.0
  contact:
    name: DGrid AI
    url: https://dgrid.ai
servers:
  - url: https://api.dgrid.ai
    description: DGrid AI Gateway
security:
  - bearerAuth: []
paths:
  /v1/models/{model}:generateContent:
    post:
      tags:
        - Chat
      summary: Generate content (Gemini)
      description: >-
        Gemini-native generateContent interface for text chat, multimodal media
        recognition (images, audio, video), speech synthesis, and image
        generation with structured parts. Use `generationConfig` to request
        specific response modalities such as speech (`speechConfig`) or images
        (`imageConfig`).
      operationId: generateContent
      parameters:
        - name: model
          in: path
          required: true
          schema:
            type: string
          description: Target model ID, such as `gemini-1.5-pro`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                contents:
                  type: array
                  description: Input content array with role and parts.
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                        description: Content role.
                      parts:
                        type: array
                        description: Content parts (text, inline data, media).
                        items:
                          type: object
                generationConfig:
                  type: object
                  description: Generation configuration.
                  properties:
                    responseModalities:
                      type: array
                      items:
                        type: string
                      description: >-
                        Requested response modalities, such as `TEXT`, `AUDIO`,
                        or `IMAGE`.
                    speechConfig:
                      type: object
                      description: Speech configuration for audio output.
                      properties:
                        voiceConfig:
                          type: object
                          properties:
                            prebuiltVoiceConfig:
                              type: object
                              properties:
                                voiceName:
                                  type: string
                                  description: Voice preset name.
                    imageConfig:
                      type: object
                      description: Image configuration for image output.
                      properties:
                        aspectRatio:
                          type: string
                          description: Aspect ratio.
                        imageSize:
                          type: string
                          description: Image size.
            example:
              contents:
                - role: user
                  parts:
                    - text: Hello from DGrid.
      responses:
        '200':
          description: Generated content candidates.
          content:
            application/json:
              schema:
                type: object
                properties:
                  candidates:
                    type: array
                    description: Candidate responses returned by the model.
                    items:
                      type: object
                      properties:
                        content:
                          type: object
                          description: Generated content object.
                          properties:
                            role:
                              type: string
                              description: Role in the generated content block.
                            parts:
                              type: array
                              description: Returned content parts.
                              items:
                                type: object
                        finishReason:
                          type: string
                          description: Finish reason string.
                        safetyRatings:
                          type: array
                          description: Safety evaluation results.
                          items:
                            type: object
                  usageMetadata:
                    type: object
                    description: Token accounting metadata.
                    properties:
                      promptTokenCount:
                        type: integer
                      candidatesTokenCount:
                        type: integer
                      totalTokenCount:
                        type: integer
        '400':
          $ref: '#/components/responses/BadRequest'
components:
  responses:
    BadRequest:
      description: Invalid request.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
            type:
              type: string
            param:
              type: string
            code:
              type: string
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        Your DGrid API key. All endpoints use `Authorization: Bearer
        <DGRID_API_KEY>`.

````