Get startedGet started for free

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

View Course

Exercise instructions

  • Create a config dictionary with "configurable", containing "thread_id" set to "single_session_memory".
  • Loop through each graph event, applying the .stream() method to graph, passing a dictionary with "messages" containing the user_input labeled "user" and the config dictionary.
  • Loop through event.values() using value and print "Agent:" followed by "messages" if it exists in value and 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?")
Edit and Run Code