dspy.ChatAdapter
dspy.ChatAdapter(callbacks: Optional[list[BaseCallback]] = None)
基类: Adapter
源代码位于 dspy/adapters/chat_adapter.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/chat_adapter.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
|
类型[Signature]
|
用于格式化输入消息的 DSPy 签名。 |
必需 |
demos
|
列表[字典[str, Any]]
|
少样本示例列表。 |
必需 |
inputs
|
字典[str, Any]
|
传递给 DSPy 模块的输入参数。 |
必需 |
返回值
类型 | 描述 |
---|---|
列表[字典[str, Any]]
|
语言模型期望的多轮消息列表。 |
源代码位于 dspy/adapters/base.py
format_assistant_message_content(signature: Type[Signature], outputs: dict[str, Any], missing_field_message=None) -> str
源代码位于 dspy/adapters/chat_adapter.py
format_conversation_history(signature: Type[Signature], history_field_name: str, inputs: dict[str, Any]) -> list[dict[str, Any]]
格式化对话历史。
此方法将对话历史和当前输入格式化为多轮消息。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
类型[Signature]
|
用于格式化对话历史的 DSPy 签名。 |
必需 |
history_field_name
|
str
|
签名中历史字段的名称。 |
必需 |
inputs
|
字典[str, Any]
|
传递给 DSPy 模块的输入参数。 |
必需 |
返回值
类型 | 描述 |
---|---|
列表[字典[str, Any]]
|
多轮消息列表。 |
源代码位于 dspy/adapters/base.py
format_demos(signature: Type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]
格式化少样本示例。
此方法将少样本示例格式化为多轮消息。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
类型[Signature]
|
用于格式化少样本示例的 DSPy 签名。 |
必需 |
demos
|
列表[字典[str, Any]]
|
少样本示例列表,每个元素都是一个字典,包含签名的输入和输出字段作为键。 |
必需 |
返回值
类型 | 描述 |
---|---|
列表[字典[str, Any]]
|
多轮消息列表。 |
源代码位于 dspy/adapters/base.py
format_field_description(signature: Type[Signature]) -> str
format_field_structure(signature: Type[Signature]) -> str
ChatAdapter
要求输入和输出字段位于各自的部分,部分标题使用标记 [[ ## field_name ## ]]
。一个任意字段 completed
([[ ## completed ## ]]) 会添加到输出字段部分的末尾,以指示输出字段的结束。
源代码位于 dspy/adapters/chat_adapter.py
format_field_with_value(fields_with_values: Dict[FieldInfoWithName, Any]) -> str
根据字段的 DSPy 类型(输入或输出)、注解(例如 str、int 等)以及值本身的类型,格式化指定字段的值。将格式化后的值连接成一个字符串,如果存在多个字段,则该字符串是多行字符串。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
fields_with_values
|
字典[FieldInfoWithName, Any]
|
将字段信息映射到其对应值的字典。 |
必需 |
返回值
类型 | 描述 |
---|---|
str
|
字段的连接格式化值,表示为字符串 |
源代码位于 dspy/adapters/chat_adapter.py
format_finetune_data(signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]) -> dict[str, list[Any]]
根据 OpenAI API 规范将调用数据格式化为微调数据。
对于聊天适配器,这意味着将数据格式化为一个消息列表,其中每条消息都是一个带有“role”和“content”键的字典。角色可以是“system”、“user”或“assistant”。然后,消息被包装在一个带有“messages”键的字典中。
源代码位于 dspy/adapters/chat_adapter.py
format_task_description(signature: Type[Signature]) -> str
源代码位于 dspy/adapters/chat_adapter.py
format_user_message_content(signature: Type[Signature], inputs: dict[str, Any], prefix: str = '', suffix: str = '', main_request: bool = False) -> str
源代码位于 dspy/adapters/chat_adapter.py
parse(signature: Type[Signature], completion: str) -> dict[str, Any]
源代码位于 dspy/adapters/chat_adapter.py
user_message_output_requirements(signature: Type[Signature]) -> str
为语言模型返回简化的格式提醒。
在基于聊天的交互中,随着对话上下文变长,语言模型可能会忘记所需的输出格式。此方法生成关于预期输出结构的简洁提醒,可以包含在用户消息中。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
signature
|
类型[Signature]
|
定义预期输入/输出字段的 DSPy 签名。 |
必需 |
返回值
名称 | 类型 | 描述 |
---|---|---|
str |
str
|
所需输出格式的简化描述。 |
注意
这是 format_field_structure
的更轻量级版本,专为聊天消息中的内联提醒而设计。