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

# Bge 在 Hugging Face 上的集成

> 使用 LangChain Python 与 Hugging Face 上的 Bge 嵌入模型进行集成。

> [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` 是一家从事人工智能研发的私营非营利组织。

本笔记本展示了如何通过 `Hugging Face` 使用 `BGE Embeddings`。

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

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

model_name = "BAAI/bge-small-en"
model_kwargs = {"device": "cpu"}
encode_kwargs = {"normalize_embeddings": True}
hf = HuggingFaceBgeEmbeddings(
    model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs
)
```

请注意，对于 `model_name="BAAI/bge-m3"`，您需要传递 `query_instruction=""`，请参阅 [BGE M3 常见问题解答](https://huggingface.co/BAAI/bge-m3#faq)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
embedding = hf.embed_query("hi this is harrison")
len(embedding)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
384
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
```

***

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