dspy.Adapter
dspy.Adapter(callbacks: list[BaseCallback] | None = None, use_native_function_calling: bool = False)
Source code in dspy/adapters/base.py
函数
__call__(lm: LM, lm_kwargs: dict[str, Any], signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
Source code in dspy/adapters/base.py
acall(lm: LM, lm_kwargs: dict[str, Any], signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
async
Source code in dspy/adapters/base.py
format(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
为LM调用格式化输入消息。
该方法将DSPy结构化输入以及少量示例和对话历史转换为LM所期望的多轮消息。对于自定义适配器,可以重写此方法以定制输入消息的格式。
通常我们建议消息采用以下结构:
[
{"role": "system", "content": system_message},
# 开始少量示例
{"role": "user", "content": few_shot_example_1_input},
{"role": "assistant", "content": few_shot_example_1_output},
{"role": "user", "content": few_shot_example_2_input},
{"role": "assistant", "content": few_shot_example_2_output},
...
# 结束少量示例
# 开始对话历史
{"role": "user", "content": conversation_history_1_input},
{"role": "assistant", "content": conversation_history_1_output},
{"role": "user", "content": conversation_history_2_input},
{"role": "assistant", "content": conversation_history_2_output},
...
# 结束对话历史
{"role": "user", "content": current_input},
]
系统消息应包含字段描述、字段结构和任务描述。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于格式化输入消息的DSPy签名。 |
必填 |
demos
|
list[dict[str, Any]]
|
少量示例的列表。 |
必填 |
inputs
|
dict[str, Any]
|
DSPy模块的输入参数。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
list[dict[str, Any]]
|
LM所期望的多轮消息列表。 |
Source code in dspy/adapters/base.py
144 145 146 147 148 149 150 151 152 153 154 155 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 | |
format_assistant_message_content(signature: type[Signature], outputs: dict[str, Any], missing_field_message: str | None = None) -> str
格式化助手消息内容。
此方法格式化助手消息内容,可用于格式化少样本示例和对话历史记录。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于格式化助手消息内容的DSPy签名。 |
必填 |
outputs
|
dict[str, Any]
|
需要格式化的输出字段。 |
必填 |
missing_field_message
|
str | None
|
当字段缺失时使用的消息。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
str
|
包含助手消息内容的字符串。 |
Source code in dspy/adapters/base.py
format_conversation_history(signature: type[Signature], history_field_name: str, inputs: dict[str, Any]) -> list[dict[str, Any]]
格式化对话历史记录。
此方法将对话历史和当前输入格式化为多轮消息。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于格式化对话历史的DSPy签名。 |
必填 |
history_field_name
|
str
|
签名中历史字段的名称。 |
必填 |
inputs
|
dict[str, Any]
|
DSPy模块的输入参数。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
list[dict[str, Any]]
|
一个多轮消息列表。 |
Source code in dspy/adapters/base.py
format_demos(signature: type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]
格式化少样本示例。
该方法将少样本示例格式化为多轮消息。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于格式化少样本示例的DSPy签名。 |
必填 |
demos
|
list[dict[str, Any]]
|
一个少样本示例列表,每个元素都是一个字典,包含签名输入和输出字段的键。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
list[dict[str, Any]]
|
一个多轮消息列表。 |
Source code in dspy/adapters/base.py
format_field_description(signature: type[Signature]) -> str
为系统消息格式化字段描述。
该方法格式化系统消息的字段描述。它应返回一个字符串,其中包含输入字段和输出字段的字段描述。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
要格式化字段描述的Dspy签名。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
str
|
一个包含输入字段和输出字段描述信息的字符串。 |
Source code in dspy/adapters/base.py
format_field_structure(signature: type[Signature]) -> str
格式化系统消息的字段结构。
该方法格式化系统消息的字段结构。它应返回一个字符串,用于指定输入字段应提供给语言模型的格式,以及输出字段在响应中的格式。 请参考ChatAdapter和JsonAdapter作为示例。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于格式化字段结构的DSPy签名。 |
必填 |
Source code in dspy/adapters/base.py
format_task_description(signature: type[Signature]) -> str
为系统消息格式化任务描述。
该方法格式化系统消息的任务描述。在大多数情况下,这只是一个对signature.instructions的简单封装。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
DSpy模块的DSPy签名。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
str
|
描述任务的字符串。 |
Source code in dspy/adapters/base.py
format_user_message_content(signature: type[Signature], inputs: dict[str, Any], prefix: str = '', suffix: str = '', main_request: bool = False) -> str
格式化用户消息内容。
该方法格式化用户消息内容,可用于格式化少样本示例、对话历史记录和当前输入。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于格式化用户消息内容的DSPy签名。 |
必填 |
inputs
|
dict[str, Any]
|
DSPy模块的输入参数。 |
必填 |
prefix
|
str
|
用户消息内容的前缀。 |
''
|
suffix
|
str
|
用户消息内容的后缀。 |
''
|
返回:
| 类型 | 描述 |
|---|---|
str
|
包含用户消息内容的字符串。 |
Source code in dspy/adapters/base.py
parse(signature: type[Signature], completion: str) -> dict[str, Any]
将语言模型输出解析为输出字段的字典。
该方法将语言模型输出解析为输出字段的字典。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
type[Signature]
|
用于解析语言模型输出的DSPy签名。 |
必填 |
completion
|
str
|
待解析的语言模型输出。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any]
|
输出字段的字典。 |
Source code in dspy/adapters/base.py
:::