建立檢索鏈
來到本章的壓軸!你將使用 LangChain 的 Expression Language(LCEL)建立一個檢索鏈。它會把你先前從 RAG 論文載入並切分後、已產生嵌入向量(embedding)的文件片段之向量儲存、提示樣板,以及一個 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?"))