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

กำหนดค่า output สำหรับหลายเครื่องมือ

กราฟของแชทบอทพร้อมใช้งานแล้ว ตอนนี้ทดสอบได้เลยว่าแชทบอทตอบสนองต่อคำถามแต่ละประเภทที่ต้องใช้เครื่องมือต่างกันอย่างไร เพื่อจัดการข้อความของแชทบอท โมดูลต่อไปนี้ถูก import ไว้แล้ว และพารามิเตอร์ config ของแชทบอทถูกตั้งค่าสำหรับ session นี้แล้ว

from langchain_core.messages import AIMessage, HumanMessage

config = {"configurable": {"thread_id": "1"}}

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

การออกแบบระบบ Agentic ด้วย LangChain

ดูคอร์ส

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

  • สร้าง dictionary ข้อความ inputs โดยใช้ query ของผู้ใช้เป็น content สำหรับ HumanMessage
  • ส่ง msg และ metadata แบบ stream จากแชทบอท app โดยวนซ้ำผลลัพธ์ด้วยเมธอด .stream() ที่รับ inputs และ config
  • ตรวจสอบว่าแต่ละ msg มี content และไม่ใช่ HumanMessage จากนั้นพิมพ์ content โดยตั้งค่า flush เป็น True เพื่อให้แสดงผลทันที
  • ทดสอบแชทบอทด้วย multi_tool_output() โดยใช้คำถามที่ต้องการเครื่องมือต่างกัน

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

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

# Create input message with the user's query
def multi_tool_output(____):
    inputs = {"messages": [____(____=____)]}
    
    # Stream messages and metadata from the chatbot application
    for ____, ____ in app.____(____, ____, stream_mode="messages"):
        
        # Check if the message has content and is not from a human
        if ____.____ and not isinstance(____, ____):
            print(____.____, end="", flush=____)    
    print("\n")

# Call the chatbot with different tools
____("Is `may a moody baby doom a yam` a palindrome?")
____("What happened on 20th July, 1969?")
แก้ไขและรันโค้ด