Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
__pycache__/
*.pyc
config.json
session_stats.json
session_stats.json.tmp
cookie.txt
cookie.json
.env
Expand Down
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ Convert Google Gemini's web interface into an OpenAI-compatible API. Zero authen
## Quick Start

```bash
python gemini_web2api.py
python -m gemini_web2api
```

Server starts at `http://localhost:8081/v1`.
Server starts at `http://localhost:8081/v1`. The web admin console is available at `http://localhost:8081/admin` with the default login `admin` / `admin`; change it in `config.json` before exposing the service.

## Client Configuration

Expand Down Expand Up @@ -73,6 +73,17 @@ Supports Google native API endpoints:
- `POST /v1beta/models/{model}:generateContent` — non-streaming
- `POST /v1beta/models/{model}:streamGenerateContent` — streaming (SSE)

## Web Admin

Open `http://localhost:8081/admin` after startup. The console includes:

- API key create, edit, delete, and copy actions
- Available model list and call examples for supported APIs
- Session statistics by model, API key, and recent requests
- Proxy configuration and connectivity test
- Streaming chat test page
- Settings for default model, retry, timeout, logging, cookie file, and web login

## Available Models

| Model | Description | Output |
Expand All @@ -99,7 +110,7 @@ gemini-3.5-flash-thinking@think=4 # shallowest
Anonymous access works for all models, but `gemini-3.1-pro` routes to Flash without authentication. To get real Pro routing, provide a cookie file:

```bash
python gemini_web2api.py --cookie-file cookie.txt
python -m gemini_web2api --cookie-file cookie.txt
```

### How to get cookies
Expand Down Expand Up @@ -134,6 +145,12 @@ Create `config.json` in the same directory:
"retry_delay_sec": 2,
"request_timeout_sec": 180,
"api_keys": ["sk-your-key"],
"web_username": "admin",
"web_password": "change-me",
"session_secret": null,
"session_cookie": "gemini2api_session",
"session_ttl_sec": 86400,
"session_stats_file": null,
"cookie_file": null,
"proxy": null,
"log_requests": true
Expand Down Expand Up @@ -171,7 +188,7 @@ If you cannot access `gemini.google.com` directly (connection timeout), configur

**Method 1: CLI argument**
```bash
python gemini_web2api.py --proxy http://127.0.0.1:7890
python -m gemini_web2api --proxy http://127.0.0.1:7890
```

**Method 2: config.json**
Expand All @@ -182,13 +199,15 @@ python gemini_web2api.py --proxy http://127.0.0.1:7890
**Method 3: Environment variable** (auto-detected)
```bash
export HTTPS_PROXY=http://127.0.0.1:7890
python gemini_web2api.py
python -m gemini_web2api
```

Works with Clash, V2Ray, Shadowsocks, or any HTTP proxy.

## Tool Calling

Tool calls are supported for both normal and streaming Chat Completions. The parser accepts OpenAI-style `tool_calls` JSON, legacy fenced `tool_call` blocks, and XML/DSML tool-call blocks, while filtering calls to the tools provided in the request.

```python
resp = client.chat.completions.create(
model="gemini-3.5-flash",
Expand Down
31 changes: 26 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
## 快速开始

```bash
python gemini_web2api.py
python -m gemini_web2api
```

服务启动在 `http://localhost:8081/v1`.
服务启动在 `http://localhost:8081/v1`。Web 管理台地址是 `http://localhost:8081/admin`,默认登录账号密码为 `admin` / `admin`,对外暴露服务前请先在 `config.json` 中修改。

## 客户端配置

Expand Down Expand Up @@ -73,6 +73,17 @@ gemini
- `POST /v1beta/models/{model}:generateContent` — 非流式生成
- `POST /v1beta/models/{model}:streamGenerateContent` — 流式生成 (SSE)

## Web 管理台

启动后打开 `http://localhost:8081/admin`。管理台包含:

- API 密钥创建、修改、删除和复制
- 可用模型列表和当前支持接口的调用样例
- 按模型、API 密钥、最近请求聚合的会话统计
- 代理配置和连通性测试
- 流式在线聊天测试页面
- 默认模型、重试、超时、日志、Cookie 文件和 Web 登录信息设置

## 可用模型

| 模型 | 说明 | 输出量 |
Expand All @@ -99,7 +110,7 @@ gemini-3.5-flash-thinking@think=4 # 最浅
匿名访问对所有模型有效, 但 `gemini-3.1-pro` 在无认证时会路由到 Flash. 要获得真正的 Pro 路由, 提供 cookie 文件:

```bash
python gemini_web2api.py --cookie-file cookie.txt
python -m gemini_web2api --cookie-file cookie.txt
```

### 如何获取 Cookie
Expand Down Expand Up @@ -134,6 +145,12 @@ SID=你的SID值; HSID=你的HSID值; SSID=你的SSID值; APISID=你的APISID值
"retry_delay_sec": 2,
"request_timeout_sec": 180,
"api_keys": ["sk-your-key"],
"web_username": "admin",
"web_password": "change-me",
"session_secret": null,
"session_cookie": "gemini2api_session",
"session_ttl_sec": 86400,
"session_stats_file": null,
"cookie_file": null,
"proxy": null,
"log_requests": true
Expand Down Expand Up @@ -171,7 +188,7 @@ docker run -d --name gemini-web2api -p 8081:8081 -v ./config.json:/app/config.js

**方式 1: 命令行参数**
```bash
python gemini_web2api.py --proxy http://127.0.0.1:7890
python -m gemini_web2api --proxy http://127.0.0.1:7890
```

**方式 2: config.json**
Expand All @@ -182,11 +199,15 @@ python gemini_web2api.py --proxy http://127.0.0.1:7890
**方式 3: 环境变量** (自动检测)
```bash
set HTTPS_PROXY=http://127.0.0.1:7890
python gemini_web2api.py
python -m gemini_web2api
```

支持 Clash, V2Ray, Shadowsocks 等任何 HTTP 代理.

## 工具调用

Chat Completions 的非流式和流式请求都支持工具调用。解析器支持 OpenAI 风格的 `tool_calls` JSON、旧版 fenced `tool_call` 代码块、XML/DSML 工具调用块,并会按请求中声明的工具名过滤输出。

## 已知限制

- **不支持图片/多模态输入**: Gemini 的图片上传需要专有的 WIZ streaming RPC 协议 (ProcessFile), 无法在标准 HTTP 代理中实现. 发送图片会被忽略并返回提示.
Expand Down
6 changes: 6 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"api_keys": [
"sk-gemini"
],
"web_username": "admin",
"web_password": "admin",
"session_secret": null,
"session_cookie": "gemini2api_session",
"session_ttl_sec": 86400,
"session_stats_file": null,
"cookie_file": null,
"proxy": null,
"log_requests": true
Expand Down
Loading