(self, string_or_strings)
| 275 | return response |
| 276 | |
| 277 | async def ainsert(self, string_or_strings): |
| 278 | await self._insert_start() |
| 279 | try: |
| 280 | if isinstance(string_or_strings, str): |
| 281 | string_or_strings = [string_or_strings] |
| 282 | # ---------- new docs |
| 283 | new_docs = { |
| 284 | compute_mdhash_id(c.strip(), prefix="doc-"): {"content": c.strip()} |
| 285 | for c in string_or_strings |
| 286 | } |
| 287 | _add_doc_keys = await self.full_docs.filter_keys(list(new_docs.keys())) |
| 288 | new_docs = {k: v for k, v in new_docs.items() if k in _add_doc_keys} |
| 289 | if not len(new_docs): |
| 290 | logger.warning(f"All docs are already in the storage") |
| 291 | return |
| 292 | logger.info(f"[New Docs] inserting {len(new_docs)} docs") |
| 293 | |
| 294 | # ---------- chunking |
| 295 | |
| 296 | inserting_chunks = get_chunks( |
| 297 | new_docs=new_docs, |
| 298 | chunk_func=self.chunk_func, |
| 299 | overlap_token_size=self.chunk_overlap_token_size, |
| 300 | max_token_size=self.chunk_token_size, |
| 301 | tokenizer_wrapper=self.tokenizer_wrapper, |
| 302 | ) |
| 303 | |
| 304 | _add_chunk_keys = await self.text_chunks.filter_keys( |
| 305 | list(inserting_chunks.keys()) |
| 306 | ) |
| 307 | inserting_chunks = { |
| 308 | k: v for k, v in inserting_chunks.items() if k in _add_chunk_keys |
| 309 | } |
| 310 | if not len(inserting_chunks): |
| 311 | logger.warning(f"All chunks are already in the storage") |
| 312 | return |
| 313 | logger.info(f"[New Chunks] inserting {len(inserting_chunks)} chunks") |
| 314 | if self.enable_naive_rag: |
| 315 | logger.info("Insert chunks for naive RAG") |
| 316 | await self.chunks_vdb.upsert(inserting_chunks) |
| 317 | |
| 318 | # TODO: don't support incremental update for communities now, so we have to drop all |
| 319 | await self.community_reports.drop() |
| 320 | |
| 321 | # ---------- extract/summary entity and upsert to graph |
| 322 | logger.info("[Entity Extraction]...") |
| 323 | maybe_new_kg = await self.entity_extraction_func( |
| 324 | inserting_chunks, |
| 325 | knwoledge_graph_inst=self.chunk_entity_relation_graph, |
| 326 | entity_vdb=self.entities_vdb, |
| 327 | tokenizer_wrapper=self.tokenizer_wrapper, |
| 328 | global_config=asdict(self), |
| 329 | using_amazon_bedrock=self.using_amazon_bedrock, |
| 330 | ) |
| 331 | if maybe_new_kg is None: |
| 332 | logger.warning("No new entities found") |
| 333 | return |
| 334 | self.chunk_entity_relation_graph = maybe_new_kg |
no test coverage detected