dspy.Adapter
dspy.Adapter(callbacks: Optional[list[BaseCallback]] = None)
源代码位于 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]]
源代码位于 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]]
异步
源代码位于 dspy/adapters/base.py
format(signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
格式化用于调用语言模型的输入消息。
此方法将 DSPy 结构化输入以及少样本示例和对话历史转换为语言模型期望的多轮消息格式。对于自定义适配器,可以覆盖此方法来自定义输入消息的格式。
通常,我们建议消息采用以下结构
[
{"role": "system", "content": system_message},
# Begin few-shot examples
{"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},
...
# End few-shot examples
# Begin conversation history
{"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},
...
# End conversation history
{"role": "user", "content": current_input},
]
And system message should contain the field description, field structure, and task description.
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
Type[Signature]
|
要为其格式化输入消息的 DSPy 签名。 |
必需 |
demos
|
list[dict[str, Any]]
|
少样本示例列表。 |
必需 |
inputs
|
dict[str, Any]
|
DSPy 模块的输入参数。 |
必需 |
返回
类型 | 描述 |
---|---|
list[dict[str, Any]]
|
语言模型期望的多轮消息列表。 |
源代码位于 dspy/adapters/base.py
format_assistant_message_content(signature: Type[Signature], outputs: dict[str, Any], missing_field_message: Optional[str] = None) -> str
格式化助手消息内容。
此方法格式化助手消息内容,可用于格式化少样本示例、对话历史。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
Type[Signature]
|
要为其格式化助手消息内容的 DSPy 签名。 |
必需 |
outputs
|
dict[str, Any]
|
要格式化的输出字段。 |
必需 |
missing_field_message
|
Optional[str]
|
当字段缺失时使用的消息。 |
None
|
返回
类型 | 描述 |
---|---|
str
|
包含助手消息内容的字符串。 |
源代码位于 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]]
|
多轮消息列表。 |
源代码位于 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]]
|
多轮消息列表。 |
源代码位于 dspy/adapters/base.py
format_field_description(signature: Type[Signature]) -> str
格式化系统消息的字段描述。
此方法格式化系统消息的字段描述。它应返回一个包含输入字段和输出字段描述的字符串。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
Type[Signature]
|
要为其格式化字段描述的 DSPy 签名。 |
必需 |
返回
类型 | 描述 |
---|---|
str
|
包含输入字段和输出字段描述的字符串。 |
源代码位于 dspy/adapters/base.py
format_field_structure(signature: Type[Signature]) -> str
格式化系统消息的字段结构。
此方法格式化系统消息的字段结构。它应返回一个字符串,指定应将输入字段提供给语言模型的格式,以及响应中输出字段的格式。请参考 ChatAdapter 和 JsonAdapter 以获取示例。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
Type[Signature]
|
要为其格式化字段结构的 DSPy 签名。 |
必需 |
源代码位于 dspy/adapters/base.py
format_task_description(signature: Type[Signature]) -> str
format_task_description(signature: Type[Signature]) -> str
格式化系统消息的任务描述。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
Type[Signature]
|
此方法格式化系统消息的任务描述。在大多数情况下,这只是对 |
必需 |
返回
类型 | 描述 |
---|---|
str
|
DSPy 模块的 DSPy 签名。 |
源代码位于 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]
|
此方法格式化用户消息内容,可用于格式化少样本示例、对话历史和当前输入。 |
必需 |
inputs
|
dict[str, Any]
|
DSPy 模块的输入参数。 |
必需 |
要为其格式化用户消息内容的 DSPy 签名。
|
str
|
prefix |
''
|
用户消息内容的前缀。
|
str
|
suffix |
''
|
返回
类型 | 描述 |
---|---|
str
|
用户消息内容的后缀。 |
源代码位于 dspy/adapters/base.py
包含用户消息内容的字符串。
parse(signature: Type[Signature], completion: str) -> dict[str, Any]
将语言模型输出解析为输出字段的字典。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
Type[Signature]
|
此方法将语言模型输出解析为输出字段的字典。 |
必需 |
要为其解析语言模型输出的 DSPy 签名。
|
str
|
completion |
必需 |
返回
类型 | 描述 |
---|---|
dict[str, Any]
|
要解析的语言模型输出。 |