跳到内容

生成追问问题

模型有时可以正确回答子问题,但对整体查询回答错误。这被称为组合性差距 (compositionality gap)1

我们如何鼓励模型利用子问题的答案来正确生成整体解决方案?

自我提问 (Self-Ask) 是一种使用单个提示词来

  • 决定是否需要追问问题
  • 生成追问问题
  • 回答追问问题
  • 回答主要查询

实现

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

client = instructor.from_openai(OpenAI())


class FollowUp(BaseModel):
    question: str = Field(description="The follow-up question")
    answer: str = Field(description="The answer to the follow-up question")


class Response(BaseModel):
    follow_ups_required: bool
    follow_ups: list[FollowUp]
    final_answer: str


def self_ask(query):
    return client.chat.completions.create(
        model="gpt-4o",
        response_model=Response,
        messages=[
            {
                "role": "system",
                "content": f"""Query: {query}
                        Are follow-up questions needed?
                        If so, generate follow-up questions, their answers, and then the final answer to the query.
                        """,  # !(1)
            },
        ],
    )


if __name__ == "__main__":
    query = "Who was president of the U.S. when superconductivity was discovered?"

    response = self_ask(query)

    print(response.follow_ups_required)
    #> True
    for follow_up in response.follow_ups:
        print(follow_up)
        """
        question='When was superconductivity discovered?' answer='Superconductivity was discovered in April 1911.'
        """
        """
        question='Who was president of the U.S. in April 1911?' answer='William Howard Taft was the President of the United States in April 1911.'
        """
    print(response.final_answer)
    """
    William Howard Taft was president of the U.S. when superconductivity was discovered.
    """
  1. 如果没有 instructor,这个提示通常会作为单样本或少样本提示1来实现,以鼓励通过追问问题进行思考。使用 instructor,我们使用零样本提示!

参考文献

1: 衡量和缩小语言模型中的组合性差距