# 生成内容

使用兼容 Google Gemini 的请求格式生成内容。

- 原文: https://www.unifyllm.com/zh/docs/api-reference/generate-content/
- 语言: zh-CN

## 接口

```http
POST https://api.unifyllm.top/v1beta/models/{model}:{GenerateContentType}
```

认证与请求头以本页调用示例为准。 Content-Type: application/json

[相关接入说明](https://www.unifyllm.com/zh/docs/gemini-image/)

## 参数

| 参数 | 位置 | 类型 | 必填 | 说明 | 可选值 | 示例 |
| --- | --- | --- | --- | --- | --- | --- |
| model | path | string | 是 | 本次请求使用的模型标识符。 |  | gemini-3.5-flash |
| GenerateContentType | path | string | 是 | 兼容 Gemini 的内容生成操作。 | generateContent, streamGenerateContent | generateContent |
| key | query | string | 否 | 可选请求参数。 |  | {{key}} |
| contents | body | array | 是 | 兼容 Gemini 的内容请求数据。 |  |  |

## 文本

```json
{
  "contents": [
    {
      "parts": [
        {
          "text": "Write a story about a magic backpack."
        }
      ]
    }
  ]
}
```

### cURL

```bash
curl --request POST \
  --url 'https://api.unifyllm.top/v1beta/models/gemini-3.5-flash:generateContent' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "contents": [
    {
      "parts": [
        {
          "text": "Write a story about a magic backpack."
        }
      ]
    }
  ]
}'
```

### JavaScript

```javascript
const response = await fetch("https://api.unifyllm.top/v1beta/models/gemini-3.5-flash:generateContent", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <YOUR_API_KEY>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "contents": [
      {
        "parts": [
          {
            "text": "Write a story about a magic backpack."
          }
        ]
      }
    ]
  }),
});

const data = await response.json();
console.log(data);
```

### Python

```python
import json
import requests

response = requests.post(
    "https://api.unifyllm.top/v1beta/models/gemini-3.5-flash:generateContent",
    headers={"Authorization": "Bearer <YOUR_API_KEY>"},
    json=json.loads("{\n  \"contents\": [\n    {\n      \"parts\": [\n        {\n          \"text\": \"Write a story about a magic backpack.\"\n        }\n      ]\n    }\n  ]\n}")
)
response.raise_for_status()
print(response.json())
```

## 识图

```json
{
  "contents": [
    {
      "parts": [
        {
          "inline_data": {
            "mime_type": "image/jpeg",
            "data": "<BASE64_DATA>"
          }
        },
        {
          "text": "Caption this image."
        }
      ]
    }
  ]
}
```

### cURL

```bash
curl --request POST \
  --url 'https://api.unifyllm.top/v1beta/models/gemini-3.5-flash:generateContent' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "contents": [
    {
      "parts": [
        {
          "inline_data": {
            "mime_type": "image/jpeg",
            "data": "<BASE64_DATA>"
          }
        },
        {
          "text": "Caption this image."
        }
      ]
    }
  ]
}'
```

### JavaScript

```javascript
const response = await fetch("https://api.unifyllm.top/v1beta/models/gemini-3.5-flash:generateContent", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <YOUR_API_KEY>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "contents": [
      {
        "parts": [
          {
            "inline_data": {
              "mime_type": "image/jpeg",
              "data": "<BASE64_DATA>"
            }
          },
          {
            "text": "Caption this image."
          }
        ]
      }
    ]
  }),
});

const data = await response.json();
console.log(data);
```

### Python

```python
import json
import requests

response = requests.post(
    "https://api.unifyllm.top/v1beta/models/gemini-3.5-flash:generateContent",
    headers={"Authorization": "Bearer <YOUR_API_KEY>"},
    json=json.loads("{\n  \"contents\": [\n    {\n      \"parts\": [\n        {\n          \"inline_data\": {\n            \"mime_type\": \"image/jpeg\",\n            \"data\": \"<BASE64_DATA>\"\n          }\n        },\n        {\n          \"text\": \"Caption this image.\"\n        }\n      ]\n    }\n  ]\n}")
)
response.raise_for_status()
print(response.json())
```

## 响应示例

### 示例 1 — HTTP 200

```json
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "xxx",
            "thoughtSignature": "xxx"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP"
    }
  ],
  "modelVersion": "gemini-3.5-flash",
  "responseId": "Qf9VavfVA9DVz7IP-NjW6AY",
  "usageMetadata": {
    "candidatesTokenCount": 1978,
    "promptTokenCount": 9,
    "thoughtsTokenCount": 1339,
    "totalTokenCount": 3326
  }
}
```
