API Reference¶
This section provides a detailed reference for the dspy-profiles Python API.
Usage Examples¶
from dspy_profiles import profile
with profile("prod"):
predictor = dspy.Predict("question -> answer")
result = predictor(question="What is the latest build status?")
from dspy_profiles import with_profile
@with_profile("staging", settings={"cache_dir": ".cache/staging"})
def run_evaluation(prompt: str) -> str:
return dspy.Predict("prompt -> answer")(prompt=prompt).answer
from dspy_profiles import current_profile, profile
with profile("analysis"):
active = current_profile()
assert active and active.name == "analysis"
dspy_profiles
¶
dspy-profiles package.
current_profile()
¶
Returns the currently active dspy-profiles profile.
This utility function provides introspection to see the fully resolved settings
of the profile that is currently active via the profile context manager
or @with_profile decorator.
Returns:
| Type | Description |
|---|---|
ResolvedProfile | None
|
ResolvedProfile | None: The active ResolvedProfile, or None if no profile is active. |
Source code in dspy_profiles/core.py
lm(profile_name, cached=True, config_path=None, **overrides)
¶
Gets a pre-configured dspy.LM instance for a given profile.
This is a convenience utility to quickly get a language model instance without needing the full context manager. It's useful for lightweight tasks or when you need an LM instance outside of a DSPy program flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile_name
|
str
|
The name of the profile to use. |
required |
cached
|
bool
|
If True, a cached LM instance will be returned if available. Set to False to force a new instance to be created. Defaults to True. |
True
|
**overrides
|
Any
|
Keyword arguments to override profile settings for the LM. |
{}
|
Returns:
| Type | Description |
|---|---|
LM | None
|
dspy.LM | None: A configured |
Source code in dspy_profiles/core.py
profile(profile_name=None, *, force=False, config_path=None, **overrides)
¶
A context manager to temporarily apply a dspy-profiles configuration.
This context manager activates a specified profile, configuring dspy.settings
with the language model (LM), retrieval model (RM), and other settings defined
in the profile. It also handles profile precedence and allows for inline overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile_name
|
str | None
|
The name of the profile to activate. If not
provided, it falls back to the |
None
|
force
|
bool
|
If True, this profile will override any profile set via
the |
False
|
config_path
|
str | None
|
Path to the |
None
|
**overrides
|
Any
|
Keyword arguments to override profile settings (e.g., |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
None |
None
|
The context manager does not yield a value. |
Example
Source code in dspy_profiles/core.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
with_profile(profile_name, *, force=False, config_path=None, **overrides)
¶
A decorator to apply a dspy-profiles configuration to a function or dspy.Module.
This decorator wraps a function or a dspy.Module class, activating the
specified profile before the decorated object is called.
When applied to a function, it wraps the function directly. When applied to a
class (like a dspy.Module), it wraps the __call__ method, ensuring the
profile is active during its execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile_name
|
str
|
The name of the profile to activate. |
required |
force
|
bool
|
If True, this profile will override any profile set via
the |
False
|
config_path
|
str | None
|
Path to the |
None
|
**overrides
|
Any
|
Keyword arguments to override profile settings. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
The decorated function or class. |
Example (Function):
@dspy_profiles.with_profile("testing", temperature=0)
def my_dspy_program(question):
return dspy.Predict("question -> answer")(question=question)
Example (dspy.Module):
@dspy_profiles.with_profile("agent-profile")
class MyAgent(dspy.Module):
def __init__(self):
super().__init__()
self.predict = dspy.Predict("question -> answer")
def __call__(self, question):
return self.predict(question=question)
Source code in dspy_profiles/core.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
Runtime Helpers¶
lm(profile_name, **overrides) returns a configured dspy.LM instance for the
given profile without entering a context manager. It supports caching and accepts
LM-specific overrides and an optional config_path.
Example