feat(rag): adatta pipeline allo schema chunks AST-based + ottimizza system prompt
config.py: - EMBED_MODEL: qwen3-embedding:0.6b → bge-m3 (multilingua, migliore su testi accademici) - SYSTEM_PROMPT: lingua esplicita, anti-allucinazione rafforzata, citazione strutturata con percorso sezione, passaggi numerati per spiegazioni, fallback al plurale ingestion/ingest.py: - embed su content_for_embedding (prefisso header contestuale) - store content_original in ChromaDB (testo pulito per retrieval) - metadata aggiornati: header_path, chunk_index, content_type, flags, start/end_line rag.py, retrieve.py: - sostituisce sezione/titolo (schema vecchio) con header_path (schema AST) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+7
-10
@@ -65,7 +65,7 @@ def embed(text: str) -> list[float]:
|
||||
def retrieve(collection: chromadb.Collection, query: str, top_k: int) -> list[dict]:
|
||||
"""
|
||||
Genera l'embedding della query e recupera i top_k chunk più simili.
|
||||
Ritorna lista di dict con chiavi: rank, similarity, sezione, titolo, text.
|
||||
Ritorna lista di dict con chiavi: rank, similarity, source, header_path, text.
|
||||
"""
|
||||
vector = embed(query)
|
||||
results = collection.query(
|
||||
@@ -83,12 +83,11 @@ def retrieve(collection: chromadb.Collection, query: str, top_k: int) -> list[di
|
||||
start=1,
|
||||
):
|
||||
chunks.append({
|
||||
"rank": rank,
|
||||
"similarity": round(1 - dist, 4),
|
||||
"source": meta.get("source", ""),
|
||||
"sezione": meta.get("sezione", ""),
|
||||
"titolo": meta.get("titolo", ""),
|
||||
"text": text,
|
||||
"rank": rank,
|
||||
"similarity": round(1 - dist, 4),
|
||||
"source": meta.get("source", ""),
|
||||
"header_path": meta.get("header_path", ""),
|
||||
"text": text,
|
||||
})
|
||||
return chunks
|
||||
|
||||
@@ -99,9 +98,7 @@ def print_results(chunks: list[dict], full: bool = False) -> None:
|
||||
print(f"── {len(chunks)} chunk recuperati ─────────────────────────────────\n")
|
||||
for c in chunks:
|
||||
src = f"[{c['source']}] " if c.get("source") else ""
|
||||
loc = c["sezione"]
|
||||
if c["titolo"]:
|
||||
loc += f" > {c['titolo']}"
|
||||
loc = c["header_path"] or ""
|
||||
print(f" [{c['rank']}] similarità: {c['similarity']:.4f} | {src}{loc}")
|
||||
if full:
|
||||
print()
|
||||
|
||||
Reference in New Issue
Block a user