Skip to content

Commit cbbe32c

Browse files
committed
feat(ai/gateway): 新增TensorZero和Portkey网关支持,重构LiteLLM配置为Qwen专用网关
- 新增TensorZero网关实现,包含完整的生产级Qwen接入模板,支持ClickHouse 观测、Valkey限流、降级和重试策略 - 新增Portkey网关模板,提供NewAPI接入的fallback重试机制 - 重构LiteLLM网关从通用模型透传改为专用Qwen网关,集成阿里百炼DashScope API,支持主备模型降级、限流保护和自动重试 -
1 parent b93f452 commit cbbe32c

18 files changed

Lines changed: 815 additions & 24 deletions

ai/gateway/litellm/.env.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
NEWAPI_KEY=sk-xxxx
2-
NEWAPI_API_BASE=https://example.com
1+
LITELLM_IMAGE=docker.litellm.ai/berriai/litellm:main-latest
2+
LITELLM_HOST_PORT=34000
3+
DASHSCOPE_API_KEY=sk-xxxx
4+
DASHSCOPE_API_BASE=https://dashscope.aliyuncs.com/compatible-mode/v1
35
LITELLM_MASTER_KEY=sk-litellm-123456
46
DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 生产环境建议固定镜像版本,并通过反向代理暴露 HTTPS。
2+
LITELLM_IMAGE=docker.litellm.ai/berriai/litellm:main-latest
3+
LITELLM_HOST_PORT=34000
4+
DASHSCOPE_API_KEY=sk-prod-xxxx
5+
DASHSCOPE_API_BASE=https://dashscope.aliyuncs.com/compatible-mode/v1
6+
LITELLM_MASTER_KEY=sk-litellm-prod-change-me
7+
DATABASE_URL=postgresql://litellm:change-me@postgres.internal:5432/litellm

ai/gateway/litellm/compose.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
services:
22
litellm:
33
container_name: litellm
4-
image: docker.litellm.ai/berriai/litellm:main-latest
4+
image: ${LITELLM_IMAGE:-docker.litellm.ai/berriai/litellm:main-latest}
55
restart: unless-stopped
66
ports:
7-
- "34000:4000"
7+
- "${LITELLM_HOST_PORT:-34000}:4000"
88
environment:
99
# LiteLLM 官方镜像会把 PORT 透传给内部 uvicorn 启动参数,未显式设置时会报错。
1010
PORT: "4000"
1111
# 通过 compose 插值保留当前默认数据库地址;start.ps1 会在存在 .env.local 时传入 --env-file 以支持本地覆盖。
1212
DATABASE_URL: ${DATABASE_URL:-postgresql://postgres:12345678@host.docker.internal:5432/litellm}
13-
# LiteLLM 运行时会从容器环境变量读取这些值,再由 newapi.yaml 中的 os.environ/... 引用。
14-
NEWAPI_KEY: ${NEWAPI_KEY:-}
15-
NEWAPI_API_BASE: ${NEWAPI_API_BASE:-}
13+
# LiteLLM 运行时会从容器环境变量读取这些值,再由 qwen.yaml 中的 os.environ/... 引用。
14+
DASHSCOPE_API_KEY: ${DASHSCOPE_API_KEY:-}
15+
DASHSCOPE_API_BASE: ${DASHSCOPE_API_BASE:-https://dashscope.aliyuncs.com/compatible-mode/v1}
1616
LITELLM_MASTER_KEY: ${LITELLM_MASTER_KEY:-}
1717
volumes:
18-
- ./newapi.yaml:/app/config.yaml:ro
18+
- ./qwen.yaml:/app/config.yaml:ro
1919
extra_hosts:
2020
# 在 Linux Docker Engine 场景补齐宿主机别名,保持默认 DATABASE_URL 可用。
2121
- "host.docker.internal:host-gateway"

ai/gateway/litellm/litellm.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
# LiteLLM 网关说明
22

3-
这个目录用于启动一个基于 LiteLLM Proxy 的统一模型网关,当前配置会把客户端传入的模型名透传到 NewAPI
3+
这个目录用于启动一个基于 LiteLLM Proxy 的生产级 Qwen 网关,默认直连阿里百炼的 OpenAI 兼容接口
44

55
相关文件职责如下:
66

7-
- `newapi.yaml`:LiteLLM 代理配置,定义模型透传和 `master_key`
7+
- `qwen.yaml`:LiteLLM 生产配置,定义主模型、降级模型、限流、重试和超时策略
88
- `compose.yaml`:LiteLLM 容器模板,定义镜像、端口、挂载和默认环境变量。
99
- `start.ps1`:统一入口,封装常用 `docker compose` 操作。
10-
- `../.env.local`:本地私有环境变量,保存 `NEWAPI_KEY``NEWAPI_API_BASE``LITELLM_MASTER_KEY`、可选 `DATABASE_URL`
10+
- `.env.example`:开发环境变量示例。
11+
- `.env.production.example`:生产环境变量示例。
12+
- `.env.local`:本地私有环境变量,保存 `DASHSCOPE_API_KEY``DASHSCOPE_API_BASE``LITELLM_MASTER_KEY`、可选 `DATABASE_URL`
1113

1214
## 环境变量
1315

14-
建议先在 `ai/gateway/.env.local` 中配置以下值:
16+
建议先在 `ai/gateway/litellm/.env.local` 中配置以下值:
1517

1618
```dotenv
17-
NEWAPI_KEY=sk-xxxx
18-
NEWAPI_API_BASE=https://example.com
19+
LITELLM_IMAGE=docker.litellm.ai/berriai/litellm:main-latest
20+
LITELLM_HOST_PORT=34000
21+
DASHSCOPE_API_KEY=sk-xxxx
22+
DASHSCOPE_API_BASE=https://dashscope.aliyuncs.com/compatible-mode/v1
1923
LITELLM_MASTER_KEY=sk-litellm-123456
2024
DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm
2125
```
2226

2327
说明:
2428

25-
- `NEWAPI_KEY`:NewAPI 的访问密钥。
26-
- `NEWAPI_API_BASE`:NewAPI 的 OpenAI 兼容接口地址。
29+
- `LITELLM_IMAGE`:LiteLLM 镜像,可用于生产环境固定到经过验证的标签。
30+
- `LITELLM_HOST_PORT`:宿主机暴露端口,默认 `34000`
31+
- `DASHSCOPE_API_KEY`:阿里百炼 API Key。
32+
- `DASHSCOPE_API_BASE`:阿里百炼 OpenAI 兼容接口地址;中国内地默认可用 `https://dashscope.aliyuncs.com/compatible-mode/v1`
2733
- `LITELLM_MASTER_KEY`:LiteLLM Proxy 对外暴露的网关密钥。
2834
- `DATABASE_URL`:LiteLLM 的数据库连接串;如果未配置,会回退到默认的宿主机 PostgreSQL 地址。
2935

36+
如果你想准备生产环境变量,可直接复制 `./.env.production.example` 再按实际环境改值。
37+
3038
## 启动方式
3139

3240
推荐直接使用 `start.ps1`
@@ -38,7 +46,7 @@ DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm
3846
默认等价于:
3947

4048
```powershell
41-
docker compose --env-file ai/gateway/.env.local `
49+
docker compose --env-file ai/gateway/litellm/.env.local `
4250
-f ai/gateway/litellm/compose.yaml `
4351
--project-directory ai/gateway/litellm `
4452
up -d
@@ -70,7 +78,7 @@ docker compose --env-file ai/gateway/.env.local `
7078
如果你想直接执行 `docker compose`,建议保持和脚本一致的参数:
7179

7280
```powershell
73-
docker compose --env-file ai/gateway/.env.local `
81+
docker compose --env-file ai/gateway/litellm/.env.local `
7482
-f ai/gateway/litellm/compose.yaml `
7583
--project-directory ai/gateway/litellm `
7684
logs -f litellm
@@ -92,13 +100,15 @@ OpenAI 兼容接口示例:
92100
curl http://127.0.0.1:34000/v1/chat/completions `
93101
-H "Content-Type: application/json" `
94102
-H "Authorization: Bearer sk-litellm-123456" `
95-
-d "{\"model\":\"gpt-4o\",\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}]}"
103+
-d "{\"model\":\"qwen-chat\",\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}]}"
96104
```
97105

98106
## 配置说明
99107

100-
当前 `newapi.yaml` 的关键点:
108+
当前 `qwen.yaml` 的关键点:
101109

102-
- `model_name: "*"` 允许客户端传入任意模型名。
103-
- `model: "openai/{{ model }}"` 会把客户端的模型名透传给下游 NewAPI。
104-
- `api_base``api_key``master_key` 都从容器环境变量读取,便于本地通过 `.env.local` 管理敏感值。
110+
- `qwen-chat`:主模型,默认走百炼 `qwen-plus`
111+
- `qwen-chat-fallback`:降级模型,默认走百炼 `qwen-flash`
112+
- `rpm`:在部署层做第一道限流保护,避免上游配额被瞬时打爆。
113+
- `num_retries` + `timeout`:对临时失败、超时和抖动做统一收敛。
114+
- `fallbacks` + `cooldown_time`:主模型异常时自动切到降级模型,并对异常部署做短暂冷却。

ai/gateway/litellm/qwen.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
model_list:
2+
- model_name: qwen-chat
3+
litellm_params:
4+
# 主力模型默认走阿里百炼的 qwen-plus,优先保证效果。
5+
model: "openai/qwen-plus"
6+
api_base: "os.environ/DASHSCOPE_API_BASE"
7+
api_key: "os.environ/DASHSCOPE_API_KEY"
8+
# 使用每分钟请求数作为网关侧的第一层限流保护,实际数值应按百炼配额再调整。
9+
rpm: 300
10+
timeout: 20
11+
12+
- model_name: qwen-chat-fallback
13+
litellm_params:
14+
# 降级模型使用 qwen-flash,在主模型限流或超时时承担兜底流量。
15+
model: "openai/qwen-flash"
16+
api_base: "os.environ/DASHSCOPE_API_BASE"
17+
api_key: "os.environ/DASHSCOPE_API_KEY"
18+
rpm: 600
19+
timeout: 15
20+
21+
litellm_settings:
22+
# 生产环境默认关闭遥测,并在网关层统一控制重试与超时。
23+
telemetry: false
24+
drop_params: true
25+
num_retries: 2
26+
request_timeout: 25
27+
28+
router_settings:
29+
# 开启前置检查,避免已知不可用的部署继续接流量。
30+
enable_pre_call_checks: true
31+
# 主模型失败后自动切到降级模型。
32+
fallbacks:
33+
- qwen-chat:
34+
- qwen-chat-fallback
35+
# 连续失败后将主模型暂时冷却,避免放大上游故障。
36+
allowed_fails: 2
37+
cooldown_time: 30
38+
num_retries: 2
39+
timeout: 20000
40+
41+
general_settings:
42+
master_key: "os.environ/LITELLM_MASTER_KEY"
43+
proxy_batch_write_at: 60
44+
database_connection_pool_limit: 10

ai/gateway/litellm/start.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ErrorActionPreference = 'Stop'
1313

1414
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
1515
$composeFile = Join-Path $scriptDir 'compose.yaml'
16-
$envFile = '.env.local'
16+
$envFile = Join-Path $scriptDir '.env.local'
1717

1818
function Show-Usage {
1919
<#

ai/gateway/portkey/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PORTKEY_IMAGE=portkeyai/gateway:latest
2+
PORTKEY_HOST_PORT=34200
3+
NEWAPI_API_BASE=https://newapi.example.com/v1
4+
NEWAPI_API_KEY=sk-xxxx
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 生产环境建议固定镜像版本,并把公网入口交给反向代理或 API 网关。
2+
PORTKEY_IMAGE=portkeyai/gateway:latest
3+
PORTKEY_HOST_PORT=34200
4+
NEWAPI_API_BASE=https://newapi.example.com/v1
5+
NEWAPI_API_KEY=sk-prod-xxxx

ai/gateway/portkey/compose.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
portkey:
3+
container_name: portkey
4+
image: ${PORTKEY_IMAGE:-portkeyai/gateway:latest}
5+
restart: unless-stopped
6+
ports:
7+
- "${PORTKEY_HOST_PORT:-34200}:8787"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"strategy": {
3+
"mode": "fallback",
4+
"on_status_codes": [408, 429, 500, 502, 503, 504]
5+
},
6+
"request_timeout": 15000,
7+
"retry": {
8+
"attempts": 2,
9+
"on_status_codes": [408, 429, 500, 502, 503, 504],
10+
"use_retry_after_headers": true
11+
},
12+
"targets": [
13+
{
14+
"provider": "openai",
15+
"api_key": "sk-your-newapi-key",
16+
"custom_host": "https://newapi-primary.example.com/v1",
17+
"override_params": {
18+
"model": "qwen-plus",
19+
"temperature": 0.3,
20+
"max_tokens": 4096
21+
}
22+
},
23+
{
24+
"provider": "openai",
25+
"api_key": "sk-your-newapi-key",
26+
"custom_host": "https://newapi-primary.example.com/v1",
27+
"override_params": {
28+
"model": "qwen-flash",
29+
"temperature": 0.3,
30+
"max_tokens": 4096
31+
}
32+
}
33+
]
34+
}

0 commit comments

Comments
 (0)