How to maintain conversation history in PandasAI V3? #1819
Unanswered
muhammadhamzaazhar
asked this question in
Q&A
Replies: 1 comment
-
|
PandasAI V3 has built-in memory support via the Agent class. Basic conversation memory: from pandasai import Agent
import pandas as pd
df = pd.read_csv("data.csv")
agent = Agent(dfs=[df])
# Each chat call maintains history
response1 = agent.chat("What is the average sales?")
response2 = agent.chat("Now filter to just Q4") # Remembers context!
response3 = agent.chat("Show me the top 5") # Still has contextAccess conversation history: # Get all messages
history = agent.conversation_history
for msg in history:
print(f"{msg.role}: {msg.content}")
# Clear when needed
agent.clear_conversation()Persist across sessions: import json
# Save
with open("chat_history.json", "w") as f:
json.dump([m.dict() for m in agent.conversation_history], f)
# Restore (on new agent)
agent = Agent(dfs=[df])
with open("chat_history.json") as f:
history = json.load(f)
for msg in history:
agent.conversation_history.append(Message(**msg))Tips:
We build data analysis agents at Revolution AI — PandasAI V3 memory works well for multi-turn analysis. The key is managing context window size for longer conversations. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
@gventuri could you please guide me on how to implement and maintain conversational memory or chat history within PandasAI? I'm looking to preserve context across multiple interactions to enable more coherent and context-aware responses.
Beta Was this translation helpful? Give feedback.
All reactions