开始使用免费开始使用

查询与响应

智能体图已经搭建完成,您的聊天机器人可以运行了!现在请定义一个函数,让聊天机器人使用 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(____)
编辑并运行代码