llmage/wwwroot/api/copy_api.dspy

48 lines
1.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 复制API配置从源llm复制所有llm_api_map记录到目标llm
result = {'widgettype':'Message','options':{'title':'Error','message':'','timeout':3}}
try:
from_llmid = params_kw.get('from_llmid')
to_llmid = params_kw.get('to_llmid')
if not from_llmid or not to_llmid:
raise Exception('请选择源模型和目标模型')
if from_llmid == to_llmid:
raise Exception('源模型和目标模型不能相同')
dbname = get_module_dbname('llmage')
async with DBPools().sqlorContext(dbname) as sor:
# 删除目标已有的映射
await sor.sqlExe("DELETE FROM llm_api_map WHERE llmid=${id}$", {'id': to_llmid})
# 复制源的所有映射到目标
src_sql = "SELECT * FROM llm_api_map WHERE llmid=${id}$"
src_recs = await sor.sqlExe(src_sql, {'id': from_llmid})
count = 0
for r in src_recs or []:
await sor.C('llm_api_map', {
'id': getID(),
'llmid': to_llmid,
'llmcatelogid': r.llmcatelogid,
'apiname': r.apiname,
'query_apiname': r.query_apiname or '',
'query_period': r.query_period or 30,
'ppid': r.ppid or '',
'isdefaultcatelog': r.isdefaultcatelog or '0',
})
count += 1
result = {'widgettype':'Message','options':{
'title':'复制完成','message':f'已复制{count}条API配置','timeout':3,'type':'success'
}}
except Exception as e:
debug(f'copy_api error: {format_exc()}')
result = {'widgettype':'Message','options':{
'title':'复制失败','message':str(e),'timeout':5,'type':'error'
}}
return result