diff --git a/pricing/pricing.py b/pricing/pricing.py index c05e567..a636b9f 100644 --- a/pricing/pricing.py +++ b/pricing/pricing.py @@ -550,7 +550,7 @@ order by b.enabled_date desc""" raise Exception(f'定价定义中没有pricing数据') # 单位映射表 - unit_values = d.get('unit_values', {'百万': 1000000, '秒': 1, '千': 1000, '次': 1, '张': 1, '毫秒': 0.001}) + unit_values = d.get('unit_values', {'百万': 1000000, '秒': 1, '千': 1000, '次': 1, '张': 1, '毫秒': 0.001, '元/百万tokens': 1000000, '元/total_tokens': 1, '元/times': 1}) ret_items = [] for i, p in enumerate(d.pricings): @@ -746,8 +746,17 @@ async def get_pricing_display(ppid): { "filters": {"model": "doubao-seed-2-0-pro"}, "filter_labels": {"模型": "doubao-seed-2-0-pro"}, - "price_factors": [...], - "formula": "..." + "price_factors": [ + { + "factor": "prompt_tokens", + "label": "输入Token", + "unit_price": 0.000006, + "unit": "百万", + "unit_label": "元/百万Token" + } + ], + "formula": "...", + "min_amount": 0.01 } ] } @@ -761,14 +770,21 @@ async def get_pricing_display(ppid): fields = d.get('fields', {}) pricings = d.get('pricings', []) pricing_type = d.get('pricing_type', 'per_use') + unit_values = d.get('unit_values', {}) items = [] for p in pricings: - # 提取过滤条件(role=filter 或无 role 的字段) + # 跳过需要人工审核的记录 + if p.get('_NEEDS_MANUAL_REVIEW'): + continue + + # 提取过滤条件(role=filter 的字段) filters = {} filter_labels = {} + skip_keys = {'formula', 'price_factors', 'unit_prices', 'unit', 'filters', 'min_amount', 'pricing_type', 'name'} + for k, v in p.items(): - if k in ('formula', 'price_factors', 'name'): + if k in skip_keys: continue fdef = fields.get(k, {}) role = fdef.get('role', 'filter') if isinstance(fdef, dict) else 'filter' @@ -777,30 +793,74 @@ async def get_pricing_display(ppid): label = fdef.get('label', k) if isinstance(fdef, dict) else k filter_labels[label] = v - # 提取 price_factors(展示层) - price_factors = p.get('price_factors', None) - if not price_factors: - # fallback: 从 fields 中构建展示信息 - price_factors = [] - for k, fdef in fields.items(): - if not isinstance(fdef, dict): - continue - role = fdef.get('role', 'filter') - if role == 'factor' or fdef.get('type') == 'float': - label = fdef.get('label', k) - price_factors.append({ - 'factor': k, - 'label': label, - 'unit_price': None, - 'unit_label': '' + # 新格式:price_factors + unit_prices + unit + is_new_format = p.get('price_factors') is not None and p.get('unit_prices') is not None + + if is_new_format: + factor_name = p['price_factors'] + unit_price = p['unit_prices'] + unit_str = p.get('unit', '次') + + # 获取 factor label + fdef = fields.get(factor_name, {}) + factor_label = fdef.get('label', factor_name) if isinstance(fdef, dict) else factor_name + + # 构建 unit_label (元/单位) + unit_label = f"元/{unit_str}" if unit_str else "元" + + price_factors_display = [{ + 'factor': factor_name, + 'label': factor_label, + 'unit_price': unit_price, + 'unit': unit_str, + 'unit_label': unit_label + }] + + # 处理 filters 区间定价 + if 'filters' in p: + tiered_pricing = [] + for fi in p['filters']: + fi_copy = fi.copy() + fi_copy.pop('unit_prices', None) + tiered_pricing.append({ + 'filters': fi_copy, + 'unit_prices': fi.get('unit_prices', unit_price) }) + if tiered_pricing: + price_factors_display[0]['tiered'] = tiered_pricing + + items.append({ + 'filters': filters, + 'filter_labels': filter_labels, + 'price_factors': price_factors_display, + 'formula': '', + 'min_amount': p.get('min_amount', 0) + }) + else: + # 旧格式:formula + price_factors = p.get('price_factors', None) + if not price_factors: + # fallback: 从 fields 中构建展示信息 + price_factors = [] + for k, fdef in fields.items(): + if not isinstance(fdef, dict): + continue + role = fdef.get('role', 'filter') + if role == 'factor' or fdef.get('type') == 'float': + label = fdef.get('label', k) + price_factors.append({ + 'factor': k, + 'label': label, + 'unit_price': None, + 'unit_label': '' + }) - items.append({ - 'filters': filters, - 'filter_labels': filter_labels, - 'price_factors': price_factors, - 'formula': p.get('formula', '') - }) + items.append({ + 'filters': filters, + 'filter_labels': filter_labels, + 'price_factors': price_factors, + 'formula': p.get('formula', '') + }) return { 'ppid': ppid,