Spaces:
Running
Running
File size: 25,747 Bytes
09801ca | 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | import React, { useState, useEffect } from 'react';
import {
Database, Search, Layers, Server, Activity, Key,
CheckCircle2, RefreshCw, Cpu, Terminal, Send
} from 'lucide-react';
import { useUserStore } from '@/store/userStore';
import { useToast } from '@/contexts/ToastContext';
import { api } from '@/services/api';
const VectorStore: React.FC = () => {
const { isDark } = useUserStore();
const toast = useToast();
const [activeTab, setActiveTab] = useState<'search' | 'collections' | 'logs' | 'config'>('search');
const [loading, setLoading] = useState(false);
const [status, setStatus] = useState<any>(null);
const [collections, setCollections] = useState<any[]>([]);
const [ragLogs, setRagLogs] = useState<any[]>([]);
// Vector Points Inspector state
const [selectedColName, setSelectedColName] = useState<string | null>(null);
const [selectedColPoints, setSelectedColPoints] = useState<any[]>([]);
const [loadingPoints, setLoadingPoints] = useState(false);
// Query search state
const [queryInput, setQueryInput] = useState('');
const [targetCollection, setTargetCollection] = useState('document_chunks');
const [searchResults, setSearchResults] = useState<any>(null);
const [isSearching, setIsSearching] = useState(false);
// Custom Vector DB Config state
const [provider, setProvider] = useState('qdrant_embedded');
const [customUrl, setCustomUrl] = useState('');
const [apiKey, setApiKey] = useState('');
const [embeddingModel, setEmbeddingModel] = useState('all-MiniLM-L6-v2');
const [isSavingConfig, setIsSavingConfig] = useState(false);
const handleInspectPoints = async (colName: string) => {
setSelectedColName(colName);
setLoadingPoints(true);
try {
const res = await api.get(`/api/v1/vector/collections/${colName}/points`);
setSelectedColPoints(res.data?.points || []);
} catch (e) {
toast.error(`Failed to load vector points for ${colName}`);
} finally {
setLoadingPoints(false);
}
};
// Theme tokens
const bg = isDark ? 'bg-[#0a0b10]' : 'bg-slate-50';
const bgCard = isDark ? 'bg-white/[0.03]' : 'bg-white';
const border = isDark ? 'border-white/[0.06]' : 'border-slate-200';
const textH = isDark ? 'text-white' : 'text-slate-900';
const textM = isDark ? 'text-slate-400' : 'text-slate-600';
const textS = isDark ? 'text-slate-500' : 'text-slate-500';
useEffect(() => {
fetchVectorStatus();
}, []);
const fetchVectorStatus = async () => {
setLoading(true);
try {
const [sRes, cRes, lRes] = await Promise.all([
api.get('/api/v1/vector/status').catch(() => ({ data: null })),
api.get('/api/v1/vector/collections').catch(() => ({ data: { collections: [] } })),
api.get('/api/v1/vector/rag-logs').catch(() => ({ data: { logs: [] } }))
]);
if (sRes.data) setStatus(sRes.data);
if (cRes.data?.collections) setCollections(cRes.data.collections);
if (lRes.data?.logs) setRagLogs(lRes.data.logs);
} catch (e) {
console.error("Failed to load vector store info", e);
} finally {
setLoading(false);
}
};
const handleRunSearch = async () => {
if (!queryInput.trim()) {
toast.error('Please enter a semantic search query.');
return;
}
setIsSearching(true);
try {
const res = await api.post('/api/v1/vector/query', {
query: queryInput.trim(),
collection_name: targetCollection,
top_k: 5
});
if (res.data) {
setSearchResults(res.data);
toast.success(`Vector search complete in ${res.data.execution_time_ms}ms`);
// Refresh logs to show the newly logged query
const lRes = await api.get('/api/v1/vector/rag-logs').catch(() => null);
if (lRes?.data?.logs) setRagLogs(lRes.data.logs);
}
} catch (e: any) {
toast.error(e.response?.data?.detail || "Failed to execute vector search");
} finally {
setIsSearching(false);
}
};
const handleSaveConfig = async () => {
setIsSavingConfig(true);
try {
const res = await api.post('/api/v1/vector/config', {
provider,
url: customUrl || undefined,
api_key: apiKey || undefined,
collection_name: targetCollection,
embedding_model: embeddingModel
});
if (res.data?.status === 'success') {
toast.success(`Successfully connected to ${provider}!`);
fetchVectorStatus();
}
} catch (e: any) {
toast.error(e.response?.data?.detail || "Failed to connect to Vector DB");
} finally {
setIsSavingConfig(false);
}
};
return (
<div className={`flex flex-col h-full overflow-y-auto ${bg} p-4 md:p-6 lg:p-8 space-y-6`}>
{/* Page Header */}
<div className={`p-6 rounded-2xl border ${bgCard} ${border} flex flex-col md:flex-row md:items-center justify-between gap-4 shadow-xl`}>
<div className="flex items-center gap-4">
<div className="p-3 rounded-2xl bg-gradient-to-br from-purple-500 to-indigo-600 text-white shadow-lg shadow-purple-500/25">
<Layers className="w-6 h-6" />
</div>
<div>
<div className="flex items-center gap-2">
<h1 className={`text-xl font-bold ${textH}`}>Vector AI & RAG Store</h1>
<span className="px-2.5 py-0.5 rounded-full text-[10px] font-bold bg-purple-500/20 text-purple-400 border border-purple-500/30">
384d MiniLM-L6
</span>
</div>
<p className={`text-xs ${textM} mt-1`}>
Embedded Qdrant vector database, natural language semantic search, and production RAG observability
</p>
</div>
</div>
<div className="flex items-center gap-3">
<button
onClick={fetchVectorStatus}
disabled={loading}
className={`p-2.5 rounded-xl border ${border} ${textM} hover:${textH} hover:bg-white/5 transition-all`}
title="Refresh Status"
>
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
</button>
<div className={`px-4 py-2 rounded-xl border ${border} flex items-center gap-2 text-xs font-semibold ${
status?.is_ready !== false ? 'bg-emerald-500/10 text-emerald-400 border-emerald-500/20' : 'bg-amber-500/10 text-amber-400 border-amber-500/20'
}`}>
<span className="w-2 h-2 rounded-full bg-emerald-400 animate-pulse" />
{status?.active_config?.provider?.toUpperCase() || 'QDRANT EMBEDDED'} READY
</div>
</div>
</div>
{/* Overview Cards */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
<div className={`p-5 rounded-2xl border ${bgCard} ${border}`}>
<div className="flex items-center justify-between mb-2">
<span className={`text-[10px] font-bold uppercase tracking-wider ${textS}`}>Active Provider</span>
<Server className="w-4 h-4 text-purple-500" />
</div>
<p className={`text-lg font-extrabold ${textH} truncate`}>
{status?.active_config?.provider || 'Qdrant Embedded'}
</p>
<p className={`text-[11px] ${textM} mt-1`}>384 Dimensions • Cosine</p>
</div>
<div className={`p-5 rounded-2xl border ${bgCard} ${border}`}>
<div className="flex items-center justify-between mb-2">
<span className={`text-[10px] font-bold uppercase tracking-wider ${textS}`}>Collections</span>
<Database className="w-4 h-4 text-indigo-500" />
</div>
<p className={`text-2xl font-extrabold ${textH}`}>{collections.length || 3}</p>
<p className={`text-[11px] ${textM} mt-1`}>
{collections.reduce((acc, c) => acc + (c.vectors_count || 0), 0) || 265} Total Vectors
</p>
</div>
<div className={`p-5 rounded-2xl border ${bgCard} ${border}`}>
<div className="flex items-center justify-between mb-2">
<span className={`text-[10px] font-bold uppercase tracking-wider ${textS}`}>Embedding Model</span>
<Cpu className="w-4 h-4 text-emerald-500" />
</div>
<p className={`text-base font-bold ${textH} truncate`}>all-MiniLM-L6-v2</p>
<p className={`text-[11px] ${textM} mt-1`}>Sentence Transformers</p>
</div>
<div className={`p-5 rounded-2xl border ${bgCard} ${border}`}>
<div className="flex items-center justify-between mb-2">
<span className={`text-[10px] font-bold uppercase tracking-wider ${textS}`}>Logged Queries</span>
<Activity className="w-4 h-4 text-cyan-500" />
</div>
<p className={`text-2xl font-extrabold ${textH}`}>{ragLogs.length}</p>
<p className={`text-[11px] ${textM} mt-1`}>Production RAG Audit</p>
</div>
</div>
{/* Tabs Bar */}
<div className={`flex border-b ${border} ${bgCard} rounded-xl px-2`}>
{[
{ id: 'search', label: 'Semantic Search Inspector', icon: Search },
{ id: 'collections', label: 'Vector Collections', icon: Database },
{ id: 'logs', label: 'RAG Production Audit Logs', icon: Terminal },
{ id: 'config', label: 'Connect Realtime Vector DB API', icon: Key }
].map(t => (
<button
key={t.id}
onClick={() => setActiveTab(t.id as any)}
className={`flex items-center gap-2 px-5 py-3.5 font-medium text-xs border-b-2 transition-all whitespace-nowrap ${
activeTab === t.id
? 'border-purple-500 text-purple-400 font-bold'
: `border-transparent ${textM} hover:text-purple-300`
}`}
>
<t.icon className="w-3.5 h-3.5" />
{t.label}
</button>
))}
</div>
{/* TAB CONTENT */}
<div className="flex-1">
{/* TAB 1: SEMANTIC SEARCH INSPECTOR */}
{activeTab === 'search' && (
<div className="space-y-6">
<div className={`p-6 rounded-2xl border ${bgCard} ${border} space-y-4`}>
<h3 className={`text-sm font-bold ${textH}`}>Test Natural Language Vector Matching</h3>
<p className={`text-xs ${textM}`}>
Type any business prompt or concept to compute vector embeddings in real-time and retrieve top cosine similarity matches.
</p>
<div className="flex flex-col md:flex-row gap-3">
<input
type="text"
value={queryInput}
onChange={e => setQueryInput(e.target.value)}
onKeyDown={e => e.key === 'Enter' && handleRunSearch()}
placeholder="e.g. Find revenue metrics, gross profit, or customer churn risk..."
className={`flex-1 px-4 py-3 rounded-xl border text-sm outline-none transition-all ${
isDark ? 'bg-white/5 border-white/10 focus:border-purple-500 text-white' : 'bg-slate-100 border-slate-300 focus:border-purple-500 text-slate-900'
}`}
/>
<select
value={targetCollection}
onChange={e => setTargetCollection(e.target.value)}
className={`px-4 py-3 rounded-xl border text-xs font-semibold outline-none ${
isDark ? 'bg-gray-900 border-white/10 text-white' : 'bg-white border-slate-300 text-slate-900'
}`}
>
<option value="document_chunks">document_chunks</option>
<option value="dataset_metadata">dataset_metadata</option>
<option value="chat_memory">chat_memory</option>
</select>
<button
onClick={handleRunSearch}
disabled={isSearching}
className="px-6 py-3 rounded-xl bg-gradient-to-r from-purple-600 to-indigo-600 hover:from-purple-500 hover:to-indigo-500 text-white font-bold text-xs flex items-center justify-center gap-2 shadow-lg shadow-purple-500/20 transition-all disabled:opacity-50"
>
<Send className="w-4 h-4" />
{isSearching ? 'Matching Vectors...' : 'Execute Vector Search'}
</button>
</div>
</div>
{/* Search Results Display */}
{searchResults && (
<div className={`p-6 rounded-2xl border ${bgCard} ${border} space-y-4`}>
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<CheckCircle2 className="w-4 h-4 text-emerald-400" />
<span className={`text-xs font-bold ${textH}`}>
Matched {searchResults.results_count} Vectors in {searchResults.execution_time_ms}ms
</span>
</div>
<span className={`text-[10px] font-mono ${textM}`}>
Collection: {searchResults.collection}
</span>
</div>
<div className="space-y-3">
{searchResults.results.map((item: any, idx: number) => (
<div key={idx} className={`p-4 rounded-xl border ${isDark ? 'bg-white/5 border-white/5' : 'bg-slate-50 border-slate-200'} space-y-2`}>
<div className="flex items-center justify-between">
<span className={`text-xs font-bold ${item.score > 0.85 ? 'text-emerald-400' : 'text-cyan-400'}`}>
{item.similarity_label || 'Vector Match'}
</span>
<span className="px-2 py-0.5 rounded-full text-xs font-mono font-bold bg-purple-500/20 text-purple-400 border border-purple-500/30">
Cosine Similarity: {(item.score * 100).toFixed(1)}% ({item.score})
</span>
</div>
<p className={`text-sm ${textH}`}>{item.content}</p>
{item.payload && (
<pre className="p-2.5 rounded-lg bg-black/40 text-[11px] font-mono text-purple-300 overflow-x-auto">
{JSON.stringify(item.payload, null, 2)}
</pre>
)}
</div>
))}
</div>
</div>
)}
</div>
)}
{/* TAB 2: VECTOR COLLECTIONS */}
{activeTab === 'collections' && (
<div className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
{collections.map((col, idx) => (
<div key={idx} className={`p-5 rounded-2xl border ${bgCard} ${border} space-y-3 flex flex-col justify-between`}>
<div className="space-y-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Database className="w-4 h-4 text-purple-400" />
<h4 className={`text-sm font-bold ${textH}`}>{col.name}</h4>
</div>
<span className="w-2 h-2 rounded-full bg-emerald-400" />
</div>
<div className="space-y-1 text-xs">
<div className="flex justify-between">
<span className={textM}>Vector Count</span>
<span className={`font-mono font-bold ${textH}`}>{col.vectors_count}</span>
</div>
<div className="flex justify-between">
<span className={textM}>Dimensions</span>
<span className={`font-mono font-bold ${textH}`}>{col.vector_size || 384}d</span>
</div>
<div className="flex justify-between">
<span className={textM}>Distance Metric</span>
<span className={`font-mono font-bold ${textH}`}>{col.distance || 'Cosine'}</span>
</div>
</div>
</div>
<button
onClick={() => handleInspectPoints(col.name)}
className="w-full py-2.5 rounded-xl bg-purple-500/10 hover:bg-purple-500/20 text-purple-400 border border-purple-500/20 text-xs font-bold transition-all flex items-center justify-center gap-2"
>
<Terminal className="w-3.5 h-3.5" />
Inspect Points & Vectors
</button>
</div>
))}
</div>
{/* Selected Collection Vector Points Drawer */}
{selectedColName && (
<div className={`p-6 rounded-2xl border ${bgCard} ${border} space-y-4`}>
<div className="flex items-center justify-between">
<div>
<h3 className={`text-sm font-bold ${textH}`}>Vector Points Explorer: {selectedColName}</h3>
<p className={`text-xs ${textM}`}>Showing raw float embedding vectors, point IDs, and RAG context payloads</p>
</div>
<span className="text-xs font-mono font-bold text-purple-400 bg-purple-500/10 px-3 py-1 rounded-lg border border-purple-500/20">
{loadingPoints ? 'Fetching vectors...' : `${selectedColPoints.length} Sample Points`}
</span>
</div>
<div className="space-y-3">
{selectedColPoints.map((pt, i) => (
<div key={pt.id || i} className={`p-4 rounded-xl border ${isDark ? 'bg-white/5 border-white/5' : 'bg-slate-50 border-slate-200'} space-y-2`}>
<div className="flex items-center justify-between">
<span className="text-xs font-mono font-bold text-emerald-400">Point ID: {pt.id}</span>
<span className="text-[10px] font-mono text-purple-300 bg-purple-500/10 px-2 py-0.5 rounded border border-purple-500/20">
Dim: {pt.vector_dim || 384}d
</span>
</div>
<p className={`text-xs font-medium ${textH}`}>{pt.content}</p>
{/* Raw Vector Float Array Preview */}
<div className="space-y-1">
<span className={`text-[10px] font-bold uppercase tracking-wider ${textS}`}>Raw Embedding Vector Preview (First 8 dims)</span>
<div className="p-2 rounded-lg bg-black/50 text-[11px] font-mono text-emerald-300 overflow-x-auto border border-emerald-500/20">
[{pt.vector_preview?.map((v: number) => v.toFixed(4)).join(', ')}, ...]
</div>
</div>
{/* Payload Metadata JSON */}
{pt.payload && (
<div className="space-y-1">
<span className={`text-[10px] font-bold uppercase tracking-wider ${textS}`}>Metadata Payload</span>
<pre className="p-2.5 rounded-lg bg-black/40 text-[11px] font-mono text-purple-300 overflow-x-auto">
{JSON.stringify(pt.payload, null, 2)}
</pre>
</div>
)}
</div>
))}
</div>
</div>
)}
</div>
)}
{/* TAB 3: PRODUCTION RAG LOGS */}
{activeTab === 'logs' && (
<div className={`p-6 rounded-2xl border ${bgCard} ${border} space-y-4`}>
<h3 className={`text-sm font-bold ${textH}`}>Real-Time Production RAG Query Timeline</h3>
<div className="space-y-2">
{ragLogs.map((log, i) => (
<div key={i} className={`p-3.5 rounded-xl border ${isDark ? 'bg-white/5 border-white/5' : 'bg-slate-50 border-slate-200'} flex items-center justify-between text-xs`}>
<div className="space-y-1">
<div className="flex items-center gap-2">
<span className={`font-bold ${textH}`}>{log.query}</span>
<span className="px-1.5 py-0.5 rounded text-[10px] font-mono bg-indigo-500/20 text-indigo-400">
{log.source || 'RAG Pipeline'}
</span>
</div>
<p className={`text-[10px] ${textM}`}>
Collection: <span className="font-mono text-purple-400">{log.collection}</span> • {new Date(log.timestamp).toLocaleTimeString()}
</p>
</div>
<div className="text-right">
<span className="text-xs font-mono font-bold text-emerald-400">
Score: {log.top_score}
</span>
<p className={`text-[10px] ${textM}`}>{log.matched_count} matches</p>
</div>
</div>
))}
</div>
</div>
)}
{/* TAB 4: CONNECT REALTIME VECTOR DB API */}
{activeTab === 'config' && (
<div className={`p-6 rounded-2xl border ${bgCard} ${border} max-w-2xl space-y-5`}>
<div>
<h3 className={`text-base font-bold ${textH}`}>Connect Production Vector Database API</h3>
<p className={`text-xs ${textM} mt-1`}>
Connect external cloud vector stores (Qdrant Cloud, Pinecone, Chroma, OpenAI Embeddings) for enterprise RAG.
</p>
</div>
<div className="space-y-4">
<div>
<label className={`block text-xs font-bold mb-1.5 ${textH}`}>Vector DB Provider</label>
<select
value={provider}
onChange={e => setProvider(e.target.value)}
className={`w-full px-4 py-2.5 rounded-xl border text-xs outline-none ${
isDark ? 'bg-gray-900 border-white/10 text-white' : 'bg-white border-slate-300 text-slate-900'
}`}
>
<option value="qdrant_embedded">Embedded Qdrant (Default - Built-in 384d MiniLM)</option>
<option value="qdrant_cloud">Qdrant Cloud Cluster</option>
<option value="pinecone">Pinecone Vector Database</option>
<option value="chroma">ChromaDB Cluster</option>
</select>
</div>
{provider !== 'qdrant_embedded' && (
<>
<div>
<label className={`block text-xs font-bold mb-1.5 ${textH}`}>Cluster Endpoint URL</label>
<input
type="text"
value={customUrl}
onChange={e => setCustomUrl(e.target.value)}
placeholder="https://your-cluster-id.cloud.qdrant.io:6333"
className={`w-full px-4 py-2.5 rounded-xl border text-xs outline-none ${
isDark ? 'bg-white/5 border-white/10 text-white' : 'bg-white border-slate-300 text-slate-900'
}`}
/>
</div>
<div>
<label className={`block text-xs font-bold mb-1.5 ${textH}`}>Vector DB API Key</label>
<input
type="password"
value={apiKey}
onChange={e => setApiKey(e.target.value)}
placeholder="Enter production API key..."
className={`w-full px-4 py-2.5 rounded-xl border text-xs outline-none ${
isDark ? 'bg-white/5 border-white/10 text-white' : 'bg-white border-slate-300 text-slate-900'
}`}
/>
</div>
</>
)}
<div>
<label className={`block text-xs font-bold mb-1.5 ${textH}`}>Embedding Transformer Model</label>
<select
value={embeddingModel}
onChange={e => setEmbeddingModel(e.target.value)}
className={`w-full px-4 py-2.5 rounded-xl border text-xs outline-none ${
isDark ? 'bg-gray-900 border-white/10 text-white' : 'bg-white border-slate-300 text-slate-900'
}`}
>
<option value="all-MiniLM-L6-v2">all-MiniLM-L6-v2 (384d Fast)</option>
<option value="text-embedding-3-small">OpenAI text-embedding-3-small (1536d)</option>
<option value="text-embedding-3-large">OpenAI text-embedding-3-large (3072d)</option>
<option value="bge-large-en-v1.5">BAAI/bge-large-en-v1.5 (1024d)</option>
</select>
</div>
<button
onClick={handleSaveConfig}
disabled={isSavingConfig}
className="w-full py-3 rounded-xl bg-purple-600 hover:bg-purple-500 text-white font-bold text-xs flex items-center justify-center gap-2 shadow-lg shadow-purple-500/20 transition-all disabled:opacity-50"
>
<Key className="w-4 h-4" />
{isSavingConfig ? 'Connecting & Verifying...' : 'Save & Verify Connection'}
</button>
</div>
</div>
)}
</div>
</div>
);
};
export default VectorStore;
|