UniterRouter
API ReferenceGoogle Compatible

Generate Content

Google Gemini API - Generate content with Gemini Pro, Gemini 1.5 models through UniterRouter. Native Google AI API format with multimodal and streaming support.

Generate Content

Generate content (text, code, etc.) from a Gemini model. This is the primary endpoint for non-streaming text generation.

Endpoint

POST https://api.uniterouter.ai/v1beta/models/{model}:generateContent

Authentication

Use your UniterRouter API key as the Google API key:

x-goog-api-key: your_uniterouter_api_key

Or via query parameter:

?key=your_uniterouter_api_key

Path Parameters

ParameterTypeRequiredDescription
modelstringYesModel ID (e.g., gemini-2.0-flash, gemini-1.5-pro)

Request Body

UniterRouter 完全兼容 Google Gemini 官方 API,支持所有参数。

基础参数

ParameterTypeRequiredDescription
contentsarrayYes消息内容数组
systemInstructionobjectNo系统指令
generationConfigobjectNo生成配置
safetySettingsarrayNo安全设置
toolsarrayNo工具/函数定义
toolConfigobjectNo工具调用配置
cachedContentstringNo缓存内容引用

Contents 格式

{
  "contents": [
    {
      "role": "user",
      "parts": [
        {"text": "Hello, how are you?"}
      ]
    }
  ]
}

GenerationConfig 参数

基础采样参数

ParameterTypeDescription
temperaturenumber控制随机性 (0.0-2.0)
topPnumber核采样参数
topKintegerTop-k 采样参数
maxOutputTokensinteger最大生成 token 数
stopSequencesarray停止序列
candidateCountinteger候选响应数量
seedinteger随机种子

惩罚参数

ParameterTypeDescription
presencePenaltynumber存在惩罚,减少重复
frequencyPenaltynumber频率惩罚,按出现次数递增

结构化输出

ParameterTypeDescription
responseMimeTypestring响应类型: text/plain, application/json, text/x.enum
responseSchemaobjectJSON Schema 定义输出结构
responseJsonSchemaobject替代 responseSchema 的 JSON Schema

思考配置 (Gemini 3+)

ParameterTypeDescription
thinkingConfig.includeThoughtsboolean是否返回思考过程
thinkingConfig.thinkingBudgetinteger思考 token 预算
thinkingConfig.thinkingLevelstring思考深度: MINIMAL, LOW, MEDIUM, HIGH

Extended Thinking: Gemini 3 及以上模型支持 thinkingConfig,允许模型在生成答案前进行深度推理。

多模态输出

ParameterTypeDescription
responseModalitiesarray响应模态: TEXT, IMAGE, AUDIO
speechConfigobject语音输出配置
imageConfigobject图像生成配置 (aspectRatio, imageSize)
mediaResolutionstring输入媒体分辨率: LOW, MEDIUM, HIGH

Example Request

curl -X POST "https://api.uniterouter.ai/v1beta/models/gemini-2.0-flash:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $UNITEROUTER_API_KEY" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{"text": "Explain quantum computing in simple terms"}]
      }
    ],
    "generationConfig": {
      "temperature": 0.7,
      "maxOutputTokens": 1024
    }
  }'
import google.generativeai as genai

genai.configure(
    api_key="your_uniterouter_api_key",
    transport="rest",
    client_options={"api_endpoint": "api.uniterouter.ai"}
)

model = genai.GenerativeModel("gemini-2.0-flash")
response = model.generate_content(
    "Explain quantum computing in simple terms",
    generation_config=genai.GenerationConfig(
        temperature=0.7,
        max_output_tokens=1024
    )
)
print(response.text)
import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI("your_uniterouter_api_key", {
  baseUrl: "https://api.uniterouter.ai"
});

const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });

const result = await model.generateContent({
  contents: [
    {
      role: "user",
      parts: [{ text: "Explain quantum computing in simple terms" }]
    }
  ],
  generationConfig: {
    temperature: 0.7,
    maxOutputTokens: 1024
  }
});

console.log(result.response.text());

Response

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Quantum computing uses quantum mechanics principles..."
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 10,
    "candidatesTokenCount": 150,
    "totalTokenCount": 160
  }
}

Multi-turn Conversations

For chat-style conversations, include the conversation history:

model = genai.GenerativeModel("gemini-2.0-flash")
chat = model.start_chat(history=[])

response1 = chat.send_message("Hello!")
print(response1.text)

response2 = chat.send_message("What's the capital of France?")
print(response2.text)

Vision Input

Gemini models support image input:

import PIL.Image

model = genai.GenerativeModel("gemini-2.0-flash")
image = PIL.Image.open("image.jpg")

response = model.generate_content([
    "Describe this image:",
    image
])
print(response.text)

Error Handling

Error CodeDescription
400Invalid request (bad parameters)
401Invalid API key
403Permission denied
404Model not found
429Rate limit exceeded
500Internal server error

How is this guide?