rk147 commited on
Commit
ddef94b
·
verified ·
1 Parent(s): c64f157

Update app/utils/rag.py

Browse files
Files changed (1) hide show
  1. app/utils/rag.py +8 -9
app/utils/rag.py CHANGED
@@ -9,32 +9,31 @@ def get_embedding_model():
9
 
10
  @st.cache_resource
11
  def get_chroma_client():
12
- return chromadb.Client()
13
 
14
  def add_document(collection_name: str, text: str):
15
  client = get_chroma_client()
16
  model = get_embedding_model()
17
  collection = client.get_or_create_collection(name=collection_name)
 
 
 
 
 
 
18
  chunks = [line.strip() for line in text.split("\n") if line.strip()]
19
  if not chunks:
20
  return
21
  embeddings = model.encode(chunks).tolist()
22
  ids = [str(uuid.uuid4()) for _ in chunks]
23
  collection.add(embeddings=embeddings, documents=chunks, ids=ids)
24
- collection.add(embeddings=embeddings, documents=chunks, ids=ids)
25
- print(f"[DEBUG] Added {len(chunks)} chunks to collection: {collection_name}") # DIAGNOSTIC
26
 
27
  def query_context(collection_name: str, query: str, n_results: int = 5) -> str:
28
  client = get_chroma_client()
29
  model = get_embedding_model()
30
-
31
-
32
-
33
  try:
34
  collection = client.get_collection(name=collection_name)
35
  count = collection.count()
36
- print(f"[DEBUG] Collection found, doc count: {count}") # DIAGNOSTIC
37
-
38
  if count == 0:
39
  return ""
40
  actual_n = min(n_results, count)
@@ -42,5 +41,5 @@ def query_context(collection_name: str, query: str, n_results: int = 5) -> str:
42
  results = collection.query(query_embeddings=query_embedding, n_results=actual_n)
43
  return "\n".join(results["documents"][0])
44
  except Exception as e:
45
- print(f"[DEBUG] query_context error: {e}") # DIAGNOSTIC
46
  return ""
 
9
 
10
  @st.cache_resource
11
  def get_chroma_client():
12
+ return chromadb.EphemeralClient() # explicit in-memory client
13
 
14
  def add_document(collection_name: str, text: str):
15
  client = get_chroma_client()
16
  model = get_embedding_model()
17
  collection = client.get_or_create_collection(name=collection_name)
18
+
19
+ # Clear existing docs first to avoid duplicate ID errors on re-upload
20
+ existing = collection.count()
21
+ if existing > 0:
22
+ collection.delete(where={"$and": []})
23
+
24
  chunks = [line.strip() for line in text.split("\n") if line.strip()]
25
  if not chunks:
26
  return
27
  embeddings = model.encode(chunks).tolist()
28
  ids = [str(uuid.uuid4()) for _ in chunks]
29
  collection.add(embeddings=embeddings, documents=chunks, ids=ids)
 
 
30
 
31
  def query_context(collection_name: str, query: str, n_results: int = 5) -> str:
32
  client = get_chroma_client()
33
  model = get_embedding_model()
 
 
 
34
  try:
35
  collection = client.get_collection(name=collection_name)
36
  count = collection.count()
 
 
37
  if count == 0:
38
  return ""
39
  actual_n = min(n_results, count)
 
41
  results = collection.query(query_embeddings=query_embedding, n_results=actual_n)
42
  return "\n".join(results["documents"][0])
43
  except Exception as e:
44
+ print(f"[RAG ERROR] {e}")
45
  return ""