跳到内容

要求模型重复查询

我们如何增强模型对查询的理解?

Re2 (Re - R eading) 是一种要求模型再次阅读问题的技术。

重复阅读提示

提示模板:再次阅读问题:<_query_> <_critical thinking prompt_>1

一个常见的批判性思维提示是:“让我们一步一步思考。”

实现

import instructor
from openai import OpenAI
from pydantic import BaseModel


client = instructor.from_openai(OpenAI())


class Response(BaseModel):
    answer: int


def re2(query, thinking_prompt):
    return client.chat.completions.create(
        model="gpt-4o",
        response_model=Response,
        messages=[
            {
                "role": "system",
                "content": f"Read the question again: {query} {thinking_prompt}",
            },
        ],
    )


if __name__ == "__main__":
    query = """Roger has 5 tennis balls.
        He buys 2 more cans of tennis balls.
        Each can has 3 tennis balls.
        How many tennis balls does he have now?
        """
    thinking_prompt = "Let's think step by step."

    response = re2(query=query, thinking_prompt=thinking_prompt)
    print(response.answer)
    #> 11

参考

1重复阅读改善大型语言模型中的推理能力