Answering subquestions

Mapping our answerer over subquestions

Now we want to use the subquestions recipe to help a question-answerer like the one we built early on in the primer. We can start with the question-answerer we built earlier and modify it as follows:

  1. Add a call to the subquestions recipe to generate subquestions.

  2. Use map_async to answer all the subquestions in parallel.

  3. Provide answers from subquestions as advice in the overall question-answering prompt.

Let’s start with (1) and (2), reusing the subquestions subrecipe:

subquestions_answered.py
from fvalues import F

from ice.recipe import recipe
from ice.recipes.primer.subquestions import ask_subquestions
from ice.utils import map_async


def make_qa_prompt(question: str) -> str:
    return F(
        f"""Answer the following question:

Question: "{question}"
Answer: "
"""
    ).strip()


async def answer(question: str) -> str:
    prompt = make_qa_prompt(question)
    answer = await recipe.agent().complete(prompt=prompt, stop='"')
    return answer


async def answer_by_amplification(
    question: str = "What is the effect of creatine on cognition?",
):
    subquestions = await ask_subquestions(question=question)
    subanswers = await map_async(subquestions, answer)
    return list(zip(subquestions, subanswers))


recipe.main(answer_by_amplification)

If we run this, we get back a list of subquestions and their answers:

The trace:

Execution trace (view online)

Last updated