dspy.LM¶
dspy.LM(model: str, model_type: Literal['chat', 'text', 'responses'] = 'chat', temperature: float = 0.0, max_tokens: int = 4000, cache: bool = True, callbacks: list[BaseCallback] | None = None, num_retries: int = 3, provider: Provider | None = None, finetuning_model: str | None = None, launch_kwargs: dict[str, Any] | None = None, train_kwargs: dict[str, Any] | None = None, use_developer_role: bool = False, **kwargs)
¶
Bases: BaseLM
A language model supporting chat or text completion requests for use with DSPy modules.
Create a new language model instance for use with DSPy modules and programs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
The model to use. This should be a string of the form |
required |
model_type
|
Literal['chat', 'text', 'responses']
|
The type of the model, either |
'chat'
|
temperature
|
float
|
The sampling temperature to use when generating responses. |
0.0
|
max_tokens
|
int
|
The maximum number of tokens to generate per response. |
4000
|
cache
|
bool
|
Whether to cache the model responses for reuse to improve performance and reduce costs. |
True
|
callbacks
|
list[BaseCallback] | None
|
A list of callback functions to run before and after each request. |
None
|
num_retries
|
int
|
The number of times to retry a request if it fails transiently due to network error, rate limiting, etc. Requests are retried with exponential backoff. |
3
|
provider
|
Provider | None
|
The provider to use. If not specified, the provider will be inferred from the model. |
None
|
finetuning_model
|
str | None
|
The model to finetune. In some providers, the models available for finetuning is different from the models available for inference. |
None
|
rollout_id
|
Optional integer used to differentiate cache entries for otherwise
identical requests. Different values bypass DSPy's caches while still caching
future calls with the same inputs and rollout ID. Note that |
required |
Source code in dspy/clients/lm.py
Functions¶
__call__(prompt=None, messages=None, **kwargs)
¶
acall(prompt=None, messages=None, **kwargs)
async
¶
aforward(prompt=None, messages=None, **kwargs)
async
¶
Source code in dspy/clients/lm.py
copy(**kwargs)
¶
Returns a copy of the language model with possibly updated parameters.
Any provided keyword arguments update the corresponding attributes or LM kwargs of
the copy. For example, lm.copy(rollout_id=1, temperature=1.0)
returns an LM whose
requests use a different rollout ID at non-zero temperature to bypass cache collisions.
Source code in dspy/clients/base_lm.py
dump_state()
¶
finetune(train_data: list[dict[str, Any]], train_data_format: TrainDataFormat | None, train_kwargs: dict[str, Any] | None = None) -> TrainingJob
¶
Source code in dspy/clients/lm.py
forward(prompt=None, messages=None, **kwargs)
¶
Source code in dspy/clients/lm.py
infer_provider() -> Provider
¶
inspect_history(n: int = 1)
¶
kill(launch_kwargs: dict[str, Any] | None = None)
¶
launch(launch_kwargs: dict[str, Any] | None = None)
¶
reinforce(train_kwargs) -> ReinforceJob
¶
Source code in dspy/clients/lm.py
update_history(entry)
¶
Source code in dspy/clients/base_lm.py
:::