Generate Content
通过 Gemini 原生协议调用 Google Gemini 模型。OfoxAI 兼容 Google GenAI SDK。
端点
POST https://api.ofox.ai/gemini/v1beta/models/{model}:generateContent
POST https://api.ofox.ai/gemini/v1beta/models/{model}:streamGenerateContent认证
Gemini 协议使用 x-goog-api-key Header:
x-goog-api-key: <你的 OFOXAI_API_KEY>请求示例
cURL
Terminal
curl "https://api.ofox.ai/gemini/v1beta/models/google/gemini-3.1-flash-lite-preview:generateContent" \
-H "x-goog-api-key: $OFOX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [{"text": "用 Python 实现一个简单的 Web 服务器"}]
}
]
}'流式响应
Python
gemini_stream.py
response = client.models.generate_content_stream(
model="google/gemini-3.1-flash-lite-preview",
contents="写一篇关于 AI 的文章"
)
for chunk in response:
print(chunk.text, end="", flush=True)多模态输入
Gemini 原生支持多模态输入,包括图像、音频和视频:
import base64
# 图像分析
with open("photo.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode()
response = client.models.generate_content(
model="google/gemini-3.1-flash-lite-preview",
contents=[
{"text": "描述这张图片的内容"},
{"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
]
)生成与编辑图像
Gemini 原生协议下,generateContent 一个端点同时承担生图(text → image)与图像编辑(image + text → image)。推荐模型 google/gemini-3.1-flash-image-preview。
POST https://api.ofox.ai/gemini/v1beta/models/google/gemini-3.1-flash-image-preview:generateContent也可通过 OpenAI 兼容端点 调用同一模型来生图,但图像编辑能力仅在 Gemini 原生协议下可用。
生图:text → image
Python
gen.py
from google import genai
client = genai.Client(
api_key="YOUR_OFOX_API_KEY",
http_options={"api_version": "v1beta", "base_url": "https://api.ofox.ai/gemini"},
)
resp = client.models.generate_content(
model="google/gemini-3.1-flash-image-preview",
contents="A simple red apple on a white table, photorealistic",
)
for part in resp.candidates[0].content.parts:
if part.inline_data and part.inline_data.data:
with open("output.png", "wb") as f:
f.write(part.inline_data.data)
break实测输出:

编辑:image + text → image
把原图以 inlineData 与文本指令一起放进 parts 即可:
Python
edit.py
from google import genai
from google.genai import types
client = genai.Client(
api_key="YOUR_OFOX_API_KEY",
http_options={"api_version": "v1beta", "base_url": "https://api.ofox.ai/gemini"},
)
with open("apple.png", "rb") as f:
image_bytes = f.read()
resp = client.models.generate_content(
model="google/gemini-3.1-flash-image-preview",
contents=[
"把苹果改成绿色,其他保持不变",
types.Part.from_bytes(data=image_bytes, mime_type="image/png"),
],
)
for part in resp.candidates[0].content.parts:
if part.inline_data and part.inline_data.data:
with open("apple_edited.png", "wb") as out:
out.write(part.inline_data.data)
break实测对比:
| 原图 | 编辑后 |
|---|---|
![]() | ![]() |
响应
{
"candidates": [{
"content": {
"role": "model",
"parts": [
{ "text": "...", "thought": true },
{ "inlineData": { "mimeType": "image/png", "data": "<图片 Base64>" } }
]
},
"finishReason": "STOP"
}],
"modelVersion": "google/gemini-3.1-flash-image-preview",
"usageMetadata": {
"promptTokenCount": 1097,
"candidatesTokenCount": 1120,
"thoughtsTokenCount": 1306,
"totalTokenCount": 3523,
"candidatesTokensDetails": [
{ "modality": "IMAGE", "tokenCount": 1120 }
]
}
}- 图片在
candidates[0].content.parts[].inlineData.data,是 Base64 字符串 parts中可能混杂若干text+thought: true的思考过程,遍历时只取inlineDatausageMetadata.candidatesTokensDetails中modality: IMAGE的tokenCount是图像输出消耗
支持的模型
下表仅列举常用代表,完整可用模型清单见:
- 编程接口:GET /gemini/v1beta/models — 实时查询当前账户在网关上可用的模型
- 浏览页面:模型目录 — 可视化检索,含定价
| 模型 | 说明 |
|---|---|
google/gemini-3.1-pro-preview | Gemini 3.1 Pro — 最强推理能力 |
google/gemini-3-pro-preview | Gemini 3 Pro — 均衡性能 |
google/gemini-3.1-flash-lite-preview | Gemini 3 Flash — 高速高性价比 |
google/gemini-3.1-flash-image-preview | Gemini Flash Image — 图像生成与编辑 |
OfoxAI 的 Gemini 协议支持 Google GenAI SDK 的主要功能,包括 Function Calling、Code Execution、Grounding、图像生成与编辑等。
Last updated on
