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,39 @@
---
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).

```python
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",
)
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?"
)
```
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