Prompt Template และการเชื่อมโซ่
ในแบบฝึกหัดนี้ จะได้เริ่มใช้งานสองส่วนประกอบหลักของ LangChain ได้แก่ prompt template และ chain!
คลาสที่จำเป็นสำหรับแบบฝึกหัดนี้ รวมถึง ChatOpenAI ได้ถูกโหลดไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain
คำแนะนำการฝึกหัด
- แปลงข้อความ
templateที่กำหนดให้เป็น prompt template แบบมาตรฐาน (ไม่ใช่แบบ chat) - สร้าง chain เพื่อส่ง prompt template เข้าสู่ LLM
- เรียกใช้งาน chain กับตัวแปร
questionที่กำหนดให้
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create a prompt template from the template string
template = "You are an artificial intelligence assistant, answer the question. {question}"
prompt = PromptTemplate.____(
template=____
)
llm = ChatOpenAI(model="gpt-4o-mini", api_key='')
# Create a chain to integrate the prompt template and LLM
llm_chain = ____ | ____
# Invoke the chain on the question
question = "How does LangChain make LLM application development easier?"
print(llm_chain.invoke({"question": ____}))