Find answers from the community

Updated 2 weeks ago

Persisting Chat History with the React Agent

how do i persist chat history when using the react agent? the docs doesn't mention anything like that. I need the agent to still remember its previous outputs
L
4
13 comments
You can either
is chat_history an import? or its just the agent.chat_history variable @Logan M
just an attribute on the agent
it returns the current list of chat messages in the memory
try it out πŸ™‚
not working, keeps running the same function.
generate_wallet_tool = FunctionTool.from_defaults(fn=generate_solana_wallet) # Wrap the transaction execution function # execute_transaction_tool = FunctionTool.from_defaults(fn=execute_solana_transaction) # Create the agent with the tools, memory, and LLM agent = ReActAgent.from_tools([generate_wallet_tool], verbose=True) agent.chat_history response = agent.chat("what was the previous wallet address?")
Not sure I understand the code you are running. You didn't use the chat history anywhere?

In the same script, it will maintain memory. But if the program/script exits, you need to store it somewhere and restore it
meaning a chatstore is needed
or you an just dump it whereever you want (since the chat messages are json serializable)
Plain Text
chat_history = agent.chat_history
chat_history_dicts = [x.model_dump() for x in chat_history]

# dump in redis, mongodb, json.dump to disk, pickle, etc...

# reload it
from llama_index.core.llms import ChatMessage

chat_history = [ChatMessage.model_validate(x) for x in chat_history_dicts]
agent.chat("Hello", chat_history=chat_history)
Or, you can use the chat store approach yes
Add a reply
Sign up and join the conversation on Discord