Skip to content

Commit 6656c93

Browse files
committed
feat(litellm): 添加 NewAPI 网关配置和启动脚本
- 新增 .env.example 文件,包含 NEWAPI_KEY、NEWAPI_API_BASE 和 LITELLM_MASTER_KEY 环境变量示例配置 - 创建 litellm.md 文档,详细介绍 LiteLLM 的使用方法,包括 Python SDK 直接调用和 Proxy 网关两种模式的完整教程 - 添加 newapi.yaml 配置文件,实现万能透传模型配置,支持客户端传入任 何模型名并转发到 NewAPI 服务 - 实现 start.ps1 PowerShell 启动脚本,使用 Docker 容器化部署 LiteLLM 代理服务,自动
1 parent 5ab6927 commit 6656c93

4 files changed

Lines changed: 323 additions & 0 deletions

File tree

ai/gateway/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NEWAPI_KEY=sk-xxxx
2+
NEWAPI_API_BASE=https://example.com
3+
LITELLM_MASTER_KEY=sk-litellm-123456

ai/gateway/litellm/litellm.md

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
最简单理解:
2+
3+
**LiteLLM = 用一套统一的 OpenAI 风格接口,去调用很多不同的大模型提供商**
4+
比如 OpenAI、Azure OpenAI、Anthropic、Bedrock、Ollama、vLLM 等。
5+
6+
它常见有两种用法:
7+
8+
1. **Python 里直接调用 LiteLLM SDK**
9+
2. **启动 LiteLLM Proxy,当成统一网关来用**
10+
11+
---
12+
13+
# 1)直接在 Python 里用 LiteLLM
14+
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"]))
95+
```
96+
97+
---
98+
99+
# 2)用 LiteLLM Proxy 当统一模型网关
100+
101+
这种方式很适合:
102+
103+
- 你有多个模型供应商
104+
- 想统一 API
105+
- 想让 Java / Node.js / Python 都走同一个入口
106+
- 想加鉴权、限流、日志、路由
107+
108+
---
109+
110+
## 安装 Proxy
111+
112+
```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
129+
```
130+
131+
这里要注意:
132+
133+
- `model_name`:你暴露给客户端看的模型名
134+
- `litellm_params.model`:真实后端模型名
135+
136+
---
137+
138+
## 启动代理
139+
140+
先设置环境变量:
141+
142+
```powershell
143+
$env:OPENAI_API_KEY="你的_openai_key"
144+
```
145+
146+
再启动:
147+
148+
```powershell
149+
litellm --config config.yaml
150+
```
151+
152+
默认一般会跑在:
153+
154+
```text
155+
http://0.0.0.0:4000
156+
```
157+
158+
---
159+
160+
## 用 curl 调代理
161+
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+
```
168+
169+
---
170+
171+
## 用 OpenAI SDK 调 LiteLLM Proxy
172+
173+
```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)
191+
```
192+
193+
---
194+
195+
# 3)模型名怎么写?
196+
197+
LiteLLM 里通常是这种格式:
198+
199+
```text
200+
provider/model
201+
```
202+
203+
常见例子:
204+
205+
- `openai/gpt-4o`
206+
- `bedrock/anthropic.claude-instant-v1`
207+
- `azure/你的部署名`
208+
- `openai/某个兼容 OpenAI 协议的模型`
209+
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"
225+
```
226+
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:你想做统一网关
274+
275+
直接用:
276+
277+
- `pip install "litellm[proxy]"`
278+
- 写 `config.yaml`
279+
- `litellm --config config.yaml`

ai/gateway/litellm/newapi.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
model_list:
2+
# 万能透传:客户端传什么模型名,就转发什么到 NewAPI
3+
- model_name: "*"
4+
litellm_params:
5+
model: "openai/{{ model }}" # 透传模型名
6+
api_base: "os.environ/NEWAPI_API_BASE" # 从环境变量读 NewAPI 地址
7+
api_key: "os.environ/NEWAPI_KEY" # 从环境变量读密钥
8+
# database_url: "sqlite:///./litellm.db"
9+
# 可选:LiteLLM 网关鉴权(防止滥用)
10+
general_settings:
11+
master_key: "os.environ/LITELLM_MASTER_KEY"
12+
# disable_frontend: true # 关闭前端页面(纯API模式)
13+
proxy_batch_write_at: 60 # 每60秒写入一次数据库
14+
database_connection_pool_limit: 10 # 每个 Worker 进程的数据库连接池上限
15+
# database_url: "postgresql://admin:12345678@localhost:5432/litellm"

ai/gateway/litellm/start.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$containerPort = '4000'
3+
4+
# 使用参数数组调用 docker,避免反引号续行导致参数被 PowerShell 错误拆分。
5+
# LiteLLM 官方镜像会通过环境变量 PORT 拼接 uvicorn 启动命令,未显式传入时会报 "--port requires an argument"。
6+
$dockerArgs = @(
7+
'run'
8+
'-d'
9+
'--name'
10+
'litellm'
11+
'-p'
12+
"34000:$containerPort"
13+
'-e'
14+
"PORT=$containerPort"
15+
'-e'
16+
"DATABASE_URL=postgresql://postgres:12345678@host.docker.internal:5432/litellm"
17+
'-v'
18+
"$scriptDir/newapi.yaml:/app/config.yaml"
19+
'--env-file'
20+
"$scriptDir/../.env.local"
21+
'--restart'
22+
'unless-stopped'
23+
'docker.litellm.ai/berriai/litellm:main-latest'
24+
)
25+
26+
docker @dockerArgs

0 commit comments

Comments
 (0)