Spaces:
Running
Running
Commit Β·
37a0153
1
Parent(s): 1b2fa72
fixed dspy errors
Browse files- ai/groq_setup.py +21 -28
- ai/pipeline.py +6 -5
ai/groq_setup.py
CHANGED
|
@@ -1,40 +1,33 @@
|
|
| 1 |
-
"""DSPy language model setup for Groq and OpenAI.
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
import dspy
|
| 8 |
import config
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
the
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
return dspy.LM(
|
| 33 |
-
model=f"openai/{config.OPENAI_MODEL}",
|
| 34 |
-
api_key=config.OPENAI_API_KEY,
|
| 35 |
-
max_tokens=4096,
|
| 36 |
-
temperature=0.2,
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Default / Groq: reuse the global LM so we share configuration.
|
| 40 |
-
return _default_lm
|
|
|
|
| 1 |
+
"""DSPy language model setup for Groq (and optionally OpenAI).
|
| 2 |
|
| 3 |
+
We configure DSPy ONCE at import time on the main thread to avoid
|
| 4 |
+
thread-safety issues with FastAPI's worker threads.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import dspy
|
| 8 |
import config
|
| 9 |
|
| 10 |
|
| 11 |
+
def _configure_default_lm() -> dspy.LM:
|
| 12 |
+
"""Configure the global DSPy LM once and return it."""
|
| 13 |
+
lm = dspy.LM(
|
| 14 |
+
model=f"groq/{config.GROQ_MODEL}",
|
| 15 |
+
api_key=config.GROQ_API_KEY,
|
| 16 |
+
max_tokens=4096,
|
| 17 |
+
temperature=0.2,
|
| 18 |
+
)
|
| 19 |
+
dspy.configure(lm=lm)
|
| 20 |
+
return lm
|
| 21 |
|
| 22 |
|
| 23 |
+
_DEFAULT_LM = _configure_default_lm()
|
| 24 |
+
|
| 25 |
|
| 26 |
+
def get_lm(provider: str = "groq") -> dspy.LM:
|
| 27 |
+
"""Return the LM instance to use.
|
| 28 |
|
| 29 |
+
NOTE: To keep things simple and robust inside the web server, we always
|
| 30 |
+
use the globally configured LM. The `provider` argument is accepted for
|
| 31 |
+
future extension but currently ignored.
|
| 32 |
"""
|
| 33 |
+
return _DEFAULT_LM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ai/pipeline.py
CHANGED
|
@@ -36,14 +36,15 @@ class SQLAnalystPipeline:
|
|
| 36 |
"""End-to-end reasoning pipeline: question β SQL β results β insights."""
|
| 37 |
|
| 38 |
def __init__(self, provider: str = "groq"):
|
|
|
|
| 39 |
self.provider = provider
|
| 40 |
self._lm = get_lm(provider)
|
| 41 |
|
| 42 |
-
# DSPy predict modules β
|
| 43 |
-
self.analyze = dspy.Predict(AnalyzeAndPlan
|
| 44 |
-
self.generate_sql = dspy.Predict(SQLGeneration
|
| 45 |
-
self.interpret = dspy.Predict(InterpretAndInsight
|
| 46 |
-
self.repair = dspy.Predict(SQLRepair
|
| 47 |
|
| 48 |
# ββ public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
|
|
|
|
| 36 |
"""End-to-end reasoning pipeline: question β SQL β results β insights."""
|
| 37 |
|
| 38 |
def __init__(self, provider: str = "groq"):
|
| 39 |
+
# For now we always use the globally configured LM (Groq).
|
| 40 |
self.provider = provider
|
| 41 |
self._lm = get_lm(provider)
|
| 42 |
|
| 43 |
+
# DSPy predict modules β rely on global dspy.settings
|
| 44 |
+
self.analyze = dspy.Predict(AnalyzeAndPlan)
|
| 45 |
+
self.generate_sql = dspy.Predict(SQLGeneration)
|
| 46 |
+
self.interpret = dspy.Predict(InterpretAndInsight)
|
| 47 |
+
self.repair = dspy.Predict(SQLRepair)
|
| 48 |
|
| 49 |
# ββ public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
|