from langchain.memory import ConversationBufferMemory
# Create a memory instance to store the conversation context.
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Initialize an agent with memory integration.
agent_with_memory = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
memory=memory
)
# Example query using memory
query_memory = "Now, using our previous conversation, calculate 2+2."
response_memory = agent_with_memory.run(query_memory)
print("Agent Response with Memory:", response_memory)
view raw agent.py hosted with ❤ by GitHub