Amazon Q to API Bridge:一个使用 FastAPI 实现的桥接服务,将 Amazon Q Developer 变成兼容 OpenAI Chat Completions 和 Claude Messages 的 API,支持多账号管理、随机负载均衡和流式响应。
|
|
hai 3 meses | |
|---|---|---|
| frontend | hai 3 meses | |
| scripts | hai 3 meses | |
| templates | hai 3 meses | |
| .env.example | hai 3 meses | |
| .gitignore | hai 3 meses | |
| README.md | hai 3 meses | |
| app.py | hai 3 meses | |
| auth_flow.py | hai 3 meses | |
| replicate.py | hai 3 meses | |
| requirements.txt | hai 3 meses |
将 Amazon Q Developer 转换为 OpenAI 兼容的 API 服务,支持流式和非流式响应。
/v1/chat/completions)# 创建虚拟环境
python -m venv .venv
# Windows
.venv\Scripts\activate
pip install -r requirements.txt
# Linux/macOS
source .venv/bin/activate
pip install -r requirements.txt
# 复制示例配置
cp .env.example .env
# 编辑 .env 文件
# OPENAI_KEYS="key1,key2,key3" # 可选,留空则为开发模式
# MAX_ERROR_COUNT=100 # 错误次数阈值
# HTTP_PROXY="http://127.0.0.1:7890" # HTTP代理(可选)
配置说明:
OPENAI_KEYS 为空或未设置:开发模式,不校验 AuthorizationOPENAI_KEYS 设置后:仅白名单中的 key 可访问 APIMAX_ERROR_COUNT:账号连续失败次数超过此值将自动禁用(默认100)HTTP_PROXY:HTTP代理地址,留空则不使用代理python -m uvicorn app:app --reload --port 8000
访问:
frontend/index.html,否则将返回 404)访问 http://localhost:8000/ 使用可视化界面管理账号:
创建账号
curl -X POST http://localhost:8000/v2/accounts \
-H "Content-Type: application/json" \
-d '{
"label": "我的账号",
"clientId": "your-client-id",
"clientSecret": "your-client-secret",
"refreshToken": "your-refresh-token",
"enabled": true
}'
列出所有账号
curl http://localhost:8000/v2/accounts
更新账号(切换启用状态)
curl -X PATCH http://localhost:8000/v2/accounts/{account_id} \
-H "Content-Type: application/json" \
-d '{"enabled": false}'
刷新 Token
curl -X POST http://localhost:8000/v2/accounts/{account_id}/refresh
删除账号
curl -X DELETE http://localhost:8000/v2/accounts/{account_id}
快速添加账号的最简单方式:
启动登录流程
curl -X POST http://localhost:8000/v2/auth/start \
-H "Content-Type: application/json" \
-d '{"label": "新账号", "enabled": true}'
返回:
{
"authId": "xxx",
"verificationUriComplete": "https://...",
"userCode": "ABCD-1234",
"expiresIn": 600,
"interval": 1
}
在浏览器中打开 verificationUriComplete 完成登录
等待并创建账号(最多5分钟)
curl -X POST http://localhost:8000/v2/auth/claim/{authId}
成功后自动创建并启用账号。
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "claude-sonnet-4",
"stream": false,
"messages": [
{"role": "system", "content": "你是一个乐于助人的助手"},
{"role": "user", "content": "你好,请讲一个简短的故事"}
]
}'
curl -N -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "claude-sonnet-4",
"stream": true,
"messages": [
{"role": "user", "content": "讲一个笑话"}
]
}'
import openai
client = openai.OpenAI(
base_url="http://localhost:8000/v1",
api_key="your-api-key" # 如果配置了 OPENAI_KEYS
)
response = client.chat.completions.create(
model="claude-sonnet-4",
messages=[
{"role": "user", "content": "你好"}
]
)
print(response.choices[0].message.content)
OPENAI_KEYS 未设置):不校验 AuthorizationOPENAI_KEYS 已设置):必须提供白名单中的 keyenabled=1 的账号中随机选择.
├── app.py # FastAPI 主应用
├── auth_flow.py # 设备授权登录
├── replicate.py # Amazon Q 请求复刻
├── requirements.txt # Python 依赖
├── .env.example # 环境变量示例
├── .gitignore # Git 忽略规则
├── data.sqlite3 # SQLite 数据库(自动创建)
├── templates/
│ └── streaming_request.json # 请求模板
└── frontend/
└── index.html # Web 控制台入口
| 变量 | 说明 | 默认值 |
|---|---|---|
OPENAI_KEYS |
API Key 白名单(逗号分隔) | 空(开发模式) |
MAX_ERROR_COUNT |
错误次数阈值,超过自动禁用账号 | 100 |
HTTP_PROXY |
HTTP代理地址(如 http://127.0.0.1:7890) | 空(不使用代理) |
CREATE TABLE accounts (
id TEXT PRIMARY KEY,
label TEXT,
clientId TEXT,
clientSecret TEXT,
refreshToken TEXT,
accessToken TEXT,
other TEXT, -- JSON 格式的额外信息
last_refresh_time TEXT,
last_refresh_status TEXT,
created_at TEXT,
updated_at TEXT,
enabled INTEGER DEFAULT 1, -- 1=启用, 0=禁用
error_count INTEGER DEFAULT 0, -- 连续错误次数
success_count INTEGER DEFAULT 0 -- 成功请求次数
);
系统会自动统计每个账号的请求结果:
success_count+1,error_count重置为0error_count+1error_count >= MAX_ERROR_COUNT时,账号自动设置为enabled=0这确保了有问题的账号不会持续影响服务质量。
OPENAI_KEYS 配置enabled=1 的账号last_refresh_status 字段POST /v2/accounts - 创建账号GET /v2/accounts - 列出所有账号GET /v2/accounts/{id} - 获取账号详情PATCH /v2/accounts/{id} - 更新账号DELETE /v2/accounts/{id} - 删除账号POST /v2/accounts/{id}/refresh - 刷新 TokenPOST /v2/auth/start - 启动登录流程GET /v2/auth/status/{authId} - 查询登录状态POST /v2/auth/claim/{authId} - 等待并创建账号POST /v1/chat/completions - Chat Completions APIGET / - Web 控制台GET /healthz - 健康检查本项目仅供学习和测试使用。
欢迎提交 Issue 和 Pull Request!