-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathagent_knowledge_sqlite.py
More file actions
319 lines (275 loc) · 11.6 KB
/
agent_knowledge_sqlite.py
File metadata and controls
319 lines (275 loc) · 11.6 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Knowledge retrieval (RAG) via a custom context provider.
Diagram:
Input ──▶ Agent ──────────────────▶ LLM ──▶ Response
│ ▲
│ search with input │ relevant knowledge
▼ │
┌────────────┐ │
│ Knowledge │───────────────┘
│ store │
│ (SQLite) │
└────────────┘
The agent retrieves knowledge from a SQLite FTS5 database *before*
asking the LLM to respond. Because the agent always needs domain-specific
knowledge to ground its answers, a deterministic search step is more
efficient and reliable than asking the LLM to decide to call a tool.
This example seeds a small product-catalog knowledge base and uses a
custom BaseContextProvider to inject matching rows into the LLM context.
"""
import asyncio
import logging
import os
import re
import sqlite3
import sys
from typing import Any
from agent_framework import Agent, AgentSession, BaseContextProvider, Message, SessionContext, SupportsAgentRun
from agent_framework.openai import OpenAIChatClient
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
from dotenv import load_dotenv
from rich import print
from rich.logging import RichHandler
# ── Logging ──────────────────────────────────────────────────────────
handler = RichHandler(show_path=False, rich_tracebacks=True, show_level=False)
logging.basicConfig(level=logging.WARNING, handlers=[handler], force=True, format="%(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# ── OpenAI client ────────────────────────────────────────────────────
load_dotenv(override=True)
API_HOST = os.getenv("API_HOST", "github")
async_credential = None
if API_HOST == "azure":
async_credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
client = OpenAIChatClient(
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/",
api_key=token_provider,
model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
)
elif API_HOST == "github":
client = OpenAIChatClient(
base_url="https://models.github.ai/inference",
api_key=os.environ["GITHUB_TOKEN"],
model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4.1-mini"),
)
else:
client = OpenAIChatClient(
api_key=os.environ["OPENAI_API_KEY"], model_id=os.environ.get("OPENAI_MODEL", "gpt-4.1-mini")
)
# ── Knowledge store (SQLite + FTS5) ─────────────────────────────────
PRODUCTS = [
{
"name": "TrailBlaze Hiking Boots",
"category": "Footwear",
"price": 149.99,
"description": (
"Waterproof hiking boots with Vibram soles, ankle support, "
"and breathable Gore-Tex lining. Ideal for rocky trails and wet conditions."
),
},
{
"name": "SummitPack 40L Backpack",
"category": "Bags",
"price": 89.95,
"description": (
"Lightweight 40-liter backpack with hydration sleeve, rain cover, "
"and ergonomic hip belt. Great for day hikes and overnight trips."
),
},
{
"name": "ArcticShield Down Jacket",
"category": "Clothing",
"price": 199.00,
"description": (
"800-fill goose down jacket rated to -20°F. "
"Features a water-resistant shell, packable design, and adjustable hood."
),
},
{
"name": "RiverRun Kayak Paddle",
"category": "Water Sports",
"price": 74.50,
"description": (
"Fiberglass kayak paddle with adjustable ferrule and drip rings. "
"Lightweight at 28 oz, suitable for touring and recreational kayaking."
),
},
{
"name": "TerraFirm Trekking Poles",
"category": "Accessories",
"price": 59.99,
"description": (
"Collapsible carbon-fiber trekking poles with cork grips and tungsten tips. "
"Adjustable from 24 to 54 inches, with anti-shock springs."
),
},
{
"name": "ClearView Binoculars 10x42",
"category": "Optics",
"price": 129.00,
"description": (
"Roof-prism binoculars with 10x magnification and 42mm objective lenses. "
"Nitrogen-purged and waterproof. Ideal for birding and wildlife observation."
),
},
{
"name": "NightGlow LED Headlamp",
"category": "Lighting",
"price": 34.99,
"description": (
"Rechargeable 350-lumen headlamp with red-light mode and adjustable beam. "
"IPX6 waterproof rating, runs up to 40 hours on low."
),
},
{
"name": "CozyNest Sleeping Bag",
"category": "Camping",
"price": 109.00,
"description": (
"Three-season mummy sleeping bag rated to 20°F. "
"Synthetic insulation, compression sack included. Weighs 2.5 lbs."
),
},
]
def create_knowledge_db(db_path: str) -> sqlite3.Connection:
"""Create (or re-create) the product catalog in SQLite with an FTS5 index."""
conn = sqlite3.connect(db_path)
# Drop existing tables so we always start fresh
conn.execute("DROP TABLE IF EXISTS products_fts")
conn.execute("DROP TABLE IF EXISTS products")
conn.execute(
"""
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
category TEXT NOT NULL,
price REAL NOT NULL,
description TEXT NOT NULL
)
"""
)
conn.executemany(
"INSERT INTO products (name, category, price, description) VALUES (?, ?, ?, ?)",
[(p["name"], p["category"], p["price"], p["description"]) for p in PRODUCTS],
)
# Build a full-text search index on name, category, and description
conn.execute(
"""
CREATE VIRTUAL TABLE products_fts USING fts5(
name, category, description,
content='products',
content_rowid='id'
)
"""
)
conn.execute(
"INSERT INTO products_fts (rowid, name, category, description) "
"SELECT id, name, category, description FROM products"
)
conn.commit()
return conn
# ── Custom context provider for knowledge retrieval ──────────────────
class SQLiteKnowledgeProvider(BaseContextProvider):
"""Retrieves relevant product knowledge from SQLite FTS5 before each LLM call.
This follows the "knowledge retrieval" pattern where the agent deterministically
searches a knowledge store *before* the LLM runs, rather than relying on
the LLM to decide whether to call a search tool. This ensures the model
always has domain-specific context to ground its response.
"""
def __init__(self, db_conn: sqlite3.Connection, max_results: int = 3):
super().__init__(source_id="sqlite-knowledge")
self.db_conn = db_conn
self.max_results = max_results
def _search(self, query: str) -> list[dict]:
"""Run an FTS5 query and return matching products."""
# Extract words, drop short ones (len <= 2 catches "a", "an", "is", etc.)
words = re.findall(r"[a-zA-Z]+", query)
tokens = [w.lower() for w in words if len(w) > 2]
if not tokens:
return []
fts_query = " OR ".join(tokens)
try:
cursor = self.db_conn.execute(
"""
SELECT p.name, p.category, p.price, p.description
FROM products_fts fts
JOIN products p ON fts.rowid = p.id
WHERE products_fts MATCH ?
ORDER BY rank
LIMIT ?
""",
(fts_query, self.max_results),
)
return [
{"name": row[0], "category": row[1], "price": row[2], "description": row[3]}
for row in cursor.fetchall()
]
except Exception:
logger.debug("FTS query failed for: %s", fts_query, exc_info=True)
return []
def _format_results(self, results: list[dict]) -> str:
"""Format search results as a text block for the LLM context."""
lines = ["Relevant product information from our catalog:\n"]
for product in results:
lines.append(
f"- **{product['name']}** ({product['category']}, ${product['price']:.2f}): "
f"{product['description']}"
)
return "\n".join(lines)
async def before_run(
self,
*,
agent: SupportsAgentRun,
session: AgentSession,
context: SessionContext,
state: dict[str, Any],
) -> None:
"""Search the knowledge base with the user's latest message and inject results."""
user_text = next((msg.text for msg in reversed(context.input_messages) if msg.role == "user" and msg.text), None)
if not user_text:
return
results = self._search(user_text)
if not results:
logger.info("[📚 Knowledge] No matching products found for: %s", user_text)
return
logger.info("[📚 Knowledge] Found %d matching product(s) for: %s", len(results), user_text)
context.extend_messages(
self.source_id,
[Message(role="user", text=self._format_results(results))],
)
# ── Agent setup ──────────────────────────────────────────────────────
DB_PATH = ":memory:" # In-memory DB — no file cleanup needed
# Create and seed the knowledge database
db_conn = create_knowledge_db(DB_PATH)
knowledge_provider = SQLiteKnowledgeProvider(db_conn=db_conn)
agent = Agent(
client=client,
instructions=(
"You are a helpful outdoor-gear shopping assistant for the store 'TrailBuddy'. "
"Answer customer questions using ONLY the product information provided in the context. "
"If no relevant products are found in the context, say you don't have information "
"about that item. Include prices when recommending products."
),
context_providers=[knowledge_provider],
)
async def main() -> None:
"""Demonstrate the knowledge retrieval (RAG) pattern with several queries."""
# Query 1: Should match hiking boots and trekking poles
print("\n[bold]=== Knowledge Retrieval (RAG) Demo ===[/bold]")
print("[blue]User:[/blue] I'm planning a hiking trip. What boots and poles do you recommend?")
response = await agent.run("I'm planning a hiking trip. What boots and poles do you recommend?")
print(f"[green]Agent:[/green] {response.text}\n")
# Query 2: No match expected — demonstrates graceful "no knowledge" handling
print("[blue]User:[/blue] Do you have any surfboards?")
response = await agent.run("Do you have any surfboards?")
print(f"[green]Agent:[/green] {response.text}\n")
db_conn.close()
if async_credential:
await async_credential.close()
if __name__ == "__main__":
if "--devui" in sys.argv:
from agent_framework.devui import serve
serve(entities=[agent], auto_open=True)
else:
asyncio.run(main())