在 Gemini CLI 中使用 oairoute
- Google 官方的命令行 AI 工具
- 支持自定义端点,可指向 OAIRoute
- 适合终端里做文件检索与批量处理
第一步:获取 API Key
在 OAIRoute 控制台 创建。
Create one in the OAIRoute console.
第二步:安装 Gemini CLI
npm install -g @google/gemini-clinpm install -g @google/gemini-cli第三步:设置环境变量
填 https://api.oairoute.com。不带 /v1,CLI 会自己拼路径。
你的 OAIRoute Key。
macOS / Linux:
echo 'export GOOGLE_GEMINI_BASE_URL="https://api.oairoute.com"' >> ~/.zshrc
echo 'export GEMINI_API_KEY="sk-xxxxxxxx"' >> ~/.zshrc
source ~/.zshrcWindows PowerShell:
[Environment]::SetEnvironmentVariable("GOOGLE_GEMINI_BASE_URL", "https://api.oairoute.com", "User")
[Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "sk-xxxxxxxx", "User")Enter https://api.oairoute.com. Omit /v1; the CLI builds the path itself.
Your OAIRoute key.
macOS / Linux:
echo 'export GOOGLE_GEMINI_BASE_URL="https://api.oairoute.com"' >> ~/.zshrc
echo 'export GEMINI_API_KEY="sk-xxxxxxxx"' >> ~/.zshrc
source ~/.zshrcWindows PowerShell:
[Environment]::SetEnvironmentVariable("GOOGLE_GEMINI_BASE_URL", "https://api.oairoute.com", "User")
[Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "sk-xxxxxxxx", "User")第四步:验证
gemini "列出当前目录下所有 Markdown 文件"gemini "List all Markdown files in the current directory"注意事项
Gemini CLI 走的是 Gemini 原生结构,不是 OpenAI 兼容结构。因此:
- 参数名是驼峰式,例如
maxOutputTokens - 角色只有
user与model,没有assistant - 不支持 OpenAI 的
tool_calls字段
如果 OAIRoute 端该模型返回结构不符,CLI 会报解析错误。此时换用 gemini-2.5-pro 试试。
Gemini CLI 的部分功能(如内置的 Google 搜索工具)依赖 Google 侧的服务端能力,经由网关调用时可能不可用。核心的对话与文件读取功能不受影响。
Gemini CLI uses Gemini's native schema, not the OpenAI-compatible one. That means:
- Parameter names are camelCase, e.g.
maxOutputTokens - Roles are only
userandmodel; there is noassistant - The OpenAI
tool_callsfield isn't supported
If a model on the OAIRoute side returns a mismatched structure, the CLI reports a parse error. In that case try gemini-2.5-pro.
Some Gemini CLI features (such as the built-in Google Search tool) rely on Google-side server capabilities and may not work through the gateway. Core chat and file-reading features are unaffected.
常见问题
提示 API key not valid
环境变量名写错。Gemini CLI 读的是 GEMINI_API_KEY,不是 GOOGLE_API_KEY。
报模型不存在
CLI 默认用 gemini-2.5-pro。如果 OAIRoute 账号没有开通该模型,需要显式指定其他模型。
输出中文乱码
Windows 下把终端编码切到 UTF-8:chcp 65001。
"API key not valid"
The environment variable name is wrong. Gemini CLI reads GEMINI_API_KEY, not GOOGLE_API_KEY.
"Model not found"
The CLI uses gemini-2.5-pro by default. If your OAIRoute account doesn't have that model enabled, specify another model explicitly.
Garbled Chinese output
On Windows, switch the terminal encoding to UTF-8: chcp 65001.