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:
1 parent
fe48ca8e38
commit
140b443980
4 files changed
+110
No files matched your search
@@ -22,6 +22,8 @@ COPY config.yaml /app/data/config.yaml
|
||||
# A second copy inside the image: start.sh syncs it over the persistent
|
||||
# /app/data volume at boot, so config changes in the repo actually deploy.
|
||||
COPY config.yaml /app/code/config.yaml
|
||||
COPY fx_sync.py /app/code/fx_sync.py
|
||||
COPY eur_prices.json /app/code/eur_prices.json
|
||||
|
||||
# Fail-closed config validator (canonical copy lives in ops-docs/scripts/;
|
||||
# this byte-identical twin ships with the image and is called from start.sh
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"_comment": "EUR list prices per 1M tokens (providers quoting EUR). fx_sync.py converts at the ECB daily rate at boot. USD values in config.yaml are fallbacks.",
|
||||
"models": {
|
||||
"greenpt/deepseek-v4.1-flash": {"input": 0.36, "output": 1.45, "cache": 0.04},
|
||||
"greenpt/glm-5.3-flash": {"input": 0.11, "output": 0.44, "cache": 0.022},
|
||||
"greenpt/glm-5.3": {"input": 1.10, "output": 4.40, "cache": 0.275},
|
||||
"greenpt/green-embeddings": {"input": 0.20, "output": 0.0},
|
||||
"greenpt/qwen3-embedding-8b": {"input": 0.25, "output": 0.0},
|
||||
"deepseek-flash-green": {"input": 0.36, "output": 1.45, "cache": 0.04},
|
||||
"glm-flash-green": {"input": 0.11, "output": 0.44, "cache": 0.022},
|
||||
"regolo/apertus-70b": {"input": 1.00, "output": 3.40},
|
||||
"regolo/qwen3.5-122b": {"input": 0.60, "output": 3.40},
|
||||
"regolo/mistral-small-4-119b": {"input": 0.20, "output": 0.70},
|
||||
"regolo/gemma4-31b": {"input": 0.40, "output": 0.60}
|
||||
}
|
||||
}
|
||||
Executable
+85
@@ -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())
|
||||
@@ -190,6 +190,13 @@ fi
|
||||
# cannot happen through this path, and the shipped /app/code/config.yaml is
|
||||
# the live catalog.
|
||||
|
||||
# --- Daily FX sync (fail-open): convert EUR-priced providers at the ECB rate ---
|
||||
# Prices are refreshed at boot; a nightly timer restarts the gateway at 04:30 UTC.
|
||||
if [ -f /app/code/fx_sync.py ]; then
|
||||
echo "Refreshing EUR->USD prices from the ECB feed (fail-open)..."
|
||||
python3 /app/code/fx_sync.py || echo "fx_sync exited non-zero; continuing with existing prices"
|
||||
fi
|
||||
|
||||
# --- Fail-closed config validation (2026-11-11) ---
|
||||
# Validate the config that will actually be SERVED, before starting anything.
|
||||
# A bad key must fail LOUD at the top of the boot log (the chat outage of
|
||||
|
||||
Reference in new issue
Block a user