跳至主要內容

開始使用

LiteLLM 是開放原始碼函式庫,提供統一介面,讓您使用 OpenAI 格式呼叫 100 多個 LLM,包括 OpenAI、Anthropic、Vertex AI、Bedrock 等提供者。

  • 透過相同的 completion() 介面呼叫任何提供者,不必為每個 API 重新學習
  • 無論使用哪個提供者或模型,都能取得一致的輸出格式
  • 透過 Router 在多個部署之間內建重試與備援邏輯
  • 自行託管 LLM Gateway(Proxy),支援虛擬金鑰、成本追蹤與管理介面

PyPI GitHub Stars


安裝

uv add litellm

若要執行完整的 Proxy Server(LLM Gateway):

uv tool install 'litellm[proxy]'

快速開始

選擇您要使用的提供者,發出第一個 LLM 請求:

from litellm import completion
import os

os.environ["OPENAI_API_KEY"] = "your-api-key"

response = completion(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hello, how are you?"}]
)
print(response.choices[0].message.content)

無論使用哪個提供者,所有回應都遵循 OpenAI Chat Completions 格式。

回應格式

非串流回應會回傳 ModelResponse 物件:

{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm doing well, thanks for asking."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 13,
"completion_tokens": 12,
"total_tokens": 25
}
}

串流回應(stream=True)會產生 ModelResponseStream 區塊:

{
"id": "chatcmpl-abc123",
"object": "chat.completion.chunk",
"created": 1677858242,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": "Hello"
},
"finish_reason": null
}
]
}

請參閱完整輸出格式參考


剛接觸 LiteLLM?

想快速開始? 前往教學,依照步驟整合 AI 程式開發工具、代理程式 SDK、Proxy 設定等功能。

想了解特定功能? 請參閱指南,查看串流、函式呼叫、提示快取與其他操作方式。

選擇使用路徑


LiteLLM Python SDK

串流

加入 stream=True,在內容生成時接收回應區塊:

from litellm import completion
import os

os.environ["OPENAI_API_KEY"] = "your-api-key"

for chunk in completion(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Write a short poem"}],
stream=True,
):
print(chunk.choices[0].delta.content or "", end="")

例外處理

LiteLLM 會將每個提供者的錯誤對應到 OpenAI 例外類型,因此既有的錯誤處理程式碼可以直接使用:

import litellm

try:
litellm.completion(
model="anthropic/claude-instant-1",
messages=[{"role": "user", "content": "Hey!"}]
)
except litellm.AuthenticationError as e:
print(f"Bad API key: {e}")
except litellm.RateLimitError as e:
print(f"Rate limited: {e}")
except litellm.APIError as e:
print(f"API error: {e}")

記錄與可觀測性

只要一行設定,就能將輸入與輸出傳送至 Langfuse、MLflow、Helicone、Lunary 等服務:

import litellm

litellm.success_callback = ["langfuse", "mlflow", "helicone"]

response = litellm.completion(
model="gpt-4o",
messages=[{"role": "user", "content": "Hi!"}]
)

請參閱所有可觀測性整合

追蹤成本與使用量

使用回呼擷取每次回應的成本:

import litellm

def track_cost(kwargs, completion_response, start_time, end_time):
print("Cost:", kwargs.get("response_cost", 0))

litellm.success_callback = [track_cost]

litellm.completion(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)

請參閱自訂回呼文件


LiteLLM Proxy Server(LLM Gateway)

Proxy 是自行託管的 OpenAI 相容 Gateway。任何能使用 OpenAI 的 Client 都能使用 Proxy,不需要修改程式碼。

步驟 1:啟動 Proxy

litellm --model huggingface/bigcode/starcoder
# Proxy running on http://0.0.0.0:4000

步驟 2:使用 OpenAI Client 呼叫

import openai

client = openai.OpenAI(api_key="anything", base_url="http://0.0.0.0:4000")

response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Write a short poem"}]
)
print(response.choices[0].message.content)

請參閱使用 Docker 的完整 Proxy 快速入門


代理程式與 MCP Gateway

LiteLLM 是整合 LLM、代理程式與 MCP 的 Gateway,不需要另外部署代理程式或 MCP Gateway。單一端點即可存取 100 多個模型、A2A 代理程式與 MCP 工具。


接下來可以探索