dspy.ChatAdapter
dspy.ChatAdapter(callbacks: list[BaseCallback] | None = None, use_native_function_calling: bool = False)
基类:Adapter
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/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]]
async
Source code in dspy/adapters/chat_adapter.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=None) -> str
Source code in dspy/adapters/chat_adapter.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
Source code in dspy/adapters/chat_adapter.py
format_field_structure(signature: type[Signature]) -> str
ChatAdapter 要求输入和输出字段分别位于各自的区块中,区块标题使用标记符
[[ ## field_name ## ]]。一个任意字段 completed ([[ ## completed ## ]]) 被添加到
输出字段区块的末尾,以指示输出字段的结束。
Source code in dspy/adapters/chat_adapter.py
format_field_with_value(fields_with_values: dict[FieldInfoWithName, Any]) -> str
根据字段的DSPy类型(输入或输出)、注解(例如字符串、整数等)以及值本身的类型,格式化指定字段的值。将格式化后的值合并为一个字符串,如果有多个字段,则形成一个多行字符串。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
fields_with_values
|
dict[FieldInfoWithName, Any]
|
一个字典,将关于字段的信息映射到其对应的值。 |
必填 |
返回:
| 类型 | 描述 |
|---|---|
str
|
字段连接后的格式化值,以字符串形式表示 |
Source code in 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"键的字典中。
Source code in dspy/adapters/chat_adapter.py
format_task_description(signature: type[Signature]) -> str
Source code in 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
Source code in dspy/adapters/chat_adapter.py
parse(signature: type[Signature], completion: str) -> dict[str, Any]
Source code in dspy/adapters/chat_adapter.py
user_message_output_requirements(signature: type[Signature]) -> str
返回语言模型的简化格式提醒。
在基于聊天的交互中,随着对话上下文的增长,语言模型可能会忘记所需的输出格式。该方法生成一个简洁的提示,提醒预期的输出结构,可以包含在用户消息中。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
signature
|
Type[Signature]
|
DSPy签名定义了预期的输入/输出字段。 |
必填 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
所需输出格式的简化描述。 |
注意
这是format_field_structure的一个更轻量级版本,专门设计用于聊天消息中的内联提醒。
Source code in dspy/adapters/chat_adapter.py
:::