Update app/utils/rag.py
Browse files- app/utils/rag.py +62 -52
app/utils/rag.py
CHANGED
|
@@ -1,55 +1,72 @@
|
|
| 1 |
"""RAG utilities — multi-file, per-session ChromaDB collections."""
|
| 2 |
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
| 4 |
import uuid
|
| 5 |
-
import warnings
|
| 6 |
import streamlit as st
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
import chromadb
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
warnings.filterwarnings("ignore", message=".*Tried to instantiate class '__path__._path'.*")
|
| 12 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 13 |
|
| 14 |
-
# Disable ChromaDB telemetry entirely to avoid posthog capture() crash
|
| 15 |
-
os.environ["ANONYMIZED_TELEMETRY"] = "False"
|
| 16 |
-
os.environ["CHROMA_TELEMETRY"] = "False"
|
| 17 |
|
| 18 |
-
_chroma_client = None
|
| 19 |
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def _get_client():
|
| 22 |
global _chroma_client
|
| 23 |
if _chroma_client is None:
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return _chroma_client
|
| 26 |
|
| 27 |
|
| 28 |
@st.cache_resource
|
| 29 |
def get_embedding_model():
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
return SentenceTransformer("all-MiniLM-L6-v2")
|
| 32 |
except Exception as e:
|
| 33 |
-
print(f"[RAG]
|
|
|
|
|
|
|
| 34 |
return None
|
| 35 |
|
| 36 |
|
| 37 |
def add_file(session_id: str, filename: str, text: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
return
|
| 54 |
|
| 55 |
embeddings = model.encode(chunks).tolist()
|
|
@@ -57,58 +74,51 @@ def add_file(session_id: str, filename: str, text: str):
|
|
| 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]
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
def remove_file(session_id: str, filename: str):
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
collection = client.get_collection(name=collection_name)
|
| 68 |
existing = collection.get(where={"source": filename})
|
| 69 |
if existing["ids"]:
|
| 70 |
-
collection.delete(ids=existing["ids"])
|
| 71 |
-
except Exception:
|
| 72 |
-
pass
|
| 73 |
|
| 74 |
|
| 75 |
def clear_session(session_id: str):
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
-
client
|
| 78 |
-
|
| 79 |
except Exception:
|
| 80 |
pass
|
| 81 |
|
| 82 |
|
| 83 |
def query_context(session_id: str, query: str, n_results: int = 8) -> str:
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
if model is None:
|
| 88 |
return ""
|
| 89 |
-
|
| 90 |
collection = client.get_collection(name=collection_name)
|
| 91 |
count = collection.count()
|
| 92 |
if count == 0:
|
| 93 |
-
return ""
|
| 94 |
-
actual_n = min(n_results, count)
|
| 95 |
-
query_embedding = model.encode([query]).tolist()
|
| 96 |
-
results = collection.query(
|
| 97 |
-
query_embeddings=query_embedding,
|
| 98 |
-
n_results=actual_n,
|
| 99 |
-
include=["documents", "metadatas"],
|
| 100 |
-
)
|
| 101 |
-
docs = results["documents"][0]
|
| 102 |
-
metas = results["metadatas"][0]
|
| 103 |
-
grouped: dict = {}
|
| 104 |
for doc, meta in zip(docs, metas):
|
| 105 |
src = meta.get("source", "unknown")
|
| 106 |
grouped.setdefault(src, []).append(doc)
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
-
print(f"[RAG]
|
| 110 |
return ""
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
def add_document(collection_name: str, text: str):
|
| 114 |
-
add_file(collection_name, "linked_file", text)
|
|
|
|
| 1 |
"""RAG utilities — multi-file, per-session ChromaDB collections."""
|
| 2 |
|
| 3 |
import os
|
| 4 |
+
# Must be set before importing chromadb
|
| 5 |
+
os.environ["CHROMA_TELEMETRY_DISABLED"] = "true"
|
| 6 |
+
|
| 7 |
import uuid
|
|
|
|
| 8 |
import streamlit as st
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
import chromadb
|
| 11 |
+
from chromadb.config import Settings
|
| 12 |
|
| 13 |
+
# Module-level client — lives for the lifetime of the server process
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
|
| 17 |
+
_chroma_client = None
|
| 18 |
+
_embedding_model_failed = False
|
| 19 |
|
| 20 |
def _get_client():
|
| 21 |
global _chroma_client
|
| 22 |
if _chroma_client is None:
|
| 23 |
+
# Create client with telemetry completely disabled
|
| 24 |
+
_chroma_client = chromadb.EphemeralClient(
|
| 25 |
+
Settings(
|
| 26 |
+
anonymous_telemetry=False,
|
| 27 |
+
chroma_telemetry_impl="none", # disable telemetry client entirely
|
| 28 |
+
allow_reset=True,
|
| 29 |
+
is_persistent=False
|
| 30 |
+
)
|
| 31 |
+
)
|
| 32 |
return _chroma_client
|
| 33 |
|
| 34 |
|
| 35 |
@st.cache_resource
|
| 36 |
def get_embedding_model():
|
| 37 |
+
global _embedding_model_failed
|
| 38 |
+
if _embedding_model_failed:
|
| 39 |
+
return None
|
| 40 |
try:
|
| 41 |
return SentenceTransformer("all-MiniLM-L6-v2")
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"[RAG WARNING] Failed to load embedding model: {e}")
|
| 44 |
+
print("[RAG WARNING] File uploads will work but semantic search will be disabled")
|
| 45 |
+
_embedding_model_failed = True
|
| 46 |
return None
|
| 47 |
|
| 48 |
|
| 49 |
def add_file(session_id: str, filename: str, text: str):
|
| 50 |
+
client = _get_client()
|
| 51 |
+
model = get_embedding_model()
|
| 52 |
+
collection_name = f"s{session_id[:32]}"
|
| 53 |
+
collection = client.get_or_create_collection(name=collection_name)
|
| 54 |
+
|
| 55 |
try:
|
| 56 |
+
existing = collection.get(where={"source": filename})
|
| 57 |
+
if existing["ids"]:
|
| 58 |
+
collection.delete(ids=existing["ids"])
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
chunks = [line.strip() for line in text.split("\n") if line.strip()]
|
| 63 |
+
if not chunks:
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
if model is None:
|
| 68 |
+
print(f"[RAG] Skipping embeddings for {filename} — embedding model unavailable")
|
| 69 |
+
|
| 70 |
return
|
| 71 |
|
| 72 |
embeddings = model.encode(chunks).tolist()
|
|
|
|
| 74 |
metadatas = [{"source": filename} for _ in chunks]
|
| 75 |
collection.add(embeddings=embeddings, documents=chunks, ids=ids, metadatas=metadatas)
|
| 76 |
except Exception as e:
|
| 77 |
+
print(f"[RAG ERROR] Failed to add file {filename}: {e}")
|
| 78 |
+
print(f"[RAG] File uploaded but semantic search disabled for this file")
|
| 79 |
|
| 80 |
|
| 81 |
def remove_file(session_id: str, filename: str):
|
| 82 |
+
client = _get_client()
|
| 83 |
+
collection_name = f"s{session_id[:32]}"
|
| 84 |
try:
|
| 85 |
+
|
| 86 |
+
|
| 87 |
collection = client.get_collection(name=collection_name)
|
| 88 |
existing = collection.get(where={"source": filename})
|
| 89 |
if existing["ids"]:
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
def clear_session(session_id: str):
|
| 93 |
+
client = _get_client()
|
| 94 |
+
collection_name = f"s{session_id[:32]}"
|
| 95 |
try:
|
| 96 |
+
client.delete_collection(name=collection_name)
|
| 97 |
+
|
| 98 |
except Exception:
|
| 99 |
pass
|
| 100 |
|
| 101 |
|
| 102 |
def query_context(session_id: str, query: str, n_results: int = 8) -> str:
|
| 103 |
+
client = _get_client()
|
| 104 |
+
model = get_embedding_model()
|
| 105 |
+
collection_name = f"s{session_id[:32]}"
|
| 106 |
try:
|
| 107 |
+
|
| 108 |
+
|
| 109 |
if model is None:
|
| 110 |
return ""
|
| 111 |
+
|
| 112 |
collection = client.get_collection(name=collection_name)
|
| 113 |
count = collection.count()
|
| 114 |
if count == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
for doc, meta in zip(docs, metas):
|
| 116 |
src = meta.get("source", "unknown")
|
| 117 |
grouped.setdefault(src, []).append(doc)
|
| 118 |
+
parts = []
|
| 119 |
+
for src, lines in grouped.items():
|
| 120 |
+
parts.append(f"# from {src}\n" + "\n".join(lines))
|
| 121 |
+
return "\n\n".join(parts)
|
| 122 |
except Exception as e:
|
| 123 |
+
print(f"[RAG ERROR] {e}")
|
| 124 |
return ""
|
|
|
|
|
|
|
|
|
|
|
|