构建检索链
来到本章的收官环节!您将使用 LangChain 的表达式语言(LCEL)创建一个检索链。它会把您先前加载的 RAG 论文所生成的嵌入文档分块的向量存储、提示模板和一个 LLM 组合起来,这样您就可以开始与文档对话。
下面是您在上一个练习中创建并可直接使用的 prompt_template 提醒:
Use the only the context provided to answer the following question. If you don't know the answer, reply that you are unsure.
Context: {context}
Question: {question}
您之前创建的嵌入文档分块的 vector_store 也已为您加载好,同时还包含所有必需的库和类。
本练习是课程的一部分
使用 LangChain 的 Retrieval Augmented Generation (RAG)
练习说明
- 将 Chroma 的
vector_store转换为可在 LCEL 检索链中使用的检索器对象。 - 创建 LCEL 检索链,组合
retriever、prompt_template、llm与字符串输出解析器,使其能够回答输入问题。 - 在给定的问题上调用该链。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Convert the vector store into a retriever
retriever = vector_store.____(search_type="similarity", search_kwargs=____)
# Create the LCEL retrieval chain
chain = (
{"____": ____, "question": ____}
| ____
| ____
| ____
)
# Invoke the chain
print(chain.____("Who are the authors?"))