from agno.agent import Agent
from agno.models.openai import OpenAIChat
from supermemory import Supermemory
from dotenv import load_dotenv
load_dotenv()
class PersonalAssistant:
def __init__(self):
self.memory = Supermemory()
def get_context(self, user_id: str, query: str) -> dict:
"""Fetch user profile and relevant history."""
result = self.memory.profile(
container_tag=user_id,
q=query,
threshold=0.5
)
return {
"profile": result.profile.static or [],
"recent": result.profile.dynamic or [],
"history": [m.memory for m in (result.search_results.results or [])[:3]]
}
def build_description(self, context: dict) -> str:
"""Turn context into agent description."""
parts = ["You are a helpful personal assistant."]
if context["profile"]:
parts.append(f"About this user: {', '.join(context['profile'])}")
if context["recent"]:
parts.append(f"They're currently: {', '.join(context['recent'])}")
if context["history"]:
parts.append(f"Past conversations: {'; '.join(context['history'])}")
parts.append("Reference what you know about them when relevant.")
return "\n\n".join(parts)
def create_agent(self, context: dict) -> Agent:
return Agent(
name="assistant",
model=OpenAIChat(id="gpt-4o"),
description=self.build_description(context),
markdown=True
)
def chat(self, user_id: str, message: str) -> str:
"""Handle a message and remember the interaction."""
context = self.get_context(user_id, message)
agent = self.create_agent(context)
response = agent.run(message)
# Store for future sessions
self.memory.add(
content=f"User: {message}\nAssistant: {response.content}",
container_tag=user_id,
metadata={"type": "chat"}
)
return response.content
def teach(self, user_id: str, fact: str):
"""Store a preference or fact about the user."""
self.memory.add(
content=fact,
container_tag=user_id,
metadata={"type": "preference"}
)
if __name__ == "__main__":
assistant = PersonalAssistant()
# Teach it some preferences
assistant.teach("user_1", "Prefers concise answers")
assistant.teach("user_1", "Works in software engineering")
# Chat
response = assistant.chat("user_1", "What's a good way to learn Rust?")
print(response)