|
1 | | -"""DSPy prompt optimization route.""" |
| 1 | +"""DSPy prompt optimization, persistence, and evaluation routes.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
|
8 | 8 |
|
9 | 9 | from app.core.config import get_settings |
10 | 10 | from app.schemas.plugins import ( |
| 11 | + DSPyEvaluateRequest, |
| 12 | + DSPyEvaluateResponse, |
| 13 | + DSPyListResponse, |
| 14 | + DSPyLoadResponse, |
11 | 15 | DSPyOptimizationRequest, |
12 | 16 | DSPyOptimizationResponse, |
| 17 | + DSPySaveRequest, |
| 18 | + DSPySaveResponse, |
| 19 | +) |
| 20 | +from app.services.dspy_optimizer import ( |
| 21 | + async_load_config, |
| 22 | + async_run_evaluation, |
| 23 | + async_run_optimization, |
| 24 | + async_save_config, |
| 25 | + list_configs, |
13 | 26 | ) |
14 | | -from app.services.dspy_optimizer import async_run_optimization |
15 | 27 |
|
16 | 28 | logger = logging.getLogger(__name__) |
17 | 29 |
|
@@ -40,15 +52,7 @@ async def optimize_prompt( |
40 | 52 | calls internally. Expect response times of 30s-5min |
41 | 53 | depending on training set size and optimizer strategy. |
42 | 54 | """ |
43 | | - settings = get_settings() |
44 | | - |
45 | | - if not settings.DSPY_ENABLED: |
46 | | - raise HTTPException( |
47 | | - status_code=503, |
48 | | - detail=( |
49 | | - "DSPy optimization is disabled. " "Set DSPY_ENABLED=true to enable." |
50 | | - ), |
51 | | - ) |
| 55 | + _check_dspy_enabled() |
52 | 56 |
|
53 | 57 | if len(request.train_texts) != len(request.expected_results): |
54 | 58 | raise HTTPException( |
@@ -83,3 +87,167 @@ async def optimize_prompt( |
83 | 87 | ) from exc |
84 | 88 |
|
85 | 89 | return DSPyOptimizationResponse(**result) |
| 90 | + |
| 91 | + |
| 92 | +# ------------------------------------------------------------------- |
| 93 | +# Config persistence endpoints |
| 94 | +# ------------------------------------------------------------------- |
| 95 | + |
| 96 | + |
| 97 | +def _check_dspy_enabled() -> None: |
| 98 | + """Raise 503 if DSPy is disabled.""" |
| 99 | + settings = get_settings() |
| 100 | + if not settings.DSPY_ENABLED: |
| 101 | + raise HTTPException( |
| 102 | + status_code=503, |
| 103 | + detail=( |
| 104 | + "DSPy is disabled. Set DSPY_ENABLED=true to enable." |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +@router.post( |
| 110 | + "/dspy/configs/save", |
| 111 | + response_model=DSPySaveResponse, |
| 112 | + summary="Save an optimized DSPy config", |
| 113 | + description=( |
| 114 | + "Persist an optimized prompt description and curated " |
| 115 | + "few-shot examples to disk under the configured " |
| 116 | + "``DSPY_CONFIG_DIR``. The saved config can later be loaded " |
| 117 | + "for extraction or evaluation without re-running " |
| 118 | + "optimization." |
| 119 | + ), |
| 120 | +) |
| 121 | +async def save_config(request: DSPySaveRequest) -> DSPySaveResponse: |
| 122 | + """Save an optimized DSPy config to disk.""" |
| 123 | + _check_dspy_enabled() |
| 124 | + |
| 125 | + try: |
| 126 | + result = await async_save_config( |
| 127 | + config_name=request.config_name, |
| 128 | + prompt_description=request.prompt_description, |
| 129 | + examples=request.examples, |
| 130 | + metadata=request.metadata, |
| 131 | + ) |
| 132 | + except Exception as exc: |
| 133 | + logger.exception("Failed to save DSPy config '%s'", request.config_name) |
| 134 | + raise HTTPException( |
| 135 | + status_code=500, |
| 136 | + detail=f"Failed to save config: {exc}", |
| 137 | + ) from exc |
| 138 | + |
| 139 | + return DSPySaveResponse(**result) |
| 140 | + |
| 141 | + |
| 142 | +@router.get( |
| 143 | + "/dspy/configs", |
| 144 | + response_model=DSPyListResponse, |
| 145 | + summary="List saved DSPy configs", |
| 146 | + description=( |
| 147 | + "Return the names of all saved optimized configs " |
| 148 | + "available under ``DSPY_CONFIG_DIR``." |
| 149 | + ), |
| 150 | +) |
| 151 | +async def list_saved_configs() -> DSPyListResponse: |
| 152 | + """List all saved DSPy config names.""" |
| 153 | + _check_dspy_enabled() |
| 154 | + return DSPyListResponse(configs=list_configs()) |
| 155 | + |
| 156 | + |
| 157 | +@router.get( |
| 158 | + "/dspy/configs/{config_name}", |
| 159 | + response_model=DSPyLoadResponse, |
| 160 | + summary="Load a saved DSPy config", |
| 161 | + description=( |
| 162 | + "Load a previously saved optimized config by name. " |
| 163 | + "Returns the prompt description, examples, and any " |
| 164 | + "stored metadata." |
| 165 | + ), |
| 166 | +) |
| 167 | +async def load_config(config_name: str) -> DSPyLoadResponse: |
| 168 | + """Load a saved DSPy config from disk.""" |
| 169 | + _check_dspy_enabled() |
| 170 | + |
| 171 | + try: |
| 172 | + result = await async_load_config(config_name) |
| 173 | + except FileNotFoundError as exc: |
| 174 | + raise HTTPException(status_code=404, detail=str(exc)) from exc |
| 175 | + except Exception as exc: |
| 176 | + logger.exception("Failed to load DSPy config '%s'", config_name) |
| 177 | + raise HTTPException( |
| 178 | + status_code=500, |
| 179 | + detail=f"Failed to load config: {exc}", |
| 180 | + ) from exc |
| 181 | + |
| 182 | + return DSPyLoadResponse(**result) |
| 183 | + |
| 184 | + |
| 185 | +# ------------------------------------------------------------------- |
| 186 | +# Evaluation endpoint |
| 187 | +# ------------------------------------------------------------------- |
| 188 | + |
| 189 | + |
| 190 | +@router.post( |
| 191 | + "/dspy/evaluate", |
| 192 | + response_model=DSPyEvaluateResponse, |
| 193 | + summary="Evaluate an optimized DSPy config", |
| 194 | + description=( |
| 195 | + "Evaluate an optimized config against test documents " |
| 196 | + "with expected extractions. Returns precision, recall, " |
| 197 | + "F1 score, and per-document metrics. Supply either a " |
| 198 | + "``config_name`` (previously saved) or inline " |
| 199 | + "``prompt_description`` + ``examples``." |
| 200 | + ), |
| 201 | +) |
| 202 | +async def evaluate_config( |
| 203 | + request: DSPyEvaluateRequest, |
| 204 | +) -> DSPyEvaluateResponse: |
| 205 | + """Evaluate a DSPy config against test data.""" |
| 206 | + _check_dspy_enabled() |
| 207 | + |
| 208 | + if len(request.test_texts) != len(request.expected_results): |
| 209 | + raise HTTPException( |
| 210 | + status_code=400, |
| 211 | + detail=( |
| 212 | + f"test_texts ({len(request.test_texts)}) and " |
| 213 | + f"expected_results ({len(request.expected_results)}) " |
| 214 | + "must have the same length." |
| 215 | + ), |
| 216 | + ) |
| 217 | + |
| 218 | + # Validate that exactly one source is provided |
| 219 | + has_config = request.config_name is not None |
| 220 | + has_inline = ( |
| 221 | + request.prompt_description is not None |
| 222 | + and request.examples is not None |
| 223 | + ) |
| 224 | + if not has_config and not has_inline: |
| 225 | + raise HTTPException( |
| 226 | + status_code=400, |
| 227 | + detail=( |
| 228 | + "Provide either config_name or both " |
| 229 | + "prompt_description and examples." |
| 230 | + ), |
| 231 | + ) |
| 232 | + |
| 233 | + try: |
| 234 | + result = await async_run_evaluation( |
| 235 | + test_texts=request.test_texts, |
| 236 | + expected_results=request.expected_results, |
| 237 | + config_name=request.config_name, |
| 238 | + prompt_description=request.prompt_description, |
| 239 | + examples=request.examples, |
| 240 | + model_id=request.model_id, |
| 241 | + ) |
| 242 | + except FileNotFoundError as exc: |
| 243 | + raise HTTPException(status_code=404, detail=str(exc)) from exc |
| 244 | + except ValueError as exc: |
| 245 | + raise HTTPException(status_code=400, detail=str(exc)) from exc |
| 246 | + except Exception as exc: |
| 247 | + logger.exception("DSPy evaluation failed") |
| 248 | + raise HTTPException( |
| 249 | + status_code=500, |
| 250 | + detail=f"Evaluation failed: {exc}", |
| 251 | + ) from exc |
| 252 | + |
| 253 | + return DSPyEvaluateResponse(**result) |
0 commit comments