Multi-turn Conversation (Interactions)

Code

1from kern.agent import Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.google import GeminiInteractions
4
5agent = Agent(
6 model=GeminiInteractions(id="gemini-3-flash-preview"),
7 add_history_to_context=True,
8 db=SqliteDb(db_file="tmp/data.db"),
9 markdown=True,
10)
11
12if __name__ == "__main__":
13 # First turn - establishes the interaction
14 agent.print_response("My name is Alice and I love hiking in the mountains.")
15
16 # Second turn - references the previous interaction for context
17 agent.print_response("What did I just tell you about myself?")
18
19 # Third turn - continues the conversation chain
20 agent.print_response(
21 "Suggest a hiking destination based on what you know about me."
22 )

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export GOOGLE_API_KEY=xxx

Install dependencies

1uv pip install -U "google-genai>=2.0" kern-ai

Run Agent

1python cookbook/90_models/google/gemini_interactions/multi_turn.py