02 Use Cultural Knowledge In Agent
Use cultural knowledge with your Agents.
1"""202 Use Cultural Knowledge In Agent3=============================45Use cultural knowledge with your Agents.6"""78from kern.agent import Agent9from kern.db.sqlite import SqliteDb10from kern.models.openai import OpenAIResponses1112# ---------------------------------------------------------------------------13# Step 1. Initialize the database (same one used in 01_create_cultural_knowledge.py)14# ---------------------------------------------------------------------------15db = SqliteDb(db_file="tmp/demo.db")1617# ---------------------------------------------------------------------------18# Create Agent19# ---------------------------------------------------------------------------20# The Agent will automatically load shared cultural knowledge (e.g., how to21# 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)3031# ---------------------------------------------------------------------------32# Run Agent33# ---------------------------------------------------------------------------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"))3738 # ---------------------------------------------------------------------------39 # Step 3. Ask the Agent to generate a response that benefits from culture40 # ---------------------------------------------------------------------------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 )5051 # (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 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 02_use_cultural_knowledge_in_agent.py