การสร้าง RAG chain
ถึงเวลาประกอบส่วนต่างๆ ทั้งหมดเข้าด้วยกันใน RAG workflow แล้ว! เตรียมเอกสารและนำเข้าสู่ฐานข้อมูล Chroma สำหรับการดึงข้อมูลเรียบร้อยแล้ว รวมถึงสร้าง prompt template เพื่อรวมข้อความที่ดึงมาจากบทความวิชาการและตอบคำถามไว้แล้วเช่นกัน
โดย prompt template ที่สร้างไว้ในแบบฝึกหัดก่อนหน้าพร้อมใช้งานในชื่อ prompt_template โมเดล OpenAI ถูก initialize ไว้เป็น llm และโค้ดสำหรับสร้าง retriever ขึ้นใหม่ได้ถูกรวมไว้ในสคริปต์แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain
คำแนะนำการฝึกหัด
- สร้าง LCEL chain โดยใช้ pipe operator (
|) เพื่อเชื่อมต่อretriever,prompt_templateและllmตามลำดับ - chain ควร map retriever ไปยัง
"context"และ input ของผู้ใช้ไปยัง"question" - เรียกใช้ chain ด้วยเมธอด
.invoke()พร้อมส่งคำถามที่กำหนดให้
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
vectorstore = Chroma.from_documents(
docs,
embedding=OpenAIEmbeddings(api_key='', model='text-embedding-3-small'),
persist_directory=os.getcwd()
)
retriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 3}
)
# Create a chain to link retriever, prompt_template, and llm
rag_chain = ({"context": ____, "question": ____}
| prompt_template
| llm)
# Invoke the chain
response = rag_chain.____("Which popular LLMs were considered in the paper?")
print(response.content)