Repository navigation
Expand file tree
/
Copy pathrag_engine.py
More file actions
58 lines (47 loc) · 1.84 KB
/
Copy pathrag_engine.py
File metadata and controls
58 lines (47 loc) · 1.84 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
import os
import chromadb
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
VAULT_DIR = os.path.join(BASE_DIR, "vault_data")
DB_PATH = os.path.join(VAULT_DIR, "chroma_db")
_db = None
_collection = None
def get_db():
global _db
if _db is None:
_db = chromadb.PersistentClient(path=DB_PATH)
return _db
def get_collection():
global _collection
if _collection is None:
_collection = get_db().get_or_create_collection(name="arsenal_docs")
return _collection
def search_vault(query_text: str, top_k: int = 5, threshold: float = 1.2) -> str:
"""
Searches the local RAG vault for technical context matching the query.
Returns formatted string of results.
"""
try:
collection = get_collection()
results = collection.query(
query_texts=[query_text],
n_results=top_k * 2,
include=["documents", "distances", "metadatas"]
)
if not results['documents'] or not results['documents'][0]:
return "No relevant data found in the vault."
docs = results['documents'][0]
distances = results['distances'][0]
metadatas = results['metadatas'][0]
filtered = [
(doc, dist, meta) for doc, dist, meta in zip(docs, distances, metadatas)
if dist < threshold
][:top_k]
if not filtered:
return "No data found matching the required relevance threshold."
context_parts = []
for doc, dist, meta in filtered:
filename = meta.get('filename', os.path.basename(meta.get('source', 'unknown')))
context_parts.append(f"[SOURCE: {filename}]\n{doc}")
return "\n\n--- [VAULT CHUNK] ---\n\n".join(context_parts)
except Exception as e:
return f"Error searching vault: {str(e)}"