3A87 commited on
Commit
2ebf6e6
·
unverified ·
1 Parent(s): 09b6026

Fix:Optimized logic for automatic switching modes when keywords do not exist

Browse files
Files changed (1) hide show
  1. lightrag/operate.py +42 -75
lightrag/operate.py CHANGED
@@ -522,15 +522,16 @@ async def kg_query(
522
  logger.warning("low_level_keywords and high_level_keywords is empty")
523
  return PROMPTS["fail_response"]
524
  if ll_keywords == [] and query_param.mode in ["local", "hybrid"]:
525
- logger.warning("low_level_keywords is empty")
526
- return PROMPTS["fail_response"]
527
- else:
528
- ll_keywords = ", ".join(ll_keywords)
529
  if hl_keywords == [] and query_param.mode in ["global", "hybrid"]:
530
- logger.warning("high_level_keywords is empty")
531
- return PROMPTS["fail_response"]
532
- else:
533
- hl_keywords = ", ".join(hl_keywords)
 
 
 
534
 
535
  # Build context
536
  keywords = [ll_keywords, hl_keywords]
@@ -596,78 +597,44 @@ async def _build_query_context(
596
  # ll_entities_context, ll_relations_context, ll_text_units_context = "", "", ""
597
  # hl_entities_context, hl_relations_context, hl_text_units_context = "", "", ""
598
 
599
- ll_kewwords, hl_keywrds = query[0], query[1]
600
- if query_param.mode in ["local", "hybrid"]:
601
- if ll_kewwords == "":
602
- ll_entities_context, ll_relations_context, ll_text_units_context = (
603
- "",
604
- "",
605
- "",
606
- )
607
- warnings.warn(
608
- "Low Level context is None. Return empty Low entity/relationship/source"
609
- )
610
- query_param.mode = "global"
611
- else:
612
- (
613
- ll_entities_context,
614
- ll_relations_context,
615
- ll_text_units_context,
616
- ) = await _get_node_data(
617
- ll_kewwords,
618
- knowledge_graph_inst,
619
- entities_vdb,
620
- text_chunks_db,
621
- query_param,
622
- )
623
- if query_param.mode in ["global", "hybrid"]:
624
- if hl_keywrds == "":
625
- hl_entities_context, hl_relations_context, hl_text_units_context = (
626
- "",
627
- "",
628
- "",
629
- )
630
- warnings.warn(
631
- "High Level context is None. Return empty High entity/relationship/source"
632
- )
633
- query_param.mode = "local"
634
- else:
635
- (
636
- hl_entities_context,
637
- hl_relations_context,
638
- hl_text_units_context,
639
- ) = await _get_edge_data(
640
- hl_keywrds,
641
- knowledge_graph_inst,
642
- relationships_vdb,
643
- text_chunks_db,
644
- query_param,
645
- )
646
- if (
647
- hl_entities_context == ""
648
- and hl_relations_context == ""
649
- and hl_text_units_context == ""
650
- ):
651
- logger.warn("No high level context found. Switching to local mode.")
652
- query_param.mode = "local"
653
- if query_param.mode == "hybrid":
654
  entities_context, relations_context, text_units_context = combine_contexts(
655
  [hl_entities_context, ll_entities_context],
656
  [hl_relations_context, ll_relations_context],
657
  [hl_text_units_context, ll_text_units_context],
658
  )
659
- elif query_param.mode == "local":
660
- entities_context, relations_context, text_units_context = (
661
- ll_entities_context,
662
- ll_relations_context,
663
- ll_text_units_context,
664
- )
665
- elif query_param.mode == "global":
666
- entities_context, relations_context, text_units_context = (
667
- hl_entities_context,
668
- hl_relations_context,
669
- hl_text_units_context,
670
- )
671
  return f"""
672
  -----Entities-----
673
  ```csv
 
522
  logger.warning("low_level_keywords and high_level_keywords is empty")
523
  return PROMPTS["fail_response"]
524
  if ll_keywords == [] and query_param.mode in ["local", "hybrid"]:
525
+ logger.warning("low_level_keywords is empty, switching from %s mode to global mode", query_param.mode)
526
+ query_param.mode = "global"
 
 
527
  if hl_keywords == [] and query_param.mode in ["global", "hybrid"]:
528
+ logger.warning("high_level_keywords is empty, switching from %s mode to local mode", query_param.mode)
529
+ query_param.mode = "local"
530
+
531
+ ll_keywords = ", ".join(ll_keywords) if ll_keywords else ""
532
+ hl_keywords = ", ".join(hl_keywords) if hl_keywords else ""
533
+
534
+ logger.info("Using %s mode for query processing", query_param.mode)
535
 
536
  # Build context
537
  keywords = [ll_keywords, hl_keywords]
 
597
  # ll_entities_context, ll_relations_context, ll_text_units_context = "", "", ""
598
  # hl_entities_context, hl_relations_context, hl_text_units_context = "", "", ""
599
 
600
+ ll_keywords, hl_keywords = query[0], query[1]
601
+
602
+ if query_param.mode == "local":
603
+ entities_context, relations_context, text_units_context = await _get_node_data(
604
+ ll_keywords,
605
+ knowledge_graph_inst,
606
+ entities_vdb,
607
+ text_chunks_db,
608
+ query_param,
609
+ )
610
+ elif query_param.mode == "global":
611
+ entities_context, relations_context, text_units_context = await _get_edge_data(
612
+ hl_keywords,
613
+ knowledge_graph_inst,
614
+ relationships_vdb,
615
+ text_chunks_db,
616
+ query_param,
617
+ )
618
+ else: # hybrid mode
619
+ ll_entities_context, ll_relations_context, ll_text_units_context = await _get_node_data(
620
+ ll_keywords,
621
+ knowledge_graph_inst,
622
+ entities_vdb,
623
+ text_chunks_db,
624
+ query_param,
625
+ )
626
+ hl_entities_context, hl_relations_context, hl_text_units_context = await _get_edge_data(
627
+ hl_keywords,
628
+ knowledge_graph_inst,
629
+ relationships_vdb,
630
+ text_chunks_db,
631
+ query_param,
632
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
  entities_context, relations_context, text_units_context = combine_contexts(
634
  [hl_entities_context, ll_entities_context],
635
  [hl_relations_context, ll_relations_context],
636
  [hl_text_units_context, ll_text_units_context],
637
  )
 
 
 
 
 
 
 
 
 
 
 
 
638
  return f"""
639
  -----Entities-----
640
  ```csv