Skip to content

dspy.TwoStepAdapter

dspy.TwoStepAdapter(extraction_model: LM, **kwargs)

基类:Adapter

一个两阶段适配器,
  1. 为主语言模型使用更简单、更自然的提示
  2. 使用带有聊天适配器的小型语言模型从主语言模型的响应中提取结构化数据

该适配器使用了基础Adapter类中定义的通用call逻辑。 当与推理模型作为主要语言模型交互时,此类特别有用,因为已知推理模型在处理结构化输出时存在困难。

示例:

import dspy
lm = dspy.LM(model="openai/o3-mini", max_tokens=16000, temperature = 1.0)
adapter = dspy.TwoStepAdapter(dspy.LM("openai/gpt-4o-mini"))
dspy.configure(lm=lm, adapter=adapter)
program = dspy.ChainOfThought("question->answer")
result = program("法国的首都是什么?")
print(result)

Source code in dspy/adapters/two_step_adapter.py
def __init__(self, extraction_model: LM, **kwargs):
    super().__init__(**kwargs)
    if not isinstance(extraction_model, LM):
        raise ValueError("extraction_model must be an instance of LM")
    self.extraction_model = extraction_model

函数

__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
def __call__(
    self,
    lm: "LM",
    lm_kwargs: dict[str, Any],
    signature: type[Signature],
    demos: list[dict[str, Any]],
    inputs: dict[str, Any],
) -> list[dict[str, Any]]:
    processed_signature = self._call_preprocess(lm, lm_kwargs, signature, inputs)
    inputs = self.format(processed_signature, demos, inputs)

    outputs = lm(messages=inputs, **lm_kwargs)
    return self._call_postprocess(processed_signature, signature, outputs)

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/two_step_adapter.py
async def acall(
    self,
    lm: "LM",
    lm_kwargs: dict[str, Any],
    signature: type[Signature],
    demos: list[dict[str, Any]],
    inputs: dict[str, Any],
) -> list[dict[str, Any]]:
    inputs = self.format(signature, demos, inputs)

    outputs = await lm.acall(messages=inputs, **lm_kwargs)
    # The signature is supposed to be "text -> {original output fields}"
    extractor_signature = self._create_extractor_signature(signature)

    values = []

    tool_call_output_field_name = self._get_tool_call_output_field_name(signature)
    for output in outputs:
        output_logprobs = None
        tool_calls = None
        text = output

        if isinstance(output, dict):
            text = output["text"]
            output_logprobs = output.get("logprobs")
            tool_calls = output.get("tool_calls")

        try:
            # Call the smaller LM to extract structured data from the raw completion text with ChatAdapter
            value = await ChatAdapter().acall(
                lm=self.extraction_model,
                lm_kwargs={},
                signature=extractor_signature,
                demos=[],
                inputs={"text": text},
            )
            value = value[0]

        except Exception as e:
            raise ValueError(f"Failed to parse response from the original completion: {output}") from e

        if tool_calls and tool_call_output_field_name:
            tool_calls = [
                {
                    "name": v["function"]["name"],
                    "args": json_repair.loads(v["function"]["arguments"]),
                }
                for v in tool_calls
            ]
            value[tool_call_output_field_name] = ToolCalls.from_dict_list(tool_calls)

        if output_logprobs is not None:
            value["logprobs"] = output_logprobs

        values.append(value)
    return values

format(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]

为主语言模型的第一阶段格式化提示。 对于主语言模型没有特定的结构要求,我们自定义格式化方法, 而不是使用format_field_description或format_field_structure。

参数:

名称 类型 描述 默认值
signature type[Signature]

原始任务的签名

必填
demos list[dict[str, Any]]

一个演示示例列表

必填
inputs dict[str, Any]

当前输入

必填

返回:

类型 描述
list[dict[str, Any]]

要传递给主语言模型的消息列表。

Source code in dspy/adapters/two_step_adapter.py
def format(
    self, signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]
) -> list[dict[str, Any]]:
    """
    Format a prompt for the first stage with the main LM.
    This no specific structure is required for the main LM, we customize the format method
    instead of format_field_description or format_field_structure.

    Args:
        signature: The signature of the original task
        demos: A list of demo examples
        inputs: The current input

    Returns:
        A list of messages to be passed to the main LM.
    """
    messages = []

    # Create a task description for the main LM
    task_description = self.format_task_description(signature)
    messages.append({"role": "system", "content": task_description})

    messages.extend(self.format_demos(signature, demos))

    # Format the current input
    messages.append({"role": "user", "content": self.format_user_message_content(signature, inputs)})

    return messages

format_assistant_message_content(signature: type[Signature], outputs: dict[str, Any], missing_field_message: str | None = None) -> str

Source code in dspy/adapters/two_step_adapter.py
def format_assistant_message_content(
    self,
    signature: type[Signature],
    outputs: dict[str, Any],
    missing_field_message: str | None = None,
) -> str:
    parts = []

    for name in signature.output_fields.keys():
        if name in outputs:
            parts.append(f"{name}: {outputs.get(name, missing_field_message)}")

    return "\n\n".join(parts).strip()

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
def format_conversation_history(
    self,
    signature: type[Signature],
    history_field_name: str,
    inputs: dict[str, Any],
) -> list[dict[str, Any]]:
    """Format the conversation history.

    This method formats the conversation history and the current input as multiturn messages.

    Args:
        signature: The DSPy signature for which to format the conversation history.
        history_field_name: The name of the history field in the signature.
        inputs: The input arguments to the DSPy module.

    Returns:
        A list of multiturn messages.
    """
    conversation_history = inputs[history_field_name].messages if history_field_name in inputs else None

    if conversation_history is None:
        return []

    messages = []
    for message in conversation_history:
        messages.append(
            {
                "role": "user",
                "content": self.format_user_message_content(signature, message),
            }
        )
        messages.append(
            {
                "role": "assistant",
                "content": self.format_assistant_message_content(signature, message),
            }
        )

    # Remove the history field from the inputs
    del inputs[history_field_name]

    return messages

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
def format_demos(self, signature: type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]:
    """Format the few-shot examples.

    This method formats the few-shot examples as multiturn messages.

    Args:
        signature: The DSPy signature for which to format the few-shot examples.
        demos: A list of few-shot examples, each element is a dictionary with keys of the input and output fields of
            the signature.

    Returns:
        A list of multiturn messages.
    """
    complete_demos = []
    incomplete_demos = []

    for demo in demos:
        # Check if all fields are present and not None
        is_complete = all(k in demo and demo[k] is not None for k in signature.fields)

        # Check if demo has at least one input and one output field
        has_input = any(k in demo for k in signature.input_fields)
        has_output = any(k in demo for k in signature.output_fields)

        if is_complete:
            complete_demos.append(demo)
        elif has_input and has_output:
            # We only keep incomplete demos that have at least one input and one output field
            incomplete_demos.append(demo)

    messages = []

    incomplete_demo_prefix = "This is an example of the task, though some input or output fields are not supplied."
    for demo in incomplete_demos:
        messages.append(
            {
                "role": "user",
                "content": self.format_user_message_content(signature, demo, prefix=incomplete_demo_prefix),
            }
        )
        messages.append(
            {
                "role": "assistant",
                "content": self.format_assistant_message_content(
                    signature, demo, missing_field_message="Not supplied for this particular example. "
                ),
            }
        )

    for demo in complete_demos:
        messages.append({"role": "user", "content": self.format_user_message_content(signature, demo)})
        messages.append(
            {
                "role": "assistant",
                "content": self.format_assistant_message_content(
                    signature, demo, missing_field_message="Not supplied for this conversation history message. "
                ),
            }
        )

    return messages

format_field_description(signature: type[Signature]) -> str

为系统消息格式化字段描述。

该方法格式化系统消息中的字段描述。它应返回一个字符串,其中包含输入字段和输出字段的字段描述。

参数:

名称 类型 描述 默认值
signature type[Signature]

要格式化字段描述的Dspy签名。

必填

返回:

类型 描述
str

一个包含输入字段和输出字段的字段描述字符串。

Source code in dspy/adapters/base.py
def format_field_description(self, signature: type[Signature]) -> str:
    """Format the field description for the system message.

    This method formats the field description for the system message. It should return a string that contains
    the field description for the input fields and the output fields.

    Args:
        signature: The DSPy signature for which to format the field description.

    Returns:
        A string that contains the field description for the input fields and the output fields.
    """
    raise NotImplementedError

format_field_structure(signature: type[Signature]) -> str

格式化系统消息的字段结构。

该方法格式化系统消息的字段结构。它应返回一个字符串,规定输入字段提供给语言模型时应遵循的格式,以及响应中输出字段的格式。 请参考ChatAdapter和JsonAdapter作为示例。

参数:

名称 类型 描述 默认值
signature type[Signature]

用于格式化字段结构的DSPy签名。

必填
Source code in dspy/adapters/base.py
def format_field_structure(self, signature: type[Signature]) -> str:
    """Format the field structure for the system message.

    This method formats the field structure for the system message. It should return a string that dictates the
    format the input fields should be provided to the LM, and the format the output fields will be in the response.
    Refer to the ChatAdapter and JsonAdapter for an example.

    Args:
        signature: The DSPy signature for which to format the field structure.
    """
    raise NotImplementedError

format_task_description(signature: Signature) -> str

根据签名创建任务描述

Source code in dspy/adapters/two_step_adapter.py
def format_task_description(self, signature: Signature) -> str:
    """Create a description of the task based on the signature"""
    parts = []

    parts.append("You are a helpful assistant that can solve tasks based on user input.")
    parts.append("As input, you will be provided with:\n" + get_field_description_string(signature.input_fields))
    parts.append("Your outputs must contain:\n" + get_field_description_string(signature.output_fields))
    parts.append("You should lay out your outputs in detail so that your answer can be understood by another agent")

    if signature.instructions:
        parts.append(f"Specific instructions: {signature.instructions}")

    return "\n".join(parts)

format_user_message_content(signature: type[Signature], inputs: dict[str, Any], prefix: str = '', suffix: str = '') -> str

Source code in dspy/adapters/two_step_adapter.py
def format_user_message_content(
    self,
    signature: type[Signature],
    inputs: dict[str, Any],
    prefix: str = "",
    suffix: str = "",
) -> str:
    parts = [prefix]

    for name in signature.input_fields.keys():
        if name in inputs:
            parts.append(f"{name}: {inputs.get(name, '')}")

    parts.append(suffix)
    return "\n\n".join(parts).strip()

parse(signature: Signature, completion: str) -> dict[str, Any]

使用较小的语言模型(extraction_model)配合聊天适配器,从主语言模型的原始完成文本中提取结构化数据。

参数:

名称 类型 描述 默认值
signature Signature

原始任务的签名

必填
completion str

来自主语言模型的完成结果

必填

返回:

类型 描述
dict[str, Any]

包含提取出的结构化数据的字典。

Source code in dspy/adapters/two_step_adapter.py
def parse(self, signature: Signature, completion: str) -> dict[str, Any]:
    """
    Use a smaller LM (extraction_model) with chat adapter to extract structured data
    from the raw completion text of the main LM.

    Args:
        signature: The signature of the original task
        completion: The completion from the main LM

    Returns:
        A dictionary containing the extracted structured data.
    """
    # The signature is supposed to be "text -> {original output fields}"
    extractor_signature = self._create_extractor_signature(signature)

    try:
        # Call the smaller LM to extract structured data from the raw completion text with ChatAdapter
        parsed_result = ChatAdapter()(
            lm=self.extraction_model,
            lm_kwargs={},
            signature=extractor_signature,
            demos=[],
            inputs={"text": completion},
        )
        return parsed_result[0]

    except Exception as e:
        raise ValueError(f"Failed to parse response from the original completion: {completion}") from e

:::