本笔记本展示了如何使用 LocalAI 重排序器 API 进行文档压缩和检索。
让我们加载 LocalAIRerank 类。为了使用 LocalAIRerank 类,您需要将 LocalAI 服务托管在某处并配置重排序器。请参阅以下位置的文档 localai.io/basics/getting_started/index.html 和 localai.io/features/reranker/index.html.
pip install -U langchain-localai
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?"
)