kboss/b/cntoai/model_management_customer_search.dspy
2026-07-22 17:21:20 +08:00

110 lines
3.9 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.

def _escape(value):
if value is None:
return None
return str(value).replace("'", "''")
# 客户侧返回字段结构保持与原 model_management 版本一致
_CUSTOMER_MODEL_COLUMNS = """
id,
id AS llmid,
provider_name AS provider,
model AS model_name,
name AS display_name,
provider_iconurl AS model_logo,
catelogname AS model_type,
NULL AS context_length,
0 AS input_token_price,
0 AS output_token_price,
0 AS cache_hit_input_price,
pricing_display AS billing_method,
'' AS billing_unit,
description AS capabilities,
'' AS limitations,
'' AS highlights,
description,
0 AS sort_order,
1 AS is_active,
0 AS experience
"""
def _customer_listed_conditions(ns):
"""已上架且启用的模型;支持按厂商、模型类别筛选"""
conditions = ["status = 'published'"]
if ns.get('provider'):
conditions.append("provider_name = '%s'" % _escape(ns.get('provider')))
if ns.get('model_type'):
conditions.append("catelogname = '%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 = "status = 'published'"
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
provider_sql = """
SELECT DISTINCT provider_name AS provider FROM llm
WHERE %s AND provider_name IS NOT NULL AND provider_name != ''
ORDER BY provider;
""" % listed_base
model_type_sql = """
SELECT DISTINCT catelogname AS model_type FROM llm
WHERE %s AND catelogname IS NOT NULL AND catelogname != ''
ORDER BY model_type;
""" % listed_base
count_sql = """
SELECT COUNT(*) AS total_count FROM llm WHERE %s;
""" % where_clause
find_sql = """
SELECT %s FROM llm
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