MCPcopy Create free account
hub / github.com/SkyworkAI/DeepResearchAgent / add_documents

Method add_documents

src/environment/faiss/service.py:265–346  ·  view source on GitHub ↗

Add documents to the FAISS index. Args: request: Add request with texts and metadata Returns: Action result with IDs and count in extra

(self, request: FaissAddRequest)

Source from the content-addressed store, hash-verified

263 return vectors
264
265 async def add_documents(self, request: FaissAddRequest) -> ActionResult:
266 """Add documents to the FAISS index.
267
268 Args:
269 request: Add request with texts and metadata
270
271 Returns:
272 Action result with IDs and count in extra
273 """
274 try:
275 # Ensure index is created
276 await self._ensure_index_created()
277
278 # Filter out empty texts
279 valid_texts = []
280 valid_metadatas = []
281 valid_indices = []
282 for i, text in enumerate(request.texts):
283 if text and text.strip():
284 valid_texts.append(text)
285 if request.metadatas and i < len(request.metadatas):
286 valid_metadatas.append(request.metadatas[i])
287 else:
288 valid_metadatas.append({})
289 valid_indices.append(i)
290
291 if not valid_texts:
292 logger.info("| ⚠️ No valid texts to add (all texts were empty)")
293 return ActionResult(
294 success=True,
295 message="No valid texts to add (all texts were empty)",
296 extra={"ids": [], "count": 0, "total_input": len(request.texts)}
297 )
298
299 # Generate IDs
300 ids = []
301 if request.ids:
302 # Use provided IDs, but only for valid texts
303 for idx in valid_indices:
304 if idx < len(request.ids):
305 ids.append(request.ids[idx])
306 else:
307 ids.append(str(uuid.uuid4()))
308 else:
309 ids = [str(uuid.uuid4()) for _ in valid_texts]
310
311 # Get embeddings
312 embeddings = await self._get_embeddings(valid_texts)
313 embeddings = self._normalize_vectors(embeddings)
314
315 # Add to FAISS index
316 start_idx = self.index.ntotal
317 self.index.add(embeddings)
318
319 # Store documents and mappings
320 for i, (text, metadata, doc_id) in enumerate(zip(valid_texts, valid_metadatas, ids)):
321 idx = start_idx + i
322 self.docstore[doc_id] = Document(page_content=text, metadata=metadata)

Callers 8

_storeMethod · 0.45
_storeMethod · 0.45
_storeMethod · 0.45
test_edge_casesMethod · 0.45
test_error_handlingMethod · 0.45

Calls 9

_ensure_index_createdMethod · 0.95
_get_embeddingsMethod · 0.95
_normalize_vectorsMethod · 0.95
_auto_saveMethod · 0.95
ActionResultClass · 0.90
DocumentClass · 0.85
appendMethod · 0.45
infoMethod · 0.45
addMethod · 0.45

Tested by 5

test_edge_casesMethod · 0.36
test_error_handlingMethod · 0.36