-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
187 lines (148 loc) · 5.65 KB
/
basic_usage.py
File metadata and controls
187 lines (148 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""Example: Encrypted ADK agent with Ollama.
Runs a real ADK agent conversation through the Runner with encrypted
session storage. Demonstrates the full UX: agent responds to user
messages, state and conversation history are encrypted at rest, and
raw database inspection proves no plaintext leaks.
Prerequisites:
- Python 3.12+
- adk-secure-sessions installed (``pip install adk-secure-sessions``)
- Ollama running locally with a model pulled
- ``.env`` file or environment variables set (see below)
Environment variables:
OLLAMA_API_BASE: Ollama server URL (e.g., ``http://localhost:11434``).
OLLAMA_MODEL: Model to use (default: ``gemma3:4b``).
Examples:
Run with defaults (expects ``.env`` file):
```bash
uv run python examples/basic_usage.py
```
Run with explicit env vars:
```bash
OLLAMA_API_BASE=http://localhost:11434 uv run python examples/basic_usage.py
```
See Also:
[`adk_secure_sessions.EncryptedSessionService`][]: Encrypted service.
[`adk_secure_sessions.FernetBackend`][]: Encryption backend.
"""
from __future__ import annotations
import asyncio
import os
import sqlite3
import tempfile
from pathlib import Path
from dotenv import load_dotenv
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
from google.adk.runners import Runner
from google.genai import types
from adk_secure_sessions import EncryptedSessionService, FernetBackend
load_dotenv()
APP_NAME = "secure-demo"
USER_ID = "user-42"
def create_agent() -> LlmAgent:
"""Create a simple medical intake agent.
Returns:
LlmAgent configured for patient intake.
"""
model_name = os.getenv("OLLAMA_MODEL", "gemma3:4b")
return LlmAgent(
name="intake_agent",
model=LiteLlm(model=f"ollama_chat/{model_name}"),
instruction=(
"You are a medical intake assistant. Collect the patient's "
"name, date of birth, and reason for visit. Be brief and "
"professional. After collecting info, summarize what you have."
),
)
async def run_turn(
runner: Runner,
service: EncryptedSessionService,
session_id: str,
message: str,
) -> None:
"""Send a message and print the agent's response.
Args:
runner: ADK Runner instance.
service: Encrypted session service.
session_id: Active session ID.
message: User message text.
"""
print(f"\n You: {message}")
content = types.Content(parts=[types.Part(text=message)])
async for event in runner.run_async(
user_id=USER_ID,
session_id=session_id,
new_message=content,
):
if event.content and event.content.parts:
for part in event.content.parts:
if hasattr(part, "text") and part.text:
print(f" Agent: {part.text}")
async def main() -> None:
"""Run a multi-turn agent conversation with encrypted sessions."""
if not os.getenv("OLLAMA_API_BASE"):
print("Error: OLLAMA_API_BASE not set.")
print("Set it in .env or run:")
print(
" OLLAMA_API_BASE=http://localhost:11434 uv run python examples/basic_usage.py"
)
return
db_dir = Path(tempfile.mkdtemp())
db_path = db_dir / "sessions.db"
db_url = f"sqlite+aiosqlite:///{db_path}"
backend = FernetBackend("my-secret-passphrase")
model_name = os.getenv("OLLAMA_MODEL", "gemma3:4b")
print("=" * 60)
print("adk-secure-sessions: Encrypted Agent Example")
print("=" * 60)
print(f" Database: {db_path}")
print(f" Model: ollama_chat/{model_name}")
print(" Backend: FernetBackend")
# -- Run agent conversation -----------------------------------------------
print("\n--- Agent Conversation (encrypted at rest) ---")
async with EncryptedSessionService(db_url=db_url, backend=backend) as service:
session = await service.create_session(
app_name=APP_NAME,
user_id=USER_ID,
)
agent = create_agent()
runner = Runner(
agent=agent,
app_name=APP_NAME,
session_service=service,
)
await run_turn(runner, service, session.id, "Hi, I need to schedule a visit.")
await run_turn(
runner, service, session.id, "My name is Jane Doe, born 1985-03-15."
)
await run_turn(
runner, service, session.id, "I've been having headaches for about a week."
)
# Show final session state
retrieved = await service.get_session(
app_name=APP_NAME,
user_id=USER_ID,
session_id=session.id,
)
assert retrieved is not None
print(f"\n Events stored: {len(retrieved.events)}")
await runner.close()
# -- Inspect raw database -------------------------------------------------
print("\n--- Raw Database Inspection (proof of encryption) ---\n")
conn = sqlite3.connect(str(db_path))
for table in ("sessions", "events"):
col = "state" if table == "sessions" else "event_data"
row = conn.execute(f"SELECT {col} FROM {table} LIMIT 1").fetchone() # noqa: S608
if row and row[0]:
raw = row[0]
preview = raw[:60] if len(raw) > 60 else raw
print(f" {table}.{col}: {preview}...")
print(f" Contains 'Jane Doe'? {('Jane Doe' in str(raw))}")
print(f" Contains 'headache'? {('headache' in str(raw))}")
conn.close()
print("\n" + "=" * 60)
print("Conversation history is encrypted at rest.")
print("Only the service with the correct key can read it.")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())