Files
moyi-model-intelligence-hub/scripts/reconcile-pricing.py
T

157 lines
9.2 KiB
Python

"""Build a reviewable, guarded pricing migration from explicit source snapshots.
No network access, database writes or execution of supplier pricing expressions.
The old and new price JSON must match before a migration can update a route.
"""
import argparse
import copy
import json
import re
from decimal import Decimal, ROUND_HALF_UP
from pathlib import Path
D = lambda x: Decimal(str(x))
API_DISCOUNT = {'claude': '4.6', 'gemini': '4.6', 'gpt': '4.6', 'chatgpt': '4.6', 'codex': '4.6', 'grok': '4.6', 'glm': '5.5', 'kimi': '5.5', 'minimax': '5.5', 'qwen': '5.5', 'deepseek': '5.5', 'doubao': '9'}
def retail_price(prices, currency, purchase_discount, exchange_rate='7.2'):
if currency not in ('USD', 'CNY'):
raise ValueError('Source currency must be explicit USD or CNY')
if not D(0) <= D(purchase_discount) <= D(9):
raise ValueError('Purchase discount plus one must be within 0-10')
fx = D(1) if currency == 'CNY' else D(exchange_rate)
if not D(0) < fx <= D(1000):
raise ValueError('Invalid exchange rate')
discount = D(purchase_discount) + D(1)
result = {'unit':'CNY / 1M tokens', 'discount':float(discount), 'discountLabel':f'{discount.normalize()}折', 'exchangeRate':float(fx)}
listed = {}
for field in ('input', 'output', 'cachedInput'):
value = prices.get(field)
if value is None:
if field != 'cachedInput':
raise ValueError('Both input and output list rates are required')
continue
value = D(value)
if not value.is_finite() or value < 0:
raise ValueError('Invalid source price')
amount = (value * fx * discount / 10).quantize(D('0.00000001'), rounding=ROUND_HALF_UP)
if amount > 1000000:
raise ValueError('Retail rate exceeds the billing limit')
listed[field] = float(value)
result[field] = float(amount)
result['listPriceUsd' if currency == 'USD' else 'listPriceCny'] = listed
return result
def normalize_model(value):
# Only punctuation normalization supported by explicit matching quote rows.
return value.lower().replace('.', '-')
def source_rate(item):
if item.get('billing_mode') == 'tiered_expr':
expr = item.get('billing_expr', '')
# A single literal tier is a fixed rate. Never eval provider expressions.
match = re.fullmatch(r'tier\("[^"]+",\s*([^()]+)\)', expr)
if not match:
return None
fields = dict(re.findall(r'\b(p|c|cr)\s*\*\s*([0-9.]+)', match[1]))
if not {'p','c'}.issubset(fields):
return None
return {'input':fields['p'], 'output':fields['c'], 'cachedInput':fields.get('cr')}
if item.get('quota_type') != 0:
return None
base = D(item['model_ratio']) * 2
return {'input':base, 'output':base*D(item['completion_ratio']), 'cachedInput':base*D(item['cache_ratio']) if item.get('cache_ratio') is not None else None}
def build(directory):
read = lambda name: json.loads((directory/name).read_text(encoding='utf-8'))
catalog = read('catalog-audit-source.json')
ready = {r['id'] for r in read('ready-audit.json')}
quote = {normalize_model(r[0]):r for r in read('supplier-quote-source.json')['报价单'][3:] if r[0]}
source = {p:{r['model_name']:r for r in read(p+'-pricing-recheck.json')['data']} for p in ('apipro','tokenfoundryx')}
hubs = {}
for file in ('apihub-page1.txt','apihub-page2.txt'):
for block in re.split(r'\d+ heading ', (directory/file).read_text(encoding='utf-8'))[1:]:
name=block.split(', Value:')[0]
line=next((line for line in block.splitlines() if 'text 输入 ¥' in line), None)
if not line or '动态计费' in block:
continue
nums=re.findall(r'¥([\d.]+)',line)
if len(nums) >= 4:
hubs[name]={'input':nums[1],'output':nums[3],'cachedInput':nums[5] if len(nums)>5 else None}
rows=[]
for row in catalog:
p=row['provider'];model=row['model'];old=row.get('pricing') or {}
if old.get('unit') != 'CNY / 1M tokens':
continue
result={**row,'ready':row['id'] in ready,'oldPricing':old,'newPricing':copy.deepcopy(old),'purchaseDiscount':None,'state':'保留现价','reason':'','sourceCurrency':None}
q=quote.get(normalize_model(model))
discount=None;rate=None;currency=None
if p=='apipro':
family=next((family for family in API_DISCOUNT if re.match(re.escape(family)+r'(?:[-.\d]|$)',model.lower())),None)
discount=API_DISCOUNT.get(family)
if model.startswith('text-embedding-'): discount='4.6'
if re.match(r'^o[134](?:-|$)',model): discount='4.6'
item=source[p].get(model)
result['source']='https://api.apipro.ai/api/pricing'
if item: rate=source_rate(item)
currency='USD'
if not item: result['reason']='当前渠道目录未匹配同名模型,保留现价,待确认映射。'
elif rate is None: result['reason']='上游含动态/阶梯或非 Token 规则,保留平台固定现价,完整条件待复核。';result['billingExpr']=item.get('billing_expr','')
elif p in ('apihub','tokenfoundryx'):
if q: discount=q[6].replace('折','');result['quoteModel']=q[0];result['quoteList']={'input':q[3],'output':q[4],'cachedInput':q[5]}
if p=='apihub': rate=hubs.get(model);currency='CNY';result['source']='https://apihub.chem-cloud.cn/pricing'
else:
item=source[p].get(model)
rate=source_rate(item) if item else None;currency='USD';result['source']='https://www.tokenfoundryx.com/api/pricing'
if not rate: result['reason']='当前原价未核实或为动态计费,保留现价。'
else: result['reason']='不在本次三个中转渠道折扣范围。'
result['purchaseDiscount']=float(discount) if discount is not None else None
result['sourceCurrency']=currency
if discount is None: result['reason']+=' 未找到用户确认的该模型采购折扣,不按相近模型猜测。'
if rate is not None and discount is not None:
computed=retail_price(rate,currency,discount)
oldout=D(old.get('output',0))
if oldout>0 and D(computed['output'])/oldout >= 100:
result['reason']='当前渠道输出原价比历史值高至少 100 倍,疑似渠道异常配置;不自动涨价。'
result['candidatePricing']=computed
else:
new=copy.deepcopy(old)
for key in ('listPriceUsd','listPriceCny','cachedInput'):
new.pop(key,None)
new.update(computed)
new['priceSource']=('ApiHub 当前人民币原价' if p=='apihub' else ('APIPro' if p=='apipro' else 'TokenFoundryX')+' 当前美元原价')+' · 2026-10-12'
result['newPricing']=new
fields=('input','output','cachedInput','discount','exchangeRate','listPriceUsd','listPriceCny')
changed=any(old.get(k)!=new.get(k) for k in fields)
result['state']='修正' if changed else '一致'
result['reason']='按渠道当前原价与币种,采购折扣加 1 个折扣点;美元按 7.2 换算一次。'
if q and p=='tokenfoundryx' and q[3] and D(rate['input'])/D(q[3]) >= 10:
result['reason']+=' 渠道当前原价明显高于历史报价表,已按当前网页计算,需向渠道复核异常高原价。'
rows.append(result)
return rows
def migration(rows):
changed=[r for r in rows if r['state']=='修正']
def sql(value): return "'"+json.dumps(value,ensure_ascii=False,separators=(',',':')).replace("'","''")+"'::jsonb"
values=[]
for r in changed:
values.append(" ('"+r['provider'].replace("'","''")+"', '"+r['model'].replace("'","''")+"', "+sql(r['oldPricing'])+", "+sql(r['newPricing'])+")")
return ("-- Verified 2026-10-12: current source currency; purchase discount + 1 point.\n"
"-- Exact old JSON guard preserves later manual edits. Does not change enabled status, credentials or balances.\n"
"-- Dynamic, missing-source and extreme-increase rows are deliberately excluded.\n"
"WITH correction(provider_code, upstream_model, old_pricing, new_pricing) AS (VALUES\n"+',\n'.join(values)+"\n)\n"
"UPDATE model_catalog m SET pricing=c.new_pricing, updated_at=now()\n"
"FROM model_providers p, correction c\n"
"WHERE m.provider_id=p.id AND p.code=c.provider_code AND m.upstream_model=c.upstream_model\n"
"AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND m.pricing=c.old_pricing;\n")
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('--sources',type=Path,required=True)
parser.add_argument('--migration',type=Path,required=True)
args=parser.parse_args()
rows=build(args.sources)
(args.sources/'pricing-corrections.json').write_text(json.dumps(rows,ensure_ascii=False,indent=2),encoding='utf-8')
args.migration.write_text(migration(rows),encoding='utf-8')
from collections import Counter
print(json.dumps({'all':dict(Counter(r['state'] for r in rows)),'ready':dict(Counter(r['state'] for r in rows if r['ready']))},ensure_ascii=False))