Using graph memory for conversation
Now that your chatbot has access to memory, you can stream its responses to follow up questions. Note that your follow up questions will need no extra context as the chatbot can access the full conversation stored in its memory. The questions have already been set up for you.
This exercise is part of the course
Designing Agentic Systems with LangChain
Exercise instructions
- Create a
configdictionary with"configurable", containing"thread_id"set to"single_session_memory". - Loop through each graph
event, applying the.stream()method tograph, passing a dictionary with"messages"containing theuser_inputlabeled"user"and theconfigdictionary. - Loop through
event.values()usingvalueand print"Agent:"followed by"messages"if it exists invalueand is not empty.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set up a streaming function for a single user
def stream_memory_responses(user_input: str):
____ = {"____": {"____": "____"}}
# Stream the events in the graph
for ____ in graph.____({"messages": [("user", ____)]}, ____):
# Return the agent's last response
for ____ in event.____():
if "messages" in ____ and value["____"]:
print("Agent:", value["messages"])
stream_memory_responses("Tell me about the Eiffel Tower.")
stream_memory_responses("Who built it?")