├── .gitignore ├── requirements.txt ├── .dockerignore ├── Dockerfile ├── business_gemini_session.json.example ├── docker-compose.yml ├── README.md ├── chat_history.html └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.pyc 3 | *.pyo 4 | *.log 5 | business_gemini_session.json 6 | image/ 7 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Business Gemini OpenAPI 兼容服务依赖 2 | # Python 3.8+ 3 | 4 | # Web框架 5 | flask>=2.0.0 6 | flask-cors>=3.0.0 7 | 8 | # HTTP请求 9 | requests>=2.25.0 10 | 11 | # SSL警告处理(requests依赖) 12 | urllib3>=1.26.0 13 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | .gitignore 3 | .idea 4 | .venv 5 | __pycache__ 6 | *.pyc 7 | *.pyo 8 | *.pyd 9 | *.log 10 | *.sqlite 11 | *.db 12 | .DS_Store 13 | Thumbs.db 14 | .env 15 | .env.local 16 | *.md 17 | !README.md 18 | docker-compose.yml 19 | Dockerfile 20 | .dockerignore 21 | business_gemini_session.json -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # syntax=docker/dockerfile:1 2 | FROM python:3.11-slim 3 | 4 | ENV PYTHONDONTWRITEBYTECODE=1 \ 5 | PYTHONUNBUFFERED=1 \ 6 | PIP_NO_CACHE_DIR=1 7 | 8 | WORKDIR /app 9 | 10 | COPY requirements.txt . 11 | RUN pip install --no-cache-dir -r requirements.txt 12 | 13 | COPY . . 14 | 15 | EXPOSE 8000 16 | 17 | HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ 18 | CMD python -c "import requests; requests.get('http://localhost:8000/health', timeout=5)" 19 | 20 | CMD ["python", "-u", "gemini.py"] -------------------------------------------------------------------------------- /business_gemini_session.json.example: -------------------------------------------------------------------------------- 1 | { 2 | "proxy": "http://127.0.0.1:7890", 3 | "proxy_enabled": false, 4 | "image_base_url": "http://127.0.0.1:8000/", 5 | "image_output_mode": "url", 6 | "log_level": "INFO", 7 | "admin_password_hash": "", 8 | "admin_secret_key": "", 9 | "api_tokens": [ 10 | "please_set_api_token_here" 11 | ], 12 | "accounts": [ 13 | ], 14 | "models": [ 15 | { 16 | "id": "gemini-3-pro-preview", 17 | "name": "gemini-3-pro-preview", 18 | "description": "gemini-3-pro-preview 模型", 19 | "context_length": 32768, 20 | "max_tokens": 8192, 21 | "price_per_1k_tokens": 0.0015 22 | } 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | name: business-gemini-pool 2 | 3 | services: 4 | app: 5 | build: . 6 | container_name: business-gemini-pool 7 | restart: unless-stopped 8 | ports: 9 | - "8000:8000" 10 | volumes: 11 | - ./business_gemini_session.json:/app/business_gemini_session.json 12 | - ./index.html:/app/index.html 13 | - ./gemini.py:/app/gemini.py 14 | environment: 15 | - PYTHONUNBUFFERED=1 16 | healthcheck: 17 | test: ["CMD", "python", "-c", "import requests; requests.get('http://localhost:8000/health', timeout=5)"] 18 | interval: 30s 19 | timeout: 10s 20 | retries: 3 21 | start_period: 5s 22 | networks: 23 | - gemini-network 24 | 25 | networks: 26 | gemini-network: 27 | driver: bridge 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Business Gemini Pool 管理系统 2 | 3 | 一个基于 Flask 的 Google Gemini Enterprise API 代理服务,支持多账号轮训、OpenAI 兼容接口和 Web 管理控制台。 4 | 5 | ## 项目结构 6 | 7 | ``` 8 | / 9 | ├── gemini.py # 后端服务主程序 10 | ├── index.html # Web 管理控制台前端 11 | ├── business_gemini_session.json # 配置文件 12 | └── README.md # 项目文档 13 | ``` 14 | 15 | ## 快速请求 16 | 17 | ### 发送聊天请求 18 | 19 | ```bash 20 | curl --location --request POST 'http://127.0.0.1:8000/v1/chat/completions' \ 21 | --header 'Content-Type: application/json' \ 22 | --data-raw '{ 23 | "model": "gemini-enterprise-2", 24 | "messages": [ 25 | { 26 | "role": "user", 27 | "content": "你好" 28 | } 29 | ], 30 | "safe_mode": false 31 | }' 32 | ``` 33 | 34 | ## 功能特性 35 | 36 | ### 核心功能 37 | - **多账号轮训**: 支持配置多个 Gemini 账号,自动轮训使用 38 | - **OpenAI 兼容接口**: 提供与 OpenAI API 兼容的接口格式 39 | - **流式响应**: 支持 SSE (Server-Sent Events) 流式输出 40 | - **代理支持**: 支持 HTTP/HTTPS 代理配置 41 | - **JWT 自动管理**: 自动获取和刷新 JWT Token 42 | 43 | ### 管理功能 44 | - **Web 控制台**: 美观的 Web 管理界面,支持明暗主题切换 45 | - **账号管理**: 添加、编辑、删除、启用/禁用账号 46 | - **模型配置**: 自定义模型参数配置 47 | - **代理测试**: 在线测试代理连接状态 48 | - **配置导入/导出**: 支持配置文件的导入导出 49 | 50 | ## 文件说明 51 | 52 | ### gemini.py 53 | 54 | 后端服务主程序,基于 Flask 框架开发。 55 | 56 | #### 主要类和函数 57 | 58 | | 名称 | 类型 | 说明 | 59 | |------|------|------| 60 | | `AccountManager` | 类 | 账号管理器,负责账号加载、保存、状态管理和轮训选择 | 61 | | `load_config()` | 方法 | 从配置文件加载账号和配置信息 | 62 | | `save_config()` | 方法 | 保存配置到文件 | 63 | | `get_next_account()` | 方法 | 轮训获取下一个可用账号 | 64 | | `mark_account_unavailable()` | 方法 | 标记账号为不可用状态 | 65 | | `create_jwt()` | 函数 | 创建 JWT Token | 66 | | `create_chat_session()` | 函数 | 创建聊天会话 | 67 | | `stream_chat()` | 函数 | 发送聊天请求并获取响应 | 68 | | `check_proxy()` | 函数 | 检测代理是否可用 | 69 | 70 | #### API 接口 71 | 72 | **OpenAI 兼容接口** 73 | 74 | | 方法 | 路径 | 说明 | 75 | |------|------|------| 76 | | GET | `/v1/models` | 获取可用模型列表 | 77 | | POST | `/v1/chat/completions` | 聊天对话接口(支持图片) | 78 | | POST | `/v1/files` | 上传文件 | 79 | | GET | `/v1/files` | 获取文件列表 | 80 | | GET | `/v1/files/` | 获取文件信息 | 81 | | DELETE | `/v1/files/` | 删除文件 | 82 | | GET | `/v1/status` | 获取系统状态 | 83 | | GET | `/health` | 健康检查 | 84 | | GET | `/image/` | 获取缓存图片 | 85 | 86 | **管理接口** 87 | 88 | | 方法 | 路径 | 说明 | 89 | |------|------|------| 90 | | GET | `/` | 返回管理页面 | 91 | | GET | `/api/accounts` | 获取账号列表 | 92 | | POST | `/api/accounts` | 添加账号 | 93 | | PUT | `/api/accounts/` | 更新账号 | 94 | | DELETE | `/api/accounts/` | 删除账号 | 95 | | POST | `/api/accounts//toggle` | 切换账号状态 | 96 | | POST | `/api/accounts//test` | 测试账号 JWT 获取 | 97 | | GET | `/api/models` | 获取模型配置 | 98 | | POST | `/api/models` | 添加模型 | 99 | | PUT | `/api/models/` | 更新模型 | 100 | | DELETE | `/api/models/` | 删除模型 | 101 | | GET | `/api/config` | 获取完整配置 | 102 | | PUT | `/api/config` | 更新配置 | 103 | | POST | `/api/config/import` | 导入配置 | 104 | | GET | `/api/config/export` | 导出配置 | 105 | | POST | `/api/proxy/test` | 测试代理 | 106 | | GET | `/api/proxy/status` | 获取代理状态 | 107 | 108 | ### business_gemini_session.json 109 | 110 | 配置文件,JSON 格式,包含以下字段: 111 | 112 | ```json 113 | { 114 | "proxy": "http://127.0.0.1:7890", 115 | "proxy_enabled": false, 116 | "accounts": [ 117 | { 118 | "team_id": "团队ID", 119 | "secure_c_ses": "安全会话Cookie", 120 | "host_c_oses": "主机Cookie", 121 | "csesidx": "会话索引", 122 | "user_agent": "浏览器UA", 123 | "available": true 124 | } 125 | ], 126 | "models": [ 127 | { 128 | "id": "模型ID", 129 | "name": "模型名称", 130 | "description": "模型描述", 131 | "context_length": 32768, 132 | "max_tokens": 8192, 133 | "price_per_1k_tokens": 0.0015 134 | } 135 | ] 136 | } 137 | ``` 138 | 139 | #### 配置字段说明 140 | 141 | | 字段 | 类型 | 说明 | 142 | |------|------|------| 143 | | `proxy` | string | HTTP 代理地址 | 144 | | `proxy_enabled` | boolean | 代理开关,`true` 启用代理,`false` 禁用代理(默认 `false`) | 145 | | `accounts` | array | 账号列表 | 146 | | `accounts[].team_id` | string | Google Cloud 团队 ID | 147 | | `accounts[].secure_c_ses` | string | 安全会话 Cookie | 148 | | `accounts[].host_c_oses` | string | 主机 Cookie | 149 | | `accounts[].csesidx` | string | 会话索引 | 150 | | `accounts[].user_agent` | string | 浏览器 User-Agent | 151 | | `accounts[].available` | boolean | 账号是否可用 | 152 | | `models` | array | 模型配置列表 | 153 | | `models[].id` | string | 模型唯一标识 | 154 | | `models[].name` | string | 模型显示名称 | 155 | | `models[].description` | string | 模型描述 | 156 | | `models[].context_length` | number | 上下文长度限制 | 157 | | `models[].max_tokens` | number | 最大输出 Token 数 | 158 | 159 | ### index.html 160 | 161 | Web 管理控制台前端,单文件 HTML 应用。 162 | 163 | #### 功能模块 164 | 165 | 1. **仪表盘**: 显示系统概览、账号统计、代理状态 166 | 2. **账号管理**: 账号的增删改查、状态切换、JWT 测试 167 | 3. **模型配置**: 模型的增删改查 168 | 4. **系统设置**: 代理配置、配置导入导出 169 | 170 | #### 界面特性 171 | 172 | - 响应式设计,适配不同屏幕尺寸 173 | - 支持明暗主题切换 174 | - Google Material Design 风格 175 | - 实时状态更新 176 | 177 | ## 快速开始 178 | 179 | ### 环境要求 180 | 181 | - Python 3.7+ 182 | - Flask 183 | - requests 184 | 185 | ### 方式一:直接运行 186 | 187 | #### 安装依赖 188 | 189 | ```bash 190 | pip install flask requests flask-cors 191 | ``` 192 | 193 | #### 配置账号 194 | 195 | 编辑 `business_gemini_session.json` 文件,添加你的 Gemini 账号信息: 196 | 197 | ```json 198 | { 199 | "proxy": "http://your-proxy:port", 200 | "proxy_enabled": true, 201 | "accounts": [ 202 | { 203 | "team_id": "your-team-id", 204 | "secure_c_ses": "your-secure-c-ses", 205 | "host_c_oses": "your-host-c-oses", 206 | "csesidx": "your-csesidx", 207 | "user_agent": "Mozilla/5.0 ...", 208 | "available": true 209 | } 210 | ], 211 | "models": [] 212 | } 213 | ``` 214 | 215 | #### 启动服务 216 | 217 | ```bash 218 | python gemini.py 219 | ``` 220 | 221 | 服务将在 `http://127.0.0.1:8000` 启动。 222 | 223 | ### 方式二:使用 docker-compose 启动服务 224 | 225 | 在项目目录下手动创建 business_gemini_session.json 后 226 | 227 | 使用命令启动: 228 | 229 | ```bash 230 | docker-compose up -d 231 | ``` 232 | 233 | ### 访问管理控制台 234 | 235 | - 直接运行:`http://127.0.0.1:8000/` 236 | - Docker 部署:`http://127.0.0.1:8000/` 237 | 238 | ## API 使用示例 239 | 240 | ### 获取模型列表 241 | 242 | ```bash 243 | curl http://127.0.0.1:8000/v1/models 244 | ``` 245 | 246 | ### 聊天对话 247 | 248 | ```bash 249 | curl -X POST http://127.0.0.1:8000/v1/chat/completions \ 250 | -H "Content-Type: application/json" \ 251 | -d '{ 252 | "model": "gemini-enterprise", 253 | "messages": [ 254 | {"role": "user", "content": "Hello!"} 255 | ], 256 | "stream": false 257 | }' 258 | ``` 259 | 260 | ### 流式对话 261 | 262 | ```bash 263 | curl -X POST http://127.0.0.1:8000/v1/chat/completions \ 264 | -H "Content-Type: application/json" \ 265 | -d '{ 266 | "model": "gemini-enterprise", 267 | "messages": [ 268 | {"role": "user", "content": "Hello!"} 269 | ], 270 | "stream": true 271 | }' 272 | ``` 273 | 274 | ### 带图片对话 275 | 276 | 支持两种图片发送方式: 277 | 278 | #### 方式1:先上传文件,再引用 file_id 279 | 280 | ```bash 281 | # 1. 上传图片 282 | curl -X POST http://127.0.0.1:8000/v1/files \ 283 | -F "file=@image.png" \ 284 | -F "purpose=assistants" 285 | # 返回: {"id": "file-xxx", ...} 286 | 287 | # 2. 引用 file_id 发送消息 288 | curl -X POST http://127.0.0.1:8000/v1/chat/completions \ 289 | -H "Content-Type: application/json" \ 290 | -d '{ 291 | "model": "gemini-enterprise", 292 | "messages": [ 293 | { 294 | "role": "user", 295 | "content": [ 296 | {"type": "text", "text": "描述这张图片"}, 297 | {"type": "file", "file_id": "file-xxx"} 298 | ] 299 | } 300 | ] 301 | }' 302 | ``` 303 | 304 | #### 方式2:内联 base64 图片(自动上传) 305 | 306 | **OpenAI 标准格式** 307 | 308 | ```bash 309 | curl -X POST http://127.0.0.1:8000/v1/chat/completions \ 310 | -H "Content-Type: application/json" \ 311 | -d '{ 312 | "model": "gemini-enterprise", 313 | "messages": [ 314 | { 315 | "role": "user", 316 | "content": [ 317 | {"type": "text", "text": "描述这张图片"}, 318 | {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}} 319 | ] 320 | } 321 | ] 322 | }' 323 | ``` 324 | 325 | **prompts 格式(files 数组)** 326 | 327 | ```bash 328 | curl -X POST http://127.0.0.1:8000/v1/chat/completions \ 329 | -H "Content-Type: application/json" \ 330 | -d '{ 331 | "model": "gemini-enterprise", 332 | "prompts": [ 333 | { 334 | "role": "user", 335 | "text": "描述这张图片", 336 | "files": [ 337 | { 338 | "data": "data:image/png;base64,...", 339 | "type": "image" 340 | } 341 | ] 342 | } 343 | ] 344 | }' 345 | ``` 346 | 347 | > **注意**: 内联 base64 图片会自动上传到 Gemini 获取 fileId,然后发送请求。 348 | 349 | ## 注意事项 350 | 351 | 1. **安全性**: 配置文件中包含敏感信息,请妥善保管,不要提交到公开仓库 352 | 2. **代理**: 如果需要访问 Google 服务,可能需要配置代理 353 | 3. **账号限制**: 请遵守 Google 的使用条款,合理使用 API 354 | 4. **JWT 有效期**: JWT Token 有效期有限,系统会自动刷新 355 | 356 | ## 许可证 357 | 358 | MIT License 359 | -------------------------------------------------------------------------------- /chat_history.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Business Gemini 智能对话 7 | 636 | 637 | 638 | 639 | 640 |
641 |

Business Gemini 智能对话

642 |
643 |
644 | 645 | 648 |
649 |
650 | 651 | 654 |
655 |
656 | 非流式 657 | 661 | 流式 662 |
663 | 666 | 669 | 672 |
673 |
674 | 675 | 676 |
677 | 678 |
679 |
680 |

会话列表

681 | 682 |
683 |
684 | 685 |
686 |
687 | 688 | 689 |
690 | 691 |
692 | 693 |
694 | 695 | 696 |
697 | 702 |
703 | 704 | 707 | 708 | 709 |
710 |
711 |
712 |
713 | 714 | 1711 | 1712 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Business Gemini Pool 管理控制台 7 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 |
567 | 568 |
569 |
570 | 571 |

Business Gemini Pool 管理控制台

572 |
573 |
574 |
服务运行中
575 |
576 | 577 | 582 |
583 | 584 | 585 | 586 | 在线对话 587 | 588 | 593 |
594 |
595 | 596 | 597 |
598 | 602 | 606 | 610 | 614 |
615 | 616 | 617 |
618 | 619 |
620 |
621 |
622 |

总账号数

623 |
624 |
625 |
626 |

0

627 |
628 |
629 |
630 |
631 |

可用账号

632 |
633 |
634 |
635 |

0

636 |
637 |
638 |
639 |
640 |

不可用账号

641 |
642 |
643 |
644 |

0

645 |
646 |
647 |
648 |
649 |

当前轮训索引

650 |
651 |
652 |
653 |

0

654 |
655 |
656 |
657 | 658 |
659 |
660 |
661 | 662 | 账号列表 663 |
664 | 668 |
669 |
670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 |
序号Team IDcsesidxUser Agent状态操作
683 |
684 |
685 |
686 | 687 | 688 |
689 |
690 |
691 |
692 | 693 | 模型列表 694 |
695 | 699 |
700 |
701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 |
模型ID名称描述上下文长度最大Token状态操作
715 |
716 |
717 |
718 | 719 | 720 |
721 |
722 |
723 |
724 | 725 | 系统配置 726 |
727 |
728 |
729 |
730 |
731 |

代理设置

732 |
733 | 734 | 735 | 用于访问Google API的代理服务器地址 736 |
737 |
738 |
739 | 740 | 744 | 控制聊天接口返回的图片是以URL形式还是以 data:image/...;base64,... 形式输出 745 |
746 |
747 | 750 | 753 |
754 |
755 | 756 |
757 |

服务信息

758 |
759 |
760 | 761 | 762 |
763 |
764 | 765 | 766 |
767 |
768 |
769 | 770 |
771 |

配置文件

772 |
773 | 774 | 775 | 配置文件路径: business_gemini_session.json 776 |
777 |
778 | 781 | 784 | 787 | 788 |
789 |
790 |
791 |
792 |
793 |
794 | 795 | 796 |
797 |
798 |
799 |
800 | 801 | Token 管理 802 |
803 |
804 | 805 | 806 | 807 |
808 |
809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 |
Token操作
加载中...
820 |
821 |
822 |
823 | 824 | 825 | 868 | 869 | 904 | 905 | 906 | 942 | 943 | 944 | 982 | 983 | 984 | 1003 | 1004 | 1005 | 1044 | 1045 | 1046 | 1047 |
1048 | 1049 |
1050 |
1051 | 1052 | 2023 | 2024 | 2025 | 2026 | --------------------------------------------------------------------------------