MCPcopy Create free account
hub / github.com/NanGePlus/LightRAGTest / _get_node_data

Function _get_node_data

LightRAG/lightrag/operate.py:621–695  ·  view source on GitHub ↗
(
    query,
    knowledge_graph_inst: BaseGraphStorage,
    entities_vdb: BaseVectorStorage,
    text_chunks_db: BaseKVStorage[TextChunkSchema],
    query_param: QueryParam,
)

Source from the content-addressed store, hash-verified

619
620
621async def _get_node_data(
622 query,
623 knowledge_graph_inst: BaseGraphStorage,
624 entities_vdb: BaseVectorStorage,
625 text_chunks_db: BaseKVStorage[TextChunkSchema],
626 query_param: QueryParam,
627):
628 # get similar entities
629 results = await entities_vdb.query(query, top_k=query_param.top_k)
630 if not len(results):
631 return None
632 # get entity information
633 node_datas = await asyncio.gather(
634 *[knowledge_graph_inst.get_node(r["entity_name"]) for r in results]
635 )
636 if not all([n is not None for n in node_datas]):
637 logger.warning("Some nodes are missing, maybe the storage is damaged")
638
639 # get entity degree
640 node_degrees = await asyncio.gather(
641 *[knowledge_graph_inst.node_degree(r["entity_name"]) for r in results]
642 )
643 node_datas = [
644 {**n, "entity_name": k["entity_name"], "rank": d}
645 for k, n, d in zip(results, node_datas, node_degrees)
646 if n is not None
647 ] # what is this text_chunks_db doing. dont remember it in airvx. check the diagram.
648 # get entitytext chunk
649 use_text_units = await _find_most_related_text_unit_from_entities(
650 node_datas, query_param, text_chunks_db, knowledge_graph_inst
651 )
652 # get relate edges
653 use_relations = await _find_most_related_edges_from_entities(
654 node_datas, query_param, knowledge_graph_inst
655 )
656 logger.info(
657 f"Local query uses {len(node_datas)} entites, {len(use_relations)} relations, {len(use_text_units)} text units"
658 )
659
660 # build prompt
661 entites_section_list = [["id", "entity", "type", "description", "rank"]]
662 for i, n in enumerate(node_datas):
663 entites_section_list.append(
664 [
665 i,
666 n["entity_name"],
667 n.get("entity_type", "UNKNOWN"),
668 n.get("description", "UNKNOWN"),
669 n["rank"],
670 ]
671 )
672 entities_context = list_of_list_to_csv(entites_section_list)
673
674 relations_section_list = [
675 ["id", "source", "target", "description", "keywords", "weight", "rank"]
676 ]
677 for i, e in enumerate(use_relations):
678 relations_section_list.append(

Callers 1

_build_query_contextFunction · 0.85

Calls 6

list_of_list_to_csvFunction · 0.85
queryMethod · 0.45
get_nodeMethod · 0.45
node_degreeMethod · 0.45

Tested by

no test coverage detected