diff --git a/b/cntoai/model_management_customer_search copy.dspy b/b/cntoai/model_management_customer_search copy.dspy new file mode 100644 index 0000000..6d82e56 --- /dev/null +++ b/b/cntoai/model_management_customer_search copy.dspy @@ -0,0 +1,94 @@ +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_logo, 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_search1(params_kw) +return ret \ No newline at end of file diff --git a/b/cntoai/model_management_customer_search.dspy b/b/cntoai/model_management_customer_search.dspy index 25c197c..1381e0f 100644 --- a/b/cntoai/model_management_customer_search.dspy +++ b/b/cntoai/model_management_customer_search.dspy @@ -3,22 +3,38 @@ def _escape(value): return None return str(value).replace("'", "''") -# 客户侧可见字段(不含 listing_status、is_active 等运营字段) +# 客户侧返回字段结构保持与原 model_management 版本一致 _CUSTOMER_MODEL_COLUMNS = """ - id, 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, description, sort_order,is_active, experience + 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 = ["listing_status = 1", "is_active = 1"] + conditions = ["status = 'published'"] if ns.get('provider'): - conditions.append("provider = '%s'" % _escape(ns.get('provider'))) + conditions.append("provider_name = '%s'" % _escape(ns.get('provider'))) if ns.get('model_type'): - conditions.append("model_type = '%s'" % _escape(ns.get('model_type'))) + conditions.append("catelogname = '%s'" % _escape(ns.get('model_type'))) return ' AND '.join(conditions) async def model_management_customer_search(ns={}): @@ -44,27 +60,27 @@ async def model_management_customer_search(ns={}): 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" + listed_base = "status = 'published'" 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 != '' + 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 model_type FROM model_management - WHERE %s AND model_type IS NOT NULL AND model_type != '' + 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 model_management WHERE %s; + SELECT COUNT(*) AS total_count FROM llm WHERE %s; """ % where_clause find_sql = """ - SELECT %s FROM model_management + SELECT %s FROM llm WHERE %s ORDER BY sort_order ASC LIMIT %s OFFSET %s;