OA0 = Omni AI 0
OA0 是一个探索 AI 的论坛
现在注册
已注册用户请  登录
OA0  ›  代码  ›  LiteLLM — 统一 LLM API 网关

LiteLLM — 统一 LLM API 网关

 
  release ·  2026-02-28 00:40:58 · 3 次点击  · 0 条评论  

🚅 LiteLLM

<p align="center">
    <p align="center">以 OpenAI 格式调用 100+ 种大语言模型。[Bedrock, Azure, OpenAI, VertexAI, Anthropic, Groq 等]
    </p>
    <p align="center">
    <a href="https://render.com/deploy?repo=https://github.com/BerriAI/litellm" target="_blank" rel="nofollow"><img src="https://render.com/images/deploy-to-render-button.svg" alt="Deploy to Render"></a>
    <a href="https://railway.app/template/HLP0Ub?referralCode=jch2ME">
      <img src="https://railway.app/button.svg" alt="Deploy on Railway">
    </a>
    </p>
</p>

LiteLLM 代理服务器 (AI 网关) | 托管代理 | 企业版

PyPI Version Y Combinator W23 Whatsapp Discord Slack

Group 7154 (1)

LiteLLM 用途

大语言模型 (LLMs) - 调用 100+ 种 LLM (Python SDK + AI 网关) [**所有支持的端点**](https://docs.litellm.ai/docs/supported_endpoints) - `/chat/completions`, `/responses`, `/embeddings`, `/images`, `/audio`, `/batches`, `/rerank`, `/a2a`, `/messages` 等。 ### Python SDK
pip install litellm
from litellm import completion
import os

os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-key"

# OpenAI
response = completion(model="openai/gpt-4o", messages=[{"role": "user", "content": "Hello!"}])

# Anthropic  
response = completion(model="anthropic/claude-sonnet-4-20250514", messages=[{"role": "user", "content": "Hello!"}])
### AI 网关 (代理服务器) [**入门指南 - 端到端教程**](https://docs.litellm.ai/docs/proxy/docker_quick_start) - 设置虚拟密钥,发起第一个请求
pip install 'litellm[proxy]'
litellm --model gpt-4o
import openai

client = openai.OpenAI(api_key="anything", base_url="http://0.0.0.0:4000")
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
[**文档:LLM 提供商**](https://docs.litellm.ai/docs/providers)
智能体 (Agents) - 调用 A2A 智能体 (Python SDK + AI 网关) [**支持的提供商**](https://docs.litellm.ai/docs/a2a#add-a2a-agents) - LangGraph, Vertex AI Agent Engine, Azure AI Foundry, Bedrock AgentCore, Pydantic AI ### Python SDK - A2A 协议
from litellm.a2a_protocol import A2AClient
from a2a.types import SendMessageRequest, MessageSendParams
from uuid import uuid4

client = A2AClient(base_url="http://localhost:10001")

request = SendMessageRequest(
    id=str(uuid4()),
    params=MessageSendParams(
        message={
            "role": "user",
            "parts": [{"kind": "text", "text": "Hello!"}],
            "messageId": uuid4().hex,
        }
    )
)
response = await client.send_message(request)
### AI 网关 (代理服务器) **步骤 1.** [将您的智能体添加到 AI 网关](https://docs.litellm.ai/docs/a2a#adding-your-agent) **步骤 2.** 通过 A2A SDK 调用智能体
from a2a.client import A2ACardResolver, A2AClient
from a2a.types import MessageSendParams, SendMessageRequest
from uuid import uuid4
import httpx

base_url = "http://localhost:4000/a2a/my-agent"  # LiteLLM 代理 + 智能体名称
headers = {"Authorization": "Bearer sk-1234"}    # LiteLLM 虚拟密钥

async with httpx.AsyncClient(headers=headers) as httpx_client:
    resolver = A2ACardResolver(httpx_client=httpx_client, base_url=base_url)
    agent_card = await resolver.get_agent_card()
    client = A2AClient(httpx_client=httpx_client, agent_card=agent_card)

    request = SendMessageRequest(
        id=str(uuid4()),
        params=MessageSendParams(
            message={
                "role": "user",
                "parts": [{"kind": "text", "text": "Hello!"}],
                "messageId": uuid4().hex,
            }
        )
    )
    response = await client.send_message(request)
[**文档:A2A 智能体网关**](https://docs.litellm.ai/docs/a2a)
MCP 工具 - 将 MCP 服务器连接到任何 LLM (Python SDK + AI 网关) ### Python SDK - MCP 桥接
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from litellm import experimental_mcp_client
import litellm

server_params = StdioServerParameters(command="python", args=["mcp_server.py"])

async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        await session.initialize()

        # 以 OpenAI 格式加载 MCP 工具
        tools = await experimental_mcp_client.load_mcp_tools(session=session, format="openai")

        # 与任何 LiteLLM 模型一起使用
        response = await litellm.acompletion(
            model="gpt-4o",
            messages=[{"role": "user", "content": "What's 3 + 5?"}],
            tools=tools
        )
### AI 网关 - MCP 网关 **步骤 1.** [将您的 MCP 服务器添加到 AI 网关](https://docs.litellm.ai/docs/mcp#adding-your-mcp) **步骤 2.** 通过 `/chat/completions` 调用 MCP 工具
curl -X POST 'http://0.0.0.0:4000/v1/chat/completions' \
  -H 'Authorization: Bearer sk-1234' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Summarize the latest open PR"}],
    "tools": [{
      "type": "mcp",
      "server_url": "litellm_proxy/mcp/github",
      "server_label": "github_mcp",
      "require_approval": "never"
    }]
  }'
### 与 Cursor IDE 一起使用
{
  "mcpServers": {
    "LiteLLM": {
      "url": "http://localhost:4000/mcp/",
      "headers": {
        "x-litellm-api-key": "Bearer sk-1234"
      }
    }
  }
}
[**文档:MCP 网关**](https://docs.litellm.ai/docs/mcp)

如何使用 LiteLLM

您可以通过代理服务器或 Python SDK 使用 LiteLLM。两者都为您提供了访问多个 LLM(100+ 种)的统一接口。请选择最适合您需求的选项:

LiteLLM AI 网关 LiteLLM Python SDK
使用场景 访问多个 LLM 的集中式服务 (LLM 网关) 直接在您的 Python 代码中使用 LiteLLM
适用人群 生成式 AI 赋能 / ML 平台团队 构建 LLM 项目的开发者
核心功能 具有身份验证和授权的集中式 API 网关,按项目/用户的多租户成本跟踪和支出管理,按项目定制(日志记录、防护栏、缓存),用于安全访问控制的虚拟密钥,用于监控和管理的管理仪表盘 UI 在您的代码库中直接集成 Python 库,跨多个部署(例如 Azure/OpenAI)具有重试/回退逻辑的路由器 - 路由器,应用级负载均衡和成本跟踪,兼容 OpenAI 的错误异常处理,可观测性回调(Lunary、MLflow、Langfuse 等)

LiteLLM 性能:在 1k RPS 下 P95 延迟为 8ms(查看基准测试此处

跳转至 LiteLLM 代理 (LLM 网关) 文档

跳转至支持的 LLM 提供商

稳定版本: 使用带有 -stable 标签的 Docker 镜像。这些镜像在发布前经过了 12 小时的负载测试。有关发布周期的更多信息请见此处

支持更多提供商。如果缺少某个提供商或 LLM 平台,请提交功能请求

开源采用者

Stripe Google ADK Greptile OpenHands

Netflix

OpenAI Agents SDK

支持的提供商 (网站支持模型 | 文档)

提供商 /chat/completions /messages /responses /embeddings /image/generations /audio/transcriptions /audio/speech /moderations /batches /rerank
Abliteration (abliteration)
AI/ML API (aiml)
AI21 (ai21)
AI21 Chat (ai21_chat)
Aleph Alpha
Amazon Nova
Anthropic (anthropic)
Anthropic Text (anthropic_text)
Anyscale
AssemblyAI (assemblyai)
Auto Router (auto_router)
AWS - Bedrock (bedrock)
AWS - Sagemaker (sagemaker)
Azure (azure)
Azure AI (azure_ai)
[Azure Text (azure_text)](https://docs.litellm.ai/docs
3 次点击  ∙  0 人收藏  
登录后收藏  
目前尚无回复
0 条回复
About   ·   Help   ·    
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
Developed with Cursor