|
1 | | -最简单理解: |
| 1 | +# LiteLLM 网关说明 |
2 | 2 |
|
3 | | -**LiteLLM = 用一套统一的 OpenAI 风格接口,去调用很多不同的大模型提供商**。 |
4 | | -比如 OpenAI、Azure OpenAI、Anthropic、Bedrock、Ollama、vLLM 等。 |
| 3 | +这个目录用于启动一个基于 LiteLLM Proxy 的统一模型网关,当前配置会把客户端传入的模型名透传到 NewAPI。 |
5 | 4 |
|
6 | | -它常见有两种用法: |
| 5 | +相关文件职责如下: |
7 | 6 |
|
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`。 |
10 | 11 |
|
11 | | ---- |
| 12 | +## 环境变量 |
12 | 13 |
|
13 | | -# 1)直接在 Python 里用 LiteLLM |
| 14 | +建议先在 `ai/gateway/.env.local` 中配置以下值: |
14 | 15 |
|
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 |
95 | 21 | ``` |
96 | 22 |
|
97 | | ---- |
| 23 | +说明: |
98 | 24 |
|
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 地址。 |
100 | 29 |
|
101 | | -这种方式很适合: |
| 30 | +## 启动方式 |
102 | 31 |
|
103 | | -- 你有多个模型供应商 |
104 | | -- 想统一 API |
105 | | -- 想让 Java / Node.js / Python 都走同一个入口 |
106 | | -- 想加鉴权、限流、日志、路由 |
107 | | - |
108 | | ---- |
109 | | - |
110 | | -## 安装 Proxy |
| 32 | +推荐直接使用 `start.ps1`: |
111 | 33 |
|
112 | 34 | ```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 |
129 | 36 | ``` |
130 | 37 |
|
131 | | -这里要注意: |
132 | | -
|
133 | | -- `model_name`:你暴露给客户端看的模型名 |
134 | | -- `litellm_params.model`:真实后端模型名 |
135 | | - |
136 | | ---- |
137 | | - |
138 | | -## 启动代理 |
139 | | - |
140 | | -先设置环境变量: |
| 38 | +默认等价于: |
141 | 39 |
|
142 | 40 | ```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 |
144 | 45 | ``` |
145 | 46 |
|
146 | | -再启动: |
| 47 | +## 常用命令 |
147 | 48 |
|
148 | 49 | ```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 |
156 | 57 | ``` |
157 | 58 |
|
158 | | ---- |
| 59 | +这些命令分别对应: |
159 | 60 |
|
160 | | -## 用 curl 调代理 |
| 61 | +- `up`:后台启动或重建 LiteLLM 容器。 |
| 62 | +- `down`:停止并移除当前 compose 管理的资源。 |
| 63 | +- `restart`:重启 LiteLLM 容器。 |
| 64 | +- `logs`:默认跟随 LiteLLM 日志,可透传额外参数如 `--tail 100`。 |
| 65 | +- `ps`:查看当前容器状态。 |
| 66 | +- `pull`:拉取最新镜像,不自动重启。 |
161 | 67 |
|
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 | +## 直接使用原生命令 |
168 | 69 |
|
169 | | ---- |
170 | | - |
171 | | -## 用 OpenAI SDK 调 LiteLLM Proxy |
| 70 | +如果你想直接执行 `docker compose`,建议保持和脚本一致的参数: |
172 | 71 |
|
173 | 72 | ```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 |
191 | 77 | ``` |
192 | 78 |
|
193 | | ---- |
| 79 | +这样可以确保 `DATABASE_URL` 默认值与 `.env.local` 中的变量覆盖行为一致。 |
194 | 80 |
|
195 | | -# 3)模型名怎么写? |
| 81 | +## 访问方式 |
196 | 82 |
|
197 | | -LiteLLM 里通常是这种格式: |
| 83 | +默认端口映射如下: |
198 | 84 |
|
199 | 85 | ```text |
200 | | -provider/model |
| 86 | +http://127.0.0.1:34000 |
201 | 87 | ``` |
202 | 88 |
|
203 | | -常见例子: |
204 | | - |
205 | | -- `openai/gpt-4o` |
206 | | -- `bedrock/anthropic.claude-instant-v1` |
207 | | -- `azure/你的部署名` |
208 | | -- `openai/某个兼容 OpenAI 协议的模型` |
| 89 | +OpenAI 兼容接口示例: |
209 | 90 |
|
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\":\"你好\"}]}" |
225 | 96 | ``` |
226 | 97 |
|
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 | +## 配置说明 |
274 | 99 |
|
275 | | -直接用: |
| 100 | +当前 `newapi.yaml` 的关键点: |
276 | 101 |
|
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