fix: reconcile source currencies and retail token pricing
This commit is contained in:
1 parent
ea9069e332
commit
512ca52262
12 files changed
+458
-12
No files matched your search
@@ -30,7 +30,7 @@ New-Item -ItemType Directory -Force -Path $artifactRoot | Out-Null
|
||||
$staging = Join-Path $artifactRoot ('moyi-test-' + $releaseId)
|
||||
New-Item -ItemType Directory -Path $staging | Out-Null
|
||||
# Copy tracked deployment files only, never .env, certificates or .git.
|
||||
$tracked = @(git ls-files infra/moyi scripts/live-gateway-smoke.py docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md docs/15-relay-onboarding.md docs/16-streaming-chat-billing.md docs/17-catalog-and-key-selection.md)
|
||||
$tracked = @(git ls-files infra/moyi scripts/live-gateway-smoke.py docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md docs/15-relay-onboarding.md docs/16-streaming-chat-billing.md docs/17-catalog-and-key-selection.md docs/18-pricing-currency-and-discounts.md)
|
||||
foreach ($relative in $tracked) {
|
||||
if ($relative -match '(^|/)(\.env\.cloud|\.deploy-mode|certs)(/|$)') { throw 'Sensitive path in release allowlist.' }
|
||||
$destination = Join-Path $staging $relative
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
"""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))
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Exercise the pricing correction against an isolated local PostgreSQL cluster."""
|
||||
import argparse
|
||||
import json
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
parser=argparse.ArgumentParser()
|
||||
parser.add_argument('--sources',type=Path,required=True)
|
||||
parser.add_argument('--postgres-bin',type=Path,required=True)
|
||||
args=parser.parse_args()
|
||||
repo=Path(__file__).resolve().parent.parent
|
||||
folder=Path(tempfile.mkdtemp(prefix='pricing-migration-',dir=repo/'.tools'))
|
||||
bin=args.postgres_bin
|
||||
with socket.socket() as sock:
|
||||
sock.bind(('127.0.0.1',0));port=sock.getsockname()[1]
|
||||
def run(command):
|
||||
# PostgreSQL's Windows child can inherit stdout pipes and keep communicate()
|
||||
# waiting after pg_ctl exits. A file avoids inheriting a pipe reader.
|
||||
with tempfile.TemporaryFile(mode='w+b') as output:
|
||||
subprocess.run([str(x) for x in command],check=True,stdout=output,stderr=output,timeout=90)
|
||||
output.seek(0)
|
||||
return output.read().decode('utf-8',errors='replace')
|
||||
def sql_literal(value):
|
||||
return "'"+value.replace("'","''")+"'"
|
||||
def sql_json(value):
|
||||
return sql_literal(json.dumps(value,ensure_ascii=False,separators=(',',':')))+'::jsonb'
|
||||
rows=json.loads((args.sources/'pricing-corrections.json').read_text(encoding='utf8'))
|
||||
changed=[r for r in rows if r['state']=='修正']
|
||||
sql=['CREATE TABLE model_providers(id text PRIMARY KEY,code text,deleted_at timestamptz);',
|
||||
'CREATE TABLE model_catalog(id text PRIMARY KEY,provider_id text,upstream_model text,pricing jsonb,status text,deleted_at timestamptz,updated_at timestamptz);']
|
||||
for provider in sorted({r['provider'] for r in rows}):
|
||||
sql.append(f'INSERT INTO model_providers VALUES ({sql_literal(provider)},{sql_literal(provider)},NULL);')
|
||||
for row in rows:
|
||||
sql.append('INSERT INTO model_catalog VALUES ('+','.join([sql_literal(row['id']),sql_literal(row['provider']),sql_literal(row['model']),sql_json(row['oldPricing']),sql_literal(row['status']),'NULL','now()'])+');')
|
||||
sample=changed[0]
|
||||
sql.append('INSERT INTO model_catalog SELECT \'manual-edit\',provider_id,upstream_model,\'{"input":123,"unit":"CNY / 1M tokens"}\'::jsonb,\'disabled\',NULL,now() FROM model_catalog WHERE id='+sql_literal(sample['id'])+';')
|
||||
sql.append('CREATE TEMP TABLE before_rows AS SELECT * FROM model_catalog;')
|
||||
migration=(repo/'control-plane-java/src/main/resources/db/migration/V34__reconcile_source_currency_and_retail_discounts.sql').read_text(encoding='utf8')
|
||||
sql.append(migration)
|
||||
for row in rows:
|
||||
expected=row['newPricing'] if row['state']=='修正' else row['oldPricing']
|
||||
sql.append('DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM model_catalog WHERE id='+sql_literal(row['id'])+' AND pricing='+sql_json(expected)+') THEN RAISE EXCEPTION \'Incorrect migrated price\'; END IF; END $$;')
|
||||
sql.append("DO $$ BEGIN IF EXISTS (SELECT 1 FROM model_catalog m JOIN before_rows b USING(id) WHERE m.status<>b.status) OR NOT EXISTS (SELECT 1 FROM model_catalog WHERE id='manual-edit' AND pricing->>'input'='123') THEN RAISE EXCEPTION 'Manual edit or status was changed'; END IF; END $$;")
|
||||
sql.append('CREATE TEMP TABLE first_pass AS SELECT * FROM model_catalog;')
|
||||
sql.append(migration)
|
||||
sql.append("DO $$ BEGIN IF EXISTS (SELECT 1 FROM model_catalog m JOIN first_pass f USING(id) WHERE m.pricing IS DISTINCT FROM f.pricing OR m.updated_at IS DISTINCT FROM f.updated_at) THEN RAISE EXCEPTION 'Not idempotent'; END IF; END $$;")
|
||||
sql.append("SELECT 'Pricing migration verified' AS result;")
|
||||
script=folder/'verify.sql';script.write_text('\n'.join(sql),encoding='utf8')
|
||||
run([bin/'initdb.exe','-D',folder/'data','-U','pricing_test','-A','trust','--encoding=UTF8','--locale=C'])
|
||||
try:
|
||||
run([bin/'pg_ctl.exe','-D',folder/'data','-l',folder/'postgres.log','-o',f'-p {port} -h 127.0.0.1','-w','start'])
|
||||
out=run([bin/'psql.exe','-h','127.0.0.1','-p',str(port),'-U','pricing_test','-d','postgres','-v','ON_ERROR_STOP=1','-f',script])
|
||||
print(f'PASS: {len(changed)} corrections; {len(rows)} prices checked; manual-edit guard, status preservation and idempotence verified.')
|
||||
finally:
|
||||
run([bin/'pg_ctl.exe','-D',folder/'data','-w','stop','-m','fast'])
|
||||
@@ -0,0 +1,31 @@
|
||||
import unittest
|
||||
from importlib.util import spec_from_file_location, module_from_spec
|
||||
from pathlib import Path
|
||||
|
||||
spec=spec_from_file_location('pricing',Path(__file__).with_name('reconcile-pricing.py'))
|
||||
p=module_from_spec(spec)
|
||||
spec.loader.exec_module(p)
|
||||
|
||||
class PricingTests(unittest.TestCase):
|
||||
def test_cny_no_fx_and_plus_one(self):
|
||||
r=p.retail_price({'input':'.8','output':'2.8','cachedInput':'.23'},'CNY','2')
|
||||
self.assertEqual((r['input'],r['output'],r['cachedInput'],r['discount'],r['exchangeRate']),(.24,.84,.069,3,1))
|
||||
self.assertNotIn('listPriceUsd',r)
|
||||
def test_usd_and_zero(self):
|
||||
r=p.retail_price({'input':'1.25','output':'10','cachedInput':'0'},'USD','2.5')
|
||||
self.assertEqual((r['input'],r['output'],r['cachedInput']),(3.15,25.2,0))
|
||||
self.assertEqual(p.retail_price({'input':1,'output':2},'USD',9)['discount'],10)
|
||||
def test_half_up(self):
|
||||
self.assertEqual(p.retail_price({'input':'.00000001','output':0},'CNY',4)['input'],.00000001)
|
||||
def test_reject_ambiguous(self):
|
||||
for currency,discount in [('RMB',2),('USD',55)]:
|
||||
with self.assertRaises(ValueError):p.retail_price({'input':1,'output':1},currency,discount)
|
||||
def test_dynamic_not_evaluated(self):
|
||||
self.assertIsNone(p.source_rate({'billing_mode':'tiered_expr','billing_expr':'len < 10 ? tier("x",p*1+c*2) : tier("y",p*2+c*4)'}))
|
||||
self.assertEqual(p.source_rate({'billing_mode':'tiered_expr','billing_expr':'tier("standard", p * 10 + c * 50 + cr * 1 + cc * 12.5)'}),{'input':'10','output':'50','cachedInput':'1'})
|
||||
def test_migration_guard(self):
|
||||
sql=p.migration([{'state':'修正','provider':'x','model':'a','oldPricing':{'input':1},'newPricing':{'input':2}}])
|
||||
self.assertIn('m.pricing=c.old_pricing',sql)
|
||||
self.assertNotIn('status=',sql)
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in new issue
Block a user