|
5d70f82…
|
lmata
|
1 |
""" |
|
5d70f82…
|
lmata
|
2 |
SemanticSearch — embedding-based similarity search over the navegador graph. |
|
5d70f82…
|
lmata
|
3 |
|
|
5d70f82…
|
lmata
|
4 |
Embeds function/class docstrings via an LLMProvider, stores the embedding |
|
5d70f82…
|
lmata
|
5 |
vectors as JSON in a node property (``embedding``), and retrieves the top-k |
|
5d70f82…
|
lmata
|
6 |
most similar nodes to a natural-language query using cosine similarity. |
|
5d70f82…
|
lmata
|
7 |
|
|
5d70f82…
|
lmata
|
8 |
Usage:: |
|
5d70f82…
|
lmata
|
9 |
|
|
5d70f82…
|
lmata
|
10 |
from navegador.graph import GraphStore |
|
5d70f82…
|
lmata
|
11 |
from navegador.llm import get_provider |
|
5d70f82…
|
lmata
|
12 |
from navegador.intelligence.search import SemanticSearch |
|
5d70f82…
|
lmata
|
13 |
|
|
5d70f82…
|
lmata
|
14 |
store = GraphStore.sqlite(".navegador/graph.db") |
|
5d70f82…
|
lmata
|
15 |
provider = get_provider("openai") |
|
5d70f82…
|
lmata
|
16 |
ss = SemanticSearch(store, provider) |
|
5d70f82…
|
lmata
|
17 |
|
|
5d70f82…
|
lmata
|
18 |
# Build / refresh the index (idempotent — re-embeds all nodes) |
|
5d70f82…
|
lmata
|
19 |
ss.index() |
|
5d70f82…
|
lmata
|
20 |
|
|
5d70f82…
|
lmata
|
21 |
# Query |
|
5d70f82…
|
lmata
|
22 |
results = ss.search("function that validates JWT tokens", limit=5) |
|
5d70f82…
|
lmata
|
23 |
for r in results: |
|
5d70f82…
|
lmata
|
24 |
print(r["name"], r["score"]) |
|
5d70f82…
|
lmata
|
25 |
""" |
|
5d70f82…
|
lmata
|
26 |
|
|
5d70f82…
|
lmata
|
27 |
from __future__ import annotations |
|
5d70f82…
|
lmata
|
28 |
|
|
5d70f82…
|
lmata
|
29 |
import json |
|
5d70f82…
|
lmata
|
30 |
import math |
|
5d70f82…
|
lmata
|
31 |
from typing import TYPE_CHECKING, Any |
|
5d70f82…
|
lmata
|
32 |
|
|
5d70f82…
|
lmata
|
33 |
if TYPE_CHECKING: |
|
5d70f82…
|
lmata
|
34 |
from navegador.graph.store import GraphStore |
|
5d70f82…
|
lmata
|
35 |
from navegador.llm import LLMProvider |
|
5d70f82…
|
lmata
|
36 |
|
|
5d70f82…
|
lmata
|
37 |
|
|
5d70f82…
|
lmata
|
38 |
# Cypher to fetch all embeddable nodes (those with a docstring or description) |
|
5d70f82…
|
lmata
|
39 |
_EMBEDDABLE_NODES = """ |
|
5d70f82…
|
lmata
|
40 |
MATCH (n) |
|
5d70f82…
|
lmata
|
41 |
WHERE (n:Function OR n:Class OR n:Method OR n:Concept OR n:Rule OR n:Decision) |
|
5d70f82…
|
lmata
|
42 |
AND (n.docstring IS NOT NULL OR n.description IS NOT NULL) |
|
5d70f82…
|
lmata
|
43 |
RETURN |
|
5d70f82…
|
lmata
|
44 |
labels(n)[0] AS type, |
|
5d70f82…
|
lmata
|
45 |
n.name AS name, |
|
5d70f82…
|
lmata
|
46 |
coalesce(n.file_path, '') AS file_path, |
|
5d70f82…
|
lmata
|
47 |
coalesce(n.docstring, n.description, '') AS text |
|
5d70f82…
|
lmata
|
48 |
LIMIT $limit |
|
5d70f82…
|
lmata
|
49 |
""" |
|
5d70f82…
|
lmata
|
50 |
|
|
5d70f82…
|
lmata
|
51 |
# Cypher to fetch nodes that already have a stored embedding |
|
5d70f82…
|
lmata
|
52 |
_NODES_WITH_EMBEDDINGS = """ |
|
5d70f82…
|
lmata
|
53 |
MATCH (n) |
|
5d70f82…
|
lmata
|
54 |
WHERE n.embedding IS NOT NULL |
|
5d70f82…
|
lmata
|
55 |
RETURN |
|
5d70f82…
|
lmata
|
56 |
labels(n)[0] AS type, |
|
5d70f82…
|
lmata
|
57 |
n.name AS name, |
|
5d70f82…
|
lmata
|
58 |
coalesce(n.file_path, '') AS file_path, |
|
5d70f82…
|
lmata
|
59 |
coalesce(n.docstring, n.description, '') AS text, |
|
5d70f82…
|
lmata
|
60 |
n.embedding AS embedding |
|
5d70f82…
|
lmata
|
61 |
""" |
|
5d70f82…
|
lmata
|
62 |
|
|
5d70f82…
|
lmata
|
63 |
# Upsert the embedding property on a matched node |
|
5d70f82…
|
lmata
|
64 |
_SET_EMBEDDING = """ |
|
5d70f82…
|
lmata
|
65 |
MATCH (n) |
|
5d70f82…
|
lmata
|
66 |
WHERE n.name = $name AND ($file_path = '' OR n.file_path = $file_path) |
|
5d70f82…
|
lmata
|
67 |
SET n.embedding = $embedding |
|
5d70f82…
|
lmata
|
68 |
""" |
|
5d70f82…
|
lmata
|
69 |
|
|
5d70f82…
|
lmata
|
70 |
|
|
5d70f82…
|
lmata
|
71 |
class SemanticSearch: |
|
5d70f82…
|
lmata
|
72 |
""" |
|
5d70f82…
|
lmata
|
73 |
Embedding-based semantic search over the navegador graph. |
|
5d70f82…
|
lmata
|
74 |
|
|
5d70f82…
|
lmata
|
75 |
Embeddings are stored as a JSON string (serialised ``list[float]``) in the |
|
5d70f82…
|
lmata
|
76 |
``embedding`` property of each node so they survive graph restarts without |
|
5d70f82…
|
lmata
|
77 |
any external vector store. |
|
5d70f82…
|
lmata
|
78 |
|
|
5d70f82…
|
lmata
|
79 |
Args: |
|
5d70f82…
|
lmata
|
80 |
store: A :class:`~navegador.graph.GraphStore` instance. |
|
5d70f82…
|
lmata
|
81 |
provider: An :class:`~navegador.llm.LLMProvider` that implements |
|
5d70f82…
|
lmata
|
82 |
:meth:`embed`. |
|
5d70f82…
|
lmata
|
83 |
""" |
|
5d70f82…
|
lmata
|
84 |
|
|
5d70f82…
|
lmata
|
85 |
def __init__(self, store: "GraphStore", provider: "LLMProvider") -> None: |
|
5d70f82…
|
lmata
|
86 |
self._store = store |
|
5d70f82…
|
lmata
|
87 |
self._provider = provider |
|
5d70f82…
|
lmata
|
88 |
|
|
5d70f82…
|
lmata
|
89 |
# ── Index ───────────────────────────────────────────────────────────────── |
|
5d70f82…
|
lmata
|
90 |
|
|
5d70f82…
|
lmata
|
91 |
def index(self, limit: int = 1000) -> int: |
|
5d70f82…
|
lmata
|
92 |
""" |
|
5d70f82…
|
lmata
|
93 |
Embed all function/class/concept docstrings and store on the nodes. |
|
5d70f82…
|
lmata
|
94 |
|
|
5d70f82…
|
lmata
|
95 |
Args: |
|
5d70f82…
|
lmata
|
96 |
limit: Maximum number of nodes to index in one pass. |
|
5d70f82…
|
lmata
|
97 |
|
|
5d70f82…
|
lmata
|
98 |
Returns: |
|
5d70f82…
|
lmata
|
99 |
The number of nodes that were (re-)embedded. |
|
5d70f82…
|
lmata
|
100 |
""" |
|
5d70f82…
|
lmata
|
101 |
result = self._store.query(_EMBEDDABLE_NODES, {"limit": limit}) |
|
5d70f82…
|
lmata
|
102 |
rows = result.result_set or [] |
|
5d70f82…
|
lmata
|
103 |
indexed = 0 |
|
5d70f82…
|
lmata
|
104 |
for row in rows: |
|
5d70f82…
|
lmata
|
105 |
node_type, name, file_path, text = row[0], row[1], row[2], row[3] |
|
5d70f82…
|
lmata
|
106 |
if not text: |
|
5d70f82…
|
lmata
|
107 |
continue |
|
5d70f82…
|
lmata
|
108 |
label = f"[{node_type}] {name}: {text}" |
|
5d70f82…
|
lmata
|
109 |
vector = self._provider.embed(label) |
|
5d70f82…
|
lmata
|
110 |
self._store.query( |
|
5d70f82…
|
lmata
|
111 |
_SET_EMBEDDING, |
|
5d70f82…
|
lmata
|
112 |
{ |
|
5d70f82…
|
lmata
|
113 |
"name": name, |
|
5d70f82…
|
lmata
|
114 |
"file_path": file_path, |
|
5d70f82…
|
lmata
|
115 |
"embedding": json.dumps(vector), |
|
5d70f82…
|
lmata
|
116 |
}, |
|
5d70f82…
|
lmata
|
117 |
) |
|
5d70f82…
|
lmata
|
118 |
indexed += 1 |
|
5d70f82…
|
lmata
|
119 |
return indexed |
|
5d70f82…
|
lmata
|
120 |
|
|
5d70f82…
|
lmata
|
121 |
# ── Search ──────────────────────────────────────────────────────────────── |
|
5d70f82…
|
lmata
|
122 |
|
|
5d70f82…
|
lmata
|
123 |
def search(self, query: str, limit: int = 10) -> list[dict[str, Any]]: |
|
5d70f82…
|
lmata
|
124 |
""" |
|
5d70f82…
|
lmata
|
125 |
Embed *query* and return the *limit* most similar indexed nodes. |
|
5d70f82…
|
lmata
|
126 |
|
|
5d70f82…
|
lmata
|
127 |
Each result dict has keys: ``type``, ``name``, ``file_path``, |
|
5d70f82…
|
lmata
|
128 |
``text``, ``score`` (cosine similarity, 0–1). |
|
5d70f82…
|
lmata
|
129 |
|
|
5d70f82…
|
lmata
|
130 |
Args: |
|
5d70f82…
|
lmata
|
131 |
query: Natural-language search query. |
|
5d70f82…
|
lmata
|
132 |
limit: Maximum number of results to return. |
|
5d70f82…
|
lmata
|
133 |
|
|
5d70f82…
|
lmata
|
134 |
Returns: |
|
5d70f82…
|
lmata
|
135 |
List of result dicts sorted by descending similarity score. |
|
5d70f82…
|
lmata
|
136 |
""" |
|
5d70f82…
|
lmata
|
137 |
query_vec = self._provider.embed(query) |
|
5d70f82…
|
lmata
|
138 |
|
|
5d70f82…
|
lmata
|
139 |
result = self._store.query(_NODES_WITH_EMBEDDINGS, {}) |
|
5d70f82…
|
lmata
|
140 |
rows = result.result_set or [] |
|
5d70f82…
|
lmata
|
141 |
|
|
5d70f82…
|
lmata
|
142 |
scored: list[dict[str, Any]] = [] |
|
5d70f82…
|
lmata
|
143 |
for row in rows: |
|
e5e1a3b…
|
lmata
|
144 |
node_type, name, file_path, text, emb_json = (row[0], row[1], row[2], row[3], row[4]) |
|
5d70f82…
|
lmata
|
145 |
if not emb_json: |
|
5d70f82…
|
lmata
|
146 |
continue |
|
5d70f82…
|
lmata
|
147 |
try: |
|
5d70f82…
|
lmata
|
148 |
node_vec: list[float] = json.loads(emb_json) |
|
5d70f82…
|
lmata
|
149 |
except (json.JSONDecodeError, TypeError): |
|
5d70f82…
|
lmata
|
150 |
continue |
|
5d70f82…
|
lmata
|
151 |
score = self._cosine_similarity(query_vec, node_vec) |
|
5d70f82…
|
lmata
|
152 |
scored.append( |
|
5d70f82…
|
lmata
|
153 |
{ |
|
5d70f82…
|
lmata
|
154 |
"type": node_type, |
|
5d70f82…
|
lmata
|
155 |
"name": name, |
|
5d70f82…
|
lmata
|
156 |
"file_path": file_path, |
|
5d70f82…
|
lmata
|
157 |
"text": text, |
|
5d70f82…
|
lmata
|
158 |
"score": score, |
|
5d70f82…
|
lmata
|
159 |
} |
|
5d70f82…
|
lmata
|
160 |
) |
|
5d70f82…
|
lmata
|
161 |
|
|
5d70f82…
|
lmata
|
162 |
scored.sort(key=lambda x: x["score"], reverse=True) |
|
5d70f82…
|
lmata
|
163 |
return scored[:limit] |
|
5d70f82…
|
lmata
|
164 |
|
|
5d70f82…
|
lmata
|
165 |
# ── Internals ───────────────────────────────────────────────────────────── |
|
5d70f82…
|
lmata
|
166 |
|
|
5d70f82…
|
lmata
|
167 |
@staticmethod |
|
5d70f82…
|
lmata
|
168 |
def _cosine_similarity(a: list[float], b: list[float]) -> float: |
|
5d70f82…
|
lmata
|
169 |
""" |
|
5d70f82…
|
lmata
|
170 |
Compute the cosine similarity between two vectors. |
|
5d70f82…
|
lmata
|
171 |
|
|
5d70f82…
|
lmata
|
172 |
Args: |
|
5d70f82…
|
lmata
|
173 |
a: First embedding vector. |
|
5d70f82…
|
lmata
|
174 |
b: Second embedding vector. |
|
5d70f82…
|
lmata
|
175 |
|
|
5d70f82…
|
lmata
|
176 |
Returns: |
|
5d70f82…
|
lmata
|
177 |
Cosine similarity in the range ``[-1, 1]``. Returns ``0.0`` if |
|
5d70f82…
|
lmata
|
178 |
either vector is the zero vector or the lengths differ. |
|
5d70f82…
|
lmata
|
179 |
""" |
|
5d70f82…
|
lmata
|
180 |
if len(a) != len(b): |
|
5d70f82…
|
lmata
|
181 |
return 0.0 |
|
5d70f82…
|
lmata
|
182 |
dot = sum(x * y for x, y in zip(a, b)) |
|
5d70f82…
|
lmata
|
183 |
mag_a = math.sqrt(sum(x * x for x in a)) |
|
5d70f82…
|
lmata
|
184 |
mag_b = math.sqrt(sum(x * x for x in b)) |
|
5d70f82…
|
lmata
|
185 |
if mag_a == 0.0 or mag_b == 0.0: |
|
5d70f82…
|
lmata
|
186 |
return 0.0 |
|
5d70f82…
|
lmata
|
187 |
return dot / (mag_a * mag_b) |