Spaces:
Sleeping
Sleeping
Update app/policy_vector_db.py
Browse files- app/policy_vector_db.py +15 -4
app/policy_vector_db.py
CHANGED
|
@@ -10,7 +10,6 @@ import logging
|
|
| 10 |
logger = logging.getLogger("app")
|
| 11 |
|
| 12 |
class PolicyVectorDB:
|
| 13 |
-
# ... (the class code is correct and does not need changes) ...
|
| 14 |
def __init__(self, persist_directory: str, top_k_default: int = 5, relevance_threshold: float = 0.5):
|
| 15 |
self.persist_directory = persist_directory
|
| 16 |
self.client = chromadb.PersistentClient(path=persist_directory, settings=Settings(allow_reset=True))
|
|
@@ -55,7 +54,16 @@ class PolicyVectorDB:
|
|
| 55 |
batch = new_chunks[i:i + batch_size]
|
| 56 |
texts = [chunk['text'] for chunk in batch]
|
| 57 |
ids = [str(chunk['id']) for chunk in batch]
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
embeddings = self.embedding_model.encode(texts, show_progress_bar=False).tolist()
|
| 60 |
collection.add(ids=ids, embeddings=embeddings, documents=texts, metadatas=metadatas)
|
| 61 |
logger.info(f"Added batch {i//batch_size + 1}/{(len(new_chunks) + batch_size - 1) // batch_size}")
|
|
@@ -90,9 +98,12 @@ def ensure_db_populated(db_instance: PolicyVectorDB, chunks_file_path: str):
|
|
| 90 |
logger.error(f"Chunks file not found at {chunks_file_path}. Cannot populate DB.")
|
| 91 |
return False
|
| 92 |
|
| 93 |
-
|
| 94 |
with open(chunks_file_path, 'r', encoding='utf-8') as f:
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
if not chunks_to_add:
|
| 98 |
logger.warning(f"Chunks file at {chunks_file_path} is empty. No data to add to DB.")
|
|
|
|
| 10 |
logger = logging.getLogger("app")
|
| 11 |
|
| 12 |
class PolicyVectorDB:
|
|
|
|
| 13 |
def __init__(self, persist_directory: str, top_k_default: int = 5, relevance_threshold: float = 0.5):
|
| 14 |
self.persist_directory = persist_directory
|
| 15 |
self.client = chromadb.PersistentClient(path=persist_directory, settings=Settings(allow_reset=True))
|
|
|
|
| 54 |
batch = new_chunks[i:i + batch_size]
|
| 55 |
texts = [chunk['text'] for chunk in batch]
|
| 56 |
ids = [str(chunk['id']) for chunk in batch]
|
| 57 |
+
|
| 58 |
+
# --- FIX: Ensure metadata is never an empty dictionary ---
|
| 59 |
+
metadatas = []
|
| 60 |
+
for chunk in batch:
|
| 61 |
+
meta = chunk.get('metadata')
|
| 62 |
+
if not meta: # Handles cases where metadata is missing or empty
|
| 63 |
+
# Provide a default, non-empty metadata dictionary
|
| 64 |
+
meta = {"description": "General information chunk."}
|
| 65 |
+
metadatas.append(self._flatten_metadata(meta))
|
| 66 |
+
|
| 67 |
embeddings = self.embedding_model.encode(texts, show_progress_bar=False).tolist()
|
| 68 |
collection.add(ids=ids, embeddings=embeddings, documents=texts, metadatas=metadatas)
|
| 69 |
logger.info(f"Added batch {i//batch_size + 1}/{(len(new_chunks) + batch_size - 1) // batch_size}")
|
|
|
|
| 98 |
logger.error(f"Chunks file not found at {chunks_file_path}. Cannot populate DB.")
|
| 99 |
return False
|
| 100 |
|
| 101 |
+
chunks_to_add = []
|
| 102 |
with open(chunks_file_path, 'r', encoding='utf-8') as f:
|
| 103 |
+
for line in f:
|
| 104 |
+
line = line.strip()
|
| 105 |
+
if line:
|
| 106 |
+
chunks_to_add.append(json.loads(line))
|
| 107 |
|
| 108 |
if not chunks_to_add:
|
| 109 |
logger.warning(f"Chunks file at {chunks_file_path} is empty. No data to add to DB.")
|