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 Agent
2from kern.db.postgres import PostgresDb
3from kern.learn import (
4 EntityMemoryConfig,
5 LearningMachine,
6 LearningMode,
7 SessionContextConfig,
8 UserProfileConfig,
9)
10from kern.models.openai import OpenAIResponses
11
12db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")
13
14
15def 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 )
37
38
39if __name__ == "__main__":
40 user_id = "alex@example.com"
41
42 # Conversation 1: Introduction
43 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 )
49
50 # Conversation 2: New session - agent remembers
51 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 )
56
57 # Conversation 3: Planning with context
58 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.git
2cd kern/cookbook/08_learning
3
4# Setup (requires Docker for Postgres)
5./setup_venv.sh
6./cookbook/scripts/run_pgvector.sh
7
8# Run
9python 07_patterns/personal_assistant.py

Full source: kern/cookbook/08_learning/07_patterns/personal_assistant.py

Related