LiteLLM
從 LiteLLM Python SDK 呼叫 cheapaimodel 的模型,或透過 LiteLLM Proxy 提供這些模型。
LiteLLM 以 openai/ 模型前綴連接 OpenAI 相容的服務。你可以直接在 Python 中使用,或把 cheapaimodel 的模型列進 LiteLLM Proxy 的設定檔。
| LiteLLM 設定 | 值 | |
|---|---|---|
| Base URL | api_base | https://api.cheapaimodel.ai/v1 |
| API Key | api_key | 在控制台的 API Key 頁面建立的 Key |
| 模型 ID | model | openai/deepseek-v4-1-flash:openai/ 加上目錄中的任一模型 ID |
設定
pip install litellmimport os
import litellm
response = litellm.completion(
model="openai/deepseek-v4-1-flash",
api_base="https://api.cheapaimodel.ai/v1",
api_key=os.environ["CHEAPAIMODEL_API_KEY"],
messages=[{"role": "user", "content": "Explain recursion in one sentence."}],
)
print(response.choices[0].message.content)
# Streaming
stream = litellm.completion(
model="openai/deepseek-v4-1-flash",
api_base="https://api.cheapaimodel.ai/v1",
api_key=os.environ["CHEAPAIMODEL_API_KEY"],
messages=[{"role": "user", "content": "Write a haiku about rivers."}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)LiteLLM 在送出前會去掉 openai/ 前綴,因此請求以 "model": "deepseek-v4-1-flash" 送往 /v1/chat/completions。
常見錯誤
LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=deepseek-v4-1-flash。 漏了openai/前綴。LiteLLM 靠前綴選擇供應商,在送出任何請求前就會失敗。請寫成openai/deepseek-v4-1-flash。litellm.NotFoundError或Invalid URL (POST /v1/chat/completions/chat/completions)。api_base漏了/v1,或寫入了端點路徑。請設為https://api.cheapaimodel.ai/v1。- 把 embeddings 指向 cheapaimodel。 cheapaimodel 目前沒有 embedding 模型,
litellm.embedding()或model_list中的 embedding 項目在這裡沒有模型可呼叫。請繼續使用其他供應商處理 embeddings。 - LiteLLM 顯示這些呼叫沒有費用。 它內建的價格表不包含 cheapaimodel 的模型:Proxy 記錄的費用為 0,
litellm.completion_cost()則會拋出This model isn't mapped yet。實際花費請看控制台的日誌頁面。若要在 Proxy 中追蹤,請在該項目的model_info設定input_cost_per_token與output_cost_per_token:取模型頁面上每百萬 token 的價格除以 1,000,000。