Fix RuntimeError: asyncio.run() cannot be called from a running event loop when calling mindmap endpoint. (#11880)

### What problem does this PR solve?

Fix RuntimeError when calling mindmap endpoint by converting
`gen_mindmap()` to async function and using `await` instead of
`asyncio.run()`.

### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)

---------

Co-authored-by: Kevin Hu <kevinhu.sh@gmail.com>
This commit is contained in:
N0bodycan
2025-12-11 09:47:44 +08:00
committed by GitHub
parent 34d29d7e8b
commit 74eb894453
2 changed files with 3 additions and 4 deletions

View File

@ -435,7 +435,7 @@ async def mindmap():
kb_ids.extend(req["kb_ids"])
kb_ids = list(set(kb_ids))
mind_map = gen_mindmap(req["question"], kb_ids, search_app.get("tenant_id", current_user.id), search_config)
mind_map = await gen_mindmap(req["question"], kb_ids, search_app.get("tenant_id", current_user.id), search_config)
if "error" in mind_map:
return server_error_response(Exception(mind_map["error"]))
return get_json_result(data=mind_map)

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import asyncio
import binascii
import logging
import re
@ -887,7 +886,7 @@ async def async_ask(question, kb_ids, tenant_id, chat_llm_name=None, search_conf
yield decorate_answer(answer)
def gen_mindmap(question, kb_ids, tenant_id, search_config={}):
async def gen_mindmap(question, kb_ids, tenant_id, search_config={}):
meta_data_filter = search_config.get("meta_data_filter", {})
doc_ids = search_config.get("doc_ids", [])
rerank_id = search_config.get("rerank_id", "")
@ -931,5 +930,5 @@ def gen_mindmap(question, kb_ids, tenant_id, search_config={}):
rank_feature=label_question(question, kbs),
)
mindmap = MindMapExtractor(chat_mdl)
mind_map = asyncio.run(mindmap([c["content_with_weight"] for c in ranks["chunks"]]))
mind_map = await mindmap([c["content_with_weight"] for c in ranks["chunks"]])
return mind_map.output