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, **kwargs)
基类: BaseLM
一种支持聊天或文本补全请求的语言模型,用于与dspy模块配合使用。
创建一个新的语言模型实例,用于与DSPy模块和程序配合使用。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
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
|
rollout_id
|
可选的整数,用于区分相同请求的缓存条目。不同的值会绕过DSPy的缓存,同时仍会缓存具有相同输入和rollout ID的未来调用。请注意, |
必填 |
Source code in dspy/clients/lm.py
函数
__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)
返回语言模型的副本,可能包含已更新的参数。
任何提供的关键字参数都会更新副本的相应属性或LM参数。例如,lm.copy(rollout_id=1, temperature=1.0)返回一个LM,其请求使用不同的rollout ID和非零温度来绕过缓存冲突。
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
:::