Merge branch 'main' of git.opencomputing.cn:yumoqing/llmage

This commit is contained in:
yumoqing 2026-05-29 17:23:54 +08:00
commit cef4859574
11 changed files with 209 additions and 189 deletions

7
.gitignore vendored
View File

@ -1 +1,8 @@
__pycache__/ __pycache__/
# CRUD definition directories (auto-generated by Sage platform)
wwwroot/llm/
wwwroot/llm_api_map/
wwwroot/llmcatelog_list/
wwwroot/llmusage/
wwwroot/llmusage_accounting_failed/
wwwroot/llmusage_history/

View File

@ -13,17 +13,19 @@ import subprocess
import os import os
import sys import sys
def find_sage_root(): def find_sage_root():
candidates = [ candidates = [
os.path.expanduser("~/repos/sage"), os.path.expanduser("~/repos/sage"),
os.path.expanduser("~/sage"), os.path.expanduser("~/sage"),
os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))), os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
] ]
for c in candidates: for c in candidates:
if os.path.isdir(os.path.join(c, "py3")) and os.path.isdir(os.path.join(c, "wwwroot")): if os.path.isdir(os.path.join(c, "py3")) and os.path.isdir(os.path.join(c, "wwwroot")):
return c return c
return None return None
SAGE_ROOT = find_sage_root() SAGE_ROOT = find_sage_root()
if not SAGE_ROOT: if not SAGE_ROOT:
print("ERROR: Cannot find Sage root directory") print("ERROR: Cannot find Sage root directory")
@ -38,96 +40,65 @@ MOD = "llmage"
# 权限路径定义 — 每次新增页面或API时同步更新 # 权限路径定义 — 每次新增页面或API时同步更新
# ============================================================ # ============================================================
# any — 无需登录(仅静态资源和菜单 # any — 无需登录(菜单、静态资源
PATHS_ANY = [ PATHS_ANY = [
f"/{MOD}/menu.ui", f"/{MOD}/menu.ui",
f"/{MOD}/imgs", f"/{MOD}/imgs/%",
f"/{MOD}/imgs/kdb.svg",
f"/{MOD}/list_catelog_models.dspy",
] ]
# logined — 需要认证的页面和 API # logined — 所有已登录用户
PATHS_LOGINED = [ PATHS_LOGINED = [
# Module entry # 模块入口
f"/{MOD}", f"/{MOD}",
f"/{MOD}/index.ui",
# Top-level pages and APIs # 顶层 .ui 页面
f"/{MOD}/llmcost.dspy",
f"/{MOD}/llminference.dspy",
f"/{MOD}/llm_dialog.ui", f"/{MOD}/llm_dialog.ui",
f"/{MOD}/show_same_catelog_llm.ui", f"/{MOD}/show_same_catelog_llm.ui",
f"/{MOD}/model_estimate.dspy",
f"/{MOD}/show_llms.ui", f"/{MOD}/show_llms.ui",
f"/{MOD}/llmcheck.dspy",
f"/{MOD}/show_llms_by_providers.ui", f"/{MOD}/show_llms_by_providers.ui",
f"/{MOD}/list_paging_catelog_llms.dspy", f"/{MOD}/failed_accounting.ui",
f"/{MOD}/llmcatelog_list.ui",
# llmusage CRUD directory # 顶层 .dspy非 api/ 目录)
f"/{MOD}/llmusage", f"/{MOD}/%.dspy",
f"/{MOD}/llmusage/update_llmusage.dspy",
f"/{MOD}/llmusage/delete_llmusage.dspy",
f"/{MOD}/llmusage/add_llmusage.dspy",
f"/{MOD}/llmusage/index.ui",
f"/{MOD}/llmusage/get_llmusage.dspy",
# llmcatelog CRUD directory # api/ 目录 — 所有 .dspy 通配
f"/{MOD}/llmcatelog", f"/{MOD}/api/%",
f"/{MOD}/llmcatelog/add_llmcatelog.dspy",
f"/{MOD}/llmcatelog/get_llmcatelog.dspy",
f"/{MOD}/llmcatelog/delete_llmcatelog.dspy",
f"/{MOD}/llmcatelog/index.ui",
f"/{MOD}/llmcatelog/update_llmcatelog.dspy",
# llm CRUD directory # CRUD 子目录 — 通配(每个子目录下的所有文件)
f"/{MOD}/llm", f"/{MOD}/llm/%",
f"/{MOD}/llm/update_llm.dspy", f"/{MOD}/llmcatelog/%",
f"/{MOD}/llm/delete_llm.dspy", f"/{MOD}/llmcatelog_list/%",
f"/{MOD}/llm/index.ui", f"/{MOD}/llmusage/%",
f"/{MOD}/llm/get_llm.dspy", f"/{MOD}/llmusage_accounting_failed/%",
f"/{MOD}/llm/add_llm.dspy", f"/{MOD}/llmusage_history/%",
f"/{MOD}/llm_api_map/%",
# API endpoints # v1 API 目录
f"/{MOD}/api/llm_list.dspy", f"/{MOD}/v1/%",
f"/{MOD}/api/llm_create.dspy",
f"/{MOD}/api/llm_update.dspy",
f"/{MOD}/api/llm_delete.dspy",
f"/{MOD}/api/get_organizations.dspy",
f"/{MOD}/api/get_upapps.dspy",
f"/{MOD}/api/llm_api_map_list.dspy",
f"/{MOD}/api/llm_api_map_create.dspy",
f"/{MOD}/api/llm_api_map_delete.dspy",
f"/{MOD}/api/llm_api_map_options.dspy",
f"/{MOD}/api/uapi_options.dspy",
f"/{MOD}/api/failed_accounting_list.dspy",
f"/{MOD}/api/llmusage_accounting_failed_create.dspy",
f"/{MOD}/api/llmusage_accounting_failed_update.dspy",
f"/{MOD}/api/llmusage_accounting_failed_delete.dspy",
f"/{MOD}/api/llmusage_create.dspy",
f"/{MOD}/api/llmusage_update.dspy",
f"/{MOD}/api/llmusage_delete.dspy",
# v1 API endpoints # 其他子目录
f"/{MOD}/v1/chat/completions", f"/{MOD}/list_llmcatelogs/%",
f"/{MOD}/v1/chat/completions/index.dspy", f"/{MOD}/list_llms/%",
f"/{MOD}/v1/models", f"/{MOD}/openai/%",
f"/{MOD}/v1/models/index.dspy", f"/{MOD}/t2t/%",
f"/{MOD}/v1/tasks", f"/{MOD}/tasks/%",
f"/{MOD}/v1/tasks/index.dspy", f"/{MOD}/upload_asset/%",
f"/{MOD}/v1/video/generations", f"/{MOD}/video/%",
f"/{MOD}/v1/video/generations/index.dspy",
f"/{MOD}/v1/image/generations",
f"/{MOD}/v1/image/generations/index.dspy",
] ]
# ============================================================ # ============================================================
# 执行注册 # 执行注册
# ============================================================ # ============================================================
def run_set_perm(role, path): def run_set_perm(role, path):
cmd = [PYTHON, SET_PERM_SCRIPT, role, path] cmd = [PYTHON, SET_PERM_SCRIPT, role, path]
result = subprocess.run(cmd, capture_output=True, text=True) result = subprocess.run(cmd, capture_output=True, text=True)
return result.returncode == 0 return result.returncode == 0
def register_role_paths(role, paths): def register_role_paths(role, paths):
count = 0 count = 0
for p in paths: for p in paths:
@ -136,6 +107,7 @@ def register_role_paths(role, paths):
print(f" {role}: {count}/{len(paths)} paths registered") print(f" {role}: {count}/{len(paths)} paths registered")
return count return count
def main(): def main():
print(f"Sage root: {SAGE_ROOT}") print(f"Sage root: {SAGE_ROOT}")
total = 0 total = 0
@ -144,5 +116,6 @@ def main():
print(f"\nDone. Total {total} permission entries registered.") print(f"\nDone. Total {total} permission entries registered.")
print("NOTE: Restart Sage after permission changes to reload RBAC cache.") print("NOTE: Restart Sage after permission changes to reload RBAC cache.")
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -11,15 +11,13 @@
"options": { "options": {
"width": "100%", "width": "100%",
"alignItems": "center", "alignItems": "center",
"padding": "16px 24px", "marginBottom": "24px"
"marginBottom": "0"
}, },
"subwidgets": [ "subwidgets": [
{ {
"widgettype": "Title2", "widgettype": "Title2",
"options": { "options": {
"text": "LLM 模型管理", "text": "LLM 模型管理"
"fontWeight": "700"
} }
}, },
{ {
@ -28,26 +26,40 @@
{ {
"widgettype": "Text", "widgettype": "Text",
"options": { "options": {
"text": "模型配置、目录分类与调用监控", "text": "模型类型、模型配置与记账失败记录",
"fontSize": "14px" "cfontsize": 1.2
} }
} }
] ]
}, },
{
"widgettype": "VScrollPanel",
"options": {
"css": "filler"
},
"subwidgets": [
{
"widgettype": "VBox",
"options": {
"spacing": 24
},
"subwidgets": [
{ {
"widgettype": "ResponsableBox", "widgettype": "ResponsableBox",
"options": { "options": {
"gap": "12px", "gap": "16px",
"minWidth": "200px", "minWidth": "250px"
"padding": "0 24px 16px 24px"
}, },
"subwidgets": [ "subwidgets": [
{ {
"widgettype": "VBox", "widgettype": "VBox",
"options": { "options": {
"css": "card", "css": "card",
"padding": "16px 24px", "cheight": 12,
"cursor": "pointer" "cwidth": 23,
"padding": "24px",
"cursor": "pointer",
"borderRadius": "8px"
}, },
"binds": [ "binds": [
{ {
@ -63,17 +75,25 @@
], ],
"subwidgets": [ "subwidgets": [
{ {
"widgettype": "Title5", "widgettype": "Svg",
"options": {
"svg": "<svg width=\"40\" height=\"40\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#3B82F6\" stroke-width=\"2\"><path d=\"M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z\"/></svg>",
"width": "40px",
"height": "40px"
}
},
{
"widgettype": "Title4",
"options": { "options": {
"text": "模型类型管理", "text": "模型类型管理",
"fontWeight": "600" "marginTop": "12px"
} }
}, },
{ {
"widgettype": "Text", "widgettype": "Text",
"options": { "options": {
"text": "管理模型的分类目录和类型定义", "text": "管理模型的分类和类型",
"fontSize": "12px" "cfontsize": 1.2
} }
} }
] ]
@ -82,8 +102,11 @@
"widgettype": "VBox", "widgettype": "VBox",
"options": { "options": {
"css": "card", "css": "card",
"padding": "16px 24px", "cheight": 12,
"cursor": "pointer" "cwidth": 23,
"padding": "24px",
"cursor": "pointer",
"borderRadius": "8px"
}, },
"binds": [ "binds": [
{ {
@ -99,17 +122,25 @@
], ],
"subwidgets": [ "subwidgets": [
{ {
"widgettype": "Title5", "widgettype": "Svg",
"options": { "options": {
"text": "模型配置", "svg": "<svg width=\"40\" height=\"40\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#22C55E\" stroke-width=\"2\"><path d=\"M9.75 3.104v5.714a2.25 2.25 0 01-.659 1.591L5 14.5M9.75 3.104c-.251.023-.501.05-.75.082m.75-.082a24.301 24.301 0 014.5 0m0 0v5.714c0 .597.237 1.17.659 1.591L19.8 15.3M14.25 3.104c.251.023.501.05.75.082M19.8 15.3l-1.57.393A9.065 9.065 0 0112 15.75c-2.062 0-4.024-.614-5.67-1.757l-1.57-.393m15.04 0L12 21 5.25 13.893\"/></svg>",
"fontWeight": "600" "width": "40px",
"height": "40px"
}
},
{
"widgettype": "Title4",
"options": {
"text": "模型管理",
"marginTop": "12px"
} }
}, },
{ {
"widgettype": "Text", "widgettype": "Text",
"options": { "options": {
"text": "管理 LLM 模型的API配置与供应商映射", "text": "管理 LLM 模型配置",
"fontSize": "12px" "cfontsize": 1.2
} }
} }
] ]
@ -118,8 +149,11 @@
"widgettype": "VBox", "widgettype": "VBox",
"options": { "options": {
"css": "card", "css": "card",
"padding": "16px 24px", "cheight": 12,
"cursor": "pointer" "cwidth": 23,
"padding": "24px",
"cursor": "pointer",
"borderRadius": "8px"
}, },
"binds": [ "binds": [
{ {
@ -135,32 +169,38 @@
], ],
"subwidgets": [ "subwidgets": [
{ {
"widgettype": "Title5", "widgettype": "Svg",
"options": {
"svg": "<svg width=\"40\" height=\"40\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#EF4444\" stroke-width=\"2\"><path d=\"M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z\"/></svg>",
"width": "40px",
"height": "40px"
}
},
{
"widgettype": "Title4",
"options": { "options": {
"text": "记账失败记录", "text": "记账失败记录",
"fontWeight": "600" "marginTop": "12px"
} }
}, },
{ {
"widgettype": "Text", "widgettype": "Text",
"options": { "options": {
"text": "查看和检索调用计费失败记录", "text": "查看和检索记账失败的记录",
"fontSize": "12px" "cfontsize": 1.2
} }
} }
] ]
} }
] ]
}
]
}, },
{ {
"widgettype": "VBox", "widgettype": "VBox",
"id": "llmage_content", "id": "llmage_content"
"css": "filler",
"options": {
"width": "100%",
"padding": "0 24px",
"overflowY": "auto"
} }
]
} }
] ]
} }

View File

@ -1,4 +1,4 @@
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
ns = params_kw.copy() ns = params_kw.copy()
if not ns.page: if not ns.page:
ns.page = 1 ns.page = 1

View File

@ -1,4 +1,4 @@
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
if params_kw.off_peak: if params_kw.off_peak:
off_peak = params_kw.off_peak off_peak = params_kw.off_peak
if off_peak in [True, "Y" "y", 1, "1"]: if off_peak in [True, "Y" "y", 1, "1"]:
@ -25,7 +25,7 @@ if kdbids:
ret = await rfexe('fusedsearch', request, params) ret = await rfexe('fusedsearch', request, params)
data.update(ret) data.update(ret)
params_kw.prompt = await tmpl_engine.renders(tmpl, data) params_kw.prompt = await tmpl_engine.renders(tmpl, data)
debug(f'{params=}rag return {data}, {params_kw.prompt=}') debug_params('rag', {'query': params.get('query',''), 'prompt_len': len(str(params_kw.prompt))})
env = DictObject(**globals()) env = DictObject(**globals())
return await inference(request, env=env) return await inference(request, env=env)

View File

@ -1,5 +1,5 @@
debug(f'model_estimate.dspy:{params_kw=}') debug_params('model_estimate', params_kw)
db = DBPools() db = DBPools()
dbname = get_module_dbname('llmage') dbname = get_module_dbname('llmage')
async with db.sqlorContext(dbname) as sor: async with db.sqlorContext(dbname) as sor:

View File

@ -1,4 +1,4 @@
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
lctype='文生文' lctype='文生文'
if params_kw.off_peak: if params_kw.off_peak:
off_peak = params_kw.off_peak off_peak = params_kw.off_peak

View File

@ -9,7 +9,7 @@ async def gen():
async for l in f(): async for l in f():
yield l yield l
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
lctype='文生文' lctype='文生文'
if params_kw.off_peak: if params_kw.off_peak:
off_peak = params_kw.off_peak off_peak = params_kw.off_peak
@ -25,7 +25,7 @@ if userid is None:
return openai_403() return openai_403()
if not params_kw.prompt and not params_kw.messages: if not params_kw.prompt and not params_kw.messages:
debug(f'not params_kw.prompt and not params_kw.messages,{params_kw=}') debug(f'missing prompt and messages, model={params_kw.model}')
d = return_error('Missing need data(prompt or messages)') d = return_error('Missing need data(prompt or messages)')
return json_response(d, status=400) return json_response(d, status=400)
env = request._run_ns env = request._run_ns

View File

@ -23,7 +23,7 @@ from appPublic.uniqueID import getID
from appPublic.timeUtils import curDateString, timestampstr from appPublic.timeUtils import curDateString, timestampstr
from sqlor.dbpools import get_sor_context from sqlor.dbpools import get_sor_context
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
userid = await get_user() userid = await get_user()
userorgid = await get_userorgid() userorgid = await get_userorgid()

View File

@ -31,7 +31,7 @@ from appPublic.uniqueID import getID
from appPublic.timeUtils import curDateString, timestampstr from appPublic.timeUtils import curDateString, timestampstr
from sqlor.dbpools import get_sor_context from sqlor.dbpools import get_sor_context
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
userid = await get_user() userid = await get_user()
userorgid = await get_userorgid() userorgid = await get_userorgid()

View File

@ -1,4 +1,4 @@
debug(f'{params_kw=}') debug_params('params_kw', params_kw)
if params_kw.off_peak: if params_kw.off_peak:
off_peak = params_kw.off_peak off_peak = params_kw.off_peak
if off_peak in [True, "Y" "y", 1, "1"]: if off_peak in [True, "Y" "y", 1, "1"]: