外观
Generate Content
generateContent 是 Gemini 原生 REST API 里最常用的生成接口,适合文本问答、多轮对话、结构化输出以及多模态理解场景。
请求地址
text
POST https://mux.la/v1beta/models/{model}:generateContent示例模型:
gemini-3-flash-previewgemini-3-pro-previewgemini-2.5-flash
鉴权
- Google 官方原生示例使用
x-goog-api-key: $GEMINI_API_KEY。 - 如果通过 Muxla 中转调用,可使用平台 API Key;下方示例按 Muxla 网关地址编写。
最小请求示例
bash
curl "https://mux.la/v1beta/models/gemini-3-flash-preview:generateContent" \
-H "Authorization: Bearer sk-xxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}'常用请求字段
| 字段 | 必填 | 说明 |
|---|---|---|
contents | 是 | 对话内容数组,至少包含一条消息 |
contents[].role | 否 | 角色,常见为 user 或 model |
contents[].parts | 是 | 消息内容片段数组 |
parts[].text | 否 | 文本输入 |
parts[].inlineData | 否 | Base64 编码的图片、音频、视频等内联数据 |
parts[].fileData | 否 | 已上传文件引用 |
system_instruction | 否 | 系统提示词;部分 SDK 中对应 systemInstruction |
generationConfig | 否 | 生成配置,例如温度、输出格式、思考配置 |
safetySettings | 否 | 安全过滤配置 |
tools | 否 | 工具定义,例如 Google Search、函数调用等 |
请求体示例
纯文本生成
json
{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}带系统提示词
json
{
"system_instruction": {
"parts": [
{
"text": "You are a concise assistant."
}
]
},
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}多轮对话
json
{
"contents": [
{
"role": "user",
"parts": [{ "text": "Hello" }]
},
{
"role": "model",
"parts": [{ "text": "Great to meet you. What would you like to know?" }]
},
{
"role": "user",
"parts": [{ "text": "Explain how AI works in a few words" }]
}
]
}返回结构示例
json
{
"candidates": [
{
"content": {
"parts": [
{
"text": "AI learns patterns from data and uses them to make predictions."
}
],
"role": "model"
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 8,
"candidatesTokenCount": 14,
"totalTokenCount": 22
},
"modelVersion": "gemini-3-flash-preview"
}流式输出
如果你要流式返回,需要使用:
text
POST /v1beta/models/{model}:streamGenerateContent?alt=sse示例:
bash
curl "https://mux.la/v1beta/models/gemini-3-flash-preview:streamGenerateContent?alt=sse" \
-H "Authorization: Bearer sk-xxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Write a short poem about APIs"
}
]
}
]
}'