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:
Add a call to the subquestions recipe to generate subquestions.
Use map_async to answer all the subquestions in parallel.
Provide answers from subquestions as advice in the overall question-answering prompt.
from fvalues import Ffrom ice.recipe import recipefrom ice.recipes.primer.subquestions import ask_subquestionsfrom ice.utils import map_asyncdefmake_qa_prompt(question:str) ->str:returnF(f"""Answer the following question:Question: "{question}"Answer: """" ).strip()asyncdefanswer(question:str) ->str: prompt =make_qa_prompt(question) answer =await recipe.agent().complete(prompt=prompt, stop='"')return answerasyncdefanswer_by_amplification(question:str="What is the effect of creatine on cognition?",): subquestions =awaitask_subquestions(question=question) subanswers =awaitmap_async(subquestions, answer)returnlist(zip(subquestions, subanswers))recipe.main(answer_by_amplification)
If we run this, we get back a list of subquestions and their answers:
[ ('What is creatine?','Creatine is a nitrogenous organic acid that helps supply energy to cells, primarily in the muscles.' ), ('What is cognition?', 'Cognition is the mental action or process of acquiring knowledge and understanding through thought, experience, and the senses.'
), ('How does creatine affect cognition?', 'Creatine is a dietary supplement that is often used by athletes to improve their performance. Some research has suggested that it may also improve cognitive function, but the evidence is mixed. Some studies have found that creatine can improve memory and reaction time, while others have found no significant effects.'
), ('What are the benefits of creatine on cognition?', 'Creatine has been shown to improve cognitive function in people with certain medical conditions, such as Parkinson’s disease and Alzheimer’s disease. It has also been shown to improve cognitive function in healthy adults.'
), ('What are the side effects of creatine on cognition?', 'There is no definitive answer to this question as the research on the topic is inconclusive. Some studies suggest that creatine may improve cognitive function, while other studies have found no significant effects. More research is needed to determine the potential cognitive effects of creatine.'
)]