LiteLLM 是一個開源的 Python SDK 和代理伺服器(LLM 網關),能夠以 OpenAI 格式調用 100 多個大型語言模型 API,包括 Bedrock、Azure、OpenAI、VertexAI、Cohere、Anthropic、Sagemaker、HuggingFace、Replicate、Groq 等主流提供商。
GitHub 地址:https://github.com/BerriAI/litellm
['choices'][0]['message']['content']
獲取from litellm import completion
import os
os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-key"
messages = [{"content": "Hello, how are you?", "role": "user"}]
# OpenAI
response = completion(model="openai/gpt-4o", messages=messages)
# Anthropic
response = completion(model="anthropic/claude-3-sonnet-20240229", messages=messages)
from litellm import acompletion
import asyncio
async def test_get_response():
user_message = "Hello, how are you?"
messages = [{"content": user_message, "role": "user"}]
response = await acompletion(model="openai/gpt-4o", messages=messages)
return response
response = asyncio.run(test_get_response())
from litellm import completion
response = completion(model="openai/gpt-4o", messages=messages, stream=True)
for part in response:
print(part.choices[0].delta.content or "")
pip install 'litellm[proxy]'
litellm --model huggingface/bigcode/starcoder
# INFO: Proxy running on http://0.0.0.0:4000
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": "this is a test request, write a short poem"
}]
)
LiteLLM 支持超過 30 個主要的 LLM 提供商,包括:
LiteLLM 內置支持多種監控和日誌平台:
import litellm
import os
os.environ["LUNARY_PUBLIC_KEY"] = "your-lunary-public-key"
os.environ["HELICONE_API_KEY"] = "your-helicone-auth-key"
os.environ["LANGFUSE_PUBLIC_KEY"] = "your-langfuse-public-key"
litellm.success_callback = [
"lunary", "mlflow", "langfuse",
"athina", "helicone"
]
response = completion(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hi 👋"}]
)
支持的監控平台:
curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data-raw '{
"models": ["gpt-3.5-turbo", "gpt-4", "claude-2"],
"duration": "20m",
"metadata": {
"user": "user@company.com",
"team": "core-infra"
}
}'
git clone https://github.com/BerriAI/litellm
cd litellm
echo 'LITELLM_MASTER_KEY="sk-1234"' > .env
echo 'LITELLM_SALT_KEY="your-salt-key"' > .env
docker-compose up
/ui
獲得可視化管理界面openai>=1.0.0
(v1.0.0+ 必需)pydantic>=2.0.0
(v1.40.14+ 必需)-stable
標籤的 Docker 鏡像pip install litellm
pip install 'litellm[proxy]'
git clone https://github.com/BerriAI/litellm
cd litellm
python -m venv .venv
source .venv/bin/activate
pip install -e ".[all]"
uvicorn litellm.proxy.proxy_server:app --host localhost --port 4000 --reload
LiteLLM 已被 Rocket Money、Samsara、Lemonade 和 Adobe 等知名公司採用。它通過提供統一的 API 接口、強大的路由功能和企業級管理特性,顯著簡化了多 LLM 環境的管理複雜性,是現代 AI 應用開發的理想選擇。