เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การสร้าง Retrieval Chain

มาถึงตอนไฮไลต์ของบทนี้แล้ว! จะได้สร้าง retrieval chain โดยใช้ Expression Language (LCEL) ของ LangChain ซึ่งจะนำ vector store ที่เก็บ embedded document chunks จาก RAG paper ที่โหลดไว้ก่อนหน้านี้ มารวมกับ prompt template และ 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 ของ embedded document chunks ที่สร้างไว้ก่อนหน้านี้ได้รับการโหลดให้แล้ว พร้อมกับไลบรารีและคลาสทั้งหมดที่จำเป็น

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Retrieval Augmented Generation (RAG) ด้วย LangChain

ดูคอร์ส

คำแนะนำการฝึกหัด

  • แปลง Chroma vector_store ให้เป็น retriever object เพื่อนำไปใช้ใน LCEL retrieval chain
  • สร้าง LCEL retrieval chain โดยนำ retriever, prompt_template, llm และ string output parser มารวมกัน เพื่อให้ chain สามารถตอบคำถามที่รับเข้ามาได้
  • เรียกใช้ chain กับคำถามที่กำหนดให้

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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?"))
แก้ไขและรันโค้ด