94 lines
3.7 KiB
Plaintext
94 lines
3.7 KiB
Plaintext
def _escape(value):
|
||
if value is None:
|
||
return None
|
||
return str(value).replace("'", "''")
|
||
|
||
# 客户侧可见字段(不含 listing_status、is_active 等运营字段)
|
||
_CUSTOMER_MODEL_COLUMNS = """
|
||
id, llmid, provider, model_name, display_name, model_type,
|
||
context_length, input_token_price, output_token_price,
|
||
cache_hit_input_price, billing_method, billing_unit,
|
||
capabilities, limitations, highlights, description, sort_order,is_active, experience
|
||
"""
|
||
|
||
|
||
def _customer_listed_conditions(ns):
|
||
"""已上架且启用的模型;支持按厂商、模型类别筛选"""
|
||
conditions = ["listing_status = 1", "is_active = 1"]
|
||
if ns.get('provider'):
|
||
conditions.append("provider = '%s'" % _escape(ns.get('provider')))
|
||
if ns.get('model_type'):
|
||
conditions.append("model_type = '%s'" % _escape(ns.get('model_type')))
|
||
return ' AND '.join(conditions)
|
||
|
||
async def model_management_customer_search(ns={}):
|
||
"""
|
||
客户查看模型列表:仅已上架且启用的模型。
|
||
|
||
可选参数:
|
||
provider (str) 厂商,精确匹配筛选
|
||
model_type (str) 模型类别,精确匹配筛选
|
||
current_page (int) 页码,默认 1
|
||
page_size (int) 每页条数,默认 10
|
||
|
||
返回 data:
|
||
provider_list 当前可见模型中的厂商列表(去重)
|
||
model_type_list 当前可见模型中的模型类别列表(去重)
|
||
filter_total 当前筛选条件下的模型数量
|
||
model_list 模型列表
|
||
page_size, current_page
|
||
|
||
调用示例见 model_management_customer_search.dspy
|
||
"""
|
||
page_size = int(ns.get('page_size', 1000))
|
||
current_page = int(ns.get('current_page', 1))
|
||
offset = (current_page - 1) * page_size
|
||
where_clause = _customer_listed_conditions(ns)
|
||
listed_base = "listing_status = 1 AND is_active = 1"
|
||
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
try:
|
||
provider_sql = """
|
||
SELECT DISTINCT provider FROM model_management
|
||
WHERE %s AND provider IS NOT NULL AND provider != ''
|
||
ORDER BY provider;
|
||
""" % listed_base
|
||
model_type_sql = """
|
||
SELECT DISTINCT model_type FROM model_management
|
||
WHERE %s AND model_type IS NOT NULL AND model_type != ''
|
||
ORDER BY model_type;
|
||
""" % listed_base
|
||
|
||
count_sql = """
|
||
SELECT COUNT(*) AS total_count FROM model_management WHERE %s;
|
||
""" % where_clause
|
||
find_sql = """
|
||
SELECT %s FROM model_management
|
||
WHERE %s
|
||
ORDER BY sort_order ASC
|
||
LIMIT %s OFFSET %s;
|
||
""" % (_CUSTOMER_MODEL_COLUMNS, where_clause, page_size, offset)
|
||
|
||
provider_rows = await sor.sqlExe(provider_sql, {})
|
||
model_type_rows = await sor.sqlExe(model_type_sql, {})
|
||
filter_total = (await sor.sqlExe(count_sql, {}))[0]['total_count']
|
||
model_list = await sor.sqlExe(find_sql, {})
|
||
|
||
return {
|
||
'status': True,
|
||
'msg': 'customer model search success',
|
||
'data': {
|
||
'provider_list': [r['provider'] for r in provider_rows],
|
||
'model_type_list': [r['model_type'] for r in model_type_rows],
|
||
'filter_total': filter_total,
|
||
'page_size': page_size,
|
||
'current_page': current_page,
|
||
'model_list': model_list,
|
||
},
|
||
}
|
||
except Exception as e:
|
||
return {'status': False, 'msg': 'customer model search failed, %s' % str(e)}
|
||
|
||
ret = await model_management_customer_search(params_kw)
|
||
return ret |