解决更简单的子问题
对于一个复杂问题,我们如何鼓励大型语言模型 (LLM) 解决更简单的子问题?
从简到繁 (Least-to-Most) 是一种提示技术,它将复杂问题分解为一系列复杂度逐渐增加的子问题。
子问题示例
原始问题:亚当年龄是玛丽的两倍。一年后亚当将满11岁。玛丽现在多大?
子问题:(1) 亚当现在多大? (2) 亚当当前年龄的一半是多少?
这些子问题是按顺序解决的,允许利用较早(较简单)子问题的答案来帮助 LLM 解决较晚(较复杂)的子问题。
import instructor
from pydantic import BaseModel
from openai import OpenAI
from typing import Iterable
class Subquestion(BaseModel):
question: str
class Answer(BaseModel):
answer: int
class SubquestionWithAnswers(BaseModel):
question: str
answer: int
client = instructor.from_openai(OpenAI())
def decompose(question):
return client.chat.completions.create(
model="gpt-4o",
response_model=Iterable[Subquestion],
messages=[
{
"role": "user",
"content": f"Break this question down into subquestions to solve sequentially: {question}",
}
],
)
def solve(question, solved_questions, original_question):
return client.chat.completions.create(
model="gpt-4o",
response_model=Answer,
messages=[
{
"role": "user",
"content": f"""
<original_question>
{original_question}
</original_question>
<solved_subquestions>
{solved_questions}
</solved_subquestions>
Solve this next subquestion: {question}
""",
}
],
).answer
if __name__ == "__main__":
question = "Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice 30 years old, how old is Kody?"
# Stage 1: Decompose Question into Subquestions
subquestions = decompose(question)
# Stage 2: Sequentially Solve Subquestions
solved_questions = []
for subquestion in subquestions:
solved_questions.append(
SubquestionWithAnswers(
question=subquestion.question,
answer=solve(subquestion, solved_questions, question),
)
)
# Print
for item in solved_questions:
print(f"{item.question} {item.answer}")
#> How old is Mohamed currently? 60
#> How old was Mohamed four years ago? 56
#> How old was Kody four years ago if he was half as old as Mohamed? 28
#> How old is Kody currently? 32