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

Chat prompt templates

เนื่องจาก chat model มีบทบาทสำคัญในแอปพลิเคชัน LLM จำนวนมาก LangChain จึงมีฟังก์ชันสำหรับสร้าง prompt template เพื่อจัดรูปแบบข้อความให้เหมาะกับ บทบาท ต่าง ๆ ในการสนทนา

คลาส ChatPromptTemplate ถูก import มาให้แล้ว และมีการกำหนด LLM ไว้เรียบร้อยแล้ว

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

การพัฒนาแอปพลิเคชัน LLM ด้วย LangChain

ดูคอร์ส

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

  • ใช้ ChatPromptTemplate.from_messages() เพื่อแปลงคู่ข้อมูล role-message ให้เป็น chat prompt template
  • กำหนดบทบาทที่เหมาะสมให้กับข้อความแต่ละรายการ เพื่อสร้างรูปแบบการสนทนา
  • สร้าง LCEL chain แล้ว invoke โดยใช้ input ที่กำหนดให้

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

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

llm = ChatOpenAI(model="gpt-4o-mini", api_key='')

# Create a chat prompt template
prompt_template = ChatPromptTemplate.____(
    [
        ("____", "You are a geography expert that returns the colors present in a country's flag."),
        ("____", "France"),
        ("____", "blue, white, red"),
        ("____", "{country}")
    ]
)

# Chain the prompt template and model, and invoke the chain
llm_chain = ____ | llm

country = "Japan"
response = llm_chain.invoke({"country": country})
print(response.content)
แก้ไขและรันโค้ด