> ## 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.

# Hugging Face 集成

> 使用 LangChain Python 与 Hugging Face 集成。

本页涵盖了所有 LangChain 与 [Hugging Face Hub](https://huggingface.co/) 以及 [transformers](https://huggingface.co/docs/transformers/index)、[sentence transformers](https://sbert.net/) 和 [datasets](https://huggingface.co/docs/datasets/index) 等库的集成。

## 聊天模型

### ChatHuggingFace

我们可以使用 `Hugging Face` LLM 类，或者直接使用 `ChatHuggingFace` 类。

参见[使用示例](/oss/python/integrations/chat/huggingface)。

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

## 大语言模型

### HuggingFaceEndpoint

我们可以使用 `HuggingFaceEndpoint` 类，通过无服务器的 [Inference Providers](https://huggingface.co/docs/inference-providers) 或专用的 [Inference Endpoints](https://huggingface.co/inference-endpoints/dedicated) 运行开源模型。

参见[使用示例](/oss/python/integrations/llms/huggingface_endpoint)。

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

### HuggingFacePipeline

我们可以使用 `HuggingFacePipeline` 类在本地运行开源模型。

参见[使用示例](/oss/python/integrations/llms/huggingface_pipelines)。

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

## 嵌入模型

### HuggingFaceEmbeddings

我们可以使用 `HuggingFaceEmbeddings` 类在本地运行开源嵌入模型。

参见[使用示例](/oss/python/integrations/embeddings/huggingfacehub)。

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

### HuggingFaceEndpointEmbeddings

我们可以使用 `HuggingFaceEndpointEmbeddings` 类，通过专用的 [Inference Endpoint](https://huggingface.co/inference-endpoints/dedicated) 运行开源嵌入模型。

参见[使用示例](/oss/python/integrations/embeddings/huggingfacehub)。

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

### HuggingFaceInferenceAPIEmbeddings

我们可以使用 `HuggingFaceInferenceAPIEmbeddings` 类，通过 [Inference Providers](https://huggingface.co/docs/inference-providers) 运行开源嵌入模型。

参见[使用示例](/oss/python/integrations/embeddings/huggingfacehub)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
```

### HuggingFaceInstructEmbeddings

我们可以使用 `HuggingFaceInstructEmbeddings` 类在本地运行开源嵌入模型。

参见[使用示例](/oss/python/integrations/embeddings/instruct_embeddings)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
```

### HuggingFaceBgeEmbeddings

> [HuggingFace 上的 BGE 模型](https://huggingface.co/BAAI/bge-large-en-v1.5)是[最佳开源嵌入模型之一](https://huggingface.co/spaces/mteb/leaderboard)。
> BGE 模型由[北京人工智能研究院 (BAAI)](https://en.wikipedia.org/wiki/Beijing_Academy_of_Artificial_Intelligence) 创建。`BAAI` 是一家从事人工智能研发的私营非营利组织。

参见[使用示例](/oss/python/integrations/embeddings/bge_huggingface)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
```

## 文档加载器

### Hugging Face 数据集

> [Hugging Face Hub](https://huggingface.co/docs/hub/index) 拥有超过 75,000 个
> [数据集](https://huggingface.co/docs/hub/index#datasets)，涵盖 100 多种语言，
> 可用于自然语言处理、计算机视觉和音频领域的广泛任务。
> 它们被用于翻译、自动语音识别和图像分类等多种任务。

我们需要安装 `datasets` Python 包。

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install datasets
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add datasets
  ```
</CodeGroup>

参见[使用示例](/oss/python/integrations/document_loaders/hugging_face_dataset)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders.hugging_face_dataset import HuggingFaceDatasetLoader
```

### Hugging Face 模型加载器

> 从 `Hugging Face Hub` 加载模型信息，包括 README 内容。
>
> 此加载器与 `Hugging Face Models API` 交互，以获取
> 并加载模型元数据和 README 文件。
> 该 API 允许您根据特定标准（如模型标签、作者等）搜索和筛选模型。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import HuggingFaceModelLoader
```

### 图像标题生成

它使用 Hugging Face 模型生成图像标题。

我们需要安装几个 Python 包。

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install transformers pillow
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add transformers pillow
  ```
</CodeGroup>

参见[使用示例](/oss/python/integrations/document_loaders/image_captions)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import ImageCaptionLoader
```

## 工具

### Hugging Face Hub 工具

> [Hugging Face 工具](https://huggingface.co/docs/transformers/v4.29.0/en/custom_tools)
> 支持文本 I/O，并使用 `load_huggingface_tool` 函数加载。

我们需要安装几个 Python 包。

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install transformers huggingface_hub
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add transformers huggingface_hub
  ```
</CodeGroup>

参见[使用示例](/oss/python/integrations/tools/huggingface_tools)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.agent_toolkits.load_tools import load_huggingface_tool
```

### Hugging Face 文本转语音模型推理。

> 它是 `OpenAI Text-to-Speech API` 的封装。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.tools.audio import HuggingFaceTextToSpeechModelInference
```

***

<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/providers/huggingface.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
