dspy.JSONAdapter
dspy.JSONAdapter(callbacks: Optional[list[BaseCallback]] = None)
Bases: ChatAdapter
源代码位于 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/json_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]]
格式化用于 LM 调用的输入消息。
此方法将 DSPy 结构化输入以及少样本示例和对话历史记录转换为 LM 期望的多轮消息。对于自定义适配器,可以覆盖此方法以自定义输入消息的格式。
通常,我们建议消息采用以下结构:
[
{"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]]
|
LM 期望的多轮消息列表。 |
源代码位于 dspy/adapters/base.py
format_assistant_message_content(signature: Type[Signature], outputs: dict[str, Any], missing_field_message=None) -> str
源代码位于 dspy/adapters/json_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]]
|
多轮消息列表。 |
源代码位于 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
format_field_structure(signature: Type[Signature]) -> str
源代码位于 dspy/adapters/json_adapter.py
format_field_with_value(fields_with_values: Dict[FieldInfoWithName, Any], role: str = 'user') -> str
根据字段的 DSPy 类型(输入或输出)、注释(例如 str、int 等)以及值本身的类型来格式化指定字段的值。将格式化后的值合并成一个字符串,如果存在多个字段,则该字符串是多行字符串。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
fields_with_values
|
Dict[FieldInfoWithName, Any]
|
一个字典,将字段信息映射到其对应的值。 |
必需 |
返回值:字段的合并格式化值,表示为字符串。