为多工具配置输出
您的聊天机器人的图已就绪!现在,您可以用不同的查询进行测试,这些查询会需要不同的工具。为便于管理聊天机器人的消息,以下模块已导入,并且您的聊天机器人的 config 参数已为一个会话设置好。
from langchain_core.messages import AIMessage, HumanMessage
config = {"configurable": {"thread_id": "1"}}
本练习是课程的一部分
使用 LangChain 设计 Agentic 系统
练习说明
- 创建一个
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?")