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

# Gemini Generate Content

> 通过兼容 API 调用 Google 的 Gemini 模型。

**Endpoint:** `POST /projects/cognition/locations/us/publishers/google/models/{model}:generateContent`

通过 Google 的 Gemini 模型生成文本或多模态内容。

<Note>
  大多数集成场景仍建议优先使用 [Create Chat Completion](/zh/api-reference/chat/create)。只有当你明确需要 Google 原生 `generateContent` 请求格式时，才使用这个端点。
</Note>

<CodeGroup>
  ```python Python theme={null}
  import os
  import requests

  url = "https://api.yourouter.ai/v1/projects/cognition/locations/us/publishers/google/models/gemini-1.5-pro-latest:generateContent"

  response = requests.post(
      url,
      headers={
          "Authorization": f"Bearer {os.environ['YOUROUTER_API_KEY']}",
          "Content-Type": "application/json",
          "vendor": "google"
      },
      json={
          "contents": [
              {"role": "user", "parts": [{"text": "hello gemini"}]}
          ]
      }
  )
  print(response.json())
  ```

  ```bash cURL theme={null}
  curl https://api.yourouter.ai/v1/projects/cognition/locations/us/publishers/google/models/gemini-1.5-pro-latest:generateContent \
    -H "Authorization: Bearer $YOUROUTER_API_KEY" \
    -H "Content-Type: application/json" \
    -H "vendor: google" \
    -d '{"contents": [{"role": "user", "parts": [{"text": "hello gemini"}]}]}'
  ```
</CodeGroup>

## 参数

<ResponseField name="model" type="string" required>
  要使用的 Gemini 模型，例如 `gemini-1.5-pro-latest`。
</ResponseField>

<ResponseField name="contents" type="array" required>
  传给模型的文本或多媒体 parts。
</ResponseField>

<ResponseField name="safetySettings" type="object">
  可选的安全设置，用来控制内容阈值。
</ResponseField>

## 图片输入

对于图片内容，请使用 `inlineData` 传递 base64 编码后的图像字节。

```bash theme={null}
curl https://api.yourouter.ai/v1/projects/cognition/locations/us/publishers/google/models/gemini-2.5-flash:generateContent \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "vendor: google" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          { "text": "Describe this image in one sentence." },
          {
            "inlineData": {
              "mimeType": "image/jpeg",
              "data": "<BASE64_IMAGE>"
            }
          }
        ]
      }
    ]
  }'
```

## PDF 输入

Gemini 原生 `generateContent` 格式可以通过 `inlineData` 传入 PDF。把 `mimeType` 设置为 `application/pdf`，`data` 传入 PDF 原始字节的 base64 编码内容。

```bash theme={null}
curl https://api.yourouter.ai/v1/projects/cognition/locations/us/publishers/google/models/gemini-2.5-flash:generateContent \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "vendor: google" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          { "text": "Summarize this PDF's key content." },
          {
            "inlineData": {
              "mimeType": "application/pdf",
              "data": "<BASE64_PDF>"
            }
          }
        ]
      }
    ]
  }'
```

<Note>
  PDF 支持取决于所选 Gemini 模型。长文档会占用更多上下文窗口；生产环境中请控制文件大小和页数，必要时拆分文档。
</Note>

更完整的集成方式请参考 [多模态指南](/zh/guides/multimodal)。
