> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatLiteLLM 和 ChatLiteLLMRouter 集成

> 使用 LangChain Python 集成 ChatLiteLLM 和 ChatLiteLLMRouter 聊天模型。

[LiteLLM](https://github.com/BerriAI/litellm) 是一个简化调用 Anthropic、Azure、Huggingface、Replicate 等服务的库。

本笔记本涵盖了如何开始使用 LangChain + LiteLLM I/O 库。

此集成包含两个主要类：

* `ChatLiteLLM`：用于基本使用 LiteLLM 的主要 LangChain 封装器（[文档](https://docs.litellm.ai/docs/)）。
* `ChatLiteLLMRouter`：一个利用 LiteLLM 路由器的 `ChatLiteLLM` 封装器（[文档](https://docs.litellm.ai/docs/routing)）。

## 概述

### 集成详情

| 类                                                                                                                            | 包                                                                  | 可序列化 | JS 支持 |                                                 下载量                                                |                                                版本                                               |
| :--------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------- | :--: | :---: | :------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------: |
| [`ChatLiteLLM`](https://reference.langchain.com/python/langchain-litellm/chat_models/litellm/ChatLiteLLM)                    | [`langchain-litellm`](https://pypi.org/project/langchain-litellm/) |   ❌  |   ❌   | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-litellm?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-litellm?style=flat-square\&label=%20) |
| [`ChatLiteLLMRouter`](https://reference.langchain.com/python/langchain-litellm/chat_models/litellm_router/ChatLiteLLMRouter) | [`langchain-litellm`](https://pypi.org/project/langchain-litellm/) |   ❌  |   ❌   | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-litellm?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-litellm?style=flat-square\&label=%20) |

### 模型功能

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | 图像输入 | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/integrations/chat/litellm#async-and-streaming-functionality) | [原生异步](/oss/python/integrations/chat/litellm#async-and-streaming-functionality) | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :--: | :--: | :--: | :--------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |   ✅  |   ❌  |   ❌  |                                          ✅                                         |                                        ✅                                        |                         ✅                         |                            ❌                           |

### 设置

要访问 `ChatLiteLLM` 和 `ChatLiteLLMRouter` 模型，您需要安装 `langchain-litellm` 包并创建一个 OpenAI、Anthropic、Azure、Replicate、OpenRouter、Hugging Face、Together AI 或 Cohere 账户。然后，您必须获取一个 API 密钥并将其导出为环境变量。

## 凭证

您必须选择所需的 LLM 提供商并注册以获取其 API 密钥。

### 示例 - Anthropic

前往 [Claude 控制台](https://console.anthropic.com) 注册并生成 Claude API 密钥。完成后，设置 `ANTHROPIC_API_KEY` 环境变量：

### 示例 - OpenAI

前往 [platform.openai.com/api-keys](https://platform.openai.com/api-keys) 注册 OpenAI 并生成 API 密钥。完成后，设置 OPENAI\_API\_KEY 环境变量。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
## 设置环境变量
import os

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

### 安装

LangChain LiteLLM 集成包含在 `langchain-litellm` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-litellm
```

## 实例化

### ChatLiteLLM

您可以通过提供一个 [LiteLLM 支持的](https://docs.litellm.ai/docs/providers) `model` 名称来实例化 `ChatLiteLLM` 模型。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_litellm import ChatLiteLLM

llm = ChatLiteLLM(model="gpt-5.4-nano", temperature=0.1)
```

### ChatLiteLLMRouter

您也可以利用 LiteLLM 的路由功能，按照 [LiteLLM 路由文档](https://docs.litellm.ai/docs/routing) 中的指定定义您的模型列表。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_litellm import ChatLiteLLMRouter
from litellm import Router

model_list = [
    {
        "model_name": "gpt-5.4",
        "litellm_params": {
            "model": "azure/gpt-5.4",
            "api_key": "<your-api-key>",
            "api_version": "2024-10-21",
            "api_base": "https://<your-endpoint>.openai.azure.com/",
        },
    },
    {
        "model_name": "gpt-5.4",
        "litellm_params": {
            "model": "azure/gpt-5.4",
            "api_key": "<your-api-key>",
            "api_version": "2024-10-21",
            "api_base": "https://<your-endpoint>.openai.azure.com/",
        },
    },
]
litellm_router = Router(model_list=model_list)
llm = ChatLiteLLMRouter(router=litellm_router, model_name="gpt-5.4", temperature=0.1)
```

## 调用

无论您实例化的是 `ChatLiteLLM` 还是 `ChatLiteLLMRouter`，现在都可以通过 LangChain 的 API 使用 ChatModel。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
response = await llm.ainvoke(
    "将文本分类为中性、负面或正面。文本：我觉得食物还可以。情感："
)
print(response)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
content='Neutral' additional_kwargs={} response_metadata={'token_usage': Usage(completion_tokens=2, prompt_tokens=30, total_tokens=32, completion_tokens_details=CompletionTokensDetailsWrapper(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0, text_tokens=None), prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=0, cached_tokens=0, text_tokens=None, image_tokens=None)), 'model': 'gpt-3.5-turbo', 'finish_reason': 'stop', 'model_name': 'gpt-3.5-turbo'} id='run-ab6a3b21-eae8-4c27-acb2-add65a38221a-0' usage_metadata={'input_tokens': 30, 'output_tokens': 2, 'total_tokens': 32}
```

## 异步和流式传输功能

`ChatLiteLLM` 和 `ChatLiteLLMRouter` 也支持异步和流式传输功能：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
async for token in llm.astream("你好，请解释一下抗生素是如何工作的"):
    print(token.text(), end="")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
抗生素是治疗体内细菌感染的药物。它们通过靶向特定细菌并杀死它们或阻止其生长和繁殖来发挥作用。

抗生素有几种不同的作用机制。一些抗生素通过破坏细菌的细胞壁，导致它们破裂死亡。另一些则干扰细菌的蛋白质合成，阻止其生长和繁殖。一些抗生素靶向细菌的 DNA 或 RNA，破坏其复制能力。

需要注意的是，抗生素仅对细菌感染有效，对病毒感染无效。同样重要的是，要按照医疗专业人员的指示服用抗生素，并完成整个疗程，即使症状在药物用完之前有所改善。这有助于防止抗生素耐药性的产生，即细菌对抗生素的作用产生抵抗力。
```

***

## API 参考

有关所有 `ChatLiteLLM` 和 `ChatLiteLLMRouter` 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/python/langchain-litellm)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/chat/litellm.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
