LiteLLM
LiteLLM 是一个 AI 网关,用于模型访问、回退和支出跟踪,支持 100 多个 LLM,全部采用 OpenAI 格式。 官方网站:litellm.ai
LiteLLM 支持三种方式集成自定义模型:
方式一:直接集成(推荐,最简单)
将提供商添加到 litellm/llms/openai_like/providers.json:
{
"luchentech": {
"base_url": "https://api.luchentech.com/inference/v1",
"api_key_env": "HPC_AI_API_KEY"
}
}
或者配置环境变量并使用:
export HPC_AI_API_KEY="your-luchentech-key"
import litellm
response = litellm.completion(
model='luchentech/minimax/minimax-m2.5',
messages=[{'role': 'user', 'content': 'test'}]
)
方式二:LiteLLM 库直接调用
pip install litellm
import litellm
import os
os.environ["HPC_AI_API_KEY"] = "your-luchentech-key"
response = litellm.completion(
model='luchentech/minimax/minimax-m2.5',
messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response.choices[0].message.content)
方式三:LiteLLM 代理
- 创建
config.yaml:
model_list:
- model_name: minimax/minimax-m2.5
litellm_params:
model: minimax/minimax-m2.5
api_base: https://api.luchentech.com/inference/v1
api_key: os.environ/HPC_AI_API_KEY
- 设置环境变量并启动代理:
export HPC_AI_API_KEY="your-luchentech-key"
pip install 'litellm[proxy]'
litellm --config config.yaml
- 通过统一 API 调用:
curl https://your-proxy:4000/v1/chat/completions \
-H "Authorization: Bearer YOUR_KEY" \
-d '{"model": "minimax/minimax-m2.5", "messages": [{"role": "user", "content": "Hello"}]}'