Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: deep copy agent memory #1583

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix formatting
manthanguptaa committed Dec 16, 2024
commit c604b4e670aa1ff25c9552cbe80f30c89d037a23
22 changes: 22 additions & 0 deletions cookbook/playground/tryout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from phi.agent.agent import Agent
from phi.memory.agent import AgentMemory
from phi.memory.db.postgres import PgMemoryDb
from phi.model.ollama import Ollama
from phi.playground.playground import Playground
from phi.playground.serve import serve_playground_app

agent = Agent(
name="Test",
agent_id="test-agent",
model=Ollama(id="llama3.1:8b"),
memory=AgentMemory(
db=PgMemoryDb(table_name="agent_memory", db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
create_user_memories=True,
create_session_summary=True,
),
)

app = Playground(agents=[agent]).get_app()

if __name__ == "__main__":
serve_playground_app("tryout:app", reload=True)
6 changes: 3 additions & 3 deletions phi/memory/agent.py
Original file line number Diff line number Diff line change
@@ -361,16 +361,16 @@ def clear(self) -> None:
def deep_copy(self):
# Create a shallow copy of the object
copied_obj = self.__class__(**self.model_dump())

# Manually deepcopy fields that are known to be safe
for field_name, field_value in self.__dict__.items():
if field_name not in ['db', 'classifier', 'manager', 'summarizer']:
if field_name not in ["db", "classifier", "manager", "summarizer"]:
try:
setattr(copied_obj, field_name, deepcopy(field_value))
except Exception as e:
logger.warning(f"Failed to deepcopy field: {field_name} - {e}")
setattr(copied_obj, field_name, field_value)

copied_obj.db = self.db
copied_obj.classifier = self.classifier
copied_obj.manager = self.manager