在本文中,我们将分析查询转换,以及如何使用路由器根据输入提示选择适当的转换。
查询转换背后的想法是,检索器有可能从数据库中检索到与用户初始提示不相关的块。在这些情况下,我们可以在检索并将其提供给语言模型之前,修改查询以增加其与源的相关性。
我们将从一个简单的RAG应用程序开始,首先加载关于尼古拉斯·凯奇、《最好的时代》(尼古拉斯·凯吉首次登台表演的电视试播)和莱昂纳多·迪卡普里奥的三个维基百科页面数据。
然后,我们将把文档分割成256个字符的块,没有重叠。这些块将嵌入并索引在Vector Store中,默认情况下,Vector Store将所有内容存储在内存中。
WikipediaReader = download_loader("WikipediaReader")
loader = WikipediaReader()
pages = ['Nicolas_Cage', 'The_Best_of_Times_(1981_film)', 'Leonardo DiCaprio']
documents = loader.load_data(pages=pages, auto_suggest=False, redirect = False)
llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
gpt3 = OpenAI(temperature=0, model="text-davinci-003")
embed_model = OpenAIEmbedding(model= OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002)
service_context_gpt3 = ServiceContext.from_defaults(llm=gpt3, chunk_size = 256, chunk_overlap=0, embed_model=embed_model)
index = VectorStoreIndex.from_documents(documents, service_context=service_context_gpt3)
retriever = index.as_retriever(similarity_top_k=3)
现在,我们必须确保模型只根据上下文进行回答,而不依赖于其训练数据,即使它以前可能已经学会了答案。
# The response from original prompt
from llama_index.prompts import PromptTemplate
template = (
"We have provided context information below. \n"
"---------------------\n"
"{context_str}"
"\n---------------------\n"
"Given this information, please answer the question: {query_str}\n"
"Don't give an answer unless it is supported by the context above.\n"
)
qa_template = PromptTemplate(template)
我们将使用两个更复杂的查询来测试刚刚创建的RAG应用程序。让我们来看看第一个。
问题1——“Who directed the pilot that marked the acting debut of Nicolas Cage?”
该查询的问题需要参考多条信息:尼古拉斯·凯奇的表演处女作和这部电影的导演。提示中只提到了尼古拉斯·凯奇,而导演的名字却没有被涉及。由于该模型不知道凯奇首次亮相的电视试播节目《最好的时代》的名字,因此无法从我们索引的文件中检索到相关细节。
question = "Who directed the pilot that marked the acting debut of Nicolas Cage?"