映射概览
典型的 Gemini API 示例在 Python 中使用模型对象、generate_content 调用和 .text 属性。将其移植到 OpenAI 风格 API 意味着将其替换为客户端、chat.completions.create 调用和 choices[0].message.content。概念保持不变;仅名称发生变化。
| 概念 | Gemini SDK 风格 | OpenAI 风格(本 API) |
|---|---|---|
| 认证和接口 | SDK 中配置的 API 密钥 | base_url 加上 Bearer 令牌 |
| 模型 | Gemini 模型名称 | uncensored |
| 系统提示词 | system_instruction | role 为 system 的消息 |
| 历史记录中的 assistant 角色 | 模型 | assistant |
| 消息文本 | parts | content |
| 最大长度 | max_output_tokens | max_tokens(可选) |
| JSON 输出 | response_mime_type | response_format json_object |
| 流式输出 | generate_content 上设置 stream=True | create 上设置 stream=True |
Google 库的方法名称在不同版本间有所不同,因此请查阅其最新文档以获取确切的调用方式。OpenAI 端是稳定的,可与任何 OpenAI 客户端配合使用。本站与 Google 无关:它提供 ID 为 uncensored 的独立开放权重模型,因此输出风格将与 Gemini 不同。
示例 1:单个提示词
之前,在典型的 Gemini 调用中:
import google.generativeai as genai
genai.configure(api_key="GEMINI_KEY")
model = genai.GenerativeModel("gemini-model-name")
resp = model.generate_content(
"Write a two-sentence product blurb for a hiking backpack.",
generation_config={"temperature": 0.7, "max_output_tokens": 200},
)
print(resp.text)
之后,调用本 API:
from openai import OpenAI
client = OpenAI(base_url="https://api.geminiuncensored.top/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Write a two-sentence product blurb for a hiking backpack."}],
temperature=0.7,
max_tokens=200,
)
print(resp.choices[0].message.content)
注意发生了哪些变化。密钥和端点现在位于客户端构造函数中。生成设置作为顶层参数,而不是配置字典。提示词是消息列表而不是字符串。密钥集成指南 展示了安全存储密钥的方法。
示例 2:system prompt 和对话历史
Gemini 历史使用 role model 表示之前的回答,并将文本包裹在 parts 中。在 OpenAI 格式中,role 为 assistant,文本位于 content 中。辅助代码用于转换存储的历史记录:
def to_openai(history):
# history: list of dicts with role user or model, and parts as a list of text
out = []
for turn in history:
role = "assistant" if turn["role"] == "model" else turn["role"]
out.append({"role": role, "content": "".join(turn["parts"])})
return out
history = [
{"role": "user", "parts": ["My name is Dana."]},
{"role": "model", "parts": ["Nice to meet you, Dana."]},
]
messages = [{"role": "system", "content": "You are a friendly assistant."}]
messages += to_openai(history)
messages.append({"role": "user", "content": "What is my name?"})
resp = client.chat.completions.create(model="uncensored", messages=messages)
print(resp.choices[0].message.content)
system prompt 从构造函数参数移动到第一条消息。实际上两个 API 都是无状态的:每次调用时重新发送历史记录,因此输入 token 会随着对话增长。上下文窗口为 100,000 token,包含提示词和补全。
示例 3:流式输出
在 Gemini 中,你迭代 chunks 并读取 chunk.text。在这里,你从每个 chunk 读取 delta:
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell a very short story about a lighthouse."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
流式输出使用服务器发送事件。流式请求会占用其并发槽位,直到流结束:完整密钥允许 8 个并发请求,试用密钥允许 2 个。如果提前跳出循环,请关闭流。
Node.js
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.geminiuncensored.top/v1",
apiKey: process.env.API_KEY,
});
const stream = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Name three birds." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}示例 4:JSON 输出和函数调用
对于 JSON,Gemini 使用响应 MIME 类型。在这里你使用 response_format。在提示词中描述字段,因为 JSON 模式保证语法有效,但不保证符合你的 schema。
import json
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Return JSON with keys city and country for: Lisbon"}],
response_format={"type": "json_object"},
temperature=0,
)
print(json.loads(resp.choices[0].message.content))
函数调用映射到 tools。与其传递函数声明,不如传递包含 type: "function" 的对象列表,根据需要设置 tool_choice,并以 role: "tool" 和匹配的 tool_call_id 返回结果。流式工具调用以片段形式出现在 delta.tool_calls 中。
tools = [{
"type": "function",
"function": {
"name": "get_time",
"description": "Get the current time in a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "What time is it in Tokyo?"}],
tools=tools,
)
print(resp.choices[0].message.tool_calls)无法移植的内容
某些 Gemini 功能在此没有等效项。本 API 仅支持文本:没有图片或音频输入,没有图片、音频或视频生成,没有嵌入向量和微调。如果你的代码向 Gemini 发送图片或文件,请保留这些调用,仅移植文本路径。Grounding 或内置搜索工具是 Gemini 端的功能,你需要用自己的工具函数替换它们。
安全设置也不会迁移。此模型经过调整,可在合法成人使用中无内容拒绝地回答问题,因此没有按类别设置的阈值。一个硬性限制是:涉及未成年人的性内容永远不允许。阅读 如何使用 Gemini API 替代品 以决定迁移哪些流量。
错误、限制和余额头
OpenAI 客户端会抛出类型异常。你最先遇到的两种是密钥错误导致的认证错误,以及 HTTP 429。429 错误代码告诉你原因:rate_limited 表示每分钟请求数(完整密钥为 300,试用密钥为 60),concurrency 表示同时请求过多。在临时过载时,API 还会返回带有 Retry-After: 5 的 429。
import time
from openai import RateLimitError
def call(messages):
for attempt in range(4):
try:
return client.chat.completions.create(model="uncensored", messages=messages)
except RateLimitError as e:
time.sleep(float(e.response.headers.get("Retry-After", 2)))
raise RuntimeError("still rate limited")
每个响应都包含 X-Request-Id、X-Balance-USD 和速率限制头。读取余额头以了解何时充值。文档 列出了所有接口:POST /v1/chat/completions、POST /v1/responses 和 GET /v1/models。
成本和支付
定价为每 1M 输入 token $0.15,每 1M 输出 token $0.50,从预付额度中扣除。之前的背包示例,约 20 个输入和 60 个输出 token,成本为 20 x $0.15 / 1M 加上 60 x $0.50 / 1M,即 $0.000003 加上 $0.00003,或 $0.000033。使用加密货币(TRC20 上的 USDT 或 Base 上的 USDC)充值任何整数金额,从 $20 到 $500;奖金为 $50 起 +5%,$100 起 +10%,$200 起 +15%。要首先测试端口,点击 API 密钥页面 一键领取 $0.50 的试用额度。详情见 定价页面。

