fix: wrap dspy async in asyncio.run_until_complete
This commit is contained in:
parent
c48aa36f1b
commit
95c71a9b88
@ -1,13 +1,13 @@
|
|||||||
env = request._run_ns
|
import asyncio
|
||||||
userorgid = await env.get_userorgid()
|
|
||||||
|
|
||||||
async with get_sor_context(env, 'rag') as sor:
|
async def _do():
|
||||||
|
env = request._run_ns
|
||||||
|
userorgid = await env.get_userorgid()
|
||||||
|
cards = []
|
||||||
|
async with get_sor_context(env, 'rag') as sor:
|
||||||
recs = await sor.R("knowledge_bases", {"org_id": userorgid})
|
recs = await sor.R("knowledge_bases", {"org_id": userorgid})
|
||||||
|
for r in recs:
|
||||||
cards = []
|
doc_recs = await sor.R("documents", {"kb_id": r.id})
|
||||||
for r in recs:
|
|
||||||
kb_id = r.id
|
|
||||||
doc_recs = await sor.R("documents", {"kb_id": kb_id})
|
|
||||||
doc_count = len(doc_recs)
|
doc_count = len(doc_recs)
|
||||||
total_size = r.total_size or 0
|
total_size = r.total_size or 0
|
||||||
if total_size > 1073741824:
|
if total_size > 1073741824:
|
||||||
@ -16,9 +16,7 @@ for r in recs:
|
|||||||
size_str = f"{total_size/1048576:.0f}MB"
|
size_str = f"{total_size/1048576:.0f}MB"
|
||||||
else:
|
else:
|
||||||
size_str = f"{total_size/1024:.0f}KB"
|
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": {
|
||||||
@ -39,9 +37,10 @@ for r in recs:
|
|||||||
"options": {"url": entire_url('/knowledge_bases_list/detail.ui')}}]
|
"options": {"url": entire_url('/knowledge_bases_list/detail.ui')}}]
|
||||||
}
|
}
|
||||||
cards.append(card)
|
cards.append(card)
|
||||||
|
return {
|
||||||
result = {
|
|
||||||
"widgettype": "HBox",
|
"widgettype": "HBox",
|
||||||
"options": {"width": "100%", "spacing": "12px", "wrap": True},
|
"options": {"width": "100%", "spacing": "12px", "wrap": True},
|
||||||
"subwidgets": cards
|
"subwidgets": cards
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = asyncio.get_event_loop().run_until_complete(_do())
|
||||||
|
|||||||
@ -1,8 +1,12 @@
|
|||||||
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():
|
||||||
|
action = params_kw.get("action", "")
|
||||||
|
kb_id = params_kw.get("kb_id", "")
|
||||||
|
env = request._run_ns
|
||||||
|
folder_label = params_kw.get("folder_label", "")
|
||||||
|
|
||||||
|
if action == "create_dir":
|
||||||
dir_name = params_kw.get("dir_name", "")
|
dir_name = params_kw.get("dir_name", "")
|
||||||
parent_id = params_kw.get("parent_id", "")
|
parent_id = params_kw.get("parent_id", "")
|
||||||
if dir_name:
|
if dir_name:
|
||||||
@ -12,19 +16,15 @@ if action == "create_dir":
|
|||||||
"INSERT INTO document_chunks (id, doc_id, kb_id, chunk_index, chunk_type, content, description, created_at) "
|
"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())",
|
"VALUES (${id}$, '', ${kb_id}$, 0, 'directory', ${name}$, ${parent}$, NOW())",
|
||||||
{"id": dir_id, "kb_id": kb_id, "name": dir_name, "parent": parent_id})
|
{"id": dir_id, "kb_id": kb_id, "name": dir_name, "parent": parent_id})
|
||||||
|
folder_label = folder_label or dir_name
|
||||||
|
|
||||||
elif action == "delete_item":
|
elif action == "delete_item":
|
||||||
item_id = params_kw.get("item_id", "")
|
item_id = params_kw.get("item_id", "")
|
||||||
if item_id:
|
if item_id:
|
||||||
async with get_sor_context(env, 'rag') as sor:
|
async with get_sor_context(env, 'rag') as sor:
|
||||||
await sor.sqlExe("DELETE FROM document_chunks WHERE id=${id}$", {"id": item_id})
|
await sor.sqlExe("DELETE FROM document_chunks WHERE id=${id}$", {"id": item_id})
|
||||||
|
|
||||||
# Return refreshed folder content widget
|
return {
|
||||||
folder_id = params_kw.get("folder_id", "")
|
|
||||||
folder_label = params_kw.get("folder_label", "")
|
|
||||||
folder_label = folder_label or dir_name or "全部文件"
|
|
||||||
|
|
||||||
result = {
|
|
||||||
"widgettype": "VBox",
|
"widgettype": "VBox",
|
||||||
"options": {"spacing": "12px"},
|
"options": {"spacing": "12px"},
|
||||||
"subwidgets": [
|
"subwidgets": [
|
||||||
@ -35,10 +35,8 @@ result = {
|
|||||||
{"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())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user