開始使用免費開始

為多個工具設定輸出

你的聊天機器人圖形已經就緒!現在你可以用不同的查詢來測試機器人如何運作,這些查詢將需要不同的工具。為了管理聊天機器人的訊息,以下模組已匯入,且你的聊天機器人單一工作階段的 config 參數也已設定好。

from langchain_core.messages import AIMessage, HumanMessage

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

本練習屬於課程

Designing Agentic Systems with LangChain

檢視課程

練習說明

  • 建立一個 inputs 訊息字典,將使用者的 query 作為 HumanMessagecontent
  • 透過迭代 .stream() 方法的結果,結合 inputsconfig,從聊天機器人 app 串流 msgmetadata
  • 檢查每個 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?")
編輯並執行程式碼