(
query,
knowledge_graph_inst: BaseGraphStorage,
entities_vdb: BaseVectorStorage,
relationships_vdb: BaseVectorStorage,
text_chunks_db: BaseKVStorage[TextChunkSchema],
query_param: QueryParam,
global_config: dict,
)
| 665 | |
| 666 | |
| 667 | async def global_query( |
| 668 | query, |
| 669 | knowledge_graph_inst: BaseGraphStorage, |
| 670 | entities_vdb: BaseVectorStorage, |
| 671 | relationships_vdb: BaseVectorStorage, |
| 672 | text_chunks_db: BaseKVStorage[TextChunkSchema], |
| 673 | query_param: QueryParam, |
| 674 | global_config: dict, |
| 675 | ) -> str: |
| 676 | context = None |
| 677 | use_model_func = global_config["llm_model_func"] |
| 678 | |
| 679 | kw_prompt_temp = PROMPTS["keywords_extraction"] |
| 680 | kw_prompt = kw_prompt_temp.format(query=query) |
| 681 | result = await use_model_func(kw_prompt) |
| 682 | json_text = locate_json_string_body_from_string(result) |
| 683 | |
| 684 | try: |
| 685 | keywords_data = json.loads(json_text) |
| 686 | keywords = keywords_data.get("high_level_keywords", []) |
| 687 | keywords = ", ".join(keywords) |
| 688 | except json.JSONDecodeError: |
| 689 | try: |
| 690 | result = ( |
| 691 | result.replace(kw_prompt[:-1], "") |
| 692 | .replace("user", "") |
| 693 | .replace("model", "") |
| 694 | .strip() |
| 695 | ) |
| 696 | result = "{" + result.split("{")[1].split("}")[0] + "}" |
| 697 | |
| 698 | keywords_data = json.loads(result) |
| 699 | keywords = keywords_data.get("high_level_keywords", []) |
| 700 | keywords = ", ".join(keywords) |
| 701 | |
| 702 | except json.JSONDecodeError as e: |
| 703 | # Handle parsing error |
| 704 | print(f"JSON parsing error: {e}") |
| 705 | return PROMPTS["fail_response"] |
| 706 | if keywords: |
| 707 | context = await _build_global_query_context( |
| 708 | keywords, |
| 709 | knowledge_graph_inst, |
| 710 | entities_vdb, |
| 711 | relationships_vdb, |
| 712 | text_chunks_db, |
| 713 | query_param, |
| 714 | ) |
| 715 | |
| 716 | if query_param.only_need_context: |
| 717 | return context |
| 718 | if context is None: |
| 719 | return PROMPTS["fail_response"] |
| 720 | |
| 721 | sys_prompt_temp = PROMPTS["rag_response"] |
| 722 | sys_prompt = sys_prompt_temp.format( |
| 723 | context_data=context, response_type=query_param.response_type |
| 724 | ) |
nothing calls this directly
no test coverage detected