Fix #7: Update dashboard.py — 3-column layout (LLM-Only / Basic RAG / GraphRAG), fix _get_demo_passages() query matching, add LLM-Judge + BERTScore display
Browse files- graphrag/dashboard.py +252 -145
graphrag/dashboard.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
"""
|
| 2 |
-
GraphRAG Comparison Dashboard — 4-Tab Gradio UI
|
| 3 |
-
================================================
|
| 4 |
-
Tab 1: Live Query Comparison
|
| 5 |
-
Tab 2: Batch Benchmark Results (HotpotQA)
|
| 6 |
Tab 3: Cost Analysis (projections + distributions)
|
| 7 |
Tab 4: Graph Explorer (interactive knowledge graph + reasoning paths)
|
| 8 |
|
| 9 |
-
|
| 10 |
"""
|
| 11 |
import json
|
| 12 |
import logging
|
|
@@ -23,7 +23,10 @@ from plotly.subplots import make_subplots
|
|
| 23 |
from graphrag.layers.graph_layer import GraphLayer
|
| 24 |
from graphrag.layers.llm_layer import LLMLayer
|
| 25 |
from graphrag.layers.orchestration_layer import InferenceOrchestrator, EmbeddingManager
|
| 26 |
-
from graphrag.layers.evaluation_layer import
|
|
|
|
|
|
|
|
|
|
| 27 |
from graphrag.benchmark import BenchmarkRunner
|
| 28 |
|
| 29 |
logger = logging.getLogger(__name__)
|
|
@@ -53,6 +56,13 @@ def initialize_system():
|
|
| 53 |
graph = GraphLayer()
|
| 54 |
tg_host = os.getenv("TG_HOST", "")
|
| 55 |
if tg_host:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
graph.connect()
|
| 57 |
|
| 58 |
orchestrator = InferenceOrchestrator(graph_layer=graph, llm_layer=llm, embedder=embedder)
|
|
@@ -64,91 +74,132 @@ def initialize_system():
|
|
| 64 |
|
| 65 |
benchmark_runner = BenchmarkRunner(orchestrator, evaluator)
|
| 66 |
_initialized = True
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
-
# ── Tab 1: Live
|
| 71 |
|
| 72 |
def run_live_comparison(query, enable_adaptive, top_k, hops):
|
|
|
|
| 73 |
if not query.strip():
|
| 74 |
-
return ("
|
| 75 |
if not _initialized:
|
| 76 |
initialize_system()
|
| 77 |
|
| 78 |
try:
|
| 79 |
passages = _get_demo_passages(query)
|
| 80 |
-
if enable_adaptive:
|
| 81 |
-
comparison = orchestrator.run_adaptive(query, passages)
|
| 82 |
-
else:
|
| 83 |
-
comparison = orchestrator.run_comparison(query, passages, int(top_k), int(hops))
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
f"**[{i+1}]:** {c[:300]}{'...' if len(c) > 300 else ''}"
|
| 90 |
-
for i, c in enumerate(b.contexts[:5])
|
| 91 |
-
]) or "No contexts."
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
entities_display = ""
|
| 99 |
if g.entities_found:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if g.relations_traversed:
|
| 104 |
entities_display += "\n\n**Relationships:**\n" + "\n".join(
|
| 105 |
[f"- 🔗 {r}" for r in g.relations_traversed[:8]])
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
def _get_demo_passages(query):
|
|
|
|
| 125 |
try:
|
| 126 |
from datasets import load_dataset
|
| 127 |
ds = load_dataset("hotpotqa/hotpot_qa", "distractor", split="validation", streaming=True)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return [f"{t}: {' '.join(s)}"
|
| 130 |
for t, s in zip(row["context"]["title"], row["context"]["sentences"])]
|
| 131 |
-
except Exception:
|
| 132 |
-
|
| 133 |
-
return [
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
-
def
|
|
|
|
| 139 |
fig = make_subplots(rows=1, cols=3, subplot_titles=("Tokens", "Latency (ms)", "Cost ($)"),
|
| 140 |
horizontal_spacing=0.12)
|
| 141 |
-
colors = ["#3498db", "#e74c3c"]
|
| 142 |
-
methods = ["
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
fig.add_trace(go.Bar(
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
fig.update_layout(height=350, margin=dict(t=40, b=20, l=20, r=20),
|
| 153 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 154 |
return fig
|
|
@@ -167,7 +218,7 @@ def run_batch_benchmark(num_samples, top_k, hops, progress=gr.Progress()):
|
|
| 167 |
try:
|
| 168 |
results = benchmark_runner.run_hotpotqa_benchmark(
|
| 169 |
num_samples=int(num_samples), top_k=int(top_k), hops=int(hops),
|
| 170 |
-
progress_callback=progress_cb)
|
| 171 |
_benchmark_results = results.get("results", [])
|
| 172 |
agg = results.get("aggregate", {})
|
| 173 |
report = results.get("report", "")
|
|
@@ -175,55 +226,77 @@ def run_batch_benchmark(num_samples, top_k, hops, progress=gr.Progress()):
|
|
| 175 |
if not _benchmark_results:
|
| 176 |
return "No results.", None, None, None, report
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
summary = pd.DataFrame({
|
| 179 |
-
"Metric": ["Avg F1", "Avg EM", "
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
f"{
|
| 183 |
-
f"{
|
| 184 |
-
f"{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
"GraphRAG": [
|
| 186 |
-
f"{
|
| 187 |
-
f"{
|
| 188 |
-
f"{
|
| 189 |
-
f"{
|
| 190 |
})
|
| 191 |
|
| 192 |
bar_fig = _build_benchmark_bar(agg)
|
| 193 |
radar_fig = _build_radar(agg)
|
| 194 |
-
return (f"✅ Done! {len(_benchmark_results)} samples
|
|
|
|
| 195 |
except Exception as e:
|
|
|
|
| 196 |
return f"❌ Error: {e}", None, None, None, ""
|
| 197 |
|
| 198 |
|
| 199 |
def _build_benchmark_bar(agg):
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
fig = go.Figure(data=[
|
| 204 |
-
go.Bar(name="
|
| 205 |
-
text=[f"{v:.3f}" for v in
|
| 206 |
-
go.Bar(name="
|
| 207 |
-
text=[f"{v:.3f}" for v in
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 210 |
return fig
|
| 211 |
|
| 212 |
|
| 213 |
def _build_radar(agg):
|
| 214 |
-
b
|
|
|
|
| 215 |
cats = ["F1", "EM", "Context Hit", "Token Eff.", "Cost Eff."]
|
| 216 |
-
te = min(b
|
| 217 |
-
ce = min(b
|
| 218 |
-
bv = [b
|
| 219 |
-
gv = [g
|
| 220 |
fig = go.Figure()
|
| 221 |
fig.add_trace(go.Scatterpolar(r=bv+[bv[0]], theta=cats+[cats[0]], fill='toself',
|
| 222 |
-
name='
|
| 223 |
fig.add_trace(go.Scatterpolar(r=gv+[gv[0]], theta=cats+[cats[0]], fill='toself',
|
| 224 |
name='GraphRAG', line_color='#e74c3c', opacity=0.6))
|
| 225 |
fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[0, 1.2])),
|
| 226 |
-
title="
|
|
|
|
| 227 |
return fig
|
| 228 |
|
| 229 |
|
|
@@ -241,41 +314,52 @@ def compute_cost_analysis(num_queries, model):
|
|
| 241 |
n = int(num_queries)
|
| 242 |
|
| 243 |
if _benchmark_results:
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
| 248 |
else:
|
| 249 |
-
ab, ag = 950, 2400
|
|
|
|
| 250 |
acb = (800/1000*p["input"] + 150/1000*p["output"])
|
| 251 |
acg = (2200/1000*p["input"] + 200/1000*p["output"])
|
| 252 |
|
| 253 |
summary = pd.DataFrame({
|
| 254 |
"Metric": ["Avg Tokens", "Cost/Query", f"Total ({n:,}q)", "Monthly (1K qpd)", "Annual"],
|
| 255 |
-
"
|
|
|
|
| 256 |
"GraphRAG": [f"{ag:.0f}", f"${acg:.6f}", f"${acg*n:.4f}", f"${acg*1000*30:.2f}", f"${acg*1000*365:.2f}"],
|
| 257 |
-
"Ratio": [f"{ag/max(ab,1):.2f}x"]*5
|
| 258 |
})
|
| 259 |
|
| 260 |
qr = list(range(0, n+1, max(n//50, 1)))
|
| 261 |
fig_cum = go.Figure()
|
| 262 |
-
fig_cum.add_trace(go.Scatter(x=qr, y=[
|
|
|
|
|
|
|
| 263 |
line=dict(color='#3498db', width=3)))
|
| 264 |
fig_cum.add_trace(go.Scatter(x=qr, y=[acg*q for q in qr], mode='lines', name='GraphRAG',
|
| 265 |
line=dict(color='#e74c3c', width=3)))
|
| 266 |
-
fig_cum.update_layout(title=f"Cumulative Cost ({model})",
|
| 267 |
-
|
|
|
|
| 268 |
|
| 269 |
fig_tok = go.Figure()
|
| 270 |
if _benchmark_results:
|
| 271 |
-
fig_tok.add_trace(go.Histogram(
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 277 |
else:
|
| 278 |
-
fig_tok.add_annotation(text="Run benchmark first for distribution", showarrow=False)
|
| 279 |
|
| 280 |
return summary, fig_cum, fig_tok
|
| 281 |
|
|
@@ -292,8 +376,11 @@ def explore_graph(query, depth):
|
|
| 292 |
|
| 293 |
G = nx.Graph()
|
| 294 |
for e in gr_result.entities_found[:20]:
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
| 297 |
for r in gr_result.relations_traversed[:30]:
|
| 298 |
parts = r.split(" -[")
|
| 299 |
if len(parts) == 2:
|
|
@@ -307,8 +394,10 @@ def explore_graph(query, depth):
|
|
| 307 |
if not G.nodes():
|
| 308 |
G.add_node("Query", entity_type="QUERY")
|
| 309 |
for e in gr_result.entities_found[:5]:
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
| 312 |
|
| 313 |
pos = nx.spring_layout(G, k=2, iterations=50, seed=42)
|
| 314 |
colors_map = {"PERSON": "#FF6B6B", "ORGANIZATION": "#4ECDC4", "LOCATION": "#45B7D1",
|
|
@@ -338,7 +427,8 @@ def explore_graph(query, depth):
|
|
| 338 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 339 |
|
| 340 |
info = {"nodes": len(G.nodes()), "edges": len(G.edges()),
|
| 341 |
-
"entities": len(gr_result.entities_found), "relations": len(gr_result.relations_traversed)
|
|
|
|
| 342 |
stats = pd.DataFrame({"Metric": ["Nodes", "Edges", "Avg Degree", "Density", "Entities", "Relations"],
|
| 343 |
"Value": [len(G.nodes()), len(G.edges()),
|
| 344 |
f"{sum(d for _,d in G.degree())/max(len(G.nodes()),1):.1f}",
|
|
@@ -348,6 +438,7 @@ def explore_graph(query, depth):
|
|
| 348 |
explanation = orchestrator.explain_graphrag_reasoning(query, gr_result)
|
| 349 |
return fig, info, stats, explanation, gr_result.answer
|
| 350 |
except Exception as e:
|
|
|
|
| 351 |
empty = go.Figure()
|
| 352 |
empty.add_annotation(text=str(e), showarrow=False)
|
| 353 |
return empty, {}, pd.DataFrame(), str(e), ""
|
|
@@ -356,12 +447,12 @@ def explore_graph(query, depth):
|
|
| 356 |
# ── Build Dashboard ───────────────────────────────────────
|
| 357 |
|
| 358 |
def build_dashboard():
|
| 359 |
-
with gr.Blocks(title="GraphRAG
|
| 360 |
gr.Markdown("""
|
| 361 |
-
# 🔍 GraphRAG Inference Hackathon — Comparison Dashboard
|
| 362 |
-
###
|
| 363 |
-
**
|
| 364 |
-
|
| 365 |
""")
|
| 366 |
|
| 367 |
with gr.Row():
|
|
@@ -370,99 +461,115 @@ def build_dashboard():
|
|
| 370 |
init_btn.click(fn=initialize_system, outputs=init_status)
|
| 371 |
|
| 372 |
with gr.Tabs():
|
| 373 |
-
# ── Tab 1: Live Comparison ───────
|
| 374 |
-
with gr.Tab("🔴 Live
|
| 375 |
-
gr.Markdown("## Side-by-Side
|
| 376 |
with gr.Row():
|
| 377 |
-
query_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 378 |
with gr.Column(scale=1):
|
| 379 |
adaptive = gr.Checkbox(label="🧠 Adaptive Routing", value=True)
|
| 380 |
topk = gr.Slider(1, 10, value=5, step=1, label="Top-K")
|
| 381 |
hops_s = gr.Slider(1, 4, value=2, step=1, label="Hops")
|
| 382 |
|
| 383 |
-
run_btn = gr.Button("▶ Run
|
| 384 |
status = gr.Textbox(label="Status", interactive=False)
|
| 385 |
routing = gr.Markdown(visible=True)
|
| 386 |
|
| 387 |
with gr.Row():
|
| 388 |
with gr.Column():
|
| 389 |
-
gr.Markdown("###
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
with gr.Row():
|
| 392 |
b_tok = gr.Number(label="Tokens", precision=0)
|
| 393 |
b_lat = gr.Number(label="Latency (ms)", precision=1)
|
| 394 |
b_cost = gr.Number(label="Cost ($)", precision=6)
|
| 395 |
with gr.Column():
|
| 396 |
-
gr.Markdown("### 🔴 GraphRAG")
|
| 397 |
-
g_ans = gr.Textbox(label="Answer", lines=
|
| 398 |
with gr.Row():
|
| 399 |
g_tok = gr.Number(label="Tokens", precision=0)
|
| 400 |
g_lat = gr.Number(label="Latency (ms)", precision=1)
|
| 401 |
g_cost = gr.Number(label="Cost ($)", precision=6)
|
| 402 |
|
| 403 |
-
chart = gr.Plot(label="Comparison")
|
| 404 |
-
with gr.Accordion("📄 Retrieved Contexts", open=False):
|
| 405 |
with gr.Row():
|
| 406 |
-
b_ctx = gr.Markdown()
|
| 407 |
-
g_ctx = gr.Markdown()
|
| 408 |
-
with gr.Accordion("🕸️ Entities &
|
| 409 |
ent_disp = gr.Markdown()
|
| 410 |
|
| 411 |
-
run_btn.click(
|
| 412 |
-
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
gr.Examples(examples=[
|
| 415 |
["Were Scott Derrickson and Ed Wood of the same nationality?"],
|
| 416 |
["What government position was held by the woman who portrayed Nora Batty?"],
|
| 417 |
["Which magazine was started first, Arthur's Magazine or First for Women?"],
|
| 418 |
["Who was born first, Arthur Conan Doyle or Agatha Christie?"],
|
| 419 |
["What is the capital of the country where the Eiffel Tower is located?"]],
|
| 420 |
-
inputs=query_input, label="📝 Example Questions")
|
| 421 |
|
| 422 |
# ── Tab 2: Batch Benchmark ──────────────────
|
| 423 |
-
with gr.Tab("📊 Batch Benchmark"):
|
| 424 |
-
gr.Markdown("## Benchmark on HotpotQA")
|
| 425 |
with gr.Row():
|
| 426 |
n_samples = gr.Slider(10, 500, value=50, step=10, label="Samples")
|
| 427 |
bk = gr.Slider(1, 10, value=5, step=1, label="Top-K")
|
| 428 |
bh = gr.Slider(1, 4, value=2, step=1, label="Hops")
|
| 429 |
-
bench_btn = gr.Button("🏃 Run Benchmark", variant="primary")
|
| 430 |
bench_status = gr.Textbox(label="Status", interactive=False)
|
| 431 |
-
summary_df = gr.Dataframe(label="Summary")
|
| 432 |
with gr.Row():
|
| 433 |
-
bar_chart = gr.Plot(label="Quality")
|
| 434 |
-
radar_chart = gr.Plot(label="Radar")
|
| 435 |
with gr.Accordion("📝 Full Report", open=False):
|
| 436 |
report = gr.Textbox(lines=30, interactive=False)
|
| 437 |
bench_btn.click(fn=run_batch_benchmark, inputs=[n_samples, bk, bh],
|
| 438 |
outputs=[bench_status, summary_df, bar_chart, radar_chart, report])
|
| 439 |
|
| 440 |
# ── Tab 3: Cost Analysis ────────────────────
|
| 441 |
-
with gr.Tab("💰 Cost Analysis"):
|
| 442 |
-
gr.Markdown("## Cost & Token Analysis")
|
| 443 |
with gr.Row():
|
| 444 |
cq = gr.Slider(100, 100000, value=10000, step=100, label="Queries to Project")
|
| 445 |
-
cm = gr.Dropdown(["gpt-4o-mini", "gpt-4o", "gpt-3.5-turbo",
|
|
|
|
| 446 |
value="gpt-4o-mini", label="Model")
|
| 447 |
cost_btn = gr.Button("💵 Calculate", variant="primary")
|
| 448 |
-
cost_df = gr.Dataframe(label="Breakdown")
|
| 449 |
with gr.Row():
|
| 450 |
-
cum_chart = gr.Plot(label="Cumulative Cost")
|
| 451 |
tok_chart = gr.Plot(label="Token Distribution")
|
| 452 |
cost_btn.click(fn=compute_cost_analysis, inputs=[cq, cm],
|
| 453 |
outputs=[cost_df, cum_chart, tok_chart])
|
| 454 |
|
| 455 |
# ── Tab 4: Graph Explorer ───────────────────
|
| 456 |
with gr.Tab("🕸️ Graph Explorer"):
|
| 457 |
-
gr.Markdown("## Interactive Knowledge Graph Explorer\n*Visualize how GraphRAG traverses the graph*")
|
| 458 |
with gr.Row():
|
| 459 |
gq = gr.Textbox(label="Query", placeholder="Enter a question...", scale=3)
|
| 460 |
gd = gr.Slider(1, 4, value=2, step=1, label="Depth", scale=1)
|
| 461 |
exp_btn = gr.Button("🔍 Explore", variant="primary", scale=1)
|
| 462 |
graph_plot = gr.Plot(label="Knowledge Graph")
|
| 463 |
with gr.Row():
|
| 464 |
-
graph_stats = gr.Dataframe(label="Stats")
|
| 465 |
-
node_info = gr.JSON(label="Details")
|
| 466 |
with gr.Accordion("🧠 Reasoning Path", open=True):
|
| 467 |
reasoning = gr.Markdown()
|
| 468 |
graph_ans = gr.Textbox(label="GraphRAG Answer", interactive=False)
|
|
@@ -476,8 +583,8 @@ def build_dashboard():
|
|
| 476 |
|
| 477 |
gr.Markdown("""
|
| 478 |
---
|
| 479 |
-
**GraphRAG Inference Hackathon** by TigerGraph |
|
| 480 |
-
**
|
| 481 |
""")
|
| 482 |
return demo
|
| 483 |
|
|
|
|
| 1 |
"""
|
| 2 |
+
GraphRAG Comparison Dashboard — 4-Tab Gradio UI (3-Pipeline)
|
| 3 |
+
=============================================================
|
| 4 |
+
Tab 1: Live Query Comparison — 3 pipelines side-by-side
|
| 5 |
+
Tab 2: Batch Benchmark Results (HotpotQA) — all 3 pipelines
|
| 6 |
Tab 3: Cost Analysis (projections + distributions)
|
| 7 |
Tab 4: Graph Explorer (interactive knowledge graph + reasoning paths)
|
| 8 |
|
| 9 |
+
Hackathon requirement: "one query in, all 3 pipelines run, side-by-side responses + metrics out"
|
| 10 |
"""
|
| 11 |
import json
|
| 12 |
import logging
|
|
|
|
| 23 |
from graphrag.layers.graph_layer import GraphLayer
|
| 24 |
from graphrag.layers.llm_layer import LLMLayer
|
| 25 |
from graphrag.layers.orchestration_layer import InferenceOrchestrator, EmbeddingManager
|
| 26 |
+
from graphrag.layers.evaluation_layer import (
|
| 27 |
+
EvaluationLayer, EvalSample, compute_f1, compute_exact_match,
|
| 28 |
+
compute_llm_judge, compute_bertscore,
|
| 29 |
+
)
|
| 30 |
from graphrag.benchmark import BenchmarkRunner
|
| 31 |
|
| 32 |
logger = logging.getLogger(__name__)
|
|
|
|
| 56 |
graph = GraphLayer()
|
| 57 |
tg_host = os.getenv("TG_HOST", "")
|
| 58 |
if tg_host:
|
| 59 |
+
graph_cfg = {
|
| 60 |
+
"host": tg_host,
|
| 61 |
+
"graphname": os.getenv("TG_GRAPH", "GraphRAG"),
|
| 62 |
+
"username": os.getenv("TG_USERNAME", "tigergraph"),
|
| 63 |
+
"password": os.getenv("TG_PASSWORD", ""),
|
| 64 |
+
}
|
| 65 |
+
graph = GraphLayer(config=graph_cfg)
|
| 66 |
graph.connect()
|
| 67 |
|
| 68 |
orchestrator = InferenceOrchestrator(graph_layer=graph, llm_layer=llm, embedder=embedder)
|
|
|
|
| 74 |
|
| 75 |
benchmark_runner = BenchmarkRunner(orchestrator, evaluator)
|
| 76 |
_initialized = True
|
| 77 |
+
mode = "TigerGraph" if graph.is_connected else "Offline (passage-based)"
|
| 78 |
+
return f"✅ System initialized! LLM: {llm.model} | Graph: {mode}"
|
| 79 |
|
| 80 |
|
| 81 |
+
# ── Tab 1: Live 3-Pipeline Comparison ─────────────────────
|
| 82 |
|
| 83 |
def run_live_comparison(query, enable_adaptive, top_k, hops):
|
| 84 |
+
"""Run all 3 pipelines on a single query and return side-by-side results."""
|
| 85 |
if not query.strip():
|
| 86 |
+
return ("Enter a query.", "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, None, "", "", "")
|
| 87 |
if not _initialized:
|
| 88 |
initialize_system()
|
| 89 |
|
| 90 |
try:
|
| 91 |
passages = _get_demo_passages(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Run all 3 pipelines
|
| 94 |
+
lo = orchestrator.run_llm_only(query)
|
| 95 |
+
b = orchestrator.run_baseline_rag(query, passages, int(top_k))
|
| 96 |
+
g = orchestrator.run_graphrag(query, passages, hops=int(hops))
|
| 97 |
|
| 98 |
+
fig = _build_triple_chart(lo, b, g)
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Routing info
|
| 101 |
+
routing_info = ""
|
| 102 |
+
if enable_adaptive:
|
| 103 |
+
score, qtype, reasoning = orchestrator.analyze_complexity(query)
|
| 104 |
+
recommended = "GraphRAG" if score >= 0.6 else "Basic RAG"
|
| 105 |
+
routing_info = (
|
| 106 |
+
f"**🧠 Adaptive Routing:**\n"
|
| 107 |
+
f"- Complexity: {score:.2f} | Type: {qtype}\n"
|
| 108 |
+
f"- Recommended: **{recommended}**\n"
|
| 109 |
+
f"- {reasoning}")
|
| 110 |
|
| 111 |
entities_display = ""
|
| 112 |
if g.entities_found:
|
| 113 |
+
ent_list = g.entities_found[:8]
|
| 114 |
+
if isinstance(ent_list[0], dict):
|
| 115 |
+
entities_display = "**Entities Found:**\n" + "\n".join(
|
| 116 |
+
[f"- 🔵 **{e.get('name','N/A')}** ({e.get('entity_type','N/A')})"
|
| 117 |
+
for e in ent_list])
|
| 118 |
+
else:
|
| 119 |
+
entities_display = "**Entities Found:**\n" + "\n".join(
|
| 120 |
+
[f"- 🔵 {e}" for e in ent_list])
|
| 121 |
if g.relations_traversed:
|
| 122 |
entities_display += "\n\n**Relationships:**\n" + "\n".join(
|
| 123 |
[f"- 🔗 {r}" for r in g.relations_traversed[:8]])
|
| 124 |
+
if g.novelty_chain:
|
| 125 |
+
entities_display += "\n\n**Novelty Chain:**\n" + "\n".join(
|
| 126 |
+
[f"- ⚡ {step}" for step in g.novelty_chain])
|
| 127 |
|
| 128 |
+
baseline_ctx = "\n\n---\n\n".join([
|
| 129 |
+
f"**[{i+1}]:** {c[:300]}{'...' if len(c) > 300 else ''}"
|
| 130 |
+
for i, c in enumerate(b.contexts[:5])
|
| 131 |
+
]) or "No contexts retrieved."
|
| 132 |
+
|
| 133 |
+
graphrag_ctx = "\n\n---\n\n".join([
|
| 134 |
+
f"**[{i+1}]:** {c[:300]}{'...' if len(c) > 300 else ''}"
|
| 135 |
+
for i, c in enumerate(g.contexts[:5])
|
| 136 |
+
]) or "No contexts retrieved."
|
| 137 |
+
|
| 138 |
+
return (
|
| 139 |
+
"✅ All 3 pipelines complete!",
|
| 140 |
+
lo.answer, b.answer, g.answer, routing_info,
|
| 141 |
+
lo.total_tokens, b.total_tokens, g.total_tokens,
|
| 142 |
+
round(lo.latency_ms, 1), round(b.latency_ms, 1), round(g.latency_ms, 1),
|
| 143 |
+
round(lo.cost_usd, 6), round(b.cost_usd, 6), round(g.cost_usd, 6),
|
| 144 |
+
fig, baseline_ctx, graphrag_ctx, entities_display,
|
| 145 |
+
)
|
| 146 |
except Exception as e:
|
| 147 |
+
logger.error(f"Live comparison error: {e}", exc_info=True)
|
| 148 |
+
return (f"❌ Error: {e}", "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, None, "", "", "")
|
| 149 |
|
| 150 |
|
| 151 |
def _get_demo_passages(query):
|
| 152 |
+
"""Get passages matching the query from HotpotQA. Falls back to first row if no match."""
|
| 153 |
try:
|
| 154 |
from datasets import load_dataset
|
| 155 |
ds = load_dataset("hotpotqa/hotpot_qa", "distractor", split="validation", streaming=True)
|
| 156 |
+
query_lower = query.lower().strip().rstrip("?").strip()
|
| 157 |
+
|
| 158 |
+
# Try to find matching question
|
| 159 |
+
for i, row in enumerate(ds):
|
| 160 |
+
row_q = row["question"].lower().strip().rstrip("?").strip()
|
| 161 |
+
if query_lower == row_q or query_lower in row_q or row_q in query_lower:
|
| 162 |
+
return [f"{t}: {' '.join(s)}"
|
| 163 |
+
for t, s in zip(row["context"]["title"], row["context"]["sentences"])]
|
| 164 |
+
if i > 200: # don't scan entire dataset
|
| 165 |
+
break
|
| 166 |
+
|
| 167 |
+
# Fallback: return first row's passages
|
| 168 |
+
ds2 = load_dataset("hotpotqa/hotpot_qa", "distractor", split="validation", streaming=True)
|
| 169 |
+
for row in ds2:
|
| 170 |
return [f"{t}: {' '.join(s)}"
|
| 171 |
for t, s in zip(row["context"]["title"], row["context"]["sentences"])]
|
| 172 |
+
except Exception as e:
|
| 173 |
+
logger.warning(f"Could not load HotpotQA: {e}")
|
| 174 |
+
return [
|
| 175 |
+
"Demo passage. Connect TigerGraph for full graph-powered retrieval.",
|
| 176 |
+
"GraphRAG extracts entities and relationships for better multi-hop retrieval.",
|
| 177 |
+
"The system supports LLM-Only, Basic RAG, and GraphRAG pipelines.",
|
| 178 |
+
]
|
| 179 |
|
| 180 |
|
| 181 |
+
def _build_triple_chart(llm_only, baseline, graphrag):
|
| 182 |
+
"""Build 3-pipeline comparison bar chart."""
|
| 183 |
fig = make_subplots(rows=1, cols=3, subplot_titles=("Tokens", "Latency (ms)", "Cost ($)"),
|
| 184 |
horizontal_spacing=0.12)
|
| 185 |
+
colors = ["#95a5a6", "#3498db", "#e74c3c"]
|
| 186 |
+
methods = ["LLM-Only", "Basic RAG", "GraphRAG"]
|
| 187 |
+
|
| 188 |
+
fig.add_trace(go.Bar(
|
| 189 |
+
x=methods, y=[llm_only.total_tokens, baseline.total_tokens, graphrag.total_tokens],
|
| 190 |
+
marker_color=colors,
|
| 191 |
+
text=[llm_only.total_tokens, baseline.total_tokens, graphrag.total_tokens],
|
| 192 |
+
textposition='auto', showlegend=False), row=1, col=1)
|
| 193 |
+
fig.add_trace(go.Bar(
|
| 194 |
+
x=methods, y=[llm_only.latency_ms, baseline.latency_ms, graphrag.latency_ms],
|
| 195 |
+
marker_color=colors,
|
| 196 |
+
text=[f"{llm_only.latency_ms:.0f}", f"{baseline.latency_ms:.0f}", f"{graphrag.latency_ms:.0f}"],
|
| 197 |
+
textposition='auto', showlegend=False), row=1, col=2)
|
| 198 |
+
fig.add_trace(go.Bar(
|
| 199 |
+
x=methods, y=[llm_only.cost_usd, baseline.cost_usd, graphrag.cost_usd],
|
| 200 |
+
marker_color=colors,
|
| 201 |
+
text=[f"${llm_only.cost_usd:.6f}", f"${baseline.cost_usd:.6f}", f"${graphrag.cost_usd:.6f}"],
|
| 202 |
+
textposition='auto', showlegend=False), row=1, col=3)
|
| 203 |
fig.update_layout(height=350, margin=dict(t=40, b=20, l=20, r=20),
|
| 204 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 205 |
return fig
|
|
|
|
| 218 |
try:
|
| 219 |
results = benchmark_runner.run_hotpotqa_benchmark(
|
| 220 |
num_samples=int(num_samples), top_k=int(top_k), hops=int(hops),
|
| 221 |
+
progress_callback=progress_cb, run_judge=True, run_bertscore=False)
|
| 222 |
_benchmark_results = results.get("results", [])
|
| 223 |
agg = results.get("aggregate", {})
|
| 224 |
report = results.get("report", "")
|
|
|
|
| 226 |
if not _benchmark_results:
|
| 227 |
return "No results.", None, None, None, report
|
| 228 |
|
| 229 |
+
lo = agg.get("llm_only", {})
|
| 230 |
+
b = agg.get("baseline", {})
|
| 231 |
+
g = agg.get("graphrag", {})
|
| 232 |
+
|
| 233 |
summary = pd.DataFrame({
|
| 234 |
+
"Metric": ["Avg F1", "Avg EM", "LLM-Judge Pass%", "Avg Tokens",
|
| 235 |
+
"Avg Cost ($)", "Avg Latency (ms)"],
|
| 236 |
+
"LLM-Only": [
|
| 237 |
+
f"{lo.get('avg_f1', 0):.4f}", f"{lo.get('avg_em', 0):.4f}",
|
| 238 |
+
f"{lo.get('judge_pass_rate', 0):.1%}",
|
| 239 |
+
f"{lo.get('avg_tokens', 0):.0f}", f"${lo.get('avg_cost', 0):.6f}",
|
| 240 |
+
f"{lo.get('avg_latency_ms', 0):.0f}"],
|
| 241 |
+
"Basic RAG": [
|
| 242 |
+
f"{b.get('avg_f1', 0):.4f}", f"{b.get('avg_em', 0):.4f}",
|
| 243 |
+
f"{b.get('judge_pass_rate', 0):.1%}",
|
| 244 |
+
f"{b.get('avg_tokens', 0):.0f}", f"${b.get('avg_cost', 0):.6f}",
|
| 245 |
+
f"{b.get('avg_latency_ms', 0):.0f}"],
|
| 246 |
"GraphRAG": [
|
| 247 |
+
f"{g.get('avg_f1', 0):.4f}", f"{g.get('avg_em', 0):.4f}",
|
| 248 |
+
f"{g.get('judge_pass_rate', 0):.1%}",
|
| 249 |
+
f"{g.get('avg_tokens', 0):.0f}", f"${g.get('avg_cost', 0):.6f}",
|
| 250 |
+
f"{g.get('avg_latency_ms', 0):.0f}"],
|
| 251 |
})
|
| 252 |
|
| 253 |
bar_fig = _build_benchmark_bar(agg)
|
| 254 |
radar_fig = _build_radar(agg)
|
| 255 |
+
return (f"✅ Done! {len(_benchmark_results)} samples evaluated across 3 pipelines.",
|
| 256 |
+
summary, bar_fig, radar_fig, report)
|
| 257 |
except Exception as e:
|
| 258 |
+
logger.error(f"Benchmark error: {e}", exc_info=True)
|
| 259 |
return f"❌ Error: {e}", None, None, None, ""
|
| 260 |
|
| 261 |
|
| 262 |
def _build_benchmark_bar(agg):
|
| 263 |
+
lo = agg.get("llm_only", {})
|
| 264 |
+
b = agg.get("baseline", {})
|
| 265 |
+
g = agg.get("graphrag", {})
|
| 266 |
+
metrics = ["F1", "EM", "Judge Pass%"]
|
| 267 |
+
lo_vals = [lo.get("avg_f1", 0), lo.get("avg_em", 0), lo.get("judge_pass_rate", 0)]
|
| 268 |
+
b_vals = [b.get("avg_f1", 0), b.get("avg_em", 0), b.get("judge_pass_rate", 0)]
|
| 269 |
+
g_vals = [g.get("avg_f1", 0), g.get("avg_em", 0), g.get("judge_pass_rate", 0)]
|
| 270 |
fig = go.Figure(data=[
|
| 271 |
+
go.Bar(name="LLM-Only", x=metrics, y=lo_vals, marker_color="#95a5a6",
|
| 272 |
+
text=[f"{v:.3f}" for v in lo_vals], textposition='auto'),
|
| 273 |
+
go.Bar(name="Basic RAG", x=metrics, y=b_vals, marker_color="#3498db",
|
| 274 |
+
text=[f"{v:.3f}" for v in b_vals], textposition='auto'),
|
| 275 |
+
go.Bar(name="GraphRAG", x=metrics, y=g_vals, marker_color="#e74c3c",
|
| 276 |
+
text=[f"{v:.3f}" for v in g_vals], textposition='auto'),
|
| 277 |
+
])
|
| 278 |
+
fig.update_layout(barmode='group', title="Answer Quality (3 Pipelines)",
|
| 279 |
+
yaxis_title="Score", height=400,
|
| 280 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 281 |
return fig
|
| 282 |
|
| 283 |
|
| 284 |
def _build_radar(agg):
|
| 285 |
+
b = agg.get("baseline", {})
|
| 286 |
+
g = agg.get("graphrag", {})
|
| 287 |
cats = ["F1", "EM", "Context Hit", "Token Eff.", "Cost Eff."]
|
| 288 |
+
te = min(b.get("avg_tokens", 1) / max(g.get("avg_tokens", 1), 1), 2.0)
|
| 289 |
+
ce = min(b.get("avg_cost", 0.001) / max(g.get("avg_cost", 0.000001), 0.000001), 2.0)
|
| 290 |
+
bv = [b.get("avg_f1", 0), b.get("avg_em", 0), b.get("avg_context_hit", 0), 1.0, 1.0]
|
| 291 |
+
gv = [g.get("avg_f1", 0), g.get("avg_em", 0), g.get("avg_context_hit", 0), te, ce]
|
| 292 |
fig = go.Figure()
|
| 293 |
fig.add_trace(go.Scatterpolar(r=bv+[bv[0]], theta=cats+[cats[0]], fill='toself',
|
| 294 |
+
name='Basic RAG', line_color='#3498db', opacity=0.6))
|
| 295 |
fig.add_trace(go.Scatterpolar(r=gv+[gv[0]], theta=cats+[cats[0]], fill='toself',
|
| 296 |
name='GraphRAG', line_color='#e74c3c', opacity=0.6))
|
| 297 |
fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[0, 1.2])),
|
| 298 |
+
title="GraphRAG vs Basic RAG Radar", height=450,
|
| 299 |
+
paper_bgcolor='rgba(0,0,0,0)')
|
| 300 |
return fig
|
| 301 |
|
| 302 |
|
|
|
|
| 314 |
n = int(num_queries)
|
| 315 |
|
| 316 |
if _benchmark_results:
|
| 317 |
+
al = sum(r.get("llm_only_tokens", 0) for r in _benchmark_results) / len(_benchmark_results)
|
| 318 |
+
ab = sum(r.get("baseline_tokens", 0) for r in _benchmark_results) / len(_benchmark_results)
|
| 319 |
+
ag = sum(r.get("graphrag_tokens", 0) for r in _benchmark_results) / len(_benchmark_results)
|
| 320 |
+
acl = sum(r.get("llm_only_cost", 0) for r in _benchmark_results) / len(_benchmark_results)
|
| 321 |
+
acb = sum(r.get("baseline_cost", 0) for r in _benchmark_results) / len(_benchmark_results)
|
| 322 |
+
acg = sum(r.get("graphrag_cost", 0) for r in _benchmark_results) / len(_benchmark_results)
|
| 323 |
else:
|
| 324 |
+
al, ab, ag = 500, 950, 2400
|
| 325 |
+
acl = (400/1000*p["input"] + 100/1000*p["output"])
|
| 326 |
acb = (800/1000*p["input"] + 150/1000*p["output"])
|
| 327 |
acg = (2200/1000*p["input"] + 200/1000*p["output"])
|
| 328 |
|
| 329 |
summary = pd.DataFrame({
|
| 330 |
"Metric": ["Avg Tokens", "Cost/Query", f"Total ({n:,}q)", "Monthly (1K qpd)", "Annual"],
|
| 331 |
+
"LLM-Only": [f"{al:.0f}", f"${acl:.6f}", f"${acl*n:.4f}", f"${acl*1000*30:.2f}", f"${acl*1000*365:.2f}"],
|
| 332 |
+
"Basic RAG": [f"{ab:.0f}", f"${acb:.6f}", f"${acb*n:.4f}", f"${acb*1000*30:.2f}", f"${acb*1000*365:.2f}"],
|
| 333 |
"GraphRAG": [f"{ag:.0f}", f"${acg:.6f}", f"${acg*n:.4f}", f"${acg*1000*30:.2f}", f"${acg*1000*365:.2f}"],
|
|
|
|
| 334 |
})
|
| 335 |
|
| 336 |
qr = list(range(0, n+1, max(n//50, 1)))
|
| 337 |
fig_cum = go.Figure()
|
| 338 |
+
fig_cum.add_trace(go.Scatter(x=qr, y=[acl*q for q in qr], mode='lines', name='LLM-Only',
|
| 339 |
+
line=dict(color='#95a5a6', width=2, dash='dash')))
|
| 340 |
+
fig_cum.add_trace(go.Scatter(x=qr, y=[acb*q for q in qr], mode='lines', name='Basic RAG',
|
| 341 |
line=dict(color='#3498db', width=3)))
|
| 342 |
fig_cum.add_trace(go.Scatter(x=qr, y=[acg*q for q in qr], mode='lines', name='GraphRAG',
|
| 343 |
line=dict(color='#e74c3c', width=3)))
|
| 344 |
+
fig_cum.update_layout(title=f"Cumulative Cost — 3 Pipelines ({model})",
|
| 345 |
+
xaxis_title="Queries", yaxis_title="Cost ($)", height=400,
|
| 346 |
+
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 347 |
|
| 348 |
fig_tok = go.Figure()
|
| 349 |
if _benchmark_results:
|
| 350 |
+
fig_tok.add_trace(go.Histogram(
|
| 351 |
+
x=[r.get("llm_only_tokens", 0) for r in _benchmark_results],
|
| 352 |
+
name="LLM-Only", opacity=0.5, marker_color="#95a5a6"))
|
| 353 |
+
fig_tok.add_trace(go.Histogram(
|
| 354 |
+
x=[r.get("baseline_tokens", 0) for r in _benchmark_results],
|
| 355 |
+
name="Basic RAG", opacity=0.6, marker_color="#3498db"))
|
| 356 |
+
fig_tok.add_trace(go.Histogram(
|
| 357 |
+
x=[r.get("graphrag_tokens", 0) for r in _benchmark_results],
|
| 358 |
+
name="GraphRAG", opacity=0.6, marker_color="#e74c3c"))
|
| 359 |
+
fig_tok.update_layout(barmode='overlay', title="Token Distribution (3 Pipelines)", height=400,
|
| 360 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 361 |
else:
|
| 362 |
+
fig_tok.add_annotation(text="Run benchmark first for distribution data", showarrow=False)
|
| 363 |
|
| 364 |
return summary, fig_cum, fig_tok
|
| 365 |
|
|
|
|
| 376 |
|
| 377 |
G = nx.Graph()
|
| 378 |
for e in gr_result.entities_found[:20]:
|
| 379 |
+
if isinstance(e, dict):
|
| 380 |
+
G.add_node(e.get("name", "?"), entity_type=e.get("entity_type", "CONCEPT"),
|
| 381 |
+
description=e.get("description", ""))
|
| 382 |
+
else:
|
| 383 |
+
G.add_node(str(e), entity_type="CONCEPT")
|
| 384 |
for r in gr_result.relations_traversed[:30]:
|
| 385 |
parts = r.split(" -[")
|
| 386 |
if len(parts) == 2:
|
|
|
|
| 394 |
if not G.nodes():
|
| 395 |
G.add_node("Query", entity_type="QUERY")
|
| 396 |
for e in gr_result.entities_found[:5]:
|
| 397 |
+
name = e.get("name", "Entity") if isinstance(e, dict) else str(e)
|
| 398 |
+
etype = e.get("entity_type", "CONCEPT") if isinstance(e, dict) else "CONCEPT"
|
| 399 |
+
G.add_node(name, entity_type=etype)
|
| 400 |
+
G.add_edge("Query", name, relation="FOUND")
|
| 401 |
|
| 402 |
pos = nx.spring_layout(G, k=2, iterations=50, seed=42)
|
| 403 |
colors_map = {"PERSON": "#FF6B6B", "ORGANIZATION": "#4ECDC4", "LOCATION": "#45B7D1",
|
|
|
|
| 427 |
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
|
| 428 |
|
| 429 |
info = {"nodes": len(G.nodes()), "edges": len(G.edges()),
|
| 430 |
+
"entities": len(gr_result.entities_found), "relations": len(gr_result.relations_traversed),
|
| 431 |
+
"novelty_chain": gr_result.novelty_chain}
|
| 432 |
stats = pd.DataFrame({"Metric": ["Nodes", "Edges", "Avg Degree", "Density", "Entities", "Relations"],
|
| 433 |
"Value": [len(G.nodes()), len(G.edges()),
|
| 434 |
f"{sum(d for _,d in G.degree())/max(len(G.nodes()),1):.1f}",
|
|
|
|
| 438 |
explanation = orchestrator.explain_graphrag_reasoning(query, gr_result)
|
| 439 |
return fig, info, stats, explanation, gr_result.answer
|
| 440 |
except Exception as e:
|
| 441 |
+
logger.error(f"Graph explorer error: {e}", exc_info=True)
|
| 442 |
empty = go.Figure()
|
| 443 |
empty.add_annotation(text=str(e), showarrow=False)
|
| 444 |
return empty, {}, pd.DataFrame(), str(e), ""
|
|
|
|
| 447 |
# ── Build Dashboard ───────────────────────────────────────
|
| 448 |
|
| 449 |
def build_dashboard():
|
| 450 |
+
with gr.Blocks(title="GraphRAG 3-Pipeline Dashboard") as demo:
|
| 451 |
gr.Markdown("""
|
| 452 |
+
# 🔍 GraphRAG Inference Hackathon — 3-Pipeline Comparison Dashboard
|
| 453 |
+
### One query in → three pipelines run → side-by-side responses + metrics out
|
| 454 |
+
**Pipelines:** ⚪ LLM-Only | 🔵 Basic RAG | 🔴 GraphRAG (TigerGraph + 6 Novelties)
|
| 455 |
+
**Evaluation:** LLM-as-a-Judge (PASS/FAIL) | BERTScore F1 | F1/EM | RAGAS | Token Tracking
|
| 456 |
""")
|
| 457 |
|
| 458 |
with gr.Row():
|
|
|
|
| 461 |
init_btn.click(fn=initialize_system, outputs=init_status)
|
| 462 |
|
| 463 |
with gr.Tabs():
|
| 464 |
+
# ── Tab 1: Live 3-Pipeline Comparison ───────
|
| 465 |
+
with gr.Tab("🔴 Live 3-Pipeline Comparison"):
|
| 466 |
+
gr.Markdown("## One Query → Three Pipelines → Side-by-Side Results")
|
| 467 |
with gr.Row():
|
| 468 |
+
query_input = gr.Textbox(
|
| 469 |
+
label="Question",
|
| 470 |
+
placeholder="e.g., Were Scott Derrickson and Ed Wood of the same nationality?",
|
| 471 |
+
lines=2, scale=3)
|
| 472 |
with gr.Column(scale=1):
|
| 473 |
adaptive = gr.Checkbox(label="🧠 Adaptive Routing", value=True)
|
| 474 |
topk = gr.Slider(1, 10, value=5, step=1, label="Top-K")
|
| 475 |
hops_s = gr.Slider(1, 4, value=2, step=1, label="Hops")
|
| 476 |
|
| 477 |
+
run_btn = gr.Button("▶ Run All 3 Pipelines", variant="primary", size="lg")
|
| 478 |
status = gr.Textbox(label="Status", interactive=False)
|
| 479 |
routing = gr.Markdown(visible=True)
|
| 480 |
|
| 481 |
with gr.Row():
|
| 482 |
with gr.Column():
|
| 483 |
+
gr.Markdown("### ⚪ Pipeline 1: LLM-Only")
|
| 484 |
+
lo_ans = gr.Textbox(label="Answer", lines=4, interactive=False)
|
| 485 |
+
with gr.Row():
|
| 486 |
+
lo_tok = gr.Number(label="Tokens", precision=0)
|
| 487 |
+
lo_lat = gr.Number(label="Latency (ms)", precision=1)
|
| 488 |
+
lo_cost = gr.Number(label="Cost ($)", precision=6)
|
| 489 |
+
with gr.Column():
|
| 490 |
+
gr.Markdown("### 🔵 Pipeline 2: Basic RAG")
|
| 491 |
+
b_ans = gr.Textbox(label="Answer", lines=4, interactive=False)
|
| 492 |
with gr.Row():
|
| 493 |
b_tok = gr.Number(label="Tokens", precision=0)
|
| 494 |
b_lat = gr.Number(label="Latency (ms)", precision=1)
|
| 495 |
b_cost = gr.Number(label="Cost ($)", precision=6)
|
| 496 |
with gr.Column():
|
| 497 |
+
gr.Markdown("### 🔴 Pipeline 3: GraphRAG")
|
| 498 |
+
g_ans = gr.Textbox(label="Answer", lines=4, interactive=False)
|
| 499 |
with gr.Row():
|
| 500 |
g_tok = gr.Number(label="Tokens", precision=0)
|
| 501 |
g_lat = gr.Number(label="Latency (ms)", precision=1)
|
| 502 |
g_cost = gr.Number(label="Cost ($)", precision=6)
|
| 503 |
|
| 504 |
+
chart = gr.Plot(label="3-Pipeline Comparison")
|
| 505 |
+
with gr.Accordion("📄 Retrieved Contexts (RAG vs GraphRAG)", open=False):
|
| 506 |
with gr.Row():
|
| 507 |
+
b_ctx = gr.Markdown(label="Basic RAG Contexts")
|
| 508 |
+
g_ctx = gr.Markdown(label="GraphRAG Contexts")
|
| 509 |
+
with gr.Accordion("🕸️ Entities, Relations & Novelty Chain", open=False):
|
| 510 |
ent_disp = gr.Markdown()
|
| 511 |
|
| 512 |
+
run_btn.click(
|
| 513 |
+
fn=run_live_comparison,
|
| 514 |
+
inputs=[query_input, adaptive, topk, hops_s],
|
| 515 |
+
outputs=[status, lo_ans, b_ans, g_ans, routing,
|
| 516 |
+
lo_tok, b_tok, g_tok,
|
| 517 |
+
lo_lat, b_lat, g_lat,
|
| 518 |
+
lo_cost, b_cost, g_cost,
|
| 519 |
+
chart, b_ctx, g_ctx, ent_disp])
|
| 520 |
gr.Examples(examples=[
|
| 521 |
["Were Scott Derrickson and Ed Wood of the same nationality?"],
|
| 522 |
["What government position was held by the woman who portrayed Nora Batty?"],
|
| 523 |
["Which magazine was started first, Arthur's Magazine or First for Women?"],
|
| 524 |
["Who was born first, Arthur Conan Doyle or Agatha Christie?"],
|
| 525 |
["What is the capital of the country where the Eiffel Tower is located?"]],
|
| 526 |
+
inputs=query_input, label="📝 Example Questions (HotpotQA)")
|
| 527 |
|
| 528 |
# ── Tab 2: Batch Benchmark ──────────────────
|
| 529 |
+
with gr.Tab("📊 Batch Benchmark (3-Pipeline)"):
|
| 530 |
+
gr.Markdown("## Benchmark on HotpotQA — All 3 Pipelines + LLM-as-a-Judge")
|
| 531 |
with gr.Row():
|
| 532 |
n_samples = gr.Slider(10, 500, value=50, step=10, label="Samples")
|
| 533 |
bk = gr.Slider(1, 10, value=5, step=1, label="Top-K")
|
| 534 |
bh = gr.Slider(1, 4, value=2, step=1, label="Hops")
|
| 535 |
+
bench_btn = gr.Button("🏃 Run 3-Pipeline Benchmark", variant="primary")
|
| 536 |
bench_status = gr.Textbox(label="Status", interactive=False)
|
| 537 |
+
summary_df = gr.Dataframe(label="3-Pipeline Summary")
|
| 538 |
with gr.Row():
|
| 539 |
+
bar_chart = gr.Plot(label="Answer Quality")
|
| 540 |
+
radar_chart = gr.Plot(label="Radar (RAG vs GraphRAG)")
|
| 541 |
with gr.Accordion("📝 Full Report", open=False):
|
| 542 |
report = gr.Textbox(lines=30, interactive=False)
|
| 543 |
bench_btn.click(fn=run_batch_benchmark, inputs=[n_samples, bk, bh],
|
| 544 |
outputs=[bench_status, summary_df, bar_chart, radar_chart, report])
|
| 545 |
|
| 546 |
# ── Tab 3: Cost Analysis ────────────────────
|
| 547 |
+
with gr.Tab("💰 Cost Analysis (3-Pipeline)"):
|
| 548 |
+
gr.Markdown("## Cost & Token Analysis — All 3 Pipelines")
|
| 549 |
with gr.Row():
|
| 550 |
cq = gr.Slider(100, 100000, value=10000, step=100, label="Queries to Project")
|
| 551 |
+
cm = gr.Dropdown(["gpt-4o-mini", "gpt-4o", "gpt-3.5-turbo",
|
| 552 |
+
"claude-3-5-sonnet", "claude-3-haiku"],
|
| 553 |
value="gpt-4o-mini", label="Model")
|
| 554 |
cost_btn = gr.Button("💵 Calculate", variant="primary")
|
| 555 |
+
cost_df = gr.Dataframe(label="3-Pipeline Cost Breakdown")
|
| 556 |
with gr.Row():
|
| 557 |
+
cum_chart = gr.Plot(label="Cumulative Cost (3 Pipelines)")
|
| 558 |
tok_chart = gr.Plot(label="Token Distribution")
|
| 559 |
cost_btn.click(fn=compute_cost_analysis, inputs=[cq, cm],
|
| 560 |
outputs=[cost_df, cum_chart, tok_chart])
|
| 561 |
|
| 562 |
# ── Tab 4: Graph Explorer ───────────────────
|
| 563 |
with gr.Tab("🕸️ Graph Explorer"):
|
| 564 |
+
gr.Markdown("## Interactive Knowledge Graph Explorer\n*Visualize how GraphRAG traverses the graph and applies novelty techniques*")
|
| 565 |
with gr.Row():
|
| 566 |
gq = gr.Textbox(label="Query", placeholder="Enter a question...", scale=3)
|
| 567 |
gd = gr.Slider(1, 4, value=2, step=1, label="Depth", scale=1)
|
| 568 |
exp_btn = gr.Button("🔍 Explore", variant="primary", scale=1)
|
| 569 |
graph_plot = gr.Plot(label="Knowledge Graph")
|
| 570 |
with gr.Row():
|
| 571 |
+
graph_stats = gr.Dataframe(label="Graph Stats")
|
| 572 |
+
node_info = gr.JSON(label="Details + Novelty Chain")
|
| 573 |
with gr.Accordion("🧠 Reasoning Path", open=True):
|
| 574 |
reasoning = gr.Markdown()
|
| 575 |
graph_ans = gr.Textbox(label="GraphRAG Answer", interactive=False)
|
|
|
|
| 583 |
|
| 584 |
gr.Markdown("""
|
| 585 |
---
|
| 586 |
+
**GraphRAG Inference Hackathon** by TigerGraph | 3 Pipelines · 14 Novelties · 12 LLM Providers · 12 Research Papers
|
| 587 |
+
**Eval:** LLM-as-a-Judge ✅ | BERTScore ✅ | RAGAS ✅ | F1/EM ✅ | Token Tracking ✅
|
| 588 |
""")
|
| 589 |
return demo
|
| 590 |
|