Skip to content

Commit b9edcaa

Browse files
committed
Update version to 0.3.1 and update DSPyMator documentation and LM param
- Bumped version from 0.3.0 to 0.3.1 in `pyproject.toml`. - Added detailed configuration instructions for language models in `dspymator.md`, including usage examples for both string identifiers and pre-configured `dspy.LM` objects. - Updated parameter references to clarify the handling of `temperature` and `max_tokens` when using `dspy.LM` objects.
1 parent 4a9b332 commit b9edcaa

3 files changed

Lines changed: 95 additions & 7 deletions

File tree

docs/user-guide/dspymator.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,65 @@ multi_input_classifier.fit(movie_data[["title", "review_text", "rating"]], None)
247247
predictions = multi_input_classifier.predict(movie_data[["title", "review_text", "rating"]])
248248
```
249249

250+
### Configuring Language Models
251+
252+
DSPyMator accepts either a model string or a pre-configured `dspy.LM` object for the `lm` parameter.
253+
254+
**Simple usage with model string:**
255+
256+
```python
257+
# Uses default OpenAI API (requires OPENAI_API_KEY env var)
258+
classifier = DSPyMator(
259+
program=dspy.Predict("text -> label"),
260+
target_names="label",
261+
lm="openai/gpt-4o-mini",
262+
temperature=0.0,
263+
max_tokens=1000,
264+
)
265+
```
266+
267+
**Using custom providers:**
268+
269+
For custom API configuration, pass a pre-configured `dspy.LM` object. DSPy uses [LiteLLM](https://docs.litellm.ai/) under the hood, so any LiteLLM-supported provider works. Extra kwargs are passed through to LiteLLM:
270+
271+
```python
272+
import dspy
273+
274+
# Pass a pre-configured LM object
275+
classifier = DSPyMator(
276+
program=dspy.Predict("text -> label"),
277+
target_names="label",
278+
lm=dspy.LM(
279+
"openrouter/anthropic/claude-3-haiku",
280+
temperature=0.1,
281+
max_tokens=1000,
282+
# Additional kwargs are passed to LiteLLM
283+
),
284+
)
285+
```
286+
287+
!!! note "Temperature and max_tokens"
288+
When passing a `dspy.LM` object, configure `temperature` and `max_tokens` on the LM directly. The DSPyMator parameters are ignored when using a pre-configured LM.
289+
290+
**Environment variables:**
291+
292+
Most providers are configured via environment variables. Set them before calling `fit()`:
293+
294+
```python
295+
import os
296+
297+
# OpenAI
298+
os.environ["OPENAI_API_KEY"] = "sk-..."
299+
300+
# OpenRouter
301+
os.environ["OPENROUTER_API_KEY"] = "sk-or-..."
302+
303+
# Anthropic
304+
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
305+
```
306+
307+
See the [DSPy LM documentation](https://dspy.ai/api/models/LM/) and [LiteLLM provider docs](https://docs.litellm.ai/docs/providers) for supported providers and configuration.
308+
250309
### Async Execution for Speed
251310

252311
By default, DSPyMator uses async execution for faster batch predictions:
@@ -291,4 +350,27 @@ llm_pipeline = make_pipeline(
291350
model="openai/text-embedding-3-small", # or your preferred embedding model
292351
feature_names=["reasoning"], # specify which columns to embed
293352
)
294-
```
353+
```
354+
355+
## Parameters Reference
356+
357+
| Parameter | Type | Default | Description |
358+
|-----------|------|---------|-------------|
359+
| `program` | `dspy.Module` | *required* | DSPy module (e.g., `dspy.Predict`, `dspy.ChainOfThought`) with a signature defining input/output fields |
360+
| `target_names` | `str \| list[str]` | *required* | Output field name(s) to use as predictions |
361+
| `feature_names` | `list[str] \| None` | `None` | Column names mapping input data to signature fields. If `None`, inferred from dataframe columns |
362+
| `lm` | `str \| dspy.LM` | `"openai/gpt-5-nano"` | Language model - either a string identifier or a pre-configured `dspy.LM` object |
363+
| `temperature` | `float` | `1.0` | Sampling temperature (ignored if `lm` is a `dspy.LM` object) |
364+
| `max_tokens` | `int` | `16000` | Maximum tokens in responses (ignored if `lm` is a `dspy.LM` object) |
365+
| `use_async` | `bool` | `True` | Use async execution for batch predictions |
366+
| `max_concurrent` | `int` | `50` | Maximum concurrent requests in async mode |
367+
| `verbose` | `bool` | `True` | Show progress bars during prediction |
368+
369+
### fit() Parameters
370+
371+
| Parameter | Type | Default | Description |
372+
|-----------|------|---------|-------------|
373+
| `X` | DataFrame/array | *required* | Training features |
374+
| `y` | Series/array | *required* | Target values (can be `None` for unsupervised) |
375+
| `optimizer` | `dspy.Optimizer \| None` | `None` | DSPy optimizer instance (e.g., `dspy.GEPA`, `dspy.BootstrapFewShot`) |
376+
| `validation_data` | `tuple \| float \| None` | `None` | Validation data as `(X_val, y_val)`, a float for train split fraction, or `None` |

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "centimators"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
description = "essential data transformers and model estimators for ML and data science competitions"
55
readme = "README.md"
66
authors = [

src/centimators/model_estimators/dspymator.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ class DSPyMator(TransformerMixin, BaseEstimator):
117117
feature_names: Column names mapping input data to signature input fields.
118118
If None, inferred from dataframe columns or uses signature field names
119119
for numpy arrays. Must match the number of input fields in the signature.
120-
lm: Language model identifier (e.g., "openai/gpt-4", "anthropic/claude-3").
120+
lm: Language model - either a string identifier (e.g., "openai/gpt-4") or a
121+
pre-configured `dspy.LM` object. Pass a `dspy.LM` directly when you need
122+
custom configuration like `api_key` or `api_base` for providers like OpenRouter.
123+
When passing an LM object, `temperature` and `max_tokens` are ignored.
121124
Defaults to "openai/gpt-5-nano".
122125
temperature: Sampling temperature for the language model. Defaults to 1.0.
123126
max_tokens: Maximum tokens in model responses. Defaults to 16000.
@@ -167,7 +170,7 @@ class DSPyMator(TransformerMixin, BaseEstimator):
167170
program: dspy.Module
168171
target_names: str | list[str]
169172
feature_names: list[str] | None = None
170-
lm: str = "openai/gpt-5-nano"
173+
lm: str | dspy.LM = "openai/gpt-5-nano"
171174
temperature: float = 1.0
172175
max_tokens: int = 16000
173176
use_async: bool = True
@@ -243,9 +246,12 @@ def fit(
243246
estimator.fit(X_train, y_train, optimizer=gepa_optimizer, validation_data=0.2)
244247
```
245248
"""
246-
self.lm_ = dspy.LM(
247-
self.lm, temperature=self.temperature, max_tokens=self.max_tokens
248-
)
249+
if isinstance(self.lm, dspy.LM):
250+
self.lm_ = self.lm
251+
else:
252+
self.lm_ = dspy.LM(
253+
self.lm, temperature=self.temperature, max_tokens=self.max_tokens
254+
)
249255

250256
self.input_fields_ = list(self.signature_.input_fields.keys())
251257

0 commit comments

Comments
 (0)