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

# Localai reranker 集成

> 使用 LangChain 集成 LocalAI 重新排序文档转换器。

> [!NOTE]
>
> **`langchain-localai` 是 LocalAI 的第三方集成包。它提供了一种简单的方法，用于在 LangChain 中使用 LocalAI 服务。**
>
> 源代码可在 [GitHub](https://github.com/mkhludnev/langchain-localai) 上找到

本笔记本展示了如何使用 [LocalAI Reranker API](https://localai.io/features/reranker/) 进行文档压缩和检索。

让我们加载 `LocalAIRerank` 类。要使用 `LocalAIRerank` 类，您需要在某个地方托管 LocalAI 服务并配置 reranker。请参阅 [localai.io/basics/getting\_started/index.html](https://localai.io/basics/getting_started/index.html) 和 [localai.io/features/reranker/index.html](https://localai.io/features/reranker/index.html) 的文档。

```python theme={null}
pip install -U langchain-localai
```

```python theme={null}
import os
from langchain_localai import LocalAIRerank
from langchain_core.documents import Document

# Set your LocalAI/OpenAI API key as an environment variable for security.
# For example, in your shell: export OPENAI_API_KEY="your-key-here"
reranker = LocalAIRerank(
    openai_api_key=os.environ.get("OPENAI_API_KEY"),
    model="bge-reranker-v2-m3",
    openai_api_base="http://localhost:8080",
)
reranked_docs = reranker.compress_documents(
    documents=[
        Document(page_content="Green tea is rich in antioxidants and may improve brain function."),
        Document(page_content="Coffee contains caffeine and can increase alertness."),
        Document(page_content="Black tea has a strong flavor and contains various polyphenols."),
    ],
    query="What are the health benefits of green tea?"
)
```

***

> 官方原文：[src/oss/python/integrations/document\_transformers/localai\_rerank.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/document_transformers/localai_rerank.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.