LiteLLM 接入
LiteLLM 是一个 Python SDK 兼代理服务,可以用 OpenAI 格式调用多家大模型 API。按本文操作,约 3 分钟即可接入。
1. 创建 API Key
- 在注册页注册账号。
- 在控制台的 API 令牌页新建令牌,复制以
sk-开头的密钥。
2. 接入 LiteLLM
Python SDK:模型名加 openai/ 前缀,api_base 填下面的地址:
import litellm
response = litellm.completion(
model="openai/claude-sonnet-5-5",
api_base="https://api.cmoso.com/v1",
api_key="sk-...",
messages=[{"role": "user", "content": "Say hello"}],
)
print(response.choices[0].message.content)
LiteLLM Proxy:在 config.yaml 里添加模型:
model_list:
- model_name: claude-sonnet-5-5
litellm_params:
model: openai/claude-sonnet-5-5
api_base: https://api.cmoso.com/v1
api_key: os.environ/GATEWAY_API_KEY
然后用 litellm --config config.yaml 启动代理。
3. 选择模型并测试
向代理地址 http://localhost:4000/v1/chat/completions 发一个请求,"model" 填 "claude-sonnet-5-5"。
常用模型 ID:claude-sonnet-5-5、gpt-5.4、gemini-3.1-flash-lite、deepseek-v4-pro、qwen3.5-plus。
常见问题
- 401 Unauthorized:API Key 不对或已停用,回控制台重新复制。
- 找不到模型:检查模型 ID 拼写,区分大小写。
- 连接失败:确认 Base URL 以
/v1结尾,末尾不要加斜杠。