Spaces:
Running
Running
Merge branch 'main' of https://huggingface.co/spaces/Kalpokoch/ChatbotDemo
Browse files- app/policy_vector_db.py +4 -2
app/policy_vector_db.py
CHANGED
|
@@ -10,6 +10,7 @@ import logging
|
|
| 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))
|
|
@@ -42,7 +43,7 @@ class PolicyVectorDB:
|
|
| 42 |
except Exception as e:
|
| 43 |
logger.warning(f"Could not retrieve existing IDs from ChromaDB: {e}. Assuming no existing IDs for now.")
|
| 44 |
|
| 45 |
-
new_chunks = [chunk for chunk in chunks if chunk.get('id') and chunk
|
| 46 |
|
| 47 |
if not new_chunks:
|
| 48 |
logger.info("No new chunks to add.")
|
|
@@ -53,7 +54,7 @@ class PolicyVectorDB:
|
|
| 53 |
for i in range(0, len(new_chunks), batch_size):
|
| 54 |
batch = new_chunks[i:i + batch_size]
|
| 55 |
texts = [chunk['text'] for chunk in batch]
|
| 56 |
-
ids = [chunk['id'] for chunk in batch]
|
| 57 |
metadatas = [self._flatten_metadata(chunk['metadata']) if chunk.get('metadata') else {} for chunk in batch]
|
| 58 |
embeddings = self.embedding_model.encode(texts, show_progress_bar=False).tolist()
|
| 59 |
collection.add(ids=ids, embeddings=embeddings, documents=texts, metadatas=metadatas)
|
|
@@ -89,6 +90,7 @@ def ensure_db_populated(db_instance: PolicyVectorDB, chunks_file_path: str):
|
|
| 89 |
logger.error(f"Chunks file not found at {chunks_file_path}. Cannot populate DB.")
|
| 90 |
return False
|
| 91 |
|
|
|
|
| 92 |
with open(chunks_file_path, 'r', encoding='utf-8') as f:
|
| 93 |
chunks_to_add = json.load(f)
|
| 94 |
|
|
|
|
| 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))
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
logger.warning(f"Could not retrieve existing IDs from ChromaDB: {e}. Assuming no existing IDs for now.")
|
| 45 |
|
| 46 |
+
new_chunks = [chunk for chunk in chunks if chunk.get('id') and str(chunk.get('id')) not in existing_ids]
|
| 47 |
|
| 48 |
if not new_chunks:
|
| 49 |
logger.info("No new chunks to add.")
|
|
|
|
| 54 |
for i in range(0, len(new_chunks), batch_size):
|
| 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 |
metadatas = [self._flatten_metadata(chunk['metadata']) if chunk.get('metadata') else {} for chunk in batch]
|
| 59 |
embeddings = self.embedding_model.encode(texts, show_progress_bar=False).tolist()
|
| 60 |
collection.add(ids=ids, embeddings=embeddings, documents=texts, metadatas=metadatas)
|
|
|
|
| 90 |
logger.error(f"Chunks file not found at {chunks_file_path}. Cannot populate DB.")
|
| 91 |
return False
|
| 92 |
|
| 93 |
+
# --- REVERTED: This is the correct method for a standard .json file ---
|
| 94 |
with open(chunks_file_path, 'r', encoding='utf-8') as f:
|
| 95 |
chunks_to_add = json.load(f)
|
| 96 |
|