File size: 642 Bytes
cfe45d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
Shared DSPy configuration. Called once at import time so all modules
share the same LM instance and thread context.
"""
import threading
import dspy
from app.config import settings

_lock = threading.Lock()
_configured = False


def ensure_dspy_configured():
    """Configure DSPy exactly once, thread-safe."""
    global _configured
    if _configured:
        return
    with _lock:
        if _configured:
            return
        lm = dspy.LM(
            "anthropic/claude-sonnet-4-5-20250929",
            api_key=settings.ANTHROPIC_API_KEY,
        )
        dspy.configure(lm=lm, async_max_workers=8)
        _configured = True