99 lines
3.6 KiB
Plaintext
99 lines
3.6 KiB
Plaintext
# model_management 可写入字段(不含 id、created_at、updated_at)
|
||
_MODEL_FIELDS = (
|
||
'llmid', 'provider', 'model_name', 'display_name', 'model_logo', 'model_type',
|
||
'context_length', 'input_token_price', 'output_token_price',
|
||
'cache_hit_input_price', 'billing_method', 'billing_unit',
|
||
'capabilities', 'limitations', 'highlights', 'is_active',
|
||
'description', 'listing_status', 'sort_order', 'experience',
|
||
)
|
||
|
||
# model_api_doc 可写入字段(不含 id、model_id、created_at、updated_at)
|
||
_API_DOC_FIELDS = ('api_url', 'curl_code', 'python_code')
|
||
|
||
|
||
def _escape(value):
|
||
if value is None:
|
||
return None
|
||
return str(value).replace("'", "''")
|
||
|
||
|
||
def _build_model_dict(ns, include_listing_status=False):
|
||
data = {}
|
||
for field in _MODEL_FIELDS:
|
||
if field in ns and ns.get(field) is not None and ns.get(field) != '':
|
||
data[field] = ns.get(field)
|
||
if include_listing_status and 'listing_status' not in data:
|
||
data['listing_status'] = ns.get('listing_status', 0)
|
||
return data
|
||
|
||
|
||
def _build_api_doc_dict(ns):
|
||
"""构建 API 文档更新字典;字段在 ns 中即参与更新(含空字符串)"""
|
||
data = {}
|
||
for field in _API_DOC_FIELDS:
|
||
if field in ns and ns.get(field) is not None:
|
||
data[field] = ns.get(field)
|
||
return data
|
||
|
||
|
||
async def model_management_update(ns={}):
|
||
"""编辑模型及 API 文档,id(model_management 主键)必传"""
|
||
model_id = ns.get('id')
|
||
if not model_id:
|
||
return {'status': False, 'msg': 'id is required'}
|
||
|
||
model_dic = _build_model_dict(ns)
|
||
api_doc_dic = _build_api_doc_dict(ns)
|
||
has_model_update = bool(model_dic)
|
||
has_api_doc_update = bool(api_doc_dic)
|
||
|
||
if not has_model_update and not has_api_doc_update:
|
||
return {'status': False, 'msg': 'no fields to update'}
|
||
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
try:
|
||
if has_model_update:
|
||
model_dic['id'] = model_id
|
||
await sor.U('model_management', model_dic)
|
||
|
||
if has_api_doc_update:
|
||
api_doc_id = ns.get('api_doc_id')
|
||
existing = None
|
||
if api_doc_id:
|
||
find_sql = (
|
||
"SELECT id FROM model_api_doc WHERE id = '%s' AND model_id = '%s' LIMIT 1;"
|
||
% (_escape(api_doc_id), _escape(str(model_id)))
|
||
)
|
||
existing = await sor.sqlExe(find_sql, {})
|
||
if not existing:
|
||
find_sql = (
|
||
"SELECT id FROM model_api_doc WHERE model_id = '%s' LIMIT 1;"
|
||
% _escape(str(model_id))
|
||
)
|
||
existing = await sor.sqlExe(find_sql, {})
|
||
|
||
if existing:
|
||
doc_update = dict(api_doc_dic)
|
||
doc_update['id'] = existing[0]['id']
|
||
await sor.U('model_api_doc', doc_update)
|
||
else:
|
||
if not api_doc_dic.get('api_url'):
|
||
await sor.rollback()
|
||
return {
|
||
'status': False,
|
||
'msg': 'api_url is required when creating api doc',
|
||
}
|
||
create_dic = dict(api_doc_dic)
|
||
create_dic['model_id'] = str(model_id)
|
||
await sor.C('model_api_doc', create_dic)
|
||
|
||
return {'status': True, 'msg': 'update model success'}
|
||
except Exception as e:
|
||
await sor.rollback()
|
||
return {'status': False, 'msg': 'update model failed, %s' % str(e)}
|
||
|
||
|
||
ret = await model_management_update(params_kw)
|
||
return ret
|