Skip to content

Commit 854097a

Browse files
committed
Refactor environment loading: support .env files from both project root and notebooks directory
Signed-off-by: Andre Bossard <anbossar@microsoft.com>
1 parent 0a6f565 commit 854097a

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

notebooks/02_optimization.ipynb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@
310310
"cats = Counter(t[\"category\"] for t in tickets)\n",
311311
"prios = Counter(t[\"priority\"] for t in tickets)\n",
312312
"\n",
313-
"# Gruppen-Verteilung (das Kernproblem)\n",
314313
"sorted_groups = groups.most_common()\n",
315314
"names = [g[0] for g in sorted_groups]\n",
316315
"counts = [g[1] for g in sorted_groups]\n",
@@ -323,7 +322,7 @@
323322
" textposition='outside'\n",
324323
"))\n",
325324
"fig.update_layout(\n",
326-
" title=\"Training-Beispiele pro Gruppe — das Kernproblem\",\n",
325+
" title=\"Training-Beispiele pro Gruppe\",\n",
327326
" xaxis_title=\"Anzahl Trainings-Tickets\",\n",
328327
" yaxis=dict(autorange=\"reversed\"),\n",
329328
" height=600, margin=dict(l=250),\n",

notebooks/dspy_tasks/config.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@
1919

2020
# Project root is two levels up from this file (notebooks/dspy_tasks/config.py → project root)
2121
PROJECT_ROOT = Path(__file__).parent.parent.parent
22+
NOTEBOOKS_DIR = Path(__file__).parent.parent
2223
ENV_FILE = PROJECT_ROOT / ".env"
24+
NOTEBOOKS_ENV_FILE = NOTEBOOKS_DIR / ".env"
2325

2426

2527
# ============================================================================
2628
# ACTIONS: Load environment
2729
# ============================================================================
2830

2931
def _load_env():
30-
"""Load .env file from project root if it exists. ACTION: file I/O."""
32+
"""Load .env file from project root and/or notebooks dir. ACTION: file I/O."""
3133
try:
3234
from dotenv import load_dotenv
33-
if ENV_FILE.exists():
34-
load_dotenv(ENV_FILE, override=False)
35-
logger.debug("Loaded .env from %s", ENV_FILE)
36-
else:
37-
logger.debug("No .env file at %s, using system environment", ENV_FILE)
35+
loaded = False
36+
for env_path in [ENV_FILE, NOTEBOOKS_ENV_FILE]:
37+
if env_path.exists():
38+
load_dotenv(env_path, override=False)
39+
logger.debug("Loaded .env from %s", env_path)
40+
loaded = True
41+
if not loaded:
42+
logger.debug("No .env file found, using system environment")
3843
except ImportError:
3944
logger.debug("python-dotenv not installed, using system environment only")
4045

@@ -113,15 +118,34 @@ def discover_models() -> list[str]:
113118
return []
114119

115120

116-
def get_available_models() -> list[str]:
117-
"""Return all available models: configured + discovered, deduplicated.
121+
def discover_lmstudio_models() -> list[str]:
122+
"""Discover models from a local LM Studio instance.
118123
119-
This mirrors backend/llm_service.py's get_model_catalog() logic.
124+
Only attempts discovery if LM_STUDIO_API_BASE is set.
125+
Uses LiteLLM's native lm_studio/ prefix convention.
126+
Returns an empty list if LM Studio is not running or not configured.
120127
"""
128+
base_url = os.getenv("LM_STUDIO_API_BASE")
129+
if not base_url:
130+
return []
131+
try:
132+
import httpx
133+
resp = httpx.get(f"{base_url}/models", timeout=2.0)
134+
resp.raise_for_status()
135+
data = resp.json().get("data", [])
136+
return [f"lm_studio/{m['id']}" for m in data if m.get("id")]
137+
except Exception as exc:
138+
logger.debug("LM Studio discovery failed (is it running?): %s", exc)
139+
return []
140+
141+
142+
def get_available_models() -> list[str]:
143+
"""Return all available models: configured + discovered + LM Studio, deduplicated."""
121144
primary = get_default_model()
122145
fallbacks = get_fallback_models()
123146
discovered = discover_models()
124-
return _build_model_list(primary, fallbacks, discovered)
147+
lmstudio = discover_lmstudio_models()
148+
return _build_model_list(primary, fallbacks, discovered + lmstudio)
125149

126150

127151
# ============================================================================

0 commit comments

Comments
 (0)