142 lines
3.1 KiB
Markdown
142 lines
3.1 KiB
Markdown
# Simple Gemini to OpenAI API Proxy
|
||
|
||
这是一个极其简单的Gemini到OpenAI API格式的代理服务,基于 `zhu327/gemini-openai-proxy` 项目。
|
||
|
||
## 特点
|
||
|
||
- ✅ 无需数据库
|
||
- ✅ 配置极其简单,只需要API密钥
|
||
- ✅ 支持Docker部署
|
||
- ✅ 立即可用,无需复杂配置
|
||
- ✅ 支持OpenAI API格式调用Gemini模型
|
||
|
||
## 快速开始
|
||
|
||
### 1. 获取Gemini API密钥
|
||
|
||
访问 [Google AI Studio](https://ai.google.dev) 获取免费的Gemini API密钥。
|
||
|
||
### 2. 启动服务
|
||
|
||
```bash
|
||
# 启动代理服务
|
||
docker-compose up -d
|
||
|
||
# 查看日志
|
||
docker-compose logs -f
|
||
```
|
||
|
||
### 3. 测试服务
|
||
|
||
```bash
|
||
# 使用你的Gemini API密钥替换 YOUR_GEMINI_API_KEY
|
||
curl http://localhost:8081/v1/chat/completions \
|
||
-H "Content-Type: application/json" \
|
||
-H "Authorization: Bearer YOUR_GEMINI_API_KEY" \
|
||
-d '{
|
||
"model": "gpt-3.5-turbo",
|
||
"messages": [{"role": "user", "content": "你好!"}],
|
||
"temperature": 0.7
|
||
}'
|
||
```
|
||
|
||
## 使用说明
|
||
|
||
### API端点
|
||
|
||
- **基础URL**: `http://localhost:8081`
|
||
- **聊天完成**: `POST /v1/chat/completions`
|
||
|
||
### 模型映射
|
||
|
||
默认的模型映射:
|
||
- `gpt-3.5-turbo` → `gemini-1.5-flash-latest`
|
||
- `gpt-4` → `gemini-1.5-pro-latest`
|
||
- `gpt-4-vision-preview` → `gemini-1.5-flash-latest`
|
||
|
||
### 环境变量配置
|
||
|
||
在 `docker-compose.yml` 中可以配置:
|
||
|
||
- `DISABLE_MODEL_MAPPING=1`: 禁用模型映射,直接使用Gemini模型名称
|
||
- `GPT_4=gemini-1.5-pro-latest`: 自定义模型映射
|
||
|
||
## 与各种客户端集成
|
||
|
||
### 使用curl
|
||
|
||
```bash
|
||
curl http://localhost:8081/v1/chat/completions \
|
||
-H "Content-Type: application/json" \
|
||
-H "Authorization: Bearer YOUR_GEMINI_API_KEY" \
|
||
-d '{
|
||
"model": "gpt-3.5-turbo",
|
||
"messages": [{"role": "user", "content": "Hello!"}]
|
||
}'
|
||
```
|
||
|
||
### 使用Python OpenAI库
|
||
|
||
```python
|
||
import openai
|
||
|
||
client = openai.OpenAI(
|
||
api_key="YOUR_GEMINI_API_KEY",
|
||
base_url="http://localhost:8081/v1"
|
||
)
|
||
|
||
response = client.chat.completions.create(
|
||
model="gpt-3.5-turbo",
|
||
messages=[{"role": "user", "content": "Hello!"}]
|
||
)
|
||
|
||
print(response.choices[0].message.content)
|
||
```
|
||
|
||
## 管理命令
|
||
|
||
```bash
|
||
# 启动服务
|
||
docker-compose up -d
|
||
|
||
# 停止服务
|
||
docker-compose down
|
||
|
||
# 重启服务
|
||
docker-compose restart
|
||
|
||
# 查看状态
|
||
docker-compose ps
|
||
|
||
# 查看日志
|
||
docker-compose logs -f gemini-proxy
|
||
```
|
||
|
||
## 注意事项
|
||
|
||
1. **API密钥安全**: 请确保妥善保管你的Gemini API密钥
|
||
2. **网络访问**: 默认绑定到8081端口,如需外部访问请配置防火墙
|
||
3. **速率限制**: 遵循Google Gemini API的速率限制
|
||
4. **生产环境**: 如用于生产环境,建议添加认证和HTTPS
|
||
|
||
## 故障排除
|
||
|
||
### 容器无法启动
|
||
```bash
|
||
# 检查端口是否被占用
|
||
sudo netstat -tlnp | grep 8081
|
||
|
||
# 查看详细错误日志
|
||
docker-compose logs gemini-proxy
|
||
```
|
||
|
||
### API调用失败
|
||
1. 检查API密钥是否正确
|
||
2. 确认网络连接正常
|
||
3. 检查请求格式是否符合OpenAI API规范
|
||
|
||
## 项目信息
|
||
|
||
- **基于项目**: [zhu327/gemini-openai-proxy](https://github.com/zhu327/gemini-openai-proxy)
|
||
- **Docker镜像**: `zhu327/gemini-openai-proxy:latest`
|
||
- **支持的模型**: Gemini 1.5 Pro, Gemini 1.5 Flash |