开始使用免费开始使用

为多工具配置输出

您的聊天机器人的图已就绪!现在,您可以用不同的查询进行测试,这些查询会需要不同的工具。为便于管理聊天机器人的消息,以下模块已导入,并且您的聊天机器人的 config 参数已为一个会话设置好。

from langchain_core.messages import AIMessage, HumanMessage

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

本练习是课程的一部分

使用 LangChain 设计 Agentic 系统

查看课程

练习说明

  • 创建一个 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?")
编辑并运行代码