02 Use Cultural Knowledge In Agent

Use cultural knowledge with your Agents.

1"""
202 Use Cultural Knowledge In Agent
3=============================
4
5Use cultural knowledge with your Agents.
6"""
7
8from kern.agent import Agent
9from kern.db.sqlite import SqliteDb
10from kern.models.openai import OpenAIResponses
11
12# ---------------------------------------------------------------------------
13# Step 1. Initialize the database (same one used in 01_create_cultural_knowledge.py)
14# ---------------------------------------------------------------------------
15db = SqliteDb(db_file="tmp/demo.db")
16
17# ---------------------------------------------------------------------------
18# Create Agent
19# ---------------------------------------------------------------------------
20# The Agent will automatically load shared cultural knowledge (e.g., how to
21# format responses, how to write tutorials, or tone/style preferences).
22agent = Agent(
23 model=OpenAIResponses(id="gpt-5.2"),
24 db=db,
25 # This flag will add the cultural knowledge to the agent's context:
26 add_culture_to_context=True,
27 # This flag will update cultural knowledge after every run:
28 # update_cultural_knowledge=True,
29)
30
31# ---------------------------------------------------------------------------
32# Run Agent
33# ---------------------------------------------------------------------------
34if __name__ == "__main__":
35 # (Optional) Quick A/B switch to show the difference without culture:
36 # agent_no_culture = Agent(model=OpenAIResponses(id="gpt-5.2"))
37
38 # ---------------------------------------------------------------------------
39 # Step 3. Ask the Agent to generate a response that benefits from culture
40 # ---------------------------------------------------------------------------
41 # If `01_create_cultural_knowledge.py` added principles like:
42 # "Start technical explanations with code examples and then reasoning"
43 # The Agent will apply that here, starting with a concrete FastAPI example.
44 print("\n=== With Culture ===\n")
45 agent.print_response(
46 "How do I set up a FastAPI service using Docker? ",
47 stream=True,
48 markdown=True,
49 )
50
51 # (Optional) Run without culture for contrast:
52 # print("\n=== Without Culture ===\n")
53 # agent_no_culture.print_response("How do I set up a FastAPI service using Docker?", stream=True, markdown=True)

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 02_use_cultural_knowledge_in_agent.py