IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Designing Agentic Systems with LangChain

Visualizza il corso

Istruzioni dell'esercizio

  • 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.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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?")
Modifica ed esegui il codice