MCPcopy Create free account
hub / github.com/Enfoirer/Text2GraphRAG / ainsert

Method ainsert

nano_graphrag/graphrag.py:277–348  ·  view source on GitHub ↗
(self, string_or_strings)

Source from the content-addressed store, hash-verified

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

Callers 1

insertMethod · 0.95

Calls 9

_insert_startMethod · 0.95
_insert_doneMethod · 0.95
compute_mdhash_idFunction · 0.85
get_chunksFunction · 0.85
filter_keysMethod · 0.45
upsertMethod · 0.45
dropMethod · 0.45
clusteringMethod · 0.45

Tested by

no test coverage detected