04 Manually Add Culture
Manually add cultural knowledge to your Agents.
1"""204 Manually Add Culture3=============================45Manually add cultural knowledge to your Agents.6"""78from kern.agent import Agent9from kern.culture.manager import CultureManager10from kern.db.schemas.culture import CulturalKnowledge11from kern.db.sqlite import SqliteDb12from kern.models.openai import OpenAIResponses13from rich.pretty import pprint1415# ---------------------------------------------------------------------------16# Step 1. Initialize the database used for storing cultural knowledge17# ---------------------------------------------------------------------------18db = SqliteDb(db_file="tmp/demo.db")1920# ---------------------------------------------------------------------------21# Step 2. Create the Culture Manager (no model needed for manual inserts)22# ---------------------------------------------------------------------------23culture_manager = CultureManager(db=db)2425# ---------------------------------------------------------------------------26# Step 3. Manually add cultural knowledge27# ---------------------------------------------------------------------------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)4243# Persist the cultural knowledge44culture_manager.add_cultural_knowledge(response_format)4546# ---------------------------------------------------------------------------47# Run Agent48# ---------------------------------------------------------------------------49if __name__ == "__main__":50 # Optional: show what is stored51 print("\n=== Cultural Knowledge (Manual Add) ===")52 pprint(culture_manager.get_all_knowledge())5354 # ---------------------------------------------------------------------------55 # Step 4. Initialize the Agent with cultural knowledge enabled56 # ---------------------------------------------------------------------------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 context62 # update_cultural_knowledge=True, # uncomment to let the agent update culture after runs63 )6465 # (Optional) A/B without culture for contrast:66 # agent_no_culture = Agent(model=OpenAIResponses(id="gpt-5.2"))6768 # ---------------------------------------------------------------------------69 # Step 5. Ask the Agent to generate a response that benefits from culture70 # ---------------------------------------------------------------------------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 )7778 # (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 repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python 04_manually_add_culture.py