Personal Assistant
A personal assistant that learns about the user over time without being asked.
Overview
A personal assistant that becomes increasingly personalized through the Learning Machine. Combines user profile tracking, session context, and entity memory to remember preferences, people, and events.
Code
1from kern.agent import Agent2from kern.db.postgres import PostgresDb3from kern.learn import (4 EntityMemoryConfig,5 LearningMachine,6 LearningMode,7 SessionContextConfig,8 UserProfileConfig,9)10from kern.models.openai import OpenAIResponses1112db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")131415def create_personal_assistant(user_id: str, session_id: str) -> Agent:16 """Create a personal assistant for a specific user."""17 return Agent(18 model=OpenAIResponses(id="gpt-5.2"),19 db=db,20 instructions=(21 "You are a helpful personal assistant. "22 "Remember user preferences without being asked. "23 "Keep track of important people and events in their life."24 ),25 learning=LearningMachine(26 user_profile=UserProfileConfig(mode=LearningMode.ALWAYS),27 session_context=SessionContextConfig(enable_planning=True),28 entity_memory=EntityMemoryConfig(29 mode=LearningMode.ALWAYS,30 namespace=f"user:{user_id}:personal",31 ),32 ),33 user_id=user_id,34 session_id=session_id,35 markdown=True,36 )373839if __name__ == "__main__":40 user_id = "alex@example.com"4142 # Conversation 1: Introduction43 agent = create_personal_assistant(user_id, "conv_1")44 agent.print_response(45 "Hi! I'm Alex Chen. I work as a product manager at Stripe. "46 "I prefer concise responses. My sister Sarah is visiting next month.",47 stream=True,48 )4950 # Conversation 2: New session - agent remembers51 agent = create_personal_assistant(user_id, "conv_2")52 agent.print_response(53 "What do you remember about me and my sister?",54 stream=True,55 )5657 # Conversation 3: Planning with context58 agent = create_personal_assistant(user_id, "conv_3")59 agent.print_response(60 "Help me plan activities for Sarah's visit. She likes hiking.",61 stream=True,62 )How It Works
- User Profile (ALWAYS mode): Automatically extracts name, workplace, and communication preferences
- Session Context (with planning): Tracks goals and progress across multi-turn conversations
- Entity Memory (ALWAYS mode): Records people (Sarah), places, and events with relationships
The namespace user:{user_id}:personal isolates each user's entity memory.
Run This Example
1git clone https://github.com/kern-ai/kern.git2cd kern/cookbook/08_learning34# Setup (requires Docker for Postgres)5./setup_venv.sh6./cookbook/scripts/run_pgvector.sh78# Run9python 07_patterns/personal_assistant.pyFull source: kern/cookbook/08_learning/07_patterns/personal_assistant.py
Related
- Learning Overview - All learning stores explained
- Support Agent - Multi-tenant learning pattern
- Agent with Memory - Simpler memory patterns