查询与响应
智能体图已经搭建完成,您的聊天机器人可以运行了!现在请定义一个函数,让聊天机器人使用 ChatGPT 回答查询。该函数会实时流式处理图事件,并将最后一条消息作为对用户查询的响应返回。
本练习是课程的一部分
使用 LangChain 设计 Agentic 系统
练习说明
- 定义
stream_graph_updates()函数,接收字符串参数user_input,用于执行聊天机器人。 - 对
graph使用.stream()方法,以user_input作为"messages"中的"user"消息来流式传递事件。 - 对于
event.values()中的每个item,使用item的"messages"键获取并打印响应。 - 将
user_query传入stream_graph_updates()函数以测试聊天机器人。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define a function to execute the chatbot based on user input
def stream_graph_updates(____: ____):
# Start streaming events from the graph with the user's input
for event in graph.____({"____": [("____", ____)]}):
# Retrieve and print the chatbot node responses
for ____ in event.values():
print("Agent:", ____["____"])
# Define the user query and run the chatbot
____ = "Who is Ada Lovelace?"
stream_graph_updates(____)