Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: LocalAI Reranker
---

<Info>
**`langchain-localai` is a 3rd party integration package for LocalAI. It provides a simple way to use LocalAI services in LangChain.**

The source code is available on [GitHub](https://github.com/mkhludnev/langchain-localai)

</Info>

This notebook shows how to use [LocalAI Reranker API](https://localai.io/features/reranker/) for document compression and retrieval.
Let's load the `LocalAIRerank` class. In order to use the `LocalAIRerank` class, you need to have the LocalAI service hosted somewhere and configure the reranker. See the documentation at [localai.io/basics/getting_started/index.html](https://localai.io/basics/getting_started/index.html) and [localai.io/features/reranker/index.html](https://localai.io/features/reranker/index.html).

```bash
pip install -U langchain-localai
```

```python
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",
)
Comment on lines +27 to +30
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is inconsistent. The arguments in the LocalAIRerank constructor have excessive indentation (8 spaces) which differs from standard Python conventions. Consider using 4 spaces for better consistency with the rest of the codebase.

Suggested change
openai_api_key=os.environ.get("OPENAI_API_KEY"),
model="bge-reranker-v2-m3",
openai_api_base="http://localhost:8080",
)
openai_api_key=os.environ.get("OPENAI_API_KEY"),
model="bge-reranker-v2-m3",
openai_api_base="http://localhost:8080",
)

Copilot uses AI. Check for mistakes.
reranked_docs = reranker.compress_documents(documents=[
Document(page_content="bar"), Document(page_content="baz")], query="foo")
```
2 changes: 1 addition & 1 deletion src/oss/python/integrations/text_embedding/localai.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: LocalAI
title: LocalAI Embeddings
---

<Info>
Expand Down