04 Manually Add Culture

Manually add cultural knowledge to your Agents.

1"""
204 Manually Add Culture
3=============================
4
5Manually add cultural knowledge to your Agents.
6"""
7
8from kern.agent import Agent
9from kern.culture.manager import CultureManager
10from kern.db.schemas.culture import CulturalKnowledge
11from kern.db.sqlite import SqliteDb
12from kern.models.openai import OpenAIResponses
13from rich.pretty import pprint
14
15# ---------------------------------------------------------------------------
16# Step 1. Initialize the database used for storing cultural knowledge
17# ---------------------------------------------------------------------------
18db = SqliteDb(db_file="tmp/demo.db")
19
20# ---------------------------------------------------------------------------
21# Step 2. Create the Culture Manager (no model needed for manual inserts)
22# ---------------------------------------------------------------------------
23culture_manager = CultureManager(db=db)
24
25# ---------------------------------------------------------------------------
26# Step 3. Manually add cultural knowledge
27# ---------------------------------------------------------------------------
28# Example: Response Format Standard (short and actionable)
29response_format = CulturalKnowledge(
30 name="Response Format Standard (Kern)",
31 summary="Keep responses concise, scannable, and runnable-first where applicable.",
32 categories=["communication", "ux"],
33 content=(
34 "- Lead with the minimal runnable snippet or example when possible.\n"
35 "- Use numbered steps for procedures; keep each step testable.\n"
36 "- Prefer metric units and explicit defaults (ports, paths, versions).\n"
37 "- End with a short validation checklist."
38 ),
39 notes=["Derived from repeated feedback favoring actionable answers."],
40 metadata={"source": "manual_seed", "version": 1},
41)
42
43# Persist the cultural knowledge
44culture_manager.add_cultural_knowledge(response_format)
45
46# ---------------------------------------------------------------------------
47# Run Agent
48# ---------------------------------------------------------------------------
49if __name__ == "__main__":
50 # Optional: show what is stored
51 print("\n=== Cultural Knowledge (Manual Add) ===")
52 pprint(culture_manager.get_all_knowledge())
53
54 # ---------------------------------------------------------------------------
55 # Step 4. Initialize the Agent with cultural knowledge enabled
56 # ---------------------------------------------------------------------------
57 # The Agent will load shared cultural knowledge and include it in context.
58 agent = Agent(
59 db=db,
60 model=OpenAIResponses(id="gpt-5.2"),
61 add_culture_to_context=True, # adds culture into the prompt context
62 # update_cultural_knowledge=True, # uncomment to let the agent update culture after runs
63 )
64
65 # (Optional) A/B without culture for contrast:
66 # agent_no_culture = Agent(model=OpenAIResponses(id="gpt-5.2"))
67
68 # ---------------------------------------------------------------------------
69 # Step 5. Ask the Agent to generate a response that benefits from culture
70 # ---------------------------------------------------------------------------
71 print("\n=== With Culture ===\n")
72 agent.print_response(
73 "How do I set up a FastAPI service using Docker? ",
74 stream=True,
75 markdown=True,
76 )
77
78 # (Optional) Run without culture for contrast:
79 # print("\n=== Without Culture ===\n")
80 # agent_no_culture.print_response(
81 # "How do I set up a FastAPI service using Docker?",
82 # stream=True,
83 # markdown=True,
84 # )

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/14_advanced
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python 04_manually_add_culture.py