gateway: refresh EUR->USD prices at boot from the ECB feed (fail-open)

eur_prices.json is the EUR source of truth; fx_sync.py converts at the ECB daily
rate before config validation and rewrites only the model_info cost fields. If the
feed is unreachable the config is left untouched and the boot continues.
This commit is contained in:
inference-admin committed 2026-11-18 17:57:09 +00:00
1 parent fe48ca8e38
commit 140b443980
4 files changed
+110

No files matched your search

Executable
+85
View File
@@ -0,0 +1,85 @@
#!/usr/bin/env python3
"""Boot-time FX sync: rewrite USD model_info from the EUR table at the ECB rate.
Fail-open by design: if the feed is unreachable or unparseable, the config is left
untouched (the repo's fallback USD values stand) and the boot continues.
Run by start.sh before config validation, so the validator sees the final prices.
"""
import json
import os
import re
import sys
import urllib.request
from datetime import datetime, timezone
CFG = "/app/data/config.yaml"
EUR_FILE = "/app/code/eur_prices.json"
STATE = "/app/data/fx_state.json"
ECB_URL = os.environ.get(
"ECB_FX_URL", "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
def fetch_rate() -> tuple[float, str]:
with urllib.request.urlopen(ECB_URL, timeout=20) as r:
body = r.read().decode("utf-8", "ignore")
m = re.search(r"currency=['\"]USD['\"]\s+rate=['\"]([0-9.]+)['\"]", body)
if not m:
raise ValueError("USD rate not found in ECB feed")
d = re.search(r"time=['\"](\d{4}-\d{2}-\d{2})['\"]", body)
return float(m.group(1)), (d.group(1) if d else "")
def usd(eur: float, rate: float) -> float:
return 0.0 if eur == 0 else eur * rate / 1_000_000
def set_cost(text: str, name: str, key: str, value: float) -> tuple[str, bool]:
pat = re.compile(r"( - model_name: " + re.escape(name) +
r"\n)(.*?)(?=\n - model_name: |\n[a-zA-Z#]|\Z)", re.S)
m = pat.search(text)
if not m:
return text, False
block = m.group(2)
new = f"{value:.10g}"
block2, n = re.subn(key + r": \S+", key + ": " + new, block, count=1)
if n == 0:
return text, False
return text[:m.start(2)] + block2 + text[m.end(2):], True
def main() -> int:
try:
rate, feed_date = fetch_rate()
eur = json.load(open(EUR_FILE, encoding="utf-8"))["models"]
text = open(CFG, encoding="utf-8").read()
changed = []
for name, prices in eur.items():
for key, eur_val in (("input_cost_per_token", prices.get("input")),
("output_cost_per_token", prices.get("output"))):
if eur_val is None:
continue
text, ok = set_cost(text, name, key, usd(eur_val, rate))
if ok:
changed.append(f"{name}:{key}")
if prices.get("cache") is not None:
text, ok = set_cost(text, name, "cache_read_input_token_cost",
usd(prices["cache"], rate))
if ok:
changed.append(f"{name}:cache")
tmp = CFG + ".fx.tmp"
open(tmp, "w", encoding="utf-8").write(text)
os.replace(tmp, CFG)
json.dump({"rate": rate, "feed_date": feed_date,
"checked_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
"fields": len(changed)},
open(STATE, "w", encoding="utf-8"), indent=2)
print(f"[fx-sync] ECB USD/EUR={rate} ({feed_date}); updated {len(changed)} fields")
return 0
except Exception as exc: # fail-open
print(f"[fx-sync] FAIL-OPEN: {type(exc).__name__}: {str(exc)[:160]}; keeping existing prices")
return 0
if __name__ == "__main__":
sys.exit(main())