Get startedGet started for free

Queries and responses

The agent graph is set up so your chatbot is ready to run! Now you can define a function that allows the chatbot to answer queries using ChatGPT. This function will stream through the graph events in real-time and return the last message as a response to the user's query.

This exercise is part of the course

Designing Agentic Systems with LangChain

View Course

Exercise instructions

  • Define the stream_graph_updates() function to accept user_input as a string parameter for chatbot execution.
  • Apply the .stream() method to graph to stream events with user_input as a "user" message in "messages".
  • For each item in event.values(), retrieve and print the response using the "messages" key in item.
  • Pass user_query to the stream_graph_updates() function to test the chatbot.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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(____)
Edit and Run Code