Skip to content

Commit b93f452

Browse files
committed
feat(litellm): 添加 LiteLLM 网关配置和管理脚本
- 新增 compose.yaml 文件,配置 LiteLLM 容器化部署 - 添加 start.ps1 脚本,提供便捷的 docker compose 操作封装 - 更新 .env.example 添加 DATABASE_URL 配置项 - 新增 litellm/compose.yaml 和 litellm/.env.example 配置文件 - 重构 litellm.md 文档,简化说明
1 parent c03281c commit b93f452

5 files changed

Lines changed: 242 additions & 263 deletions

File tree

ai/gateway/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
NEWAPI_KEY=sk-xxxx
22
NEWAPI_API_BASE=https://example.com
3-
LITELLM_MASTER_KEY=sk-litellm-123456
3+
LITELLM_MASTER_KEY=sk-litellm-123456
4+
DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm

ai/gateway/litellm/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NEWAPI_KEY=sk-xxxx
2+
NEWAPI_API_BASE=https://example.com
3+
LITELLM_MASTER_KEY=sk-litellm-123456
4+
DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm

ai/gateway/litellm/compose.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
litellm:
3+
container_name: litellm
4+
image: docker.litellm.ai/berriai/litellm:main-latest
5+
restart: unless-stopped
6+
ports:
7+
- "34000:4000"
8+
environment:
9+
# LiteLLM 官方镜像会把 PORT 透传给内部 uvicorn 启动参数,未显式设置时会报错。
10+
PORT: "4000"
11+
# 通过 compose 插值保留当前默认数据库地址;start.ps1 会在存在 .env.local 时传入 --env-file 以支持本地覆盖。
12+
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:-}
16+
LITELLM_MASTER_KEY: ${LITELLM_MASTER_KEY:-}
17+
volumes:
18+
- ./newapi.yaml:/app/config.yaml:ro
19+
extra_hosts:
20+
# 在 Linux Docker Engine 场景补齐宿主机别名,保持默认 DATABASE_URL 可用。
21+
- "host.docker.internal:host-gateway"

ai/gateway/litellm/litellm.md

Lines changed: 63 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -1,279 +1,104 @@
1-
最简单理解:
1+
# LiteLLM 网关说明
22

3-
**LiteLLM = 用一套统一的 OpenAI 风格接口,去调用很多不同的大模型提供商**
4-
比如 OpenAI、Azure OpenAI、Anthropic、Bedrock、Ollama、vLLM 等。
3+
这个目录用于启动一个基于 LiteLLM Proxy 的统一模型网关,当前配置会把客户端传入的模型名透传到 NewAPI。
54

6-
它常见有两种用法
5+
相关文件职责如下
76

8-
1. **Python 里直接调用 LiteLLM SDK**
9-
2. **启动 LiteLLM Proxy,当成统一网关来用**
7+
- `newapi.yaml`:LiteLLM 代理配置,定义模型透传和 `master_key`
8+
- `compose.yaml`:LiteLLM 容器模板,定义镜像、端口、挂载和默认环境变量。
9+
- `start.ps1`:统一入口,封装常用 `docker compose` 操作。
10+
- `../.env.local`:本地私有环境变量,保存 `NEWAPI_KEY``NEWAPI_API_BASE``LITELLM_MASTER_KEY`、可选 `DATABASE_URL`
1011

11-
---
12+
## 环境变量
1213

13-
# 1)直接在 Python 里用 LiteLLM
14+
建议先在 `ai/gateway/.env.local` 中配置以下值:
1415

15-
## 安装
16-
17-
```powershell
18-
pip install litellm
19-
```
20-
21-
如果你用的是 OpenAI,先在 Windows PowerShell 里设置环境变量:
22-
23-
```powershell
24-
$env:OPENAI_API_KEY="你的_openai_key"
25-
```
26-
27-
---
28-
29-
## 最小可运行示例
30-
31-
```python
32-
from litellm import completion
33-
34-
resp = completion(
35-
model="openai/gpt-4o",
36-
messages=[
37-
{"role": "system", "content": "你是一个 helpful assistant"},
38-
{"role": "user", "content": "用一句话解释什么是 LiteLLM"}
39-
]
40-
)
41-
42-
print(resp.choices[0].message.content)
43-
```
44-
45-
---
46-
47-
## 流式输出
48-
49-
```python
50-
from litellm import completion
51-
52-
response = completion(
53-
model="openai/gpt-4o",
54-
messages=[{"role": "user", "content": "写一个简短的自我介绍"}],
55-
stream=True
56-
)
57-
58-
for chunk in response:
59-
content = chunk.choices[0].delta.content
60-
if content:
61-
print(content, end="")
62-
```
63-
64-
---
65-
66-
## 异步调用
67-
68-
```python
69-
import asyncio
70-
from litellm import acompletion
71-
72-
async def main():
73-
resp = await acompletion(
74-
model="openai/gpt-4o",
75-
messages=[{"role": "user", "content": "你好,介绍一下异步调用"}]
76-
)
77-
print(resp.choices[0].message.content)
78-
79-
asyncio.run(main())
80-
```
81-
82-
---
83-
84-
## Embedding 示例
85-
86-
```python
87-
from litellm import embedding
88-
89-
resp = embedding(
90-
model="text-embedding-3-small",
91-
input=["你好,LiteLLM"]
92-
)
93-
94-
print(len(resp.data[0]["embedding"]))
16+
```dotenv
17+
NEWAPI_KEY=sk-xxxx
18+
NEWAPI_API_BASE=https://example.com
19+
LITELLM_MASTER_KEY=sk-litellm-123456
20+
DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm
9521
```
9622

97-
---
23+
说明:
9824

99-
# 2)用 LiteLLM Proxy 当统一模型网关
25+
- `NEWAPI_KEY`:NewAPI 的访问密钥。
26+
- `NEWAPI_API_BASE`:NewAPI 的 OpenAI 兼容接口地址。
27+
- `LITELLM_MASTER_KEY`:LiteLLM Proxy 对外暴露的网关密钥。
28+
- `DATABASE_URL`:LiteLLM 的数据库连接串;如果未配置,会回退到默认的宿主机 PostgreSQL 地址。
10029

101-
这种方式很适合:
30+
## 启动方式
10231

103-
- 你有多个模型供应商
104-
- 想统一 API
105-
- 想让 Java / Node.js / Python 都走同一个入口
106-
- 想加鉴权、限流、日志、路由
107-
108-
---
109-
110-
## 安装 Proxy
32+
推荐直接使用 `start.ps1`
11133

11234
```powershell
113-
pip install "litellm[proxy]"
114-
```
115-
116-
---
117-
118-
## 写一个最小 `config.yaml`
119-
120-
```yaml
121-
model_list:
122-
- model_name: gpt-4o
123-
litellm_params:
124-
model: openai/gpt-4o
125-
api_key: "os.environ/OPENAI_API_KEY"
126-
127-
general_settings:
128-
master_key: sk-1234
35+
./ai/gateway/litellm/start.ps1
12936
```
13037

131-
这里要注意:
132-
133-
- `model_name`:你暴露给客户端看的模型名
134-
- `litellm_params.model`:真实后端模型名
135-
136-
---
137-
138-
## 启动代理
139-
140-
先设置环境变量:
38+
默认等价于:
14139

14240
```powershell
143-
$env:OPENAI_API_KEY="你的_openai_key"
41+
docker compose --env-file ai/gateway/.env.local `
42+
-f ai/gateway/litellm/compose.yaml `
43+
--project-directory ai/gateway/litellm `
44+
up -d
14445
```
14546

146-
再启动:
47+
## 常用命令
14748

14849
```powershell
149-
litellm --config config.yaml
150-
```
151-
152-
默认一般会跑在:
153-
154-
```text
155-
http://0.0.0.0:4000
50+
./ai/gateway/litellm/start.ps1
51+
./ai/gateway/litellm/start.ps1 up
52+
./ai/gateway/litellm/start.ps1 down
53+
./ai/gateway/litellm/start.ps1 restart
54+
./ai/gateway/litellm/start.ps1 logs --tail 100
55+
./ai/gateway/litellm/start.ps1 ps
56+
./ai/gateway/litellm/start.ps1 pull
15657
```
15758

158-
---
59+
这些命令分别对应:
15960

160-
## 用 curl 调代理
61+
- `up`:后台启动或重建 LiteLLM 容器。
62+
- `down`:停止并移除当前 compose 管理的资源。
63+
- `restart`:重启 LiteLLM 容器。
64+
- `logs`:默认跟随 LiteLLM 日志,可透传额外参数如 `--tail 100`
65+
- `ps`:查看当前容器状态。
66+
- `pull`:拉取最新镜像,不自动重启。
16167

162-
```powershell
163-
curl http://127.0.0.1:4000/v1/chat/completions `
164-
-H "Content-Type: application/json" `
165-
-H "Authorization: Bearer sk-1234" `
166-
-d "{\"model\":\"gpt-4o\",\"messages\":[{\"role\":\"user\",\"content\":\"你好,介绍一下你自己\"}]}"
167-
```
68+
## 直接使用原生命令
16869

169-
---
170-
171-
## 用 OpenAI SDK 调 LiteLLM Proxy
70+
如果你想直接执行 `docker compose`,建议保持和脚本一致的参数:
17271

17372
```powershell
174-
pip install openai
175-
```
176-
177-
```python
178-
from openai import OpenAI
179-
180-
client = OpenAI(
181-
api_key="sk-1234",
182-
base_url="http://127.0.0.1:4000"
183-
)
184-
185-
resp = client.chat.completions.create(
186-
model="gpt-4o",
187-
messages=[{"role": "user", "content": "LiteLLM Proxy 是什么?"}]
188-
)
189-
190-
print(resp.choices[0].message.content)
73+
docker compose --env-file ai/gateway/.env.local `
74+
-f ai/gateway/litellm/compose.yaml `
75+
--project-directory ai/gateway/litellm `
76+
logs -f litellm
19177
```
19278

193-
---
79+
这样可以确保 `DATABASE_URL` 默认值与 `.env.local` 中的变量覆盖行为一致。
19480

195-
# 3)模型名怎么写?
81+
## 访问方式
19682

197-
LiteLLM 里通常是这种格式
83+
默认端口映射如下
19884

19985
```text
200-
provider/model
86+
http://127.0.0.1:34000
20187
```
20288

203-
常见例子:
204-
205-
- `openai/gpt-4o`
206-
- `bedrock/anthropic.claude-instant-v1`
207-
- `azure/你的部署名`
208-
- `openai/某个兼容 OpenAI 协议的模型`
89+
OpenAI 兼容接口示例:
20990

210-
如果你接的是 **OpenAI 兼容接口**(比如某些自建网关、vLLM),一般还需要:
211-
212-
- `api_base`
213-
- `api_key`
214-
- `openai/` 前缀
215-
216-
例如:
217-
218-
```yaml
219-
model_list:
220-
- model_name: local-model
221-
litellm_params:
222-
model: openai/facebook/opt-125m
223-
api_base: http://127.0.0.1:8000/v1
224-
api_key: "none"
91+
```powershell
92+
curl http://127.0.0.1:34000/v1/chat/completions `
93+
-H "Content-Type: application/json" `
94+
-H "Authorization: Bearer sk-litellm-123456" `
95+
-d "{\"model\":\"gpt-4o\",\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}]}"
22596
```
22697

227-
---
228-
229-
# 4)常见问题
230-
231-
## 1. 报 401 / Unauthorized
232-
233-
通常是:
234-
235-
- 环境变量没设置成功
236-
- Proxy 的 `master_key` 不匹配
237-
- 真实供应商 key 错了
238-
239-
---
240-
241-
## 2. 报 model not found
242-
243-
通常是:
244-
245-
- 你请求的 `model` 和 `config.yaml` 里的 `model_name` 不一致
246-
- 或者 `litellm_params.model` 写错了
247-
248-
---
249-
250-
## 3. 想切换供应商怎么办?
251-
252-
通常只需要改:
253-
254-
- `model=...`
255-
- 对应的 API Key 环境变量
256-
- 如果是私有服务,再加 `api_base`
257-
258-
这正是 LiteLLM 的核心价值。
259-
260-
---
261-
262-
# 5)建议你这样入门
263-
264-
如果你是第一次用,建议按这个顺序:
265-
266-
### 方案 A:只想在 Python 里快速调用
267-
268-
直接用:
269-
270-
- `pip install litellm`
271-
- `completion(...)`
272-
273-
### 方案 B:你想做统一网关
98+
## 配置说明
27499

275-
直接用
100+
当前 `newapi.yaml` 的关键点
276101

277-
- `pip install "litellm[proxy]"`
278-
- 写 `config.yaml`
279-
- `litellm --config config.yaml`
102+
- `model_name: "*"` 允许客户端传入任意模型名。
103+
- `model: "openai/{{ model }}"` 会把客户端的模型名透传给下游 NewAPI。
104+
- `api_base``api_key``master_key` 都从容器环境变量读取,便于本地通过 `.env.local` 管理敏感值。

0 commit comments

Comments
 (0)