-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentDemo.py
More file actions
516 lines (412 loc) · 15.6 KB
/
AgentDemo.py
File metadata and controls
516 lines (412 loc) · 15.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import os
from datetime import datetime
from typing import Dict, Any, List
import streamlit as st
from pymongo import MongoClient
from dotenv import load_dotenv
import voyageai
st.set_page_config(
page_title="RFP Agent Demo",
layout="wide",
)
# =========================
# CONFIG (NO ENV BS 😄)
# =========================
MONGODB_URI = ""
DB_NAME = "RFP_Demo"
VOYAGE_API_KEY = ""
INDEX_NAME = "vector_index"
EMBED_MODEL = "voyage-4-large"
COLLECTIONS = [
"knowledge_base",
"historical_rfp_answers",
"source_documents"
]
AGENT_RUNS_COLLECTION = "agent_runs"
@st.cache_resource
def get_db():
if not MONGODB_URI:
return None
client = MongoClient(MONGODB_URI)
return client[DB_NAME]
@st.cache_resource
def get_voyage():
if not VOYAGE_API_KEY:
return None
return voyageai.Client(api_key=VOYAGE_API_KEY)
db = get_db()
vo = get_voyage()
def source_label(name: str) -> str:
return {
"knowledge_base": "Knowledge Base",
"historical_rfp_answers": "Historical RFP Answers",
"source_documents": "Source Documents",
}.get(name, name)
def parse_questions(text: str) -> List[str]:
return [line.strip() for line in text.splitlines() if line.strip()]
def check_runtime_readiness() -> None:
if not MONGODB_URI:
st.error("MONGODB_URI is missing. Add it to your .env file or environment.")
st.stop()
if not VOYAGE_API_KEY:
st.error("VOYAGE_API_KEY is missing. Add it to your .env file or environment.")
st.stop()
if db is None:
st.error("MongoDB client could not be initialized.")
st.stop()
if vo is None:
st.error("Voyage client could not be initialized.")
st.stop()
def safe_float(value: Any, default: float = 0.0) -> float:
try:
return float(value)
except Exception:
return default
def embed_query(text: str) -> List[float]:
return vo.embed(
texts=[text],
model=EMBED_MODEL,
).embeddings[0]
def search_collection(name: str, query_vec: List[float], limit: int = 3) -> List[Dict[str, Any]]:
pipeline = [
{
"$vectorSearch": {
"index": INDEX_NAME,
"path": "embedding",
"queryVector": query_vec,
"numCandidates": 20,
"limit": limit,
}
},
{
"$project": {
"_id": 1,
"question_text": 1,
"answer_text": 1,
"title": 1,
"chunk_text": 1,
"review_status": 1,
"outcome": 1,
"score": {"$meta": "vectorSearchScore"},
}
},
]
results = list(db[name].aggregate(pipeline))
for r in results:
r["source_collection"] = name
r["_id"] = str(r["_id"])
return results
def rerank_score(doc: Dict[str, Any]) -> float:
score = safe_float(doc.get("score", 0))
if doc.get("review_status") == "approved":
score += 0.08
if doc.get("review_status") == "stale":
score -= 0.08
if doc.get("outcome") == "won":
score += 0.08
if doc.get("outcome") == "used":
score += 0.03
if doc.get("outcome") == "lost":
score -= 0.08
return score
def explain_signals(doc: Dict[str, Any]) -> List[str]:
signals = []
if doc.get("review_status") == "approved":
signals.append("APPROVED")
if doc.get("review_status") == "stale":
signals.append("STALE")
if doc.get("outcome") == "won":
signals.append("PRIOR_WIN")
if doc.get("outcome") == "used":
signals.append("PREVIOUSLY_USED")
if doc.get("outcome") == "lost":
signals.append("PRIOR_LOSS")
if doc.get("source_collection") == "source_documents":
signals.append("POLICY_SOURCE")
return signals
def run_search(query: str, per_collection_limit: int = 3) -> List[Dict[str, Any]]:
query_vec = embed_query(query)
all_results: List[Dict[str, Any]] = []
for name in COLLECTIONS:
all_results.extend(search_collection(name, query_vec, per_collection_limit))
for r in all_results:
r["final_score"] = rerank_score(r)
r["signals"] = explain_signals(r)
return sorted(all_results, key=lambda x: x["final_score"], reverse=True)
def classify_question(question: str) -> Dict[str, Any]:
q = question.lower()
if any(term in q for term in ["hipaa", "soc2", "iso", "compliance", "audit"]):
category = "COMPLIANCE"
route = "sme_team_compliance"
elif any(term in q for term in ["security", "encryption", "access control", "incident"]):
category = "SECURITY"
route = "sme_team_security"
elif any(term in q for term in ["sla", "uptime", "availability", "disaster recovery"]):
category = "PLATFORM"
route = "sme_team_platform"
elif any(term in q for term in ["pricing", "cost", "commercial", "license"]):
category = "COMMERCIAL"
route = "sme_team_commercial"
else:
category = "GENERAL"
route = "sme_team_general"
return {
"category": category,
"suggested_route": route,
}
def assess_result(top_result: Dict[str, Any]) -> Dict[str, Any]:
score = safe_float(top_result.get("final_score", 0))
signals = top_result.get("signals", [])
risk_flags: List[str] = []
if "STALE" in signals:
risk_flags.append("Content may be stale")
if "PRIOR_LOSS" in signals:
risk_flags.append("Based on prior losing content")
if top_result.get("source_collection") == "source_documents":
risk_flags.append("Grounded in source policy document")
if score >= 0.90:
confidence = "HIGH"
elif score >= 0.75:
confidence = "MEDIUM"
else:
confidence = "LOW"
return {
"confidence_band": confidence,
"risk_flags": risk_flags,
}
def build_trace(
question: str,
classification: Dict[str, Any],
ranked: List[Dict[str, Any]],
assessment: Dict[str, Any],
) -> List[Dict[str, Any]]:
top = ranked[0] if ranked else {}
now = datetime.utcnow().isoformat()
return [
{
"stage": "QUESTION_CLASSIFICATION",
"timestamp": now,
"result": {
"question": question,
**classification,
},
},
{
"stage": "ANSWER_SELECTION",
"timestamp": now,
"result": {
"selected_source": top.get("source_collection"),
"selected_score": top.get("final_score"),
"vector_score": top.get("score"),
"signals": top.get("signals", []),
},
},
{
"stage": "RISK_ASSESSMENT",
"timestamp": now,
"result": assessment,
},
]
def save_agent_run(
question: str,
classification: Dict[str, Any],
ranked: List[Dict[str, Any]],
assessment: Dict[str, Any],
trace: List[Dict[str, Any]],
) -> str:
top = ranked[0] if ranked else {}
record = {
"question": question,
"agent_type": "RFP_SELECTION_AGENT",
"classification": classification,
"selected_result": top,
"top_results": ranked[:5],
"assessment": assessment,
"trace": trace,
"created_at": datetime.utcnow(),
}
result = db[AGENT_RUNS_COLLECTION].insert_one(record)
return str(result.inserted_id)
def run_agent_pipeline(question: str, per_collection_limit: int) -> Dict[str, Any]:
classification = classify_question(question)
ranked = run_search(question, per_collection_limit=per_collection_limit)
if not ranked:
return {
"success": False,
"question": question,
"message": "No ranked results returned from search engine.",
}
assessment = assess_result(ranked[0])
trace = build_trace(question, classification, ranked, assessment)
run_id = save_agent_run(question, classification, ranked, assessment, trace)
return {
"success": True,
"question": question,
"run_id": run_id,
"classification": classification,
"ranked": ranked,
"assessment": assessment,
"trace": trace,
}
def get_past_runs(limit: int = 10) -> List[Dict[str, Any]]:
docs = list(
db[AGENT_RUNS_COLLECTION]
.find(
{},
{
"question": 1,
"classification": 1,
"assessment": 1,
"selected_result.source_collection": 1,
"selected_result.final_score": 1,
"created_at": 1,
},
)
.sort("created_at", -1)
.limit(limit)
)
for doc in docs:
doc["_id"] = str(doc["_id"])
return docs
st.title("🤖 RFP Agent Demo")
st.caption("Multi-question agentic answer selection with Atlas trace persistence")
check_runtime_readiness()
with st.sidebar:
st.subheader("Agent Settings")
per_collection_limit = st.slider(
"Results per collection",
min_value=1,
max_value=5,
value=3,
)
st.subheader("Environment")
st.write(f"**DB Name:** `{DB_NAME}`")
st.write(f"**Saved Runs Collection:** `{AGENT_RUNS_COLLECTION}`")
st.write(f"**Atlas Vector Index:** `{INDEX_NAME}`")
st.write(f"**Embedding Model:** `{EMBED_MODEL}`")
st.subheader("Past Runs")
past_limit = st.slider(
"Show recent runs",
min_value=3,
max_value=25,
value=10,
)
left_panel, right_panel = st.columns([2, 1])
with left_panel:
questions_raw = st.text_area(
"RFP Questions (one per line)",
value="",
height=180,
placeholder="Paste one or more RFP questions, one per line...",
)
run_agent = st.button("Analyze Questions", type="primary", use_container_width=True)
with right_panel:
st.subheader("How this agent works")
st.write("1. Classifies the incoming question")
st.write("2. Runs vector search across collections")
st.write("3. Reranks using approval / outcome signals")
st.write("4. Assesses confidence and risk")
st.write("5. Saves the full trace in Atlas")
tab1, tab2 = st.tabs(["Analysis Results", "View Past Runs"])
with tab1:
if run_agent:
questions = parse_questions(questions_raw)
if not questions:
st.warning("Please enter at least one question.")
st.stop()
with st.spinner("Running agent pipeline..."):
all_results = [run_agent_pipeline(question, per_collection_limit) for question in questions]
success_count = sum(1 for r in all_results if r.get("success"))
st.success(f"Processed {success_count} of {len(all_results)} questions.")
for idx, result in enumerate(all_results, start=1):
with st.container(border=True):
st.subheader(f"Question #{idx}")
st.write(f"**Question:** {result.get('question', '')}")
if not result["success"]:
st.error(result["message"])
continue
st.caption(f"Saved Run ID: {result['run_id']}")
ranked = result["ranked"]
top = ranked[0]
classification = result["classification"]
assessment = result["assessment"]
trace = result["trace"]
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Category", classification["category"])
with col2:
st.metric("Suggested Route", classification["suggested_route"])
with col3:
st.metric("Confidence", assessment["confidence_band"])
top_col, risk_col = st.columns([2, 1])
with top_col:
st.subheader("Top Selected Result")
st.write(f"**Source:** {source_label(top.get('source_collection'))}")
st.write(f"**Final Score:** {safe_float(top.get('final_score')):.4f}")
st.write(f"**Vector Score:** {safe_float(top.get('score')):.4f}")
st.write(f"**Signals:** {', '.join(top.get('signals', [])) or 'None'}")
if top.get("review_status"):
st.write(f"**Status:** {top['review_status']}")
if top.get("outcome"):
st.write(f"**Outcome:** {top['outcome']}")
if top.get("source_collection") == "source_documents":
if top.get("title"):
st.write(f"**Title:** {top.get('title')}")
st.write(top.get("chunk_text"))
else:
if top.get("question_text"):
st.write(f"**Matched Question:** {top.get('question_text')}")
st.write(top.get("answer_text"))
with risk_col:
st.subheader("Risk Assessment")
risk_flags = assessment.get("risk_flags", [])
if risk_flags:
for flag in risk_flags:
st.warning(flag)
else:
st.success("No major risk flags detected")
st.subheader("Agent Trace")
for step in trace:
with st.container(border=True):
st.write(f"**Stage:** {step['stage']}")
st.write(f"**Timestamp:** {step['timestamp']}")
st.json(step["result"])
st.subheader("Ranked Results")
for i, r in enumerate(ranked, start=1):
with st.container(border=True):
st.write(f"### Result #{i}")
st.write(f"**Source:** {source_label(r.get('source_collection'))}")
st.write(f"**Final Score:** {safe_float(r.get('final_score')):.4f}")
st.write(f"**Signals:** {', '.join(r.get('signals', [])) or 'None'}")
if r.get("source_collection") == "source_documents":
if r.get("title"):
st.write(r.get("title"))
st.write(r.get("chunk_text"))
else:
if r.get("question_text"):
st.write(r.get("question_text"))
st.write(r.get("answer_text"))
with tab2:
st.subheader("Recent Agent Runs")
st.caption("These are the traces already saved in Atlas.")
runs = get_past_runs(limit=past_limit)
if not runs:
st.info("No past runs found yet.")
else:
for run in runs:
with st.container(border=True):
st.write(f"**Run ID:** {run['_id']}")
st.write(f"**Created At:** {run.get('created_at')}")
st.write(f"**Question:** {run.get('question', '')}")
classification = run.get("classification", {})
assessment = run.get("assessment", {})
selected = run.get("selected_result", {})
c1, c2, c3 = st.columns(3)
with c1:
st.write(f"**Category:** {classification.get('category', 'N/A')}")
with c2:
st.write(f"**Route:** {classification.get('suggested_route', 'N/A')}")
with c3:
st.write(f"**Confidence:** {assessment.get('confidence_band', 'N/A')}")
st.write(f"**Selected Source:** {source_label(selected.get('source_collection'))}")
st.write(f"**Top Final Score:** {safe_float(selected.get('final_score')):.4f}")