LiteLLM + APIMaster.ai
Kết nối API tương thích OpenAI của APIMaster.ai qua LiteLLM Python SDK hoặc LiteLLM Proxy.
LiteLLM cung cấp SDK LLM thống nhất và máy chủ Proxy cục bộ. APIMaster.ai cung cấp API tương thích OpenAI — sử dụng định dạng openai/<model id> và thiết lập api_base.
Lấy Khóa API trước. Sao chép chính xác model id từ marketplace.
URL gốc:
https://apimaster.ai/v1
Định dạng tên model LiteLLM:
openai/<APIMaster model id>
Ví dụ: openai/claude-sonnet-4-6.
Điều kiện tiên quyết
- Python 3.10+ (khuyến nghị 3.11+).
- Khóa API APIMaster từ bảng điều khiển.
- Model id mục tiêu từ marketplace.
Bước 1 — Cài đặt LiteLLM
Chỉ SDK:
pip install litellm
Có hỗ trợ Proxy:
pip install "litellm[proxy]"
Bước 2 — Kiểm tra Python SDK tối thiểu
Tạo minimal_apimaster_test.py:
import litellm
API_KEY = "khóa APIMaster của bạn"
response = litellm.completion(
model="openai/claude-sonnet-4-6",
api_base="https://apimaster.ai/v1",
api_key=API_KEY,
messages=[
{"role": "user", "content": "Nói xin chào trong một câu ngắn."},
],
max_tokens=64,
)
print(response.choices[0].message.content)
Hoặc tải xuống tập lệnh mẫu.
python minimal_apimaster_test.py
Bước 3 — Cấu hình LiteLLM Proxy
Tạo 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
Hoặc tải xuống cấu hình mẫu.
| Khóa | Mục đích |
|---|---|
APIMASTER_API_KEY |
Khóa APIMaster thực — LiteLLM sử dụng khóa này cho upstream |
master_key |
Khóa truy cập Proxy cục bộ — máy khách sử dụng khóa này |
Bước 4 — Khởi động LiteLLM Proxy
export APIMASTER_API_KEY="khóa APIMaster của bạn"
litellm --config config.apimaster.yaml --port 4000
Nếu litellm không có trong PATH:
python -m litellm --config config.apimaster.yaml --port 4000
Bước 5 — Kiểm tra Proxy cục bộ
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": "Nói xin chào trong một câu ngắn."}],
"max_tokens": 64
}'
Bước 6 — Nhiều model
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
Máy khách gọi apimaster-gpt, không phải openai/gpt-5.4.
Xử lý sự cố
401 Token không hợp lệ
Khóa sai hoặc đã bị vô hiệu hóa. Xác minh:
curl https://apimaster.ai/v1/models \
-H "Authorization: Bearer KHÓA_CỦA_BẠN"
Không tìm thấy model
Sử dụng model id từ marketplace với tiền tố openai/:
model: openai/claude-sonnet-4-6
Sai api_base
Phải bao gồm /v1:
api_base: https://apimaster.ai/v1
Thứ tự xác minh khuyến nghị
- Tập lệnh Python SDK tối thiểu — xác thực khóa APIMaster và model.
- Khởi động LiteLLM Proxy.
- Gọi Proxy cục bộ với JSON tương thích OpenAI.
Danh sách kiểm tra
- Đã cài đặt
litellm(litellm[proxy]cho Proxy) - SDK:
openai/<model id>+api_base=https://apimaster.ai/v1 - Proxy:
APIMASTER_API_KEYvàmaster_keyđược cấu hình riêng biệt - Kiểm tra SDK hoặc Proxy trả về phản hồi