Pointing OPENAI_BASE_URL at a local llama-server or Ollama sends every gpt-* call there instead of to OpenAI, and litellm reports that as a bare "Connection Error" — which reads like a network problem even though the host can reach api.openai.com fine. Deploying a .env that still carries the local endpoint reproduces it on the server, with nothing in the message naming the culprit. Add OPENAI_BASE_URL and OPENAI_API_BASE to the settings allowlist so the endpoint can be corrected without editing .env and recreating the container. Both are listed because litellm reads OPENAI_API_BASE while the OpenAI SDK reads OPENAI_BASE_URL, so fixing one can leave the other in effect. Neither is a secret, so the panel shows the current value. No cache invalidation is needed alongside this: litellm keys its client cache on the endpoint, so a changed value takes effect on the next call. Document the trap where it will be read — a commented template in .env.example and a note in the Lightsail deploy guide about carrying a local .env over. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
import os
|
|
|
|
from sqlalchemy.orm import Session as DBSession
|
|
|
|
from app.db.models import AppSetting
|
|
|
|
# GUIから編集可能な環境変数の許可リスト。ここに無いキーは save()/clear() で拒否する。
|
|
CONFIGURABLE_KEYS = [
|
|
"OPENAI_API_KEY",
|
|
# OpenAI 互換エンドポイント。ローカルの llama-server / Ollama を向けたまま忘れると、
|
|
# OpenAI のモデルを選んでも接続先がそちらのままになる(litellm は OPENAI_API_BASE、
|
|
# OpenAI SDK は OPENAI_BASE_URL を見るので両方を出しておく)。
|
|
"OPENAI_BASE_URL",
|
|
"OPENAI_API_BASE",
|
|
"ANTHROPIC_API_KEY",
|
|
"AWS_ACCESS_KEY_ID",
|
|
"AWS_SECRET_ACCESS_KEY",
|
|
"AWS_SESSION_TOKEN",
|
|
"AWS_BEARER_TOKEN_BEDROCK",
|
|
"AWS_REGION_NAME",
|
|
"OLLAMA_API_BASE",
|
|
"TAVILY_API_KEY",
|
|
"LOCAL_KB_EMBEDDING_MODEL",
|
|
"SEARCH_SUMMARY_MODEL",
|
|
]
|
|
|
|
# 秘匿情報ではないキー。値をマスクせずそのまま返し、GUIでも通常のテキスト入力として扱う。
|
|
# モデル名やURLは「今どれが効いているか」が見えないと設定しづらいため。
|
|
NON_SECRET_KEYS = {
|
|
"OPENAI_BASE_URL",
|
|
"OPENAI_API_BASE",
|
|
"AWS_REGION_NAME",
|
|
"OLLAMA_API_BASE",
|
|
"LOCAL_KB_EMBEDDING_MODEL",
|
|
"SEARCH_SUMMARY_MODEL",
|
|
}
|
|
|
|
# .env由来の起動時の値のスナップショット。clear() で戻す先として使う。
|
|
_env_defaults: dict[str, str | None] = {}
|
|
_snapshot_taken = False
|
|
|
|
|
|
def _ensure_snapshot() -> None:
|
|
global _snapshot_taken
|
|
if _snapshot_taken:
|
|
return
|
|
for key in CONFIGURABLE_KEYS:
|
|
_env_defaults[key] = os.environ.get(key)
|
|
_snapshot_taken = True
|
|
|
|
|
|
def _mask(value: str) -> str:
|
|
if len(value) <= 4:
|
|
return "••••"
|
|
return f"••••{value[-4:]}"
|
|
|
|
|
|
def load_into_env(db: DBSession) -> None:
|
|
"""DBに保存済みの値をos.environに適用する。起動時に一度呼ぶ。"""
|
|
_ensure_snapshot()
|
|
for row in db.query(AppSetting).all():
|
|
if row.key in CONFIGURABLE_KEYS:
|
|
os.environ[row.key] = row.value
|
|
|
|
|
|
def list_masked(db: DBSession) -> list[dict]:
|
|
"""GUI設定画面に出す一覧。秘匿キーの値はマスクし、そうでないキーは現在値をそのまま見せる。"""
|
|
_ensure_snapshot()
|
|
saved = {row.key: row.value for row in db.query(AppSetting).all()}
|
|
result = []
|
|
for key in CONFIGURABLE_KEYS:
|
|
current = os.environ.get(key)
|
|
secret = key not in NON_SECRET_KEYS
|
|
result.append({
|
|
"key": key,
|
|
"is_set": bool(current),
|
|
"display_value": (_mask(current) if secret else current) if current else None,
|
|
"secret": secret,
|
|
"overridden": key in saved,
|
|
})
|
|
return result
|
|
|
|
|
|
def save(db: DBSession, key: str, value: str) -> None:
|
|
if key not in CONFIGURABLE_KEYS:
|
|
raise ValueError(f"Unknown setting key: {key}")
|
|
_ensure_snapshot()
|
|
row = db.get(AppSetting, key)
|
|
if row:
|
|
row.value = value
|
|
else:
|
|
db.add(AppSetting(key=key, value=value))
|
|
db.commit()
|
|
os.environ[key] = value
|
|
|
|
|
|
def clear(db: DBSession, key: str) -> None:
|
|
if key not in CONFIGURABLE_KEYS:
|
|
raise ValueError(f"Unknown setting key: {key}")
|
|
_ensure_snapshot()
|
|
row = db.get(AppSetting, key)
|
|
if row:
|
|
db.delete(row)
|
|
db.commit()
|
|
original = _env_defaults.get(key)
|
|
if original is None:
|
|
os.environ.pop(key, None)
|
|
else:
|
|
os.environ[key] = original
|