กำหนดค่า 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?")