CoolFace
Apppublic

Akjava/open_Deep-Research-DuckDuckGo

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
4likes
reformulator.py87 linesDownload Raw Back to scripts
1# Shamelessly stolen from Microsoft Autogen team: thanks to them for this great resource!2# https://github.com/microsoft/autogen/blob/gaia_multiagent_v01_march_1st/autogen/browser_utils.py3import copy4 5from smolagents.models import MessageRole, Model6 7 8def prepare_response(original_task: str, inner_messages, reformulation_model: Model) -> str:9    messages = [10        {11            "role": MessageRole.SYSTEM,12            "content": [13                {14                    "type": "text",15                    "text": f"""Earlier you were asked the following:16 17{original_task}18 19Your team then worked diligently to address that request. Read below a transcript of that conversation:""",20                }21            ],22        }23    ]24 25    # The first message just repeats the question, so remove it26    # if len(inner_messages) > 1:27    #    del inner_messages[0]28 29    # copy them to this context30    try:31        for message in inner_messages:32            if not message.get("content"):33                continue34            message = copy.deepcopy(message)35            message["role"] = MessageRole.USER36            messages.append(message)37    except Exception:38        messages += [{"role": MessageRole.ASSISTANT, "content": str(inner_messages)}]39 40    # ask for the final answer41    messages.append(42        {43            "role": MessageRole.USER,44            "content": [45                {46                    "type": "text",47                    "text": f"""48Read the above conversation and output a FINAL ANSWER to the question. The question is repeated here for convenience:49 50{original_task}51 52To output the final answer, use the following template: FINAL ANSWER: [YOUR FINAL ANSWER]53Your FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.54ADDITIONALLY, your FINAL ANSWER MUST adhere to any formatting instructions specified in the original question (e.g., alphabetization, sequencing, units, rounding, decimal places, etc.)55If you are asked for a number, express it numerically (i.e., with digits rather than words), don't use commas, and DO NOT INCLUDE UNITS such as $ or USD or percent signs unless specified otherwise.56If you are asked for a string, don't use articles or abbreviations (e.g. for cities), unless specified otherwise. Don't output any final sentence punctuation such as '.', '!', or '?'.57If you are asked for a comma separated list, apply the above rules depending on whether the elements are numbers or strings.58If you are unable to determine the final answer, output 'FINAL ANSWER: Unable to determine'59""",60                }61            ],62        }63    )64 65    response = reformulation_model(messages).content66 67    final_answer = response.split("FINAL ANSWER: ")[-1].strip()68    print("> Reformulated answer: ", final_answer)69 70    #     if "unable to determine" in final_answer.lower():71    #         messages.append({"role": MessageRole.ASSISTANT, "content": response })72    #         messages.append({"role": MessageRole.USER, "content": [{"type": "text", "text": """73    # I understand that a definitive answer could not be determined. Please make a well-informed EDUCATED GUESS based on the conversation.74 75    # To output the educated guess, use the following template: EDUCATED GUESS: [YOUR EDUCATED GUESS]76    # Your EDUCATED GUESS should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. DO NOT OUTPUT 'I don't know', 'Unable to determine', etc.77    # ADDITIONALLY, your EDUCATED GUESS MUST adhere to any formatting instructions specified in the original question (e.g., alphabetization, sequencing, units, rounding, decimal places, etc.)78    # If you are asked for a number, express it numerically (i.e., with digits rather than words), don't use commas, and don't include units such as $ or percent signs unless specified otherwise.79    # If you are asked for a string, don't use articles or abbreviations (e.g. cit for cities), unless specified otherwise. Don't output any final sentence punctuation such as '.', '!', or '?'.80    # If you are asked for a comma separated list, apply the above rules depending on whether the elements are numbers or strings.81    # """.strip()}]})82 83    #         response = model(messages).content84    #         print("\n>>>Making an educated guess.\n", response)85    #         final_answer = response.split("EDUCATED GUESS: ")[-1].strip()86    return final_answer87