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
Exercise instructions
- Define the
stream_graph_updates()
function to acceptuser_input
as a string parameter for chatbot execution. - Apply the
.stream()
method tograph
to stream events withuser_input
as a"user"
message in"messages"
. - For each
item
inevent.values()
, retrieve and print the response using the"messages"
key initem
. - Pass
user_query
to thestream_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(____)