跳到内容

先生成示例

类比提示(Analogical Prompting)1 是一种方法,旨在让大型语言模型(LLM)在开始处理用户查询之前,先生成与问题相关的示例。

这利用了大型语言模型在训练过程中获得的各种知识形式,并明确提示它们回忆相关的问题和解决方案。我们可以使用以下模板来进行类比提示:

类比提示模板

问题:[用户提示]

相关问题:回忆三个相关且独特的问题。对于每个问题,描述它并解释其解决方案。

解决问题

我们可以使用如下所示的 instructor 来实现这一点,并进行了一些微小修改。

from openai import OpenAI
from pydantic import BaseModel, Field
import instructor
from textwrap import dedent

client = instructor.from_openai(OpenAI())


class RelevantProblem(BaseModel):
    problem_explanation: str
    solution: str


class Response(BaseModel):
    relevant_problems: list[RelevantProblem] = Field(
        max_length=3,
        min_length=3,
    )
    answer: RelevantProblem


def analogical_prompting(query: str):
    return client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": dedent(
                    f"""
                <problem>
                {query}
                </problem>

                Relevant Problems: Recall three relevant and
                distinct problems. For each problem, describe
                it and explain the solution before solving
                the problem
                """
                ),
            }
        ],
        model="gpt-4o",
        response_model=Response,
    )


if __name__ == "__main__":
    query = (
        "What is the area of the square with the four "
        "vertices at (-2, 2), (2, -2), (-2, -6), and "
        "(-6, -2)?"
    )
    response = analogical_prompting(query)
    for problem in response.relevant_problems:
        print(problem.model_dump_json(indent=2))
        """
        {
          "problem_explanation": "Determine the distance
          between two points in a coordinate plane.",
          "solution": "To find the distance between two
          points, use the distance formula: \\(d =
          \\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}\\). This
          formula calculates the Euclidean distance between
          points (x_1, y_1) and (x_2, y_2)."
        }
        """
        """
        {
          "problem_explanation": "Calculate the area of a
          square given its side length.",
          "solution": "The area of a square can be found
          using the formula: \\(A = s^2\\), where \\(s\\) is
          the length of one side of the square."
        }
        """
        """
        {
          "problem_explanation": "Identify vertices and
          properties of a geometry shape such as
          parallelogram.",
          "solution": "For any quadrilateral, verify that
          all sides are equal and angles are right angles to
          confirm it is a square. Use properties of
          quadrilaterals and distance formula."
        }
        """

    print(response.answer.model_dump_json(indent=2))
    """
    {
      "problem_explanation": "Calculate the area of a
      square given its vertices.",
      "solution": "First, confirm the shape is a square by
      checking the distance between consecutive vertices
      and ensuring all sides are of equal length using the
      distance formula. For vertices (-2,2), (2,-2),
      (-2,-6), and (-6,-2), calculate distances between
      consecutive points. If distances are equal, use the
      side length to compute area using \\(A = s^2\\)."
    }
    """

参考文献

1: 大型语言模型作为类比推理器