import os
from langchain_localai import LocalAIRerank
from langchain_core.documents import Document
# 将您的 LocalAI/OpenAI API 密钥设置为环境变量以确保安全。
# 例如,在 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?"
)