-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
1376 lines (1159 loc) · 49.6 KB
/
Copy pathmain.py
File metadata and controls
1376 lines (1159 loc) · 49.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import random
import numpy as np
import time
import threading
import heapq
import json
import requests
from dataclasses import dataclass, field
from typing import List, Dict, Optional, Tuple, Callable, Generator
from fastapi import FastAPI, Request, Response, UploadFile, File
from fastapi.responses import JSONResponse, FileResponse, StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import os
import fitz # PyMuPDF
# =====================================================================
# CONSTANTS
# =====================================================================
DIMS = 16 # demo vectors dimension
SAVE_FILE = "doc_db_save.json" # persistence file for DocumentDB
# =====================================================================
# DATA TYPES
# =====================================================================
@dataclass
class VectorItem:
id: int
metadata: str
category: str
emb: List[float]
DistFn = Callable[[List[float], List[float]], float]
# =====================================================================
# DISTANCE METRICS
# =====================================================================
def euclidean(a: List[float], b: List[float]) -> float:
return float(np.linalg.norm(np.array(a) - np.array(b)))
def cosine(a: List[float], b: List[float]) -> float:
a_ = np.array(a)
b_ = np.array(b)
na = np.linalg.norm(a_)
nb = np.linalg.norm(b_)
if na < 1e-9 or nb < 1e-9:
return 1.0
return float(1.0 - np.dot(a_, b_) / (na * nb))
def manhattan(a: List[float], b: List[float]) -> float:
return float(np.sum(np.abs(np.array(a) - np.array(b))))
def get_dist_fn(m: str) -> DistFn:
if m == "cosine":
return cosine
if m == "manhattan":
return manhattan
return euclidean
# =====================================================================
# BRUTE FORCE
# =====================================================================
class BruteForce:
def __init__(self):
self.items: List[VectorItem] = []
def insert(self, v: VectorItem):
self.items.append(v)
def knn(self, q: List[float], k: int, dist: DistFn) -> List[Tuple[float, int]]:
r = [(dist(q, v.emb), v.id) for v in self.items]
r.sort()
return r[:k]
def remove(self, id: int):
self.items = [v for v in self.items if v.id != id]
# =====================================================================
# KD-TREE
# =====================================================================
class KDNode:
def __init__(self, item: VectorItem):
self.item = item
self.left: Optional['KDNode'] = None
self.right: Optional['KDNode'] = None
class KDTree:
def __init__(self, dims: int):
self.root: Optional[KDNode] = None
self.dims = dims
def _ins(self, n: Optional[KDNode], v: VectorItem, d: int) -> KDNode:
if n is None:
return KDNode(v)
ax = d % self.dims
if v.emb[ax] < n.item.emb[ax]:
n.left = self._ins(n.left, v, d + 1)
else:
n.right = self._ins(n.right, v, d + 1)
return n
def insert(self, v: VectorItem):
self.root = self._ins(self.root, v, 0)
def _knn(self, n: Optional[KDNode], q: List[float], k: int, d: int,
dist: DistFn, heap: list):
if n is None:
return
dn = dist(q, n.item.emb)
# Max-heap: store negative distances
if len(heap) < k or dn < -heap[0][0]:
heapq.heappush(heap, (-dn, n.item.id))
if len(heap) > k:
heapq.heappop(heap)
ax = d % self.dims
diff = q[ax] - n.item.emb[ax]
closer = n.left if diff < 0 else n.right
farther = n.right if diff < 0 else n.left
self._knn(closer, q, k, d + 1, dist, heap)
if len(heap) < k or abs(diff) < -heap[0][0]:
self._knn(farther, q, k, d + 1, dist, heap)
def knn(self, q: List[float], k: int, dist: DistFn) -> List[Tuple[float, int]]:
heap = []
self._knn(self.root, q, k, 0, dist, heap)
r = [(-neg_d, id_) for neg_d, id_ in heap]
r.sort()
return r
def rebuild(self, items: List[VectorItem]):
self.root = None
for v in items:
self.insert(v)
# =====================================================================
# HNSW — Hierarchical Navigable Small World
# =====================================================================
@dataclass
class HNSWNode:
item: VectorItem
max_lyr: int
nbrs: List[List[int]] = field(default_factory=list)
class HNSW:
def __init__(self, m: int = 16, ef_build: int = 200):
self.M = m
self.M0 = 2 * m
self.ef_build = ef_build
self.mL = 1.0 / math.log(float(m))
self.top_layer = -1
self.entry_pt = -1
self.G: Dict[int, HNSWNode] = {}
self._rng = random.Random(42)
def _rand_level(self) -> int:
u = self._rng.random()
return int(math.floor(-math.log(u) * self.mL))
def _search_layer(self, q: List[float], ep: int, ef: int, lyr: int,
dist: DistFn) -> List[Tuple[float, int]]:
vis = {ep: True}
# Min-heap for candidates
d0 = dist(q, self.G[ep].item.emb)
cands = [(d0, ep)]
# Max-heap for found (store negatives)
found = [(-d0, ep)]
while cands:
cd, cid = heapq.heappop(cands)
best_found_dist = -found[0][0]
if len(found) >= ef and cd > best_found_dist:
break
if lyr >= len(self.G[cid].nbrs):
continue
for nid in self.G[cid].nbrs[lyr]:
if vis.get(nid) or nid not in self.G:
continue
vis[nid] = True
nd = dist(q, self.G[nid].item.emb)
best_found_dist = -found[0][0]
if len(found) < ef or nd < best_found_dist:
heapq.heappush(cands, (nd, nid))
heapq.heappush(found, (-nd, nid))
if len(found) > ef:
heapq.heappop(found)
res = [(-neg_d, id_) for neg_d, id_ in found]
res.sort()
return res
def _select_nbrs(self, cands: List[Tuple[float, int]], max_m: int) -> List[int]:
return [cands[i][1] for i in range(min(len(cands), max_m))]
def insert(self, item: VectorItem, dist: DistFn):
id_ = item.id
lvl = self._rand_level()
node = HNSWNode(item=item, max_lyr=lvl, nbrs=[[] for _ in range(lvl + 1)])
self.G[id_] = node
if self.entry_pt == -1:
self.entry_pt = id_
self.top_layer = lvl
return
ep = self.entry_pt
for lc in range(self.top_layer, lvl, -1):
if lc < len(self.G[ep].nbrs):
W = self._search_layer(item.emb, ep, 1, lc, dist)
if W:
ep = W[0][1]
for lc in range(min(self.top_layer, lvl), -1, -1):
W = self._search_layer(item.emb, ep, self.ef_build, lc, dist)
max_m = self.M0 if lc == 0 else self.M
sel = self._select_nbrs(W, max_m)
self.G[id_].nbrs[lc] = sel
for nid in sel:
if nid not in self.G:
continue
if len(self.G[nid].nbrs) <= lc:
self.G[nid].nbrs.extend([] for _ in range(lc + 1 - len(self.G[nid].nbrs)))
conn = self.G[nid].nbrs[lc]
conn.append(id_)
if len(conn) > max_m:
ds = []
for c in conn:
if c in self.G:
ds.append((dist(self.G[nid].item.emb, self.G[c].item.emb), c))
ds.sort()
self.G[nid].nbrs[lc] = [ds[i][1] for i in range(min(max_m, len(ds)))]
if W:
ep = W[0][1]
if lvl > self.top_layer:
self.top_layer = lvl
self.entry_pt = id_
def knn(self, q: List[float], k: int, ef: int, dist: DistFn) -> List[Tuple[float, int]]:
if self.entry_pt == -1:
return []
ep = self.entry_pt
for lc in range(self.top_layer, 0, -1):
if lc < len(self.G[ep].nbrs):
W = self._search_layer(q, ep, 1, lc, dist)
if W:
ep = W[0][1]
W = self._search_layer(q, ep, max(ef, k), 0, dist)
return W[:k]
def remove(self, id_: int):
if id_ not in self.G:
return
for nid, nd in self.G.items():
for layer in nd.nbrs:
if id_ in layer:
layer.remove(id_)
if self.entry_pt == id_:
self.entry_pt = -1
for nid in self.G:
if nid != id_:
self.entry_pt = nid
break
del self.G[id_]
def get_info(self) -> dict:
top_layer = self.top_layer
node_count = len(self.G)
max_l = max(top_layer + 1, 1)
nodes_per_layer = [0] * max_l
edges_per_layer = [0] * max_l
nodes = []
edges = []
for id_, nd in self.G.items():
nodes.append({
"id": id_,
"metadata": nd.item.metadata,
"category": nd.item.category,
"maxLyr": nd.max_lyr
})
for lc in range(min(nd.max_lyr + 1, max_l)):
nodes_per_layer[lc] += 1
if lc < len(nd.nbrs):
for nid in nd.nbrs[lc]:
if id_ < nid:
edges_per_layer[lc] += 1
edges.append({"src": id_, "dst": nid, "lyr": lc})
return {
"topLayer": top_layer,
"nodeCount": node_count,
"nodesPerLayer": nodes_per_layer,
"edgesPerLayer": edges_per_layer,
"nodes": nodes,
"edges": edges
}
def size(self) -> int:
return len(self.G)
# =====================================================================
# VECTOR DATABASE (demo 16D index)
# =====================================================================
@dataclass
class Hit:
id: int
meta: str
cat: str
emb: List[float]
dist: float
@dataclass
class SearchOut:
hits: List[Hit]
us: int
algo: str
metric: str
@dataclass
class BenchOut:
bf_us: int
kd_us: int
hnsw_us: int
n: int
class VectorDB:
def __init__(self, d: int):
self.dims = d
self.store: Dict[int, VectorItem] = {}
self.bf = BruteForce()
self.kdt = KDTree(d)
self.hnsw = HNSW(16, 200)
self.mu = threading.Lock()
self.next_id = 1
def insert(self, meta: str, cat: str, emb: List[float], dist: DistFn) -> int:
with self.mu:
v = VectorItem(id=self.next_id, metadata=meta, category=cat, emb=emb)
self.next_id += 1
self.store[v.id] = v
self.bf.insert(v)
self.kdt.insert(v)
self.hnsw.insert(v, dist)
return v.id
def remove(self, id_: int) -> bool:
with self.mu:
if id_ not in self.store:
return False
del self.store[id_]
self.bf.remove(id_)
self.hnsw.remove(id_)
rem = list(self.store.values())
self.kdt.rebuild(rem)
return True
def search(self, q: List[float], k: int, metric: str, algo: str) -> SearchOut:
with self.mu:
dfn = get_dist_fn(metric)
t0 = time.monotonic_ns()
if algo == "bruteforce":
raw = self.bf.knn(q, k, dfn)
elif algo == "kdtree":
raw = self.kdt.knn(q, k, dfn)
else:
raw = self.hnsw.knn(q, k, 50, dfn)
us = (time.monotonic_ns() - t0) // 1000
hits = []
for d, id_ in raw:
if id_ in self.store:
v = self.store[id_]
hits.append(Hit(id=id_, meta=v.metadata, cat=v.category, emb=v.emb, dist=d))
return SearchOut(hits=hits, us=us, algo=algo, metric=metric)
def benchmark(self, q: List[float], k: int, metric: str) -> BenchOut:
with self.mu:
dfn = get_dist_fn(metric)
def time_fn(fn) -> int:
t = time.monotonic_ns()
fn()
return (time.monotonic_ns() - t) // 1000
bf_us = time_fn(lambda: self.bf.knn(q, k, dfn))
kd_us = time_fn(lambda: self.kdt.knn(q, k, dfn))
hnsw_us = time_fn(lambda: self.hnsw.knn(q, k, 50, dfn))
return BenchOut(bf_us=bf_us, kd_us=kd_us, hnsw_us=hnsw_us, n=len(self.store))
def all(self) -> List[VectorItem]:
with self.mu:
return list(self.store.values())
def hnsw_info(self) -> dict:
with self.mu:
return self.hnsw.get_info()
def size(self) -> int:
with self.mu:
return len(self.store)
# =====================================================================
# DOCUMENT DATABASE — HNSW over real Ollama embeddings
# =====================================================================
@dataclass
class DocItem:
id: int
title: str
text: str
emb: List[float]
class DocumentDB:
def __init__(self):
self.store: Dict[int, DocItem] = {}
self.hnsw = HNSW(16, 200)
self.bf = BruteForce()
self.mu = threading.Lock()
self.next_id = 1
self.dims = 0
def insert(self, title: str, text: str, emb: List[float]) -> int:
with self.mu:
if self.dims == 0:
self.dims = len(emb)
item = DocItem(id=self.next_id, title=title, text=text, emb=emb)
self.next_id += 1
self.store[item.id] = item
vi = VectorItem(id=item.id, metadata=title, category="doc", emb=emb)
self.hnsw.insert(vi, cosine)
self.bf.insert(vi)
return item.id
def search(self, q: List[float], k: int, max_dist: float = 0.7) -> List[Tuple[float, 'DocItem']]:
with self.mu:
if not self.store:
return []
if len(self.store) < 10:
raw = self.bf.knn(q, k, cosine)
else:
raw = self.hnsw.knn(q, k, 50, cosine)
out = []
for d, id_ in raw:
if id_ in self.store and d <= max_dist:
out.append((d, self.store[id_]))
return out
def remove(self, id_: int) -> bool:
with self.mu:
if id_ not in self.store:
return False
del self.store[id_]
self.hnsw.remove(id_)
self.bf.remove(id_)
return True
def all(self) -> List['DocItem']:
with self.mu:
return list(self.store.values())
def size(self) -> int:
with self.mu:
return len(self.store)
def get_dims(self) -> int:
return self.dims
# =====================================================================
# OLLAMA CLIENT — wraps local Ollama REST API
# Install: https://ollama.com
# Models: ollama pull nomic-embed-text
# ollama pull llama3.2
# =====================================================================
class OllamaClient:
def __init__(self, host: str = "127.0.0.1", port: int = 11434):
self.host = host
self.port = port
self.embed_model = "nomic-embed-text"
self.gen_model = "mistral"
def _base_url(self) -> str:
return f"http://{self.host}:{self.port}"
def is_available(self) -> bool:
try:
r = requests.get(f"{self._base_url()}/api/tags", timeout=2)
return r.status_code == 200
except Exception:
return False
def embed(self, text: str) -> List[float]:
try:
r = requests.post(
f"{self._base_url()}/api/embeddings",
json={"model": self.embed_model, "prompt": text},
timeout=30
)
if r.status_code != 200:
return []
data = r.json()
return data.get("embedding", [])
except Exception:
return []
def generate(self, prompt: str) -> str:
try:
r = requests.post(
f"{self._base_url()}/api/generate",
json={"model": self.gen_model, "prompt": prompt, "stream": False},
timeout=180
)
if r.status_code != 200:
return "ERROR: Ollama unavailable. Run: ollama serve"
return r.json().get("response", "")
except Exception:
return "ERROR: Ollama unavailable. Run: ollama serve"
def generate_stream(self, prompt: str) -> Generator[str, None, None]:
"""Yields one token at a time from Ollama streaming API."""
try:
with requests.post(
f"{self._base_url()}/api/generate",
json={"model": self.gen_model, "prompt": prompt, "stream": True},
timeout=180,
stream=True
) as r:
if r.status_code != 200:
yield "__ERROR__: Ollama returned non-200. Run: ollama serve"
return
for line in r.iter_lines():
if not line:
continue
try:
data = json.loads(line)
except json.JSONDecodeError:
continue
token = data.get("response", "")
if token:
yield token
# Ollama sets done=true on the final chunk
if data.get("done", False):
return
except requests.exceptions.ConnectionError:
yield "__ERROR__: Ollama went offline mid-stream. Run: ollama serve"
except Exception as e:
yield f"__ERROR__: {str(e)}"
# =====================================================================
# TEXT CHUNKER
# =====================================================================
def chunk_text(text: str, chunk_words: int = 250, overlap_words: int = 30) -> List[str]:
words = text.split()
if not words:
return []
if len(words) <= chunk_words:
return [text]
chunks = []
step = chunk_words - overlap_words
i = 0
while i < len(words):
end = min(i + chunk_words, len(words))
chunk = " ".join(words[i:end])
chunks.append(chunk)
if end == len(words):
break
i += step
return chunks
# =====================================================================
# DEMO DATA (16D categorical vectors)
# =====================================================================
def load_demo(db: VectorDB):
dist = get_dist_fn("cosine")
# Dims 0-3: CS | Dims 4-7: Math | Dims 8-11: Food | Dims 12-15: Sports
db.insert("Linked List: nodes connected by pointers", "cs",
[0.90,0.85,0.72,0.68,0.12,0.08,0.15,0.10,0.05,0.08,0.06,0.09,0.07,0.11,0.08,0.06], dist)
db.insert("Binary Search Tree: O(log n) search and insert", "cs",
[0.88,0.82,0.78,0.74,0.15,0.10,0.08,0.12,0.06,0.07,0.08,0.05,0.09,0.06,0.07,0.10], dist)
db.insert("Dynamic Programming: memoization overlapping subproblems", "cs",
[0.82,0.76,0.88,0.80,0.20,0.18,0.12,0.09,0.07,0.06,0.08,0.07,0.08,0.09,0.06,0.07], dist)
db.insert("Graph BFS and DFS: breadth and depth first traversal", "cs",
[0.85,0.80,0.75,0.82,0.18,0.14,0.10,0.08,0.06,0.09,0.07,0.06,0.10,0.08,0.09,0.07], dist)
db.insert("Hash Table: O(1) lookup with collision chaining", "cs",
[0.87,0.78,0.70,0.76,0.13,0.11,0.09,0.14,0.08,0.07,0.06,0.08,0.07,0.10,0.08,0.09], dist)
db.insert("Calculus: derivatives integrals and limits", "math",
[0.12,0.15,0.18,0.10,0.91,0.86,0.78,0.72,0.08,0.06,0.07,0.09,0.07,0.08,0.06,0.10], dist)
db.insert("Linear Algebra: matrices eigenvalues eigenvectors", "math",
[0.20,0.18,0.15,0.12,0.88,0.90,0.82,0.76,0.09,0.07,0.08,0.06,0.10,0.07,0.08,0.09], dist)
db.insert("Probability: distributions random variables Bayes theorem", "math",
[0.15,0.12,0.20,0.18,0.84,0.80,0.88,0.82,0.07,0.08,0.06,0.10,0.09,0.06,0.09,0.08], dist)
db.insert("Number Theory: primes modular arithmetic RSA cryptography", "math",
[0.22,0.16,0.14,0.20,0.80,0.85,0.76,0.90,0.08,0.09,0.07,0.06,0.08,0.10,0.07,0.06], dist)
db.insert("Combinatorics: permutations combinations generating functions", "math",
[0.18,0.20,0.16,0.14,0.86,0.78,0.84,0.80,0.06,0.07,0.09,0.08,0.06,0.09,0.10,0.07], dist)
db.insert("Neapolitan Pizza: wood-fired dough San Marzano tomatoes", "food",
[0.08,0.06,0.09,0.07,0.07,0.08,0.06,0.09,0.90,0.86,0.78,0.72,0.08,0.06,0.09,0.07], dist)
db.insert("Sushi: vinegared rice raw fish and nori rolls", "food",
[0.06,0.08,0.07,0.09,0.09,0.06,0.08,0.07,0.86,0.90,0.82,0.76,0.07,0.09,0.06,0.08], dist)
db.insert("Ramen: noodle soup with chashu pork and soft-boiled eggs", "food",
[0.09,0.07,0.06,0.08,0.08,0.09,0.07,0.06,0.82,0.78,0.90,0.84,0.09,0.07,0.08,0.06], dist)
db.insert("Tacos: corn tortillas with carnitas salsa and cilantro", "food",
[0.07,0.09,0.08,0.06,0.06,0.07,0.09,0.08,0.78,0.82,0.86,0.90,0.06,0.08,0.07,0.09], dist)
db.insert("Croissant: laminated pastry with buttery flaky layers", "food",
[0.06,0.07,0.10,0.09,0.10,0.06,0.07,0.10,0.85,0.80,0.76,0.82,0.09,0.07,0.10,0.06], dist)
db.insert("Basketball: fast-paced shooting dribbling slam dunks", "sports",
[0.09,0.07,0.08,0.10,0.08,0.09,0.07,0.06,0.08,0.07,0.09,0.06,0.91,0.85,0.78,0.72], dist)
db.insert("Football: tackles touchdowns field goals and strategy", "sports",
[0.07,0.09,0.06,0.08,0.09,0.07,0.10,0.08,0.07,0.09,0.08,0.07,0.87,0.89,0.82,0.76], dist)
db.insert("Tennis: racket volleys groundstrokes and Wimbledon serves", "sports",
[0.08,0.06,0.09,0.07,0.07,0.08,0.06,0.09,0.09,0.06,0.07,0.08,0.83,0.80,0.88,0.82], dist)
db.insert("Chess: openings endgames tactics strategic board game", "sports",
[0.25,0.20,0.22,0.18,0.22,0.18,0.20,0.15,0.06,0.08,0.07,0.09,0.80,0.84,0.78,0.90], dist)
db.insert("Swimming: butterfly freestyle backstroke Olympic competition", "sports",
[0.06,0.08,0.07,0.09,0.08,0.06,0.09,0.07,0.10,0.08,0.06,0.07,0.85,0.82,0.86,0.80], dist)
# ── CS: 8 more ────────────────────────────────────────────────────
db.insert("Sorting Algorithms: quicksort mergesort heapsort comparison", "cs",
[0.86,0.80,0.74,0.70,0.14,0.09,0.11,0.13,0.07,0.06,0.08,0.07,0.08,0.07,0.09,0.06], dist)
db.insert("Stack and Queue: LIFO FIFO push pop enqueue dequeue", "cs",
[0.84,0.78,0.68,0.72,0.11,0.13,0.10,0.08,0.06,0.08,0.07,0.05,0.09,0.08,0.06,0.07], dist)
db.insert("Recursion: base case call stack factorial Fibonacci", "cs",
[0.80,0.74,0.84,0.78,0.17,0.15,0.11,0.10,0.08,0.07,0.06,0.08,0.07,0.10,0.08,0.06], dist)
db.insert("Trie: prefix tree autocomplete dictionary word search", "cs",
[0.83,0.77,0.71,0.75,0.12,0.10,0.08,0.13,0.07,0.06,0.09,0.07,0.08,0.06,0.10,0.08], dist)
db.insert("Heap: priority queue min-heap max-heap Dijkstra", "cs",
[0.85,0.79,0.73,0.77,0.16,0.12,0.09,0.11,0.06,0.09,0.07,0.08,0.10,0.07,0.08,0.09], dist)
db.insert("Big O Notation: time complexity space complexity analysis", "cs",
[0.81,0.75,0.86,0.76,0.19,0.14,0.13,0.08,0.07,0.06,0.08,0.06,0.09,0.08,0.07,0.06], dist)
db.insert("Neural Network: layers weights backpropagation gradient descent", "cs",
[0.79,0.83,0.76,0.80,0.21,0.17,0.14,0.11,0.08,0.07,0.06,0.09,0.07,0.09,0.08,0.07], dist)
db.insert("Operating System: process thread scheduling memory management", "cs",
[0.82,0.76,0.69,0.73,0.13,0.10,0.12,0.09,0.06,0.08,0.07,0.06,0.10,0.07,0.09,0.08], dist)
# ── Math: 8 more ──────────────────────────────────────────────────
db.insert("Differential Equations: ODEs PDEs initial value problems", "math",
[0.14,0.17,0.19,0.11,0.89,0.83,0.75,0.70,0.07,0.06,0.08,0.09,0.08,0.07,0.06,0.09], dist)
db.insert("Fourier Transform: frequency domain signal decomposition", "math",
[0.19,0.14,0.16,0.13,0.85,0.88,0.80,0.74,0.08,0.07,0.06,0.08,0.09,0.06,0.08,0.07], dist)
db.insert("Set Theory: unions intersections subsets cardinality", "math",
[0.13,0.11,0.18,0.16,0.83,0.79,0.86,0.78,0.06,0.08,0.07,0.09,0.07,0.08,0.10,0.06], dist)
db.insert("Topology: open sets continuity homeomorphism manifolds", "math",
[0.17,0.13,0.15,0.19,0.81,0.86,0.74,0.88,0.09,0.07,0.08,0.06,0.06,0.09,0.07,0.08], dist)
db.insert("Graph Theory: vertices edges paths cycles connectivity", "math",
[0.21,0.19,0.17,0.15,0.87,0.76,0.82,0.79,0.07,0.08,0.06,0.07,0.08,0.07,0.09,0.10], dist)
db.insert("Statistics: mean variance standard deviation hypothesis testing", "math",
[0.16,0.13,0.21,0.17,0.82,0.77,0.85,0.81,0.08,0.06,0.09,0.07,0.07,0.10,0.06,0.08], dist)
db.insert("Abstract Algebra: groups rings fields homomorphisms", "math",
[0.12,0.18,0.14,0.20,0.78,0.83,0.77,0.86,0.06,0.09,0.08,0.07,0.09,0.06,0.08,0.07], dist)
db.insert("Complex Analysis: analytic functions residues contour integration", "math",
[0.20,0.15,0.12,0.17,0.84,0.81,0.73,0.89,0.07,0.08,0.06,0.08,0.08,0.07,0.09,0.06], dist)
# ── Food: 7 more ──────────────────────────────────────────────────
db.insert("Biryani: aromatic basmati rice slow-cooked spiced meat", "food",
[0.07,0.09,0.08,0.06,0.08,0.07,0.09,0.06,0.88,0.83,0.77,0.80,0.07,0.08,0.06,0.09], dist)
db.insert("Pasta Carbonara: eggs pecorino guanciale black pepper", "food",
[0.08,0.06,0.07,0.09,0.07,0.09,0.06,0.08,0.84,0.88,0.80,0.74,0.06,0.09,0.08,0.07], dist)
db.insert("Dim Sum: steamed dumplings har gow siu mai bamboo basket", "food",
[0.06,0.08,0.09,0.07,0.09,0.06,0.08,0.07,0.80,0.76,0.88,0.86,0.08,0.07,0.09,0.06], dist)
db.insert("Paella: saffron rice seafood chorizo Valencia Spain", "food",
[0.09,0.07,0.06,0.08,0.06,0.08,0.07,0.09,0.76,0.80,0.84,0.88,0.07,0.06,0.08,0.09], dist)
db.insert("Butter Chicken: tomato cream sauce tandoor marinated chicken", "food",
[0.07,0.06,0.10,0.08,0.08,0.07,0.06,0.10,0.83,0.79,0.75,0.85,0.08,0.09,0.07,0.06], dist)
db.insert("Tiramisu: espresso soaked ladyfingers mascarpone cocoa", "food",
[0.06,0.09,0.07,0.08,0.09,0.06,0.10,0.07,0.81,0.85,0.79,0.73,0.09,0.07,0.06,0.08], dist)
db.insert("Pho: Vietnamese beef broth rice noodles herbs anise", "food",
[0.08,0.07,0.06,0.09,0.07,0.09,0.06,0.08,0.87,0.81,0.73,0.77,0.06,0.08,0.09,0.07], dist)
# ── Sports: 7 more ────────────────────────────────────────────────
db.insert("Cricket: batting bowling fielding wickets test match IPL", "sports",
[0.08,0.07,0.09,0.06,0.09,0.08,0.07,0.06,0.07,0.09,0.08,0.06,0.88,0.83,0.77,0.74], dist)
db.insert("Badminton: shuttlecock racket smash drop shot net play", "sports",
[0.06,0.09,0.07,0.08,0.07,0.06,0.09,0.08,0.08,0.07,0.06,0.09,0.84,0.88,0.80,0.76], dist)
db.insert("Cycling: Tour de France peloton sprint cadence endurance", "sports",
[0.09,0.06,0.08,0.07,0.08,0.09,0.06,0.07,0.06,0.08,0.09,0.07,0.80,0.78,0.86,0.84], dist)
db.insert("Volleyball: spike serve block rotation libero beach court", "sports",
[0.07,0.08,0.06,0.09,0.06,0.07,0.08,0.09,0.09,0.06,0.07,0.08,0.82,0.86,0.84,0.78], dist)
db.insert("Boxing: jab cross hook uppercut footwork ring knockouts", "sports",
[0.08,0.06,0.09,0.07,0.07,0.08,0.09,0.06,0.07,0.08,0.06,0.09,0.86,0.80,0.82,0.88], dist)
db.insert("Gymnastics: floor beam vault uneven bars artistic routines", "sports",
[0.06,0.07,0.08,0.09,0.09,0.07,0.06,0.08,0.08,0.07,0.09,0.06,0.78,0.84,0.88,0.82], dist)
db.insert("Golf: fairway iron wedge putt handicap birdie eagle swing", "sports",
[0.09,0.08,0.07,0.06,0.06,0.09,0.07,0.08,0.06,0.09,0.08,0.07,0.83,0.79,0.85,0.87], dist)
# =====================================================================
# HELPERS
# =====================================================================
def parse_vec(s: str) -> List[float]:
v = []
for t in s.split(","):
try:
v.append(float(t))
except Exception:
pass
return v
# =====================================================================
# EMBED AND STORE HELPER
# =====================================================================
async def embed_and_store(title: str, text: str) -> dict:
"""
Chunk text, embed each chunk via Ollama, and store in doc_db.
Returns a result dict on success, or an error dict on failure.
Extracted to avoid duplicating this logic across /doc/insert,
/doc/upload-pdf, and /doc/upload-txt.
"""
chunks = chunk_text(text, 250, 30)
ids = []
for i, chunk in enumerate(chunks):
emb = ollama.embed(chunk)
if not emb:
return {
"error": (
"Ollama unavailable. "
"Install from https://ollama.com then run: "
"ollama pull nomic-embed-text && ollama pull llama3.2"
)
}
chunk_title = (
f"{title} [{i+1}/{len(chunks)}]" if len(chunks) > 1 else title
)
ids.append(doc_db.insert(chunk_title, chunk, emb))
return {
"ids": ids,
"chunks": len(chunks),
"dims": doc_db.get_dims()
}
# =====================================================================
# PERSISTENCE — save/load DocumentDB to JSON
# =====================================================================
def save_doc_db():
"""
Write every DocItem in doc_db to SAVE_FILE as a JSON array.
Called after every insert or delete so the file is always current.
"""
data = []
for item in doc_db.all():
data.append({
"id": item.id,
"title": item.title,
"text": item.text,
"emb": item.emb
})
try:
with open(SAVE_FILE, "w", encoding="utf-8") as f:
json.dump(data, f)
except Exception as e:
print(f"[persistence] WARNING: could not save doc_db — {e}")
def load_doc_db():
"""
Re-hydrate doc_db from SAVE_FILE on startup.
Silently skips if the file does not exist yet (first run).
"""
if not os.path.exists(SAVE_FILE):
return
try:
with open(SAVE_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
for item in data:
doc_db.insert(item["title"], item["text"], item["emb"])
# Restore next_id so new inserts never reuse an existing ID
if data:
doc_db.next_id = max(item["id"] for item in data) + 1
print(f"[persistence] Loaded {len(data)} document chunk(s) from {SAVE_FILE}")
except Exception as e:
print(f"[persistence] WARNING: could not load doc_db — {e}")
# =====================================================================
# FASTAPI APPLICATION
# =====================================================================
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["GET", "POST", "DELETE", "OPTIONS"],
allow_headers=["Content-Type"],
)
# Global state
db = VectorDB(DIMS)
doc_db = DocumentDB()
ollama = OllamaClient()
load_demo(db)
load_doc_db()
ollama_up = ollama.is_available()
print("=== VectorDB Engine ===")
print("http://localhost:8080")
print(f"{db.size()} demo vectors | {DIMS} dims | HNSW+KD-Tree+BruteForce")
print(f"Ollama: {'ONLINE' if ollama_up else 'OFFLINE (install from ollama.com)'}")
if ollama_up:
print(f" embed model: {ollama.embed_model} gen model: {ollama.gen_model}")
# ── DEMO VECTOR ENDPOINTS ─────────────────────────────────────────
@app.get("/search")
def search(v: str = "", k: int = 5, metric: str = "cosine", algo: str = "hnsw", category: str = ""):
q = parse_vec(v)
if len(q) != DIMS:
return JSONResponse({"error": f"need {DIMS}D vector"})
out = db.search(q, k, metric, algo)
results = []
for h in out.hits:
# ── Category filter: skip if category given and doesn't match ──
if category and h.cat != category:
continue
results.append({
"id": h.id,
"metadata": h.meta,
"category": h.cat,
"distance": round(h.dist, 6),
"embedding": [round(x, 4) for x in h.emb]
})
return {
"results": results,
"latencyUs": out.us,
"algo": out.algo,
"metric": out.metric,
"categoryFilter": category if category else None
}
@app.post("/insert")
async def insert(request: Request):
body = await request.json()
meta = body.get("metadata", "")
cat = body.get("category", "")
emb = body.get("embedding", [])
if not meta or not emb or len(emb) != DIMS:
return JSONResponse({"error": "invalid body"})
id_ = db.insert(meta, cat, emb, get_dist_fn("cosine"))
return {"id": id_}
@app.delete("/delete/{id_}")
def delete(id_: int):
ok = db.remove(id_)
return {"ok": ok}
@app.get("/items")
def items():
all_items = db.all()
return [
{
"id": v.id,
"metadata": v.metadata,
"category": v.category,
"embedding": [round(x, 4) for x in v.emb]
}
for v in all_items
]
@app.get("/benchmark")
def benchmark(v: str = "", k: int = 5, metric: str = "cosine"):
q = parse_vec(v)
if len(q) != DIMS:
return JSONResponse({"error": f"need {DIMS}D vector"})
b = db.benchmark(q, k, metric)
return {
"bruteforceUs": b.bf_us,
"kdtreeUs": b.kd_us,
"hnswUs": b.hnsw_us,
"itemCount": b.n
}
@app.get("/hnsw-info")
def hnsw_info():
gi = db.hnsw_info()
return gi
# ── DOCUMENT + RAG ENDPOINTS ──────────────────────────────────────
@app.post("/doc/insert")
async def doc_insert(request: Request):
body = await request.json()
title = body.get("title", "")
text = body.get("text", "")
if not title or not text:
return JSONResponse({"error": "need title and text"})
result = await embed_and_store(title, text)
if "error" in result:
return JSONResponse(result)
save_doc_db()
return result
@app.post("/doc/upload-pdf")
async def doc_upload_pdf(file: UploadFile = File(...)):
# ── Validate file type ────────────────────────────────────────────
if not file.filename.lower().endswith(".pdf"):
return JSONResponse({"error": "only .pdf files are accepted"})
# ── Read raw bytes ────────────────────────────────────────────────
raw = await file.read()
if not raw:
return JSONResponse({"error": "uploaded file is empty"})
# ── Extract text with PyMuPDF ─────────────────────────────────────
try:
pdf = fitz.open(stream=raw, filetype="pdf")
except Exception:
return JSONResponse({"error": "corrupt or unreadable PDF"})
text = ""
for page in pdf:
text += page.get_text()
pdf.close()
text = text.strip()
if not text:
return JSONResponse({"error": "PDF has no extractable text (may be scanned image)"})
# ── Use filename (without .pdf) as the title ──────────────────────
title = file.filename.removesuffix(".pdf")
# ── Chunk → embed → store via shared helper ─────────────────────
result = await embed_and_store(title, text)
if "error" in result:
return JSONResponse(result)
save_doc_db()
return result
@app.post("/doc/upload-txt")
async def doc_upload_txt(file: UploadFile = File(...)):
# ── Validate file type ────────────────────────────────────────────
if not file.filename.lower().endswith(".txt"):
return JSONResponse({"error": "only .txt files are accepted"})
# ── Read and decode ───────────────────────────────────────────────
raw = await file.read()
if not raw:
return JSONResponse({"error": "uploaded file is empty"})
try:
text = raw.decode("utf-8").strip()
except UnicodeDecodeError:
return JSONResponse({"error": "could not decode file — make sure it is UTF-8 encoded"})
if not text:
return JSONResponse({"error": "text file has no content"})
# ── Use filename (without .txt) as the title ──────────────────────
title = file.filename.removesuffix(".txt")
# ── Chunk → embed → store via shared helper ─────────────────────
result = await embed_and_store(title, text)
if "error" in result:
return JSONResponse(result)