為多個工具設定輸出
你的聊天機器人圖形已經就緒!現在你可以用不同的查詢來測試機器人如何運作,這些查詢將需要不同的工具。為了管理聊天機器人的訊息,以下模組已匯入,且你的聊天機器人單一工作階段的 config 參數也已設定好。
from langchain_core.messages import AIMessage, HumanMessage
config = {"configurable": {"thread_id": "1"}}
本練習屬於課程
Designing Agentic Systems with LangChain
練習說明
- 建立一個
inputs訊息字典,將使用者的query作為HumanMessage的content。 - 透過迭代
.stream()方法的結果,結合inputs與config,從聊天機器人app串流msg與metadata。 - 檢查每個
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?")