Compare commits
No commits in common. "1086d69419f469409050c3f83da76509ef2dfb15" and "f57d27d1bd59a959c56aecdf360c8f7ece1e8ac0" have entirely different histories.
1086d69419
...
f57d27d1bd
@ -1,15 +1,12 @@
|
|||||||
# model_management 可写入字段(不含 id、created_at、updated_at)
|
# 可写入/更新的字段(不含 id、created_at、updated_at)
|
||||||
_MODEL_FIELDS = (
|
_MODEL_FIELDS = (
|
||||||
'llmid', 'provider', 'model_name', 'display_name', 'model_type',
|
'llmid', 'provider', 'model_name', 'display_name', 'model_type',
|
||||||
'context_length', 'input_token_price', 'output_token_price',
|
'context_length', 'input_token_price', 'output_token_price',
|
||||||
'cache_hit_input_price', 'billing_method', 'billing_unit',
|
'cache_hit_input_price', 'billing_method', 'billing_unit',
|
||||||
'capabilities', 'limitations', 'highlights', 'is_active',
|
'capabilities', 'limitations', 'highlights', 'is_active',
|
||||||
'description', 'listing_status', 'sort_order', 'experience',
|
'description', 'listing_status',
|
||||||
)
|
)
|
||||||
|
|
||||||
# model_api_doc 可写入字段(不含 id、model_id、created_at、updated_at)
|
|
||||||
_API_DOC_FIELDS = ('api_url', 'curl_code', 'python_code')
|
|
||||||
|
|
||||||
|
|
||||||
def _escape(value):
|
def _escape(value):
|
||||||
if value is None:
|
if value is None:
|
||||||
@ -27,63 +24,26 @@ def _build_model_dict(ns, include_listing_status=False):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def _build_api_doc_dict(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_add(ns={}):
|
async def model_management_add(ns={}):
|
||||||
"""新增模型及 API 文档,provider、model_name 必传"""
|
"""新增模型,默认待上架 listing_status=0"""
|
||||||
if not ns.get('provider') or not ns.get('model_name'):
|
if not ns.get('provider') or not ns.get('model_name'):
|
||||||
return {'status': False, 'msg': 'provider and model_name are required'}
|
return {'status': False, 'msg': 'provider and model_name are required'}
|
||||||
|
|
||||||
model_dic = _build_model_dict(ns, include_listing_status=True)
|
ns_dic = _build_model_dict(ns, include_listing_status=True)
|
||||||
if 'listing_status' not in model_dic:
|
if 'listing_status' not in ns_dic:
|
||||||
model_dic['listing_status'] = 0
|
ns_dic['listing_status'] = 0
|
||||||
if 'is_active' not in model_dic:
|
if 'is_active' not in ns_dic:
|
||||||
model_dic['is_active'] = 1
|
ns_dic['is_active'] = 1
|
||||||
|
|
||||||
api_doc_dic = _build_api_doc_dict(ns)
|
|
||||||
if api_doc_dic and not api_doc_dic.get('api_url'):
|
|
||||||
return {'status': False, 'msg': 'api_url is required when creating api doc'}
|
|
||||||
|
|
||||||
db = DBPools()
|
db = DBPools()
|
||||||
async with db.sqlorContext('kboss') as sor:
|
async with db.sqlorContext('kboss') as sor:
|
||||||
try:
|
try:
|
||||||
await sor.C('model_management', model_dic)
|
await sor.C('model_management', ns_dic)
|
||||||
|
return {'status': True, 'msg': 'create model success', 'data': ns_dic}
|
||||||
id_rows = await sor.sqlExe('SELECT LAST_INSERT_ID() AS id;', {})
|
|
||||||
model_id = id_rows[0]['id'] if id_rows else None
|
|
||||||
if not model_id:
|
|
||||||
await sor.rollback()
|
|
||||||
return {'status': False, 'msg': 'create model failed, missing model id'}
|
|
||||||
|
|
||||||
# 未传 sort_order 时,默认与新建主键 id 相同
|
|
||||||
if not (
|
|
||||||
'sort_order' in ns
|
|
||||||
and ns.get('sort_order') is not None
|
|
||||||
and ns.get('sort_order') != ''
|
|
||||||
):
|
|
||||||
await sor.U('model_management', {'id': model_id, 'sort_order': model_id})
|
|
||||||
model_dic['sort_order'] = model_id
|
|
||||||
|
|
||||||
result_data = dict(model_dic)
|
|
||||||
result_data['id'] = model_id
|
|
||||||
|
|
||||||
if api_doc_dic:
|
|
||||||
create_dic = dict(api_doc_dic)
|
|
||||||
create_dic['model_id'] = str(model_id)
|
|
||||||
await sor.C('model_api_doc', create_dic)
|
|
||||||
result_data['api_doc'] = create_dic
|
|
||||||
|
|
||||||
return {'status': True, 'msg': 'create model success', 'data': result_data}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await sor.rollback()
|
await sor.rollback()
|
||||||
return {'status': False, 'msg': 'create model failed, %s' % str(e)}
|
return {'status': False, 'msg': 'create model failed, %s' % str(e)}
|
||||||
|
|
||||||
|
|
||||||
ret = await model_management_add(params_kw)
|
ret = await model_management_add(params_kw)
|
||||||
return ret
|
return ret
|
||||||
@ -1,15 +1,12 @@
|
|||||||
# model_management 可写入字段(不含 id、created_at、updated_at)
|
# 可写入/更新的字段(不含 id、created_at、updated_at)
|
||||||
_MODEL_FIELDS = (
|
_MODEL_FIELDS = (
|
||||||
'llmid', 'provider', 'model_name', 'display_name', 'model_type',
|
'llmid', 'provider', 'model_name', 'display_name', 'model_type',
|
||||||
'context_length', 'input_token_price', 'output_token_price',
|
'context_length', 'input_token_price', 'output_token_price',
|
||||||
'cache_hit_input_price', 'billing_method', 'billing_unit',
|
'cache_hit_input_price', 'billing_method', 'billing_unit',
|
||||||
'capabilities', 'limitations', 'highlights', 'is_active',
|
'capabilities', 'limitations', 'highlights', 'is_active',
|
||||||
'description', 'listing_status', 'sort_order', 'experience',
|
'description', 'listing_status',
|
||||||
)
|
)
|
||||||
|
|
||||||
# model_api_doc 可写入字段(不含 id、model_id、created_at、updated_at)
|
|
||||||
_API_DOC_FIELDS = ('api_url', 'curl_code', 'python_code')
|
|
||||||
|
|
||||||
|
|
||||||
def _escape(value):
|
def _escape(value):
|
||||||
if value is None:
|
if value is None:
|
||||||
@ -26,73 +23,23 @@ def _build_model_dict(ns, include_listing_status=False):
|
|||||||
data['listing_status'] = ns.get('listing_status', 0)
|
data['listing_status'] = ns.get('listing_status', 0)
|
||||||
return data
|
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={}):
|
async def model_management_update(ns={}):
|
||||||
"""编辑模型及 API 文档,id(model_management 主键)必传"""
|
"""编辑模型,id 必传"""
|
||||||
model_id = ns.get('id')
|
model_id = ns.get('id')
|
||||||
if not model_id:
|
if not model_id:
|
||||||
return {'status': False, 'msg': 'id is required'}
|
return {'status': False, 'msg': 'id is required'}
|
||||||
|
|
||||||
model_dic = _build_model_dict(ns)
|
ns_dic = _build_model_dict(ns)
|
||||||
api_doc_dic = _build_api_doc_dict(ns)
|
ns_dic['id'] = model_id
|
||||||
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()
|
db = DBPools()
|
||||||
async with db.sqlorContext('kboss') as sor:
|
async with db.sqlorContext('kboss') as sor:
|
||||||
try:
|
try:
|
||||||
if has_model_update:
|
await sor.U('model_management', ns_dic)
|
||||||
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'}
|
return {'status': True, 'msg': 'update model success'}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await sor.rollback()
|
await sor.rollback()
|
||||||
return {'status': False, 'msg': 'update model failed, %s' % str(e)}
|
return {'status': False, 'msg': 'update model failed, %s' % str(e)}
|
||||||
|
|
||||||
|
|
||||||
ret = await model_management_update(params_kw)
|
ret = await model_management_update(params_kw)
|
||||||
return ret
|
return ret
|
||||||
Loading…
x
Reference in New Issue
Block a user