Spaces:
Sleeping
Sleeping
File size: 19,473 Bytes
944f820 | 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 | """
embedder.py
-----------
Converts parsed codebase (FileInfo objects) into hybrid chunks and
stores them in ChromaDB across two collections:
- class_chunks : one chunk per class (for macro / cross-module queries)
- function_chunks : one chunk per function/method (for micro queries)
Each chunk carries rich metadata so the retriever can filter precisely.
Depends on:
- ast_parser.parse_codebase() β list[FileInfo]
- chromadb
- sentence-transformers (local embedding, no API needed)
Install:
pip install chromadb sentence-transformers rich
"""
import json
import hashlib
from pathlib import Path
from pinecone import Pinecone, ServerlessSpec
from sentence_transformers import SentenceTransformer
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn
from rich.panel import Panel
from rich.table import Table
from rich import box
from config.config import PINECONE_API_KEY, PINECONE_INDEX, EMBEDDING_DIM
from ingest.parse_ast import parse_codebase, FileInfo, ClassInfo, FunctionInfo
console = Console()
# ββ Constants βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
EMBEDDING_MODEL = "all-MiniLM-L6-v2" # fast, lightweight, good quality
CLASS_COLLECTION = "class_chunks"
FUNCTION_COLLECTION = "function_chunks"
COMPLETE_COLLECTION = "complete_chunks"
# ββ Embedding Model βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def load_embedding_model() -> SentenceTransformer:
"""Load the sentence transformer embedding model."""
with console.status("[bold cyan]Loading embedding model...[/bold cyan]"):
model = SentenceTransformer(EMBEDDING_MODEL)
console.print(f"[green]β[/green] Embedding model loaded: [cyan]{EMBEDDING_MODEL}[/cyan]")
return model
# ββ ChromaDB Client βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_pinecone_index():
"""Return a Pinecone index, creating it if it does not exist."""
pc = Pinecone(api_key=PINECONE_API_KEY)
existing = [i.name for i in pc.list_indexes()]
if PINECONE_INDEX not in existing:
pc.create_index(
name=PINECONE_INDEX,
dimension=EMBEDDING_DIM,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
)
return pc.Index(PINECONE_INDEX)
# ββ Chunk Builders ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _make_id(text: str) -> str:
"""Generate a stable unique ID from chunk text."""
return hashlib.md5(text.encode()).hexdigest()
def build_class_chunk(cls: ClassInfo, file_info: FileInfo) -> dict:
"""
Build a class-level chunk document.
Contains: class name, bases, docstring, all method signatures.
"""
method_signatures = []
for m in cls.methods:
params = ", ".join(
f"{p.name}: {p.annotation}" if p.annotation else p.name
for p in m.parameters
)
ret = f" -> {m.return_type}" if m.return_type else ""
method_signatures.append(f" def {m.name}({params}){ret}")
methods_block = "\n".join(method_signatures) if method_signatures else " # no methods"
bases_str = ", ".join(cls.bases) if cls.bases else "object"
docstring = cls.docstring or "No docstring provided."
text = (
f"Class: {cls.name}\n"
f"Inherits: {bases_str}\n"
f"Module: {file_info.module}\n"
f"File: {file_info.relative}\n"
f"Docstring: {docstring}\n"
f"Methods:\n{methods_block}"
)
metadata = {
"type": "class",
"name": cls.name,
"module": file_info.module,
"file": file_info.relative,
"bases": json.dumps(cls.bases),
"methods": json.dumps([m.name for m in cls.methods]),
"lineno": cls.lineno,
}
return {"id": _make_id(text), "text": text, "metadata": metadata}
def build_function_chunk(func: FunctionInfo,
file_info: FileInfo,
class_name: str | None = None,
class_docstring: str | None = None) -> dict:
"""
Build a function/method-level chunk document.
Carries class context as metadata so micro queries stay grounded.
"""
params = ", ".join(
f"{p.name}: {p.annotation}" if p.annotation else p.name
for p in func.parameters
)
ret = f" -> {func.return_type}" if func.return_type else ""
signature = f"def {func.name}({params}){ret}"
docstring = func.docstring or "No docstring provided."
calls_str = ", ".join(func.calls[:15]) if func.calls else "none"
class_ctx = (
f"Class: {class_name}\nClass purpose: {class_docstring or 'N/A'}\n"
if class_name else "Scope: top-level function\n"
)
text = (
f"{class_ctx}"
f"Function: {func.name}\n"
f"Module: {file_info.module}\n"
f"File: {file_info.relative}\n"
f"Signature: {signature}\n"
f"Docstring: {docstring}\n"
f"Calls: {calls_str}"
)
metadata = {
"type": "function",
"name": func.name,
"module": file_info.module,
"file": file_info.relative,
"class_name": class_name or "",
"return_type": func.return_type or "",
"parameters": json.dumps([p.name for p in func.parameters]),
"calls": json.dumps(func.calls[:15]),
"is_method": str(func.is_method),
"lineno": func.lineno,
}
return {"id": _make_id(text), "text": text, "metadata": metadata}
def build_module_chunk(file_info: FileInfo) -> dict:
"""
Build a module-level chunk for files that contain no classes or functions.
Captures imports and docstring as the indexable content.
"""
imports_str = ", ".join(file_info.imports) if file_info.imports else "none"
docstring = file_info.docstring or "No module docstring."
text = (
f"Module: {file_info.module}\n"
f"File: {file_info.relative}\n"
f"Docstring: {docstring}\n"
f"Imports: {imports_str}\n"
f"Note: This file contains only module-level statements."
)
metadata = {
"type": "module",
"name": Path(file_info.relative).stem,
"module": file_info.module,
"file": file_info.relative,
"class_name": "",
"return_type": "",
"parameters": "[]",
"calls": "[]",
"is_method": "False",
"lineno": 0,
}
return {"id": _make_id(text), "text": text, "metadata": metadata}
def build_complete_function_chunk(func: FunctionInfo,
file_info: FileInfo,
class_name: str | None = None,
class_docstring: str | None = None) -> dict:
"""
Build a complete function chunk including full source code.
Used for edge case analysis and usage example generation.
"""
params = ", ".join(
f"{p.name}: {p.annotation}" if p.annotation else p.name
for p in func.parameters
)
ret = f" -> {func.return_type}" if func.return_type else ""
signature = f"def {func.name}({params}){ret}"
docstring = func.docstring or "No docstring provided."
calls_str = ", ".join(func.calls[:15]) if func.calls else "none"
class_ctx = (
f"Class: {class_name}\nClass purpose: {class_docstring or 'N/A'}\n"
if class_name else "Scope: top-level function\n"
)
source_block = func.source if func.source else "Source not available."
text = (
f"{class_ctx}"
f"Function: {func.name}\n"
f"Module: {file_info.module}\n"
f"File: {file_info.relative}\n"
f"Signature: {signature}\n"
f"Docstring: {docstring}\n"
f"Calls: {calls_str}\n"
f"Source Code:\n{source_block}"
)
metadata = {
"type": "complete_function",
"name": func.name,
"module": file_info.module,
"file": file_info.relative,
"class_name": class_name or "",
"return_type": func.return_type or "",
"parameters": json.dumps([p.name for p in func.parameters]),
"calls": json.dumps(func.calls[:15]),
"is_method": str(func.is_method),
"lineno": func.lineno,
}
return {"id": _make_id(text), "text": text, "metadata": metadata}
def build_complete_class_chunk(cls: ClassInfo, file_info: FileInfo) -> dict:
"""
Build a complete class chunk including full source code.
Used for class-level deep queries.
"""
bases_str = ", ".join(cls.bases) if cls.bases else "object"
docstring = cls.docstring or "No docstring provided."
source_block = cls.source if cls.source else "Source not available."
text = (
f"Class: {cls.name}\n"
f"Inherits: {bases_str}\n"
f"Module: {file_info.module}\n"
f"File: {file_info.relative}\n"
f"Docstring: {docstring}\n"
f"Source Code:\n{source_block}"
)
metadata = {
"type": "complete_class",
"name": cls.name,
"module": file_info.module,
"file": file_info.relative,
"bases": json.dumps(cls.bases),
"methods": json.dumps([m.name for m in cls.methods]),
"lineno": cls.lineno,
}
return {"id": _make_id(text), "text": text, "metadata": metadata}
def build_file_chunk(file_info: FileInfo) -> dict:
"""
Build a file-level chunk containing the entire source of a file.
Used for file-wide queries.
"""
try:
source_block = Path(file_info.path).read_text(encoding="utf-8", errors="ignore")
except Exception:
source_block = "Source not available."
docstring = file_info.docstring or "No module docstring."
imports_str = ", ".join(file_info.imports) if file_info.imports else "none"
text = (
f"File: {file_info.relative}\n"
f"Module: {file_info.module}\n"
f"Docstring: {docstring}\n"
f"Imports: {imports_str}\n"
f"Source Code:\n{source_block}"
)
metadata = {
"type": "file",
"name": Path(file_info.relative).stem,
"module": file_info.module,
"file": file_info.relative,
"class_name": "",
"return_type": "",
"parameters": "[]",
"calls": "[]",
"is_method": "False",
"lineno": 0,
}
return {"id": _make_id(text), "text": text, "metadata": metadata}
# ββ Embedding & Upserting βββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _upsert_batch(index, chunks: list[dict], model: SentenceTransformer, namespace: str) -> None:
"""Embed and upsert a list of chunks into a Pinecone namespace."""
if not chunks:
return
texts = [c["text"] for c in chunks]
ids = [c["id"] for c in chunks]
metadatas = [c["metadata"] for c in chunks]
embeddings = model.encode(texts, show_progress_bar=False).tolist()
vectors = [
{"id": vid, "values": vec, "metadata": {**meta, "text": txt}}
for vid, vec, meta, txt in zip(ids, embeddings, metadatas, texts)
]
index.upsert(vectors=vectors, namespace=namespace)
# ββ Main Embed Pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def embed_codebase(root_path: str) -> None:
"""
Full pipeline:
1. Parse codebase via ast_parser
2. Build hybrid chunks (class + function level)
3. Embed with sentence-transformers
4. Store in ChromaDB (two collections)
Args:
root_path: Absolute path to the monolithic codebase root.
"""
console.rule("[bold cyan]Codebase Oracle β Embedder[/bold cyan]")
# Step 1 β Parse
console.print(f"\n[bold]π Root:[/bold] {root_path}\n")
parsed_files: list[FileInfo] = parse_codebase(root_path)
if not parsed_files:
console.print("[yellow]β No Python files parsed. Exiting.[/yellow]")
return
# Step 2 β Build chunks
class_chunks: list[dict] = []
function_chunks: list[dict] = []
for file_info in parsed_files:
# Class-level chunks
for cls in file_info.classes:
class_chunks.append(build_class_chunk(cls, file_info))
# Method-level chunks (carry class context)
for method in cls.methods:
function_chunks.append(build_function_chunk(
method, file_info,
class_name=cls.name,
class_docstring=cls.docstring,
))
# Top-level function chunks
for func in file_info.functions:
function_chunks.append(build_function_chunk(func, file_info))
# Module-level chunk for files with no classes and no functions
if not file_info.classes and not file_info.functions:
function_chunks.append(build_module_chunk(file_info))
complete_chunks: list[dict] = []
for file_info in parsed_files:
complete_chunks.append(build_file_chunk(file_info))
for cls in file_info.classes:
complete_chunks.append(build_complete_class_chunk(cls, file_info))
for method in cls.methods:
complete_chunks.append(build_complete_function_chunk(
method, file_info,
class_name=cls.name,
class_docstring=cls.docstring,
))
for func in file_info.functions:
complete_chunks.append(build_complete_function_chunk(func, file_info))
console.print(
f"[green]β[/green] Chunks built: "
f"[magenta]{len(class_chunks)}[/magenta] class chunks Β· "
f"[cyan]{len(function_chunks)}[/cyan] function chunks Β· "
f"[yellow]{len(complete_chunks)}[/yellow] complete chunks\n"
)
# Step 3 β Load model
model = load_embedding_model()
# Step 4 β Pinecone
index = get_pinecone_index()
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("{task.completed}/{task.total}"),
console=console,
) as progress:
# Embed class chunks in batches of 32
BATCH = 32
task1 = progress.add_task(
"[magenta]Embedding class chunks...", total=len(class_chunks)
)
for i in range(0, len(class_chunks), BATCH):
batch = class_chunks[i:i + BATCH]
_upsert_batch(index, batch, model, CLASS_COLLECTION)
progress.advance(task1, len(batch))
task2 = progress.add_task(
"[cyan]Embedding function chunks...", total=len(function_chunks)
)
for i in range(0, len(function_chunks), BATCH):
batch = function_chunks[i:i + BATCH]
_upsert_batch(index, batch, model, FUNCTION_COLLECTION)
progress.advance(task2, len(batch))
task3 = progress.add_task(
"[yellow]Embedding complete chunks...", total=len(complete_chunks)
)
for i in range(0, len(complete_chunks), BATCH):
batch = complete_chunks[i:i + BATCH]
_upsert_batch(index, batch, model, COMPLETE_COLLECTION)
progress.advance(task3, len(batch))
# Step 5 β Summary
_render_embed_summary(root_path, class_chunks, function_chunks, complete_chunks)
def _render_embed_summary(root_path: str,
class_chunks: list[dict],
function_chunks: list[dict],
complete_chunks: list[dict]) -> None:
"""Render a rich summary panel after embedding."""
table = Table(box=box.SIMPLE, show_header=False, padding=(0, 2))
table.add_column(style="dim")
table.add_column(style="bold white")
table.add_row("Codebase", root_path)
table.add_row("Embedding model", EMBEDDING_MODEL)
table.add_row("Class chunks", str(len(class_chunks)))
table.add_row("Function chunks", str(len(function_chunks)))
table.add_row("Complete chunks", str(len(complete_chunks)))
table.add_row("Total chunks", str(len(class_chunks) + len(function_chunks) + len(complete_chunks)))
table.add_row("Collections", f"{CLASS_COLLECTION}, {FUNCTION_COLLECTION}, {COMPLETE_COLLECTION}")
table.add_row("Status", "[bold green]β Indexing complete[/bold green]")
console.print(Panel(table, title="[bold cyan]Embedding Summary[/bold cyan]",
border_style="cyan"))
console.print("\n[bold green]β Codebase indexed. Ready for queries.[/bold green]\n")
# ββ Query Helper (for retriever.py later) ββββββββββββββββββββββββββββββββββββ
def query_chunks(query: str,
collection_name: str,
model: SentenceTransformer,
n_results: int = 5,
filters: dict | None = None) -> list[dict]:
"""
Query a Pinecone namespace and return top-n matching chunks.
Args:
query: Natural language query string.
collection_name: Namespace β CLASS_COLLECTION, FUNCTION_COLLECTION, or COMPLETE_COLLECTION.
model: Loaded SentenceTransformer model.
n_results: Number of results to return.
filters: Optional Pinecone metadata filters.
Returns:
List of dicts with keys: text, metadata, distance.
"""
index = get_pinecone_index()
embedding = model.encode([query]).tolist()[0]
kwargs: dict = {
"vector": embedding,
"top_k": n_results,
"namespace": collection_name,
"include_metadata": True,
}
if filters:
kwargs["filter"] = filters
results = index.query(**kwargs)
output = []
for match in results["matches"]:
meta = dict(match["metadata"])
text = meta.pop("text", "")
output.append({
"text": text,
"metadata": meta,
"distance": 1 - match["score"],
})
return output
# ββ Entry Point βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
import sys
path = sys.argv[1] if len(sys.argv) > 1 else "."
try:
embed_codebase(path)
except (FileNotFoundError, NotADirectoryError) as e:
console.print(f"[red]β {e}[/red]")
sys.exit(1) |