# any-api **Repository Path**: xiaoyybb/any-api ## Basic Information - **Project Name**: any-api - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-23 - **Last Updated**: 2026-01-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Gemini API Server 将 Google Gemini 转换为 OpenAI 兼容的 RESTful API,支持多轮对话和持久会话。 ## ✨ 特性 ### 核心功能 - 🚀 **OpenAI 兼容格式** - 完全兼容 `/v1/chat/completions` 接口 - 💬 **持久会话** - 每个会话一个浏览器实例,在同一对话框继续聊天 - 🧠 **自动上下文记忆** - Gemini 自动记住对话历史 - 🔒 **安全登录** - 使用真实的 Google 账号 ### V4 新增功能 🎉 - 🎯 **思考模式选择** - Fast/Thinking/Pro 三种模式 - 🛠️ **Tools 功能** - 深度搜索、创建图片 - 📷 **图片上传** - 上传图片让 Gemini 分析 - 📦 **即插即用** - 只需修改 `base_url` 即可替换 OpenAI API ## 📁 项目结构 ``` any-api/ ├── docs/ # 📖 文档 │ ├── README.md # 详细文档 │ ├── QUICKSTART.md # 快速开始 │ └── V4_FEATURES.md # V4 新功能说明 ├── src/ # 💻 源代码 │ ├── server.py # API 服务器 (V4) │ └── save_login.py # 保存登录状态 ├── tests/ # 🧪 测试 │ ├── test_client.py # 基础测试 │ └── test_advanced.py # V4 功能测试 ├── tools/ # 🔧 工具脚本 │ └── simple_debug.py # 调试工具 ├── requirements.txt # Python 依赖 ├── start_server.bat # Windows 启动脚本 ├── start.sh # Linux/Mac 启动脚本 └── README.md # 本文档 ``` ## 🚀 快速开始 ### 1. 安装依赖 ```bash pip install -r requirements.txt playwright install chromium ``` ### 2. 保存登录状态 ```bash python src/save_login.py ``` 在打开的浏览器中完成 Google 账号登录,登录成功后按回车。 ### 3. 启动服务器 **Windows:** ```bash start_server.bat ``` **Mac/Linux:** ```bash chmod +x start.sh ./start.sh ``` 或直接运行: ```bash python src/server.py ``` 服务器启动后: - API 地址: `http://localhost:8000` - 交互式文档: `http://localhost:8000/docs` ### 4. 测试 **基础测试:** ```bash python tests/test_client.py ``` **V4 新功能测试:** ```bash python tests/test_advanced.py ``` ## 💡 使用示例 ### 基础对话 ```python import requests response = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "你好"}] }) print(response.json()["choices"][0]["message"]["content"]) ``` ### 思考模式(V4 新功能) ```python # Fast 模式 - 快速响应 response = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "简单问题"}], "thinking_mode": "fast" }) # Pro 模式 - 深度思考 response = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "复杂问题"}], "thinking_mode": "pro" }) ``` ### Tools 功能(V4 新功能) ```python # 深度搜索 response = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "搜索最新的AI技术进展"}], "tools": ["deep_search"] }) # 创建图片 response = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "创建一张可爱的猫咪图片"}], "tools": ["image_generation"] }) # 组合使用 response = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "深入研究AI技术"}], "thinking_mode": "thinking", "tools": ["deep_search"] }) ``` ### 图片上传(V4 新功能) ```python with open("image.jpg", "rb") as f: files = {"image": f} data = {"message": "请分析这张图片"} response = requests.post("http://localhost:8000/v4/chat/upload", files=files, data=data) print(response.json()["choices"][0]["message"]["content"]) ``` ### 持久会话 ```python session_id = None # 第一轮 r = requests.post("http://localhost:8000/v4/chat/completions", json={ "messages": [{"role": "user", "content": "我叫张三"}] }) session_id = r.json()["session_id"] # 第十轮(记得第一轮的内容) r = requests.post("http://localhost:8000/v4/chat/completions", json={ "session_id": session_id, "messages": [{"role": "user", "content": "我叫什么名字?"}] }) print(r.json()["choices"][0]["message"]["content"]) # 输出:你叫张三 ``` ### OpenAI SDK(推荐) ```python from openai import OpenAI client = OpenAI( api_key="fake-key", base_url="http://localhost:8000/v4" ) response = client.chat.completions.create( model="gemini-2.0-flash-exp", messages=[{"role": "user", "content": "你好"}] ) print(response.choices[0].message.content) ``` ## 📚 文档 - **[完整使用文档](docs/README.md)** - 详细的功能说明和 API 文档 - **[快速开始指南](docs/QUICKSTART.md)** - 5 分钟上手教程 - **[V4 新功能说明](docs/V4_FEATURES.md)** - 思考模式、Tools、图片上传详解 ## 🎯 V4 API 参数 ### POST /v4/chat/completions | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | model | string | 否 | 模型名称,默认 "gemini-2.0-flash-exp" | | messages | array | 是 | 消息列表 | | session_id | string | 否 | 会话 ID,用于多轮对话 | | thinking_mode | string | 否 | "fast" \| "thinking" \| "pro" | | tools | array | 否 | ["deep_search", "image_generation"] | ### POST /v4/chat/upload | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | message | string | 是 | 文本消息 | | image | file | 是 | 图片文件 | | session_id | string | 否 | 会话 ID | | thinking_mode | string | 否 | 思考模式 | | tools | string | 否 | 工具列表 JSON 字符串 | ## ⚠️ 注意事项 1. **登录有效期** - Google 登录可能会过期,如返回错误,重新运行 `python src/save_login.py` 2. **响应速度** - 使用浏览器自动化,响应时间约 5-10 秒,Pro 模式可能需要 30 秒+ 3. **资源占用** - 每个会话占用约 200-300MB 内存,建议定期清理 4. **图片大小** - 建议上传的图片 < 10MB ## 🐛 故障排查 运行调试工具: ```bash python tools/simple_debug.py ``` ### 常见问题 **Q: 思考模式设置失败?** A: Gemini UI 可能更新,检查服务器日志查看具体错误 **Q: Tools 无法启用?** A: 确保 Google 账号有权限使用这些功能 **Q: 图片上传失败?** A: 检查图片格式(支持 JPG/PNG/GIF/WebP)和大小 ## 📄 License MIT License --- **提示**: - 首次使用请先查看 [快速开始指南](docs/QUICKSTART.md) - 了解 V4 新功能请查看 [V4 功能说明](docs/V4_FEATURES.md)