Merge pull request #547 from n3A87/main
Fix:Optimized logic for automatic switching modes when keywords do not exist
This commit is contained in:
@@ -605,15 +605,16 @@ async def kg_query(
|
|||||||
logger.warning("low_level_keywords and high_level_keywords is empty")
|
logger.warning("low_level_keywords and high_level_keywords is empty")
|
||||||
return PROMPTS["fail_response"]
|
return PROMPTS["fail_response"]
|
||||||
if ll_keywords == [] and query_param.mode in ["local", "hybrid"]:
|
if ll_keywords == [] and query_param.mode in ["local", "hybrid"]:
|
||||||
logger.warning("low_level_keywords is empty")
|
logger.warning("low_level_keywords is empty, switching from %s mode to global mode", query_param.mode)
|
||||||
return PROMPTS["fail_response"]
|
query_param.mode = "global"
|
||||||
else:
|
|
||||||
ll_keywords = ", ".join(ll_keywords)
|
|
||||||
if hl_keywords == [] and query_param.mode in ["global", "hybrid"]:
|
if hl_keywords == [] and query_param.mode in ["global", "hybrid"]:
|
||||||
logger.warning("high_level_keywords is empty")
|
logger.warning("high_level_keywords is empty, switching from %s mode to local mode", query_param.mode)
|
||||||
return PROMPTS["fail_response"]
|
query_param.mode = "local"
|
||||||
else:
|
|
||||||
hl_keywords = ", ".join(hl_keywords)
|
ll_keywords = ", ".join(ll_keywords) if ll_keywords else ""
|
||||||
|
hl_keywords = ", ".join(hl_keywords) if hl_keywords else ""
|
||||||
|
|
||||||
|
logger.info("Using %s mode for query processing", query_param.mode)
|
||||||
|
|
||||||
# Build context
|
# Build context
|
||||||
keywords = [ll_keywords, hl_keywords]
|
keywords = [ll_keywords, hl_keywords]
|
||||||
@@ -679,78 +680,44 @@ async def _build_query_context(
|
|||||||
# ll_entities_context, ll_relations_context, ll_text_units_context = "", "", ""
|
# ll_entities_context, ll_relations_context, ll_text_units_context = "", "", ""
|
||||||
# hl_entities_context, hl_relations_context, hl_text_units_context = "", "", ""
|
# hl_entities_context, hl_relations_context, hl_text_units_context = "", "", ""
|
||||||
|
|
||||||
ll_kewwords, hl_keywrds = query[0], query[1]
|
ll_keywords, hl_keywords = query[0], query[1]
|
||||||
if query_param.mode in ["local", "hybrid"]:
|
|
||||||
if ll_kewwords == "":
|
if query_param.mode == "local":
|
||||||
ll_entities_context, ll_relations_context, ll_text_units_context = (
|
entities_context, relations_context, text_units_context = await _get_node_data(
|
||||||
"",
|
ll_keywords,
|
||||||
"",
|
knowledge_graph_inst,
|
||||||
"",
|
entities_vdb,
|
||||||
)
|
text_chunks_db,
|
||||||
warnings.warn(
|
query_param,
|
||||||
"Low Level context is None. Return empty Low entity/relationship/source"
|
)
|
||||||
)
|
elif query_param.mode == "global":
|
||||||
query_param.mode = "global"
|
entities_context, relations_context, text_units_context = await _get_edge_data(
|
||||||
else:
|
hl_keywords,
|
||||||
(
|
knowledge_graph_inst,
|
||||||
ll_entities_context,
|
relationships_vdb,
|
||||||
ll_relations_context,
|
text_chunks_db,
|
||||||
ll_text_units_context,
|
query_param,
|
||||||
) = await _get_node_data(
|
)
|
||||||
ll_kewwords,
|
else: # hybrid mode
|
||||||
knowledge_graph_inst,
|
ll_entities_context, ll_relations_context, ll_text_units_context = await _get_node_data(
|
||||||
entities_vdb,
|
ll_keywords,
|
||||||
text_chunks_db,
|
knowledge_graph_inst,
|
||||||
query_param,
|
entities_vdb,
|
||||||
)
|
text_chunks_db,
|
||||||
if query_param.mode in ["global", "hybrid"]:
|
query_param,
|
||||||
if hl_keywrds == "":
|
)
|
||||||
hl_entities_context, hl_relations_context, hl_text_units_context = (
|
hl_entities_context, hl_relations_context, hl_text_units_context = await _get_edge_data(
|
||||||
"",
|
hl_keywords,
|
||||||
"",
|
knowledge_graph_inst,
|
||||||
"",
|
relationships_vdb,
|
||||||
)
|
text_chunks_db,
|
||||||
warnings.warn(
|
query_param,
|
||||||
"High Level context is None. Return empty High entity/relationship/source"
|
)
|
||||||
)
|
|
||||||
query_param.mode = "local"
|
|
||||||
else:
|
|
||||||
(
|
|
||||||
hl_entities_context,
|
|
||||||
hl_relations_context,
|
|
||||||
hl_text_units_context,
|
|
||||||
) = await _get_edge_data(
|
|
||||||
hl_keywrds,
|
|
||||||
knowledge_graph_inst,
|
|
||||||
relationships_vdb,
|
|
||||||
text_chunks_db,
|
|
||||||
query_param,
|
|
||||||
)
|
|
||||||
if (
|
|
||||||
hl_entities_context == ""
|
|
||||||
and hl_relations_context == ""
|
|
||||||
and hl_text_units_context == ""
|
|
||||||
):
|
|
||||||
logger.warn("No high level context found. Switching to local mode.")
|
|
||||||
query_param.mode = "local"
|
|
||||||
if query_param.mode == "hybrid":
|
|
||||||
entities_context, relations_context, text_units_context = combine_contexts(
|
entities_context, relations_context, text_units_context = combine_contexts(
|
||||||
[hl_entities_context, ll_entities_context],
|
[hl_entities_context, ll_entities_context],
|
||||||
[hl_relations_context, ll_relations_context],
|
[hl_relations_context, ll_relations_context],
|
||||||
[hl_text_units_context, ll_text_units_context],
|
[hl_text_units_context, ll_text_units_context],
|
||||||
)
|
)
|
||||||
elif query_param.mode == "local":
|
|
||||||
entities_context, relations_context, text_units_context = (
|
|
||||||
ll_entities_context,
|
|
||||||
ll_relations_context,
|
|
||||||
ll_text_units_context,
|
|
||||||
)
|
|
||||||
elif query_param.mode == "global":
|
|
||||||
entities_context, relations_context, text_units_context = (
|
|
||||||
hl_entities_context,
|
|
||||||
hl_relations_context,
|
|
||||||
hl_text_units_context,
|
|
||||||
)
|
|
||||||
return f"""
|
return f"""
|
||||||
-----Entities-----
|
-----Entities-----
|
||||||
```csv
|
```csv
|
||||||
|
Reference in New Issue
Block a user