MCPcopy Index your code
hub / github.com/KnowledgeXLab/LeanRAG / process_relation

Function process_relation

_cluster_utils.py:448–498  ·  view source on GitHub ↗
( 
    use_llm_func,community_report,maybe_edge,relations,generate_relations,\
     cluster_cluster_relation_prompt,layer,tokenizer,max_depth
    
)

Source from the content-addressed store, hash-verified

446 'index':indices
447 }
448def process_relation(
449 use_llm_func,community_report,maybe_edge,relations,generate_relations,\
450 cluster_cluster_relation_prompt,layer,tokenizer,max_depth
451
452):
453 cluster1_nodes=community_report[maybe_edge[0]]['children']
454 cluster2_nodes=community_report[maybe_edge[1]]['children']
455
456 threshold=min(len(cluster1_nodes)*0.2,len(cluster2_nodes)*0.2)
457 # threshold=1
458 exists_relation={**get_direct_relations(cluster1_nodes,cluster2_nodes,relations),
459 **get_direct_relations(cluster1_nodes,cluster2_nodes,generate_relations)}
460 if exists_relation=={}:
461 return None
462
463 cluster1_description=community_report[maybe_edge[0]]['findings']
464 cluster2_description=community_report[maybe_edge[1]]['findings']
465 relation_infromation=[
466 f"relationship<|>{v['src_tgt']}<|>{v['tgt_src']}<|>{v['description']} "
467 for k,v in exists_relation.items()
468 ]
469 temp_relations={}
470 tokens=len(tokenizer.encode("\n".join(relation_infromation)))
471 gene_tokens=(layer+1)*40
472 allowed_tokens=(max_depth-layer)*40*2
473 # allowed_tokens=100000
474 # allowed_tokens=1
475 if tokens>allowed_tokens:
476 print(f"{tokens}大于{allowed_tokens},进行llm生成\n{maybe_edge[0]}和{maybe_edge[1]} in processing")
477 exact_prompt=cluster_cluster_relation_prompt.format(entity_a=maybe_edge[0],entity_b=maybe_edge[1],\
478 entity_a_description=cluster1_description,entity_b_description=cluster2_description,\
479 relation_information="\n".join(relation_infromation),tokens=gene_tokens)
480
481 response = use_llm_func(exact_prompt)
482 temp_relations[maybe_edge]={
483 'src_tgt':maybe_edge[0],
484 'tgt_src':maybe_edge[1],
485 'description':response,
486 'weight':1,
487 'level':layer+1
488 }
489 else:
490 print(f"{tokens}小于{allowed_tokens},不进行llm生成")
491 temp_relations[maybe_edge]={
492 'src_tgt':maybe_edge[0],
493 'tgt_src':maybe_edge[1],
494 'description':"\n".join(relation_infromation),
495 'weight':1,
496 'level':layer+1
497 }
498 return temp_relations
499
500class Hierarchical_Clustering(ClusteringAlgorithm):
501 def perform_clustering(

Callers

nothing calls this directly

Calls 2

get_direct_relationsFunction · 0.85
formatMethod · 0.80

Tested by

no test coverage detected