Spaces:
Sleeping
Sleeping
Commit
·
b259f00
1
Parent(s):
4803266
fix: Correct object access for adding documents to ChromaDB
Browse files- app/main_api.py +21 -6
app/main_api.py
CHANGED
|
@@ -86,14 +86,29 @@ class RAGPipeline:
|
|
| 86 |
self.collection_name = collection_name
|
| 87 |
self.collection = chroma_client.get_or_create_collection(name=self.collection_name)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
self.collection.add(
|
| 92 |
-
embeddings=embedding_model.encode(
|
| 93 |
-
documents=
|
| 94 |
-
metadatas=
|
| 95 |
-
ids=
|
| 96 |
)
|
|
|
|
| 97 |
|
| 98 |
def query_documents(self, query: str, n_results: int = 5) -> List[Dict]:
|
| 99 |
if not self.collection.count(): return []
|
|
|
|
| 86 |
self.collection_name = collection_name
|
| 87 |
self.collection = chroma_client.get_or_create_collection(name=self.collection_name)
|
| 88 |
|
| 89 |
+
# In app/main_api.py, inside the RAGPipeline class
|
| 90 |
+
|
| 91 |
+
def add_documents(self, chunks: List[Dict]): # Note: It receives dicts
|
| 92 |
+
if not chunks:
|
| 93 |
+
logger.warning("No chunks provided to add_documents.")
|
| 94 |
+
return
|
| 95 |
+
|
| 96 |
+
logger.info(f"Starting to add {len(chunks)} chunks...")
|
| 97 |
+
|
| 98 |
+
# --- START OF FIX ---
|
| 99 |
+
# The list now contains dictionaries, so we use dictionary access `c['key']`
|
| 100 |
+
contents = [c['content'] for c in chunks]
|
| 101 |
+
metadatas = [c['metadata'] for c in chunks]
|
| 102 |
+
ids = [c['chunk_id'] for c in chunks]
|
| 103 |
+
# --- END OF FIX ---
|
| 104 |
+
|
| 105 |
self.collection.add(
|
| 106 |
+
embeddings=self.embedding_model.encode(contents, show_progress_bar=True).tolist(),
|
| 107 |
+
documents=contents,
|
| 108 |
+
metadatas=metadatas,
|
| 109 |
+
ids=ids
|
| 110 |
)
|
| 111 |
+
logger.info(f"Finished adding {len(chunks)} chunks to collection '{self.collection_name}'")
|
| 112 |
|
| 113 |
def query_documents(self, query: str, n_results: int = 5) -> List[Dict]:
|
| 114 |
if not self.collection.count(): return []
|