Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
487 lines (419 loc) · 16.3 KB
/
Copy pathapp.py
File metadata and controls
487 lines (419 loc) · 16.3 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
"""Streamlit Dashboard - AgentGPA Evaluation Results.
Visualizes v1 vs v2 evaluation in order:
1. v1 prompts and scores
2. v2 improvements and scores
3. v1 vs v2 comparison
Run with: uv run streamlit run app.py
"""
import json
from pathlib import Path
import altair as alt
import pandas as pd
import streamlit as st
# --- Page Config ---
st.set_page_config(
page_title="AgentGPA - Internal Developer Assistant",
page_icon="🎓",
layout="wide",
)
# --- Constants ---
RESULTS_PATH = Path(__file__).parent / "evaluation" / "trulens_results.json"
# --- Load Results ---
def _load_results() -> dict | None:
"""Load pre-computed v1/v2 results."""
if RESULTS_PATH.exists():
data = json.loads(RESULTS_PATH.read_text())
if isinstance(data, dict) and "v1" in data and "v2" in data:
return data
return None
# --- Helper: Score Chart ---
def _score_chart(results: list[dict]) -> alt.Chart:
"""Create a grouped bar chart for Goal/Plan/Act scores."""
chart_data = pd.DataFrame(
[
{
"Case": r["case_id"],
"Goal": r["goal"],
"Plan": r["plan"],
"Act": r["act"],
}
for r in results
]
)
melted = chart_data.melt(id_vars="Case", var_name="Metric", value_name="Score")
return (
alt.Chart(melted)
.mark_bar()
.encode(
x=alt.X("Case:N", title="Case"),
y=alt.Y("Score:Q", scale=alt.Scale(domain=[0, 1])),
color=alt.Color("Metric:N"),
xOffset="Metric:N",
)
.properties(height=300)
)
def _render_trace(r: dict) -> None:
"""Render the step-by-step agent trace for one evaluation result."""
timings = r.get("step_timings", {})
token_usage = r.get("token_usage", {})
prompts = r.get("prompts", {})
gpa = (r["goal"] + r["plan"] + r["act"]) / 3
st.subheader(f"{r['version']} — GPA {gpa:.2f}")
# Step 1: Tool Selection
t1 = timings.get("tool_selection_s", "")
t1_label = f" ⏱ {t1}s" if t1 != "" else ""
st.markdown(f"**Step 1 — Tool Selection** `[TOOL span]`{t1_label}")
st.success(f"`{r['tool_used']}`")
sel_usage = token_usage.get("tool_selection", {})
if sel_usage.get("total_tokens"):
st.caption(
f"Tokens — prompt: {sel_usage['prompt_tokens']} / "
f"completion: {sel_usage['completion_tokens']} / "
f"total: {sel_usage['total_tokens']}"
)
if r.get("selection_raw_response"):
with st.expander("LLM raw response (tool selection)"):
st.text(r["selection_raw_response"])
if prompts.get("tool_selection"):
with st.expander("Prompt sent to LLM (tool selection)"):
st.code(
prompts["tool_selection"].replace("{tool_names}", r["tool_used"]),
language="text",
)
# Step 2: Tool Execution
t2 = timings.get("tool_execution_s", "")
t2_label = f" ⏱ {t2}s" if t2 != "" else ""
st.markdown(f"**Step 2 — Tool Execution** `[RETRIEVAL span]`{t2_label}")
context = r.get("retrieved_context", "")
if context:
st.code(context[:400], language="text")
else:
st.info("Calculator: deterministic result, no document retrieval.")
# Step 3: Answer Generation
t3 = timings.get("answer_generation_s", "")
t3_label = f" ⏱ {t3}s" if t3 != "" else ""
st.markdown(f"**Step 3 — Answer Generation** `[AGENT span]`{t3_label}")
st.info(r["answer"])
ans_usage = token_usage.get("answer_generation", {})
if ans_usage.get("total_tokens"):
st.caption(
f"Tokens — prompt: {ans_usage['prompt_tokens']} / "
f"completion: {ans_usage['completion_tokens']} / "
f"total: {ans_usage['total_tokens']}"
)
if prompts.get("answer_generation"):
with st.expander("Prompt sent to LLM (answer generation)"):
st.code(prompts["answer_generation"], language="text")
# GPA scores
c1, c2, c3, c4 = st.columns(4)
c1.metric("Goal", f"{r['goal']:.2f}", help=r["goal_reason"])
c2.metric("Plan", f"{r['plan']:.2f}", help=r["plan_reason"])
c3.metric("Act", f"{r['act']:.2f}", help=r["act_reason"])
if timings.get("total_s"):
c4.metric("Total", f"{timings['total_s']}s")
# --- Dashboard UI ---
def main():
"""Render the Streamlit dashboard."""
st.title("AgentGPA Evaluation Dashboard")
st.markdown("**Internal Developer Assistant**")
st.markdown("*AI Agent for developer questions: docs, policies, calculations*")
st.divider()
# =========================================================
# Agent Architecture Diagram
# =========================================================
st.header("Agent Architecture")
mermaid_diagram = """\
flowchart LR
User([Developer Query])
LLM_Router["LLM Router\\n(Tool Selection Prompt)"]
DocSearch["documentation_search\\nInternal Docs"]
HRSearch["hr_policy_search\\nHR Policies"]
Calc["calculator\\nMath Engine"]
LLM_Answer["LLM Answer Generator\\n(Answer Generation Prompt)"]
Response([Agent Response])
User --> LLM_Router
LLM_Router -->|technical| DocSearch
LLM_Router -->|policy| HRSearch
LLM_Router -->|math| Calc
DocSearch --> LLM_Answer
HRSearch --> LLM_Answer
Calc --> Response
LLM_Answer --> Response
"""
st.components.v1.html(
f"""
<script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
<div class="mermaid">
{mermaid_diagram}
</div>
<script>
mermaid.initialize({{
startOnLoad: true,
theme: 'dark',
themeVariables: {{
primaryColor: '#5470c6',
primaryTextColor: '#fff',
lineColor: '#91cc75',
secondaryColor: '#fac858'
}}
}});
</script>
""",
height=250,
)
st.caption(
"LLM Router and Answer Generator are powered by Snowflake Cortex "
"(claude-sonnet-4-5). Calculator results bypass the Answer Generator."
)
st.divider()
data = _load_results()
if not data:
st.error(
"No evaluation results found. "
"Run `uv run python evaluation/trulens_eval.py` first."
)
return
v1_results = data["v1"]
v2_results = data["v2"]
# =========================================================
# SECTION 1: v1 - Initial Agent
# =========================================================
st.header("1. Agent v1 - Initial Prompts")
st.markdown("**System Prompts (v1):**")
col_tool, col_answer = st.columns(2)
with col_tool:
st.markdown("*Tool Selection:*")
st.code(
"Pick the best tool.\n"
"Available tools: {tool_names}\n\n"
"ROUTING RULES:\n"
"- APIs, requests, quotas → documentation_search\n"
"- Employee policies → hr_policy_search\n"
"- Only pure math (e.g. '2+2') → calculator",
language="text",
)
with col_answer:
st.markdown("*Answer Generation:*")
st.code(
"You are a senior developer assistant.\n"
"Answer using context as a starting point,\n"
"but ENRICH with industry best practices\n"
"and additional helpful details.\n\n"
"Provide a comprehensive, expert-level answer.",
language="text",
)
st.markdown("**v1 Scores:**")
v1_avg_goal = sum(r["goal"] for r in v1_results) / len(v1_results)
v1_avg_plan = sum(r["plan"] for r in v1_results) / len(v1_results)
v1_avg_act = sum(r["act"] for r in v1_results) / len(v1_results)
v1_gpa = (v1_avg_goal + v1_avg_plan + v1_avg_act) / 3
mc1, mc2, mc3, mc4 = st.columns(4)
mc1.metric("Goal", f"{v1_avg_goal:.2f}")
mc2.metric("Plan", f"{v1_avg_plan:.2f}")
mc3.metric("Act", f"{v1_avg_act:.2f}")
mc4.metric("GPA", f"{v1_gpa:.2f}")
st.altair_chart(_score_chart(v1_results), use_container_width=True)
# Show v1 case details
for r in v1_results:
gpa = (r["goal"] + r["plan"] + r["act"]) / 3
with st.expander(
f"{r['case_id'].upper()} - {r['description']} [GPA: {gpa:.2f}]"
):
st.markdown(f"**Query:** {r['query']}")
st.markdown(f"**Tool:** `{r['tool_used']}`")
st.markdown(f"**Answer:** {r['answer']}")
st.markdown(f"**Expected:** {r.get('expected_answer', 'N/A')}")
c1, c2, c3 = st.columns(3)
c1.metric("Goal", f"{r['goal']:.2f}", help=r["goal_reason"])
c2.metric("Plan", f"{r['plan']:.2f}", help=r["plan_reason"])
c3.metric("Act", f"{r['act']:.2f}", help=r["act_reason"])
st.divider()
# =========================================================
# SECTION 2: v2 - Improved Agent
# =========================================================
st.header("2. Agent v2 - Feedback Loop Improvements")
st.markdown("**Issues identified by AgentGPA on Case 3:**")
st.markdown(
"1. Knowledge base missing internal coding standards "
"→ LLM cannot find correct answer\n"
"2. Prompt says *'enrich with best practices'* "
"→ LLM fabricates PEP8 answer (4 spaces instead of our 2 spaces)"
)
st.markdown("**Fixes applied:**")
# Fix 1: Knowledge base
st.markdown("*Fix 1 - Knowledge base expansion:*")
st.markdown(
"""
<div style="font-family: monospace; font-size: 0.82em; line-height: 1.6; background: #1e1e1e; padding: 12px; border-radius: 6px;">
<span style="color: #3fb950;">+ doc-006: "Python Coding Standards"</span><br>
<span style="color: #3fb950;">+ "All Python code in our monorepo uses</span><br>
<span style="color: #3fb950;">+ 2-space indentation. This differs from</span><br>
<span style="color: #3fb950;">+ PEP8 (4 spaces) and is enforced by our</span><br>
<span style="color: #3fb950;">+ pre-commit hooks."</span><br>
</div>
""",
unsafe_allow_html=True,
)
# Fix 2: Prompt diff
st.markdown("*Fix 2 - Answer generation prompt (grounding constraint):*")
st.markdown(
"""
<div style="font-family: monospace; font-size: 0.82em; line-height: 1.6; background: #1e1e1e; padding: 12px; border-radius: 6px;">
<span style="color: #f85149;">- You are a senior developer assistant with</span><br>
<span style="color: #f85149;">- expertise in deployment, CI/CD...</span><br>
<span style="color: #f85149;">- Answer using context as a starting point,</span><br>
<span style="color: #f85149;">- but ENRICH with industry best practices</span><br>
<span style="color: #f85149;">- and additional helpful details.</span><br>
<span style="color: #f85149;">- Provide comprehensive, expert-level answer.</span><br>
<br>
<span style="color: #3fb950;">+ Answer using ONLY the context below.</span><br>
<span style="color: #3fb950;">+ Do NOT add information not in context.</span><br>
<span style="color: #3fb950;">+ If not enough info, say</span><br>
<span style="color: #3fb950;">+ "I don't have enough information".</span><br>
<span style="color: #3fb950;">+ Answer based strictly on context above.</span><br>
</div>
""",
unsafe_allow_html=True,
)
st.markdown("**v2 Scores:**")
v2_avg_goal = sum(r["goal"] for r in v2_results) / len(v2_results)
v2_avg_plan = sum(r["plan"] for r in v2_results) / len(v2_results)
v2_avg_act = sum(r["act"] for r in v2_results) / len(v2_results)
v2_gpa = (v2_avg_goal + v2_avg_plan + v2_avg_act) / 3
mc1, mc2, mc3, mc4 = st.columns(4)
mc1.metric("Goal", f"{v2_avg_goal:.2f}")
mc2.metric("Plan", f"{v2_avg_plan:.2f}")
mc3.metric("Act", f"{v2_avg_act:.2f}")
mc4.metric("GPA", f"{v2_gpa:.2f}")
st.altair_chart(_score_chart(v2_results), use_container_width=True)
# Show v2 case details
for r in v2_results:
gpa = (r["goal"] + r["plan"] + r["act"]) / 3
with st.expander(
f"{r['case_id'].upper()} - {r['description']} [GPA: {gpa:.2f}]"
):
st.markdown(f"**Query:** {r['query']}")
st.markdown(f"**Tool:** `{r['tool_used']}`")
st.markdown(f"**Answer:** {r['answer']}")
st.markdown(f"**Expected:** {r.get('expected_answer', 'N/A')}")
c1, c2, c3 = st.columns(3)
c1.metric("Goal", f"{r['goal']:.2f}", help=r["goal_reason"])
c2.metric("Plan", f"{r['plan']:.2f}", help=r["plan_reason"])
c3.metric("Act", f"{r['act']:.2f}", help=r["act_reason"])
st.divider()
# =========================================================
# SECTION 3: Comparison
# =========================================================
st.header("3. Comparison: v1 → v2")
col1, col2, col3 = st.columns(3)
col1.metric("v1 GPA", f"{v1_gpa:.2f}")
col2.metric("v2 GPA", f"{v2_gpa:.2f}")
col3.metric(
"Improvement",
f"{v2_gpa:.2f}",
delta=f"{v2_gpa - v1_gpa:+.2f}",
)
# Side-by-side chart
rows = []
for r in v1_results:
rows.append(
{
"Case": r["case_id"],
"Version": "v1",
"Goal": r["goal"],
"Plan": r["plan"],
"Act": r["act"],
}
)
for r in v2_results:
rows.append(
{
"Case": r["case_id"],
"Version": "v2",
"Goal": r["goal"],
"Plan": r["plan"],
"Act": r["act"],
}
)
df = pd.DataFrame(rows)
melted = df.melt(
id_vars=["Case", "Version"],
var_name="Metric",
value_name="Score",
)
melted["Group"] = melted["Version"] + " " + melted["Metric"]
chart = (
alt.Chart(melted)
.mark_bar()
.encode(
x=alt.X("Case:N", title="Case"),
y=alt.Y(
"Score:Q",
scale=alt.Scale(domain=[0, 1.05]),
title="Score",
),
color=alt.Color(
"Metric:N",
scale=alt.Scale(
domain=["Goal", "Plan", "Act"],
range=["#5470c6", "#91cc75", "#fac858"],
),
),
xOffset=alt.XOffset("Group:N"),
opacity=alt.condition(
alt.datum.Version == "v2",
alt.value(1.0),
alt.value(0.4),
),
tooltip=["Case", "Version", "Metric", "Score"],
)
.properties(height=350, width=600)
)
st.altair_chart(chart, use_container_width=True)
st.caption("Solid = v2 (improved) / Faded = v1 (original)")
# Key takeaway
st.markdown(
"""
**Key Finding:** Case 3 (Python indentation rule) — v1 hallucinates PEP8
(4 spaces) because the doc is missing AND the prompt says "enrich."
v2 fixes both: adds internal doc (2 spaces) + grounding constraint.
"""
)
st.divider()
# =========================================================
# SECTION 4: Observability
# =========================================================
st.header("4. Observability — Agent Trace")
st.markdown(
"Step-by-step execution trace showing which tool was selected, "
"what was retrieved, and how the answer was generated."
)
v1_by_case = {r["case_id"]: r for r in v1_results}
for r2 in v2_results:
r1 = v1_by_case.get(r2["case_id"])
with st.expander(
f"{r2['case_id'].upper()} — {r2['description']}",
expanded=(r2["case_id"] == "case_3"),
):
st.markdown(f"*Query:* {r2['query']}")
col_v1, col_v2 = st.columns(2)
if r1:
with col_v1:
_render_trace(r1)
with col_v2:
_render_trace(r2)
st.divider()
# --- Framework Reference ---
st.header("AgentGPA Framework")
st.markdown(
"""
| Dimension | Measures | Evaluation | Formal Metric |
|-----------|----------|-----------|---------------|
| **Goal** | User's intent achieved? | LLM: answer vs expected | 1A Answer Correctness |
| **Plan** | Right tool selected? | LLM: tool appropriateness | 4B Tool Selection |
| **Act** | Faithful to source? | LLM: groundedness | 1C Groundedness |
All metrics evaluated by **Snowflake Cortex** (claude-sonnet-4-5) as LLM-as-Judge.
"""
)
if __name__ == "__main__":
main()