Update app/utils/rag.py
Browse files- app/utils/rag.py +52 -14
app/utils/rag.py
CHANGED
|
@@ -1,19 +1,31 @@
|
|
| 1 |
-
"""RAG utilities —
|
| 2 |
|
|
|
|
| 3 |
import uuid
|
| 4 |
import streamlit as st
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import chromadb
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
_chroma_client = None
|
| 11 |
_embedding_model_failed = False
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def _get_client():
|
| 14 |
global _chroma_client
|
| 15 |
if _chroma_client is None:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return _chroma_client
|
| 18 |
|
| 19 |
|
|
@@ -32,11 +44,24 @@ def get_embedding_model():
|
|
| 32 |
|
| 33 |
|
| 34 |
def add_file(session_id: str, filename: str, text: str):
|
|
|
|
|
|
|
| 35 |
client = _get_client()
|
|
|
|
|
|
|
| 36 |
model = get_embedding_model()
|
|
|
|
|
|
|
|
|
|
| 37 |
collection_name = f"s{session_id[:32]}"
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
try:
|
| 41 |
existing = collection.get(where={"source": filename})
|
| 42 |
if existing["ids"]:
|
|
@@ -49,20 +74,20 @@ def add_file(session_id: str, filename: str, text: str):
|
|
| 49 |
return
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
if model is None:
|
| 53 |
-
print(f"[RAG] Skipping embeddings for {filename} — embedding model unavailable")
|
| 54 |
-
return
|
| 55 |
embeddings = model.encode(chunks).tolist()
|
| 56 |
ids = [str(uuid.uuid4()) for _ in chunks]
|
| 57 |
metadatas = [{"source": filename} for _ in chunks]
|
| 58 |
collection.add(embeddings=embeddings, documents=chunks, ids=ids, metadatas=metadatas)
|
| 59 |
except Exception as e:
|
| 60 |
print(f"[RAG ERROR] Failed to add file {filename}: {e}")
|
| 61 |
-
print(f"[RAG] File uploaded but semantic search disabled for this file")
|
| 62 |
|
| 63 |
|
| 64 |
def remove_file(session_id: str, filename: str):
|
|
|
|
|
|
|
| 65 |
client = _get_client()
|
|
|
|
|
|
|
| 66 |
collection_name = f"s{session_id[:32]}"
|
| 67 |
try:
|
| 68 |
collection = client.get_collection(name=collection_name)
|
|
@@ -74,7 +99,11 @@ def remove_file(session_id: str, filename: str):
|
|
| 74 |
|
| 75 |
|
| 76 |
def clear_session(session_id: str):
|
|
|
|
|
|
|
| 77 |
client = _get_client()
|
|
|
|
|
|
|
| 78 |
collection_name = f"s{session_id[:32]}"
|
| 79 |
try:
|
| 80 |
client.delete_collection(name=collection_name)
|
|
@@ -83,14 +112,23 @@ def clear_session(session_id: str):
|
|
| 83 |
|
| 84 |
|
| 85 |
def query_context(session_id: str, query: str, n_results: int = 8) -> str:
|
|
|
|
|
|
|
| 86 |
client = _get_client()
|
|
|
|
|
|
|
| 87 |
model = get_embedding_model()
|
|
|
|
|
|
|
|
|
|
| 88 |
collection_name = f"s{session_id[:32]}"
|
| 89 |
try:
|
| 90 |
-
if model is None:
|
| 91 |
-
print("[RAG] Semantic search disabled — embedding model unavailable")
|
| 92 |
-
return ""
|
| 93 |
collection = client.get_collection(name=collection_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
count = collection.count()
|
| 95 |
if count == 0:
|
| 96 |
return ""
|
|
@@ -103,7 +141,7 @@ def query_context(session_id: str, query: str, n_results: int = 8) -> str:
|
|
| 103 |
)
|
| 104 |
docs = results["documents"][0]
|
| 105 |
metas = results["metadatas"][0]
|
| 106 |
-
grouped
|
| 107 |
for doc, meta in zip(docs, metas):
|
| 108 |
src = meta.get("source", "unknown")
|
| 109 |
grouped.setdefault(src, []).append(doc)
|
|
@@ -112,7 +150,7 @@ def query_context(session_id: str, query: str, n_results: int = 8) -> str:
|
|
| 112 |
parts.append(f"# from {src}\n" + "\n".join(lines))
|
| 113 |
return "\n\n".join(parts)
|
| 114 |
except Exception as e:
|
| 115 |
-
print(f"[RAG ERROR] {e}")
|
| 116 |
return ""
|
| 117 |
|
| 118 |
|
|
|
|
| 1 |
+
"""RAG utilities — persistent ChromaDB with graceful fallback."""
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
import uuid
|
| 5 |
import streamlit as st
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
import chromadb
|
| 8 |
|
| 9 |
+
# Storage directory for ChromaDB (persistent across calls, but not across Space restart)
|
| 10 |
+
CHROMA_PERSIST_DIR = "/tmp/secforge_chroma"
|
| 11 |
+
os.makedirs(CHROMA_PERSIST_DIR, exist_ok=True)
|
| 12 |
+
|
| 13 |
_chroma_client = None
|
| 14 |
_embedding_model_failed = False
|
| 15 |
+
_rag_enabled = True # global flag to disable RAG after any fatal error
|
| 16 |
+
|
| 17 |
|
| 18 |
def _get_client():
|
| 19 |
global _chroma_client
|
| 20 |
if _chroma_client is None:
|
| 21 |
+
try:
|
| 22 |
+
# Use persistent client instead of ephemeral
|
| 23 |
+
_chroma_client = chromadb.PersistentClient(path=CHROMA_PERSIST_DIR)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"[RAG FATAL] Could not create ChromaDB client: {e}")
|
| 26 |
+
global _rag_enabled
|
| 27 |
+
_rag_enabled = False
|
| 28 |
+
return None
|
| 29 |
return _chroma_client
|
| 30 |
|
| 31 |
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
def add_file(session_id: str, filename: str, text: str):
|
| 47 |
+
if not _rag_enabled:
|
| 48 |
+
return
|
| 49 |
client = _get_client()
|
| 50 |
+
if client is None:
|
| 51 |
+
return
|
| 52 |
model = get_embedding_model()
|
| 53 |
+
if model is None:
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
collection_name = f"s{session_id[:32]}"
|
| 57 |
+
try:
|
| 58 |
+
# Get or create collection - use get_or_create_collection to avoid race conditions
|
| 59 |
+
collection = client.get_or_create_collection(name=collection_name)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"[RAG ERROR] Failed to get/create collection: {e}")
|
| 62 |
+
return
|
| 63 |
|
| 64 |
+
# Remove existing chunks for this file
|
| 65 |
try:
|
| 66 |
existing = collection.get(where={"source": filename})
|
| 67 |
if existing["ids"]:
|
|
|
|
| 74 |
return
|
| 75 |
|
| 76 |
try:
|
|
|
|
|
|
|
|
|
|
| 77 |
embeddings = model.encode(chunks).tolist()
|
| 78 |
ids = [str(uuid.uuid4()) for _ in chunks]
|
| 79 |
metadatas = [{"source": filename} for _ in chunks]
|
| 80 |
collection.add(embeddings=embeddings, documents=chunks, ids=ids, metadatas=metadatas)
|
| 81 |
except Exception as e:
|
| 82 |
print(f"[RAG ERROR] Failed to add file {filename}: {e}")
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
def remove_file(session_id: str, filename: str):
|
| 86 |
+
if not _rag_enabled:
|
| 87 |
+
return
|
| 88 |
client = _get_client()
|
| 89 |
+
if client is None:
|
| 90 |
+
return
|
| 91 |
collection_name = f"s{session_id[:32]}"
|
| 92 |
try:
|
| 93 |
collection = client.get_collection(name=collection_name)
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
def clear_session(session_id: str):
|
| 102 |
+
if not _rag_enabled:
|
| 103 |
+
return
|
| 104 |
client = _get_client()
|
| 105 |
+
if client is None:
|
| 106 |
+
return
|
| 107 |
collection_name = f"s{session_id[:32]}"
|
| 108 |
try:
|
| 109 |
client.delete_collection(name=collection_name)
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
def query_context(session_id: str, query: str, n_results: int = 8) -> str:
|
| 115 |
+
if not _rag_enabled:
|
| 116 |
+
return ""
|
| 117 |
client = _get_client()
|
| 118 |
+
if client is None:
|
| 119 |
+
return ""
|
| 120 |
model = get_embedding_model()
|
| 121 |
+
if model is None:
|
| 122 |
+
return ""
|
| 123 |
+
|
| 124 |
collection_name = f"s{session_id[:32]}"
|
| 125 |
try:
|
|
|
|
|
|
|
|
|
|
| 126 |
collection = client.get_collection(name=collection_name)
|
| 127 |
+
except Exception:
|
| 128 |
+
# Collection does not exist – no context
|
| 129 |
+
return ""
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
count = collection.count()
|
| 133 |
if count == 0:
|
| 134 |
return ""
|
|
|
|
| 141 |
)
|
| 142 |
docs = results["documents"][0]
|
| 143 |
metas = results["metadatas"][0]
|
| 144 |
+
grouped = {}
|
| 145 |
for doc, meta in zip(docs, metas):
|
| 146 |
src = meta.get("source", "unknown")
|
| 147 |
grouped.setdefault(src, []).append(doc)
|
|
|
|
| 150 |
parts.append(f"# from {src}\n" + "\n".join(lines))
|
| 151 |
return "\n\n".join(parts)
|
| 152 |
except Exception as e:
|
| 153 |
+
print(f"[RAG ERROR] query failed: {e}")
|
| 154 |
return ""
|
| 155 |
|
| 156 |
|