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}:generateContentAuthentication
Use your UniterRouter API key as the Google API key:
x-goog-api-key: your_uniterouter_api_keyOr via query parameter:
?key=your_uniterouter_api_keyPath Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model ID (e.g., gemini-2.0-flash, gemini-1.5-pro) |
Request Body
UniterRouter 完全兼容 Google Gemini 官方 API,支持所有参数。
基础参数
| Parameter | Type | Required | Description |
|---|---|---|---|
contents | array | Yes | 消息内容数组 |
systemInstruction | object | No | 系统指令 |
generationConfig | object | No | 生成配置 |
safetySettings | array | No | 安全设置 |
tools | array | No | 工具/函数定义 |
toolConfig | object | No | 工具调用配置 |
cachedContent | string | No | 缓存内容引用 |
Contents 格式
{
"contents": [
{
"role": "user",
"parts": [
{"text": "Hello, how are you?"}
]
}
]
}GenerationConfig 参数
基础采样参数
| Parameter | Type | Description |
|---|---|---|
temperature | number | 控制随机性 (0.0-2.0) |
topP | number | 核采样参数 |
topK | integer | Top-k 采样参数 |
maxOutputTokens | integer | 最大生成 token 数 |
stopSequences | array | 停止序列 |
candidateCount | integer | 候选响应数量 |
seed | integer | 随机种子 |
惩罚参数
| Parameter | Type | Description |
|---|---|---|
presencePenalty | number | 存在惩罚,减少重复 |
frequencyPenalty | number | 频率惩罚,按出现次数递增 |
结构化输出
| Parameter | Type | Description |
|---|---|---|
responseMimeType | string | 响应类型: text/plain, application/json, text/x.enum |
responseSchema | object | JSON Schema 定义输出结构 |
responseJsonSchema | object | 替代 responseSchema 的 JSON Schema |
思考配置 (Gemini 3+)
| Parameter | Type | Description |
|---|---|---|
thinkingConfig.includeThoughts | boolean | 是否返回思考过程 |
thinkingConfig.thinkingBudget | integer | 思考 token 预算 |
thinkingConfig.thinkingLevel | string | 思考深度: MINIMAL, LOW, MEDIUM, HIGH |
Extended Thinking: Gemini 3 及以上模型支持 thinkingConfig,允许模型在生成答案前进行深度推理。
多模态输出
| Parameter | Type | Description |
|---|---|---|
responseModalities | array | 响应模态: TEXT, IMAGE, AUDIO |
speechConfig | object | 语音输出配置 |
imageConfig | object | 图像生成配置 (aspectRatio, imageSize) |
mediaResolution | string | 输入媒体分辨率: 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 Code | Description |
|---|---|
400 | Invalid request (bad parameters) |
401 | Invalid API key |
403 | Permission denied |
404 | Model not found |
429 | Rate limit exceeded |
500 | Internal server error |
Related
- Stream Generate Content - Streaming responses
- Embed Content - Text embeddings
- Count Tokens - Token counting
How is this guide?
Google Compatible
Google Gemini API proxy - Access Gemini Pro, Gemini 1.5 Pro, Gemini Flash through UniterRouter. Native Google AI SDK support with multimodal, grounding and code execution.
Stream Generate Content POST
Gemini Streaming API - Real-time content generation with Gemini Pro, Gemini 1.5. Reduce latency and improve UX with Server-Sent Events streaming.