Spaces:
Sleeping
Sleeping
File size: 8,884 Bytes
3786a3f f14eb69 3786a3f a67b1ec 3786a3f a67b1ec 3786a3f a67b1ec 3786a3f f14eb69 3786a3f | 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 | 'use client';
import { useState } from 'react';
import { Loader2, CheckCircle2, AlertCircle } from 'lucide-react';
import { Card, CardContent } from '@/components/ui/card';
import { QueryPanel } from '@/components/query/QueryPanel';
import { QueryMode } from '@/components/query/QueryPanel';
import { Button } from '@/components/ui/button';
import ReactMarkdown from 'react-markdown';
import api from '@/lib/axios';
interface ColResult {
answer: string;
confidence: number;
timeMs: number;
context: string;
}
interface ComparisonData {
query: string;
results: { graph: ColResult; vector: ColResult; hybrid: ColResult };
verdict: string;
}
const COL_META = [
{ key: 'graph', label: 'Graph Only', accent: 'text-accent-violet', bar: 'bg-accent-violet' },
{ key: 'vector', label: 'Vector Only', accent: 'text-accent-indigo', bar: 'bg-accent-indigo' },
{ key: 'hybrid', label: 'Hybrid', accent: 'text-accent-cyan', bar: 'bg-accent-cyan' },
] as const;
function generateVerdict(comps: Record<string, any>): string {
const times: Record<string, number> = {};
const hasAnswer: Record<string, boolean> = {};
for (const mode of ['graph', 'vector', 'hybrid']) {
times[mode] = comps[mode]?.response_time ?? 999;
hasAnswer[mode] = !!(comps[mode]?.answer && comps[mode].answer.length > 10);
}
const fastest = Object.entries(times).sort((a, b) => a[1] - b[1])[0]?.[0] || 'hybrid';
const answered = Object.entries(hasAnswer).filter(([, v]) => v).map(([k]) => k);
if (answered.length === 0) return 'No retrieval mode produced a valid answer.';
if (answered.includes('hybrid')) {
return `Hybrid performed best — combines graph traversal with vector context for comprehensive answers. Fastest mode: ${fastest} (${Math.round(times[fastest] * 1000)}ms).`;
}
return `${answered[0]} produced the best result for this query. Fastest mode: ${fastest}.`;
}
function ConfidenceBar({ value, bar }: { value: number; bar: string }) {
return (
<div>
<div className="mb-1 flex justify-between text-xs">
<span className="text-text-muted">Confidence</span>
<span className="font-medium">{value}%</span>
</div>
<div className="h-2 w-full overflow-hidden rounded-full bg-bg-elevated">
<div className={`h-full rounded-full ${bar}`} style={{ width: `${value}%` }} />
</div>
</div>
);
}
export function ComparisonView() {
const [query, setQuery] = useState('');
const [mode, setMode] = useState<QueryMode>('hybrid');
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [data, setData] = useState<ComparisonData | null>(null);
async function compare() {
if (!query.trim()) return;
setLoading(true);
setError(null);
try {
const { data: res } = await api.post('/query/compare/', { query });
const comps = res.comparisons || res.results || {};
const mapped: ComparisonData = {
query,
results: {
graph: {
answer: comps.graph?.answer || '',
confidence: Math.round((comps.graph?.confidence ?? 0) * 100),
timeMs: Math.round((comps.graph?.response_time ?? 0) * 1000),
context: comps.graph?.context || comps.graph?.strategy || 'GRAPH',
},
vector: {
answer: comps.vector?.answer || '',
confidence: Math.round((comps.vector?.confidence ?? 0) * 100),
timeMs: Math.round((comps.vector?.response_time ?? 0) * 1000),
context: comps.vector?.context || comps.vector?.strategy || 'VECTOR',
},
hybrid: {
answer: comps.hybrid?.answer || '',
confidence: Math.round((comps.hybrid?.confidence ?? 0) * 100),
timeMs: Math.round((comps.hybrid?.response_time ?? 0) * 1000),
context: comps.hybrid?.context || comps.hybrid?.strategy || 'HYBRID',
},
},
verdict: res.verdict || generateVerdict(comps),
};
setData(mapped);
} catch (err: any) {
const msg = err?.response?.data?.error || 'Failed to compare retrieval modes.';
setError(msg);
} finally {
setLoading(false);
}
}
return (
<div className="flex h-full flex-col gap-4 overflow-y-auto p-4 scrollbar-thin">
<div className="rounded-md border border-border bg-bg-surface p-3">
<QueryPanel
value={query}
onChange={setQuery}
mode={mode}
onModeChange={setMode}
onSubmit={compare}
loading={loading}
/>
</div>
{loading && (
<div className="flex items-center justify-center gap-2 text-sm text-text-secondary">
<Loader2 className="h-4 w-4 animate-spin" /> Running Graph, Vector & Hybrid retrieval...
</div>
)}
{error && (
<div className="flex items-start gap-2 rounded-md border border-error/30 bg-error/5 p-3 text-sm text-error">
<AlertCircle className="mt-0.5 h-4 w-4 shrink-0" />
<span>{error}</span>
</div>
)}
{data && (
<>
<Card>
<CardContent className="overflow-x-auto p-4">
<h3 className="mb-3 text-sm font-semibold text-text-primary">Comparison Metrics</h3>
<table className="w-full text-xs">
<thead>
<tr className="border-b border-border">
<th className="pb-2 pr-4 text-left font-medium text-text-muted">Metric</th>
{COL_META.map((c) => (
<th key={c.key} className={`pb-2 px-4 text-right font-medium ${c.accent}`}>
{c.label}
</th>
))}
</tr>
</thead>
<tbody className="divide-y divide-border/50">
<tr>
<td className="py-2 pr-4 text-text-secondary">Confidence</td>
{COL_META.map((c) => (
<td key={c.key} className="py-2 px-4 text-right font-medium text-text-primary">
{data.results[c.key].confidence}%
</td>
))}
</tr>
<tr>
<td className="py-2 pr-4 text-text-secondary">Response Time</td>
{COL_META.map((c) => (
<td key={c.key} className="py-2 px-4 text-right font-medium text-text-primary">
{data.results[c.key].timeMs}ms
</td>
))}
</tr>
<tr>
<td className="py-2 pr-4 text-text-secondary">Context Length</td>
{COL_META.map((c) => (
<td key={c.key} className="py-2 px-4 text-right font-medium text-text-primary">
{data.results[c.key].context.length} chars
</td>
))}
</tr>
<tr>
<td className="py-2 pr-4 text-text-secondary">Answer Length</td>
{COL_META.map((c) => (
<td key={c.key} className="py-2 px-4 text-right font-medium text-text-primary">
{data.results[c.key].answer.length} chars
</td>
))}
</tr>
</tbody>
</table>
</CardContent>
</Card>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
{COL_META.map((c) => {
const r = data.results[c.key];
return (
<Card key={c.key}>
<CardContent className="space-y-3 p-4">
<h3 className={`text-sm font-semibold ${c.accent}`}>{c.label}</h3>
<div className="text-sm leading-relaxed text-text-secondary">
<ReactMarkdown>{r.answer}</ReactMarkdown>
</div>
<p className="text-xs text-text-muted">Context: {r.context}</p>
<ConfidenceBar value={r.confidence} bar={c.bar} />
<p className="text-xs text-text-muted">⏱️ {r.timeMs}ms</p>
</CardContent>
</Card>
);
})}
</div>
<Card className="border-success/30 bg-success/5">
<CardContent className="flex items-start gap-2 p-4 text-sm text-text-primary">
<CheckCircle2 className="mt-0.5 h-4 w-4 shrink-0 text-success" />
<div>
<p className="font-semibold text-success">Verdict</p>
<p className="mt-1 text-text-secondary">{data.verdict}</p>
</div>
</CardContent>
</Card>
</>
)}
</div>
);
}
|