feat: update unit_values and get_pricing_display for new format

- unit_values: only include units actually used in pricing_data
- get_pricing_display: support new format (price_factors + unit_prices + unit)
- Add fallback unit values for common units
This commit is contained in:
yumoqing 2026-06-05 14:06:58 +08:00
parent 7200454c46
commit da07250049

View File

@ -550,7 +550,7 @@ order by b.enabled_date desc"""
raise Exception(f'定价定义中没有pricing数据') 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 = [] ret_items = []
for i, p in enumerate(d.pricings): for i, p in enumerate(d.pricings):
@ -746,8 +746,17 @@ async def get_pricing_display(ppid):
{ {
"filters": {"model": "doubao-seed-2-0-pro"}, "filters": {"model": "doubao-seed-2-0-pro"},
"filter_labels": {"模型": "doubao-seed-2-0-pro"}, "filter_labels": {"模型": "doubao-seed-2-0-pro"},
"price_factors": [...], "price_factors": [
"formula": "..." {
"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', {}) fields = d.get('fields', {})
pricings = d.get('pricings', []) pricings = d.get('pricings', [])
pricing_type = d.get('pricing_type', 'per_use') pricing_type = d.get('pricing_type', 'per_use')
unit_values = d.get('unit_values', {})
items = [] items = []
for p in pricings: for p in pricings:
# 提取过滤条件role=filter 或无 role 的字段) # 跳过需要人工审核的记录
if p.get('_NEEDS_MANUAL_REVIEW'):
continue
# 提取过滤条件role=filter 的字段)
filters = {} filters = {}
filter_labels = {} filter_labels = {}
skip_keys = {'formula', 'price_factors', 'unit_prices', 'unit', 'filters', 'min_amount', 'pricing_type', 'name'}
for k, v in p.items(): for k, v in p.items():
if k in ('formula', 'price_factors', 'name'): if k in skip_keys:
continue continue
fdef = fields.get(k, {}) fdef = fields.get(k, {})
role = fdef.get('role', 'filter') if isinstance(fdef, dict) else 'filter' 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 label = fdef.get('label', k) if isinstance(fdef, dict) else k
filter_labels[label] = v filter_labels[label] = v
# 提取 price_factors展示层 # 新格式price_factors + unit_prices + unit
price_factors = p.get('price_factors', None) is_new_format = p.get('price_factors') is not None and p.get('unit_prices') is not 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({ if is_new_format:
'filters': filters, factor_name = p['price_factors']
'filter_labels': filter_labels, unit_price = p['unit_prices']
'price_factors': price_factors, unit_str = p.get('unit', '')
'formula': p.get('formula', '')
}) # 获取 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', '')
})
return { return {
'ppid': ppid, 'ppid': ppid,