> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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` 类，通过无服务器 [推理提供商](https://huggingface.co/docs/inference-providers) 或专用 [推理端点](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` 类通过专用 [推理端点](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` 类通过 [推理提供商](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 Tools](https://huggingface.co/docs/transformers/v4.29.0/en/custom_tools) 支持文本输入/输出，并使用 `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 文本转语音 API` 的封装。

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\providers\huggingface.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
