LiteLLM 接入 APIMaster.ai
通过 LiteLLM Python SDK 或 LiteLLM Proxy 接入 APIMaster.ai 的 OpenAI 兼容接口。
LiteLLM 是统一的 LLM 调用库,也提供本地 Proxy 服务。APIMaster.ai 暴露 OpenAI 兼容 接口,在 LiteLLM 里用 openai/<model id> 格式并指定 api_base 即可接入。
开始前请 获取 API Key。模型名称须从 模型广场 复制 model id(如
claude-sonnet-4-6、gpt-5.4)。
APIMaster 接口地址:
https://apimaster.ai/v1
LiteLLM 接入 OpenAI-compatible 服务时,模型名需带前缀:
openai/<APIMaster 支持的 model id>
例如 openai/claude-sonnet-4-6。
前置条件
第 1 步:安装 LiteLLM
仅使用 Python SDK:
pip install litellm
需要启动 LiteLLM Proxy:
pip install "litellm[proxy]"
Windows PowerShell:
python -m pip install litellm
python -m pip install "litellm[proxy]"
第 2 步:Python SDK 最小测试
新建 minimal_apimaster_test.py:
import litellm
API_KEY = "替换为你的 APIMaster key"
response = litellm.completion(
model="openai/claude-sonnet-4-6",
api_base="https://apimaster.ai/v1",
api_key=API_KEY,
messages=[
{"role": "user", "content": "Say hi in one short sentence."},
],
max_tokens=64,
)
print(response.choices[0].message.content)
也可 下载示例脚本 后填入 Key。
运行:
python minimal_apimaster_test.py
Windows:
python .\minimal_apimaster_test.py
配置正确时会打印模型回复。
第 3 步:LiteLLM Proxy 配置
新建 config.apimaster.yaml:
model_list:
- model_name: apimaster-claude-sonnet
litellm_params:
model: openai/claude-sonnet-4-6
api_base: https://apimaster.ai/v1
api_key: os.environ/APIMASTER_API_KEY
general_settings:
master_key: sk-local-test
也可 下载示例配置 后按需修改。
两层 Key 说明:
| Key | 用途 |
|---|---|
APIMASTER_API_KEY |
APIMaster 真实 API Key,LiteLLM 请求 APIMaster 时使用 |
master_key |
LiteLLM Proxy 自己的访问 Key,客户端请求本地 Proxy 时使用 |
第 4 步:启动 LiteLLM Proxy
设置 APIMaster Key 并启动:
export APIMASTER_API_KEY="你的 APIMaster key"
litellm --config config.apimaster.yaml --port 4000
Windows PowerShell:
$env:APIMASTER_API_KEY="你的 APIMaster key"
litellm --config config.apimaster.yaml --port 4000
若 Windows 提示找不到 litellm 命令,可使用用户级安装路径(Python 版本按实际调整):
& "$env:APPDATA\Python\Python313\Scripts\litellm.exe" --config config.apimaster.yaml --port 4000
第 5 步:测试本地 LiteLLM Proxy
curl http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer sk-local-test" \
-H "Content-Type: application/json" \
-d '{
"model": "apimaster-claude-sonnet",
"messages": [{"role": "user", "content": "Say hi in one short sentence."}],
"max_tokens": 64
}'
Windows PowerShell:
$body = @{
model = "apimaster-claude-sonnet"
messages = @(@{ role = "user"; content = "Say hi in one short sentence." })
max_tokens = 64
} | ConvertTo-Json -Depth 8 -Compress
curl.exe http://localhost:4000/v1/chat/completions `
-H "Authorization: Bearer sk-local-test" `
-H "Content-Type: application/json" `
--data-raw $body
成功时返回 OpenAI Chat Completions 格式 JSON。
第 6 步:多模型配置示例
同一 LiteLLM 配置可挂载多个 APIMaster 模型:
model_list:
- model_name: apimaster-claude-sonnet
litellm_params:
model: openai/claude-sonnet-4-6
api_base: https://apimaster.ai/v1
api_key: os.environ/APIMASTER_API_KEY
- model_name: apimaster-gpt
litellm_params:
model: openai/gpt-5.4
api_base: https://apimaster.ai/v1
api_key: os.environ/APIMASTER_API_KEY
general_settings:
master_key: sk-local-test
客户端调用时使用 model_name(如 apimaster-gpt),而非上游 openai/gpt-5.4。
常见问题
401 Invalid token
LiteLLM 已请求到 APIMaster,但 Key 不正确或无权限。检查环境变量:
echo $APIMASTER_API_KEY
直连 APIMaster 验证 Key:
curl https://apimaster.ai/v1/models \
-H "Authorization: Bearer 你的 APIMaster key"
找不到 litellm 命令
Windows 常见原因是 Python Scripts 目录未加入 PATH。可改用完整路径:
& "$env:APPDATA\Python\Python313\Scripts\litellm.exe" --help
macOS / Linux 可尝试:
python -m litellm --config config.apimaster.yaml --port 4000
APIMaster 返回模型不存在
确认 model 是 模型广场 上的 model id,且 LiteLLM 配置带 openai/ 前缀。
正确:
model: openai/claude-sonnet-4-6
错误:
model: claude-sonnet-4-6
api_base 配置错误
api_base 须包含 /v1:
api_base: https://apimaster.ai/v1
推荐验证顺序
- 先用 Python SDK 最小脚本 验证 APIMaster Key 与模型可用。
- 再启动 LiteLLM Proxy。
- 最后用 OpenAI-compatible 格式请求本地 Proxy。
这样可以快速区分问题出在 APIMaster Key、模型名,还是 LiteLLM Proxy 配置。
核对清单
- 已安装
litellm(Proxy 需litellm[proxy]) - SDK 中
model为openai/<model id>,api_base=https://apimaster.ai/v1 - Proxy 中
APIMASTER_API_KEY与master_key已区分配置 -
minimal_apimaster_test.py或本地 Proxy 测试能正常返回