fix: wrap dspy async in asyncio.run_until_complete

This commit is contained in:
yumoqing 2026-07-22 18:42:24 +08:00
parent c48aa36f1b
commit 95c71a9b88
2 changed files with 81 additions and 84 deletions

View File

@ -1,47 +1,46 @@
env = request._run_ns import asyncio
userorgid = await env.get_userorgid()
async with get_sor_context(env, 'rag') as sor: async def _do():
recs = await sor.R("knowledge_bases", {"org_id": userorgid}) env = request._run_ns
userorgid = await env.get_userorgid()
cards = [] cards = []
for r in recs: async with get_sor_context(env, 'rag') as sor:
kb_id = r.id recs = await sor.R("knowledge_bases", {"org_id": userorgid})
doc_recs = await sor.R("documents", {"kb_id": kb_id}) for r in recs:
doc_count = len(doc_recs) doc_recs = await sor.R("documents", {"kb_id": r.id})
total_size = r.total_size or 0 doc_count = len(doc_recs)
if total_size > 1073741824: total_size = r.total_size or 0
size_str = f"{total_size/1073741824:.1f}GB" if total_size > 1073741824:
elif total_size > 1048576: size_str = f"{total_size/1073741824:.1f}GB"
size_str = f"{total_size/1048576:.0f}MB" elif total_size > 1048576:
else: size_str = f"{total_size/1048576:.0f}MB"
size_str = f"{total_size/1024:.0f}KB" else:
size_str = f"{total_size/1024:.0f}KB"
engine = r.embedding_engine or "CLIP" engine = r.embedding_engine or "CLIP"
card = {
card = { "widgettype": "VBox",
"widgettype": "VBox", "options": {
"options": { "cwidth": 16, "cheight": 12, "bgcolor": "#f0f7ff",
"cwidth": 16, "cheight": 12, "bgcolor": "#f0f7ff", "padding": "16px", "css": "card clickable",
"padding": "16px", "css": "card clickable", "border": "1px solid #d0e4f7"
"border": "1px solid #d0e4f7" },
}, "subwidgets": [
"subwidgets": [ {"widgettype": "Text", "options": {"text": "📚 " + str(r.name), "cfontsize": 16, "fontWeight": "bold"}},
{"widgettype": "Text", "options": {"text": "📚 " + str(r.name), "cfontsize": 16, "fontWeight": "bold"}}, {"widgettype": "Text", "options": {"text": str(engine) + " · 1024维", "cfontsize": 12, "color": "#888"}},
{"widgettype": "Text", "options": {"text": str(engine) + " · 1024维", "cfontsize": 12, "color": "#888"}}, {"widgettype": "HBox", "options": {"spacing": "12px"}, "subwidgets": [
{"widgettype": "HBox", "options": {"spacing": "12px"}, "subwidgets": [ {"widgettype": "Text", "options": {"text": "📄 " + str(doc_count) + "文档", "cfontsize": 12, "color": "#666"}},
{"widgettype": "Text", "options": {"text": "📄 " + str(doc_count) + "文档", "cfontsize": 12, "color": "#666"}}, {"widgettype": "Text", "options": {"text": "💾 " + size_str, "cfontsize": 12, "color": "#666"}}
{"widgettype": "Text", "options": {"text": "💾 " + size_str, "cfontsize": 12, "color": "#666"}} ]}
]} ],
], "binds": [{"wid": "self", "event": "click", "actiontype": "urlwidget",
"binds": [{"wid": "self", "event": "click", "actiontype": "urlwidget", "target": "app.rag_main_content", "mode": "replace",
"target": "app.rag_main_content", "mode": "replace", "options": {"url": entire_url('/knowledge_bases_list/detail.ui')}}]
"options": {"url": entire_url('/knowledge_bases_list/detail.ui')}}] }
cards.append(card)
return {
"widgettype": "HBox",
"options": {"width": "100%", "spacing": "12px", "wrap": True},
"subwidgets": cards
} }
cards.append(card)
result = { result = asyncio.get_event_loop().run_until_complete(_do())
"widgettype": "HBox",
"options": {"width": "100%", "spacing": "12px", "wrap": True},
"subwidgets": cards
}

View File

@ -1,44 +1,42 @@
action = params_kw.get("action", "") import asyncio
kb_id = params_kw.get("kb_id", "")
env = request._run_ns
if action == "create_dir": async def _do():
dir_name = params_kw.get("dir_name", "") action = params_kw.get("action", "")
parent_id = params_kw.get("parent_id", "") kb_id = params_kw.get("kb_id", "")
if dir_name: env = request._run_ns
async with get_sor_context(env, 'rag') as sor: folder_label = params_kw.get("folder_label", "")
dir_id = "dir_" + str(abs(hash(dir_name + parent_id)))[:12]
await sor.sqlExe(
"INSERT INTO document_chunks (id, doc_id, kb_id, chunk_index, chunk_type, content, description, created_at) "
"VALUES (${id}$, '', ${kb_id}$, 0, 'directory', ${name}$, ${parent}$, NOW())",
{"id": dir_id, "kb_id": kb_id, "name": dir_name, "parent": parent_id})
elif action == "delete_item": if action == "create_dir":
item_id = params_kw.get("item_id", "") dir_name = params_kw.get("dir_name", "")
if item_id: parent_id = params_kw.get("parent_id", "")
async with get_sor_context(env, 'rag') as sor: if dir_name:
await sor.sqlExe("DELETE FROM document_chunks WHERE id=${id}$", {"id": item_id}) async with get_sor_context(env, 'rag') as sor:
dir_id = "dir_" + str(abs(hash(dir_name + parent_id)))[:12]
await sor.sqlExe(
"INSERT INTO document_chunks (id, doc_id, kb_id, chunk_index, chunk_type, content, description, created_at) "
"VALUES (${id}$, '', ${kb_id}$, 0, 'directory', ${name}$, ${parent}$, NOW())",
{"id": dir_id, "kb_id": kb_id, "name": dir_name, "parent": parent_id})
folder_label = folder_label or dir_name
# Return refreshed folder content widget elif action == "delete_item":
folder_id = params_kw.get("folder_id", "") item_id = params_kw.get("item_id", "")
folder_label = params_kw.get("folder_label", "") if item_id:
folder_label = folder_label or dir_name or "全部文件" async with get_sor_context(env, 'rag') as sor:
await sor.sqlExe("DELETE FROM document_chunks WHERE id=${id}$", {"id": item_id})
result = { return {
"widgettype": "VBox", "widgettype": "VBox",
"options": {"spacing": "12px"}, "options": {"spacing": "12px"},
"subwidgets": [ "subwidgets": [
{ {
"widgettype": "HBox", "widgettype": "HBox",
"options": {"spacing": "4px"}, "options": {"spacing": "4px"},
"subwidgets": [ "subwidgets": [
{"widgettype": "Text", "options": {"text": "📂 " + str(folder_label), "cfontsize": 16, "fontWeight": "bold", "color": "#333"}}, {"widgettype": "Text", "options": {"text": "📂 " + str(folder_label), "cfontsize": 16, "fontWeight": "bold", "color": "#333"}},
{"widgettype": "Text", "options": {"text": "操作成功", "cfontsize": 12, "color": "#50b86c"}} {"widgettype": "Text", "options": {"text": "操作成功", "cfontsize": 12, "color": "#50b86c"}}
] ]
}, }
{ ]
"widgettype": "Text", }
"options": {"text": "操作已完成,请刷新目录树", "color": "#888", "cfontsize": 13}
} result = asyncio.get_event_loop().run_until_complete(_do())
]
}