改用甲骨文机上已有的 ai-gateway (129.146.203.203:5100):它本身就 OpenAI 兼容,内部串联 nvidia/gemini/ollama 并轮换 4 个 Gemini key, 比在客户端自己串联更能吸收单厂商的配额和超时。回包里的 provider 字段透传为 meta.upstream,网关侧发生降级时前端也看得见。 fix(ai): 目录里两个 NVIDIA 模型 id 根本不存在 - qwen/qwen2.5-72b-instruct 和 deepseek-ai/deepseek-r1 是我凭印象写的, 实际 GET /v1/models 里没有,调用一律 404 - 改为该账号清单里确实存在的 nemotron-49b / mistral-large, 并在注释里写明 id 必须取自实时清单、不能猜 fix(ai): 请求被本机代理劫持导致网关不可达 - requests 默认读 HTTP_PROXY/ALL_PROXY,把发往甲骨文公网 IP 的请求 也塞进了 127.0.0.1:7897,120s 后超时 - 按 provider 区分:境外厂商(Gemini/NVIDIA)仍走代理,自建网关直连 (session.trust_env=False) fix(ai): 承诺的按模型裁剪从未实现 - 模块注释写着 payload 按 (模型窗口, 天数预算) 取小者裁剪,但实际是 用全局预算构建一次 prompt 发给链上所有模型;365 天数据对 Gemini 的 1M 窗口无碍,却会撑爆 128k 的模型 - 新增 max_days_for(),在循环内按各模型窗口分别构建 prompt fix(ai): 推理模型的思考过程吃光输出预算 - 网关首选 nemotron-3-ultra-550b 是推理模型,回答前先输出一段 chain-of-thought;默认 1024 tokens 全被思考占用,JSON 还没开始 就被截断 - max_tokens 改为可按 provider 声明,网关条目给 3000 fix(ai): 配置在 import 时被冻结 - DEFAULT_CHAIN/TIMEOUT/DAY_BUDGET 是模块级常量,改环境变量不生效, 且让开发机 .env 泄漏进测试进程(测试会读到真实 key 和链配置) - 改为 default_chain()/default_timeout()/default_day_budget() 按调用读取 - conftest 增加 autouse fixture 清空全部 AI_* 变量,测试不再继承 .env 测试 (184 passed, 1 skipped): - 新增 TestGatewayProvider: 透传 upstream、目标 URL/鉴权头、 token 失效时继续降级 - 新增 TestProxyPolicy: 境外厂商与自建端点的代理策略相反 - 新增 TestPerModelSizing: 128k 模型收到的 prompt 必须小于 1M 模型 - 新增 TestMaxTokens: 推理端点预算大于默认,且真正写进两种 payload - 新增 TestLazyConfig: 改环境变量立即生效 - mock 目标从 requests.post 改为 requests.Session.post 实测: 网关链路可返回合法 JSON,但 nemotron-550B 排队较久(约 160s), 故 AI_TIMEOUT_SECONDS 默认调到 180。 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
60 lines
2.3 KiB
Plaintext
60 lines
2.3 KiB
Plaintext
# --- Server ---
|
|
# BACKEND_PORT takes precedence over PORT. Prefer it: many tools inject PORT
|
|
# for the frontend, and Flask would otherwise take the React dev server's port.
|
|
BACKEND_PORT=5000
|
|
|
|
# --- Database: sqlite (default) or mariadb ---
|
|
DB_TYPE=sqlite
|
|
# SQLite file (used when DB_TYPE=sqlite)
|
|
DATABASE_PATH=./data/health.db
|
|
|
|
# MariaDB (used when DB_TYPE=mariadb) — runs on the NAS
|
|
# MARIADB_SOCKET=/run/mysqld/mysqld10.sock
|
|
# MARIADB_HOST=127.0.0.1
|
|
# MARIADB_PORT=3306
|
|
# MARIADB_USER=root
|
|
# MARIADB_PASSWORD=your_nas_mariadb_root_password
|
|
# MARIADB_DATABASE=garmin_health_lab
|
|
|
|
# --- Auth ---
|
|
# CHANGE THIS in production! Used to sign JWTs (7-day expiry by default).
|
|
JWT_SECRET=dev_secret_change_me
|
|
JWT_EXPIRY_DAYS=7
|
|
|
|
# --- CORS (comma-separated allowed front-end origins) ---
|
|
CORS_ORIGIN=http://localhost:3000,http://localhost:5173
|
|
|
|
# --- AI models (text-only, large context) ---
|
|
# Put REAL keys in backend/.env — that file is gitignored. Never commit keys.
|
|
# Any model whose credentials are absent is skipped automatically.
|
|
|
|
# Self-hosted AI gateway (model id "gateway"). OpenAI-compatible; it fans out
|
|
# over nvidia/gemini/ollama itself and rotates several Gemini keys, so it
|
|
# absorbs single-vendor quota limits. Reached directly, bypassing any local
|
|
# HTTP proxy. NOTE: its NVIDIA upstream is a large reasoning model — replies
|
|
# can take 2-3 minutes, so set AI_TIMEOUT_SECONDS accordingly.
|
|
AI_GATEWAY_BASE_URL=http://129.146.203.203:5100/v1
|
|
AI_GATEWAY_TOKEN=
|
|
AI_GATEWAY_MODEL=ai-gateway-auto
|
|
|
|
# Google AI Studio -> "gemini-flash". Free-tier quota is small; 429s are common.
|
|
GEMINI_API_KEY=
|
|
|
|
# NVIDIA NIM -> "llama-70b", "nemotron-49b", "mistral-large".
|
|
# Model ids come from that account's live GET /v1/models — do not guess them.
|
|
NVIDIA_API_KEY=
|
|
# NVIDIA_BASE_URL=https://integrate.api.nvidia.com/v1
|
|
|
|
# Preference order. The first configured model answers; if it fails or times
|
|
# out, the next is tried. Read per request, so changes need no restart.
|
|
AI_MODEL_CHAIN=gateway,gemini-flash,llama-70b
|
|
|
|
# Max days of history sent (CSV-encoded). Trimmed further per model so the
|
|
# payload always fits that model's own context window.
|
|
AI_DAY_BUDGET=365
|
|
|
|
AI_TIMEOUT_SECONDS=180
|
|
# Output cap. Reasoning models spend part of it thinking before they answer;
|
|
# entries that need more declare their own budget in services/ai.py.
|
|
AI_MAX_TOKENS=1024
|