diff --git a/src/main/askai/core/features/processors/task_splitter.py b/src/main/askai/core/features/processors/task_splitter.py index 918c3a17..4967cb14 100644 --- a/src/main/askai/core/features/processors/task_splitter.py +++ b/src/main/askai/core/features/processors/task_splitter.py @@ -26,12 +26,12 @@ from askai.core.component.geo_location import geo_location from askai.core.engine.openai.temperature import Temperature from askai.core.enums.acc_color import AccColor -from askai.core.model.acc_response import AccResponse from askai.core.enums.routing_model import RoutingModel from askai.core.features.router.agent_tools import features from askai.core.features.router.evaluation import assert_accuracy from askai.core.features.router.task_agent import agent from askai.core.features.tools.general import final_answer +from askai.core.model.acc_response import AccResponse from askai.core.model.action_plan import ActionPlan from askai.core.model.ai_reply import AIReply from askai.core.model.model_result import ModelResult @@ -70,20 +70,24 @@ def wrap_answer( query: str, answer: str, model_result: ModelResult = ModelResult.default(), - acc_threshold: AccResponse | None = None, + acc_response: AccResponse | None = None, ) -> str: """Provide a final answer to the user by wrapping the AI response with additional context. :param query: The user's question. :param answer: The AI's response to the question. :param model_result: The result from the selected routing model (default is ModelResult.default()). - :param acc_threshold: The final accuracy threshold, if available. + :param acc_response: The final accuracy response, if available. :return: A formatted string containing the final answer. """ output: str = answer - model: RoutingModel = RoutingModel.of_model(model_result.mid) - events.reply.emit(reply=AIReply.full(msg.model_select(model))) args = {"user": shared.username, "idiom": shared.idiom, "context": answer, "question": query} prompt_args: list[str] = [k for k in args.keys()] + model: RoutingModel = ( + RoutingModel.REFINER + if acc_response and (acc_response.acc_color > AccColor.GOOD) + else RoutingModel.of_model(model_result.mid) + ) + events.reply.emit(reply=AIReply.full(msg.model_select(model))) match model, configs.is_speak: case RoutingModel.TERMINAL_COMMAND, True: @@ -93,20 +97,14 @@ def wrap_answer( case RoutingModel.CHAT_MASTER, _: output = final_answer("taius-jarvis", prompt_args, **args) case RoutingModel.REFINER, _: - if acc_threshold and acc_threshold.reasoning: + if acc_response and acc_response.reasoning: ctx: str = str(shared.context.flat("HISTORY")) - args = { - "improvements": acc_threshold.reasoning, - "context": ctx, - "response": answer, - "question": query, - } + args = {"improvements": acc_response.details, "context": ctx, "response": answer, "question": query} prompt_args = [k for k in args.keys()] events.reply.emit(reply=AIReply.debug(msg.refine_answer(answer))) output = final_answer("taius-refiner", prompt_args, **args) case _: - # Default is to leave the last AI response intact. - pass + pass # Default is to leave the last AI response as is shared.context.push("HISTORY", query) shared.context.push("HISTORY", output, "assistant") @@ -172,25 +170,26 @@ def _splitter_wrapper_() -> Optional[str]: events.reply.emit(reply=AIReply.info(plan.speak)) else: # Most of the times, indicates the LLM responded directly. + acc_response: AccResponse = assert_accuracy(question, response, AccColor.MODERATE) if output := plan.speak: shared.context.push("HISTORY", question) shared.context.push("HISTORY", output, "assistant") else: output = msg.no_output("Task-Splitter") - return output + return self.wrap_answer(question, output, plan.model, acc_response) else: return response # Most of the times, indicates a failure. try: - wrapper_output = self._process_tasks(task_list) - assert_accuracy(question, wrapper_output, AccColor.MODERATE) + agent_output = self._process_tasks(task_list) + acc_response: AccResponse = assert_accuracy(question, agent_output, AccColor.MODERATE) except (InterruptionRequest, TerminatingQuery) as err: return str(err) except self.RETRIABLE_ERRORS: events.reply.emit(reply=AIReply.error(msg.sorry_retry())) raise - return wrapper_output + return self.wrap_answer(question, agent_output, plan.model, acc_response) return _splitter_wrapper_() diff --git a/src/main/askai/resources/prompts/refine-response.txt b/src/main/askai/resources/prompts/refine-response.txt index 070cfcb4..ef83d9cd 100644 --- a/src/main/askai/resources/prompts/refine-response.txt +++ b/src/main/askai/resources/prompts/refine-response.txt @@ -1,30 +1,51 @@ Act as a text editor and formatter. Refine the AI response to ensure they are clear, localized for "{idiom}", and adherent to formatting and detail requirements. -Instructions: +**Instructions:** -1. **Response Integrity**: -- If the initial response already meets the required standards and fully answers the question, do not modify it. The refinement should only enhance clarity, relevance, and localization without altering the core answer. -- Process the text without modifying any Markdown formatting or structure. Keep all Markdown elements such as headers, lists, code blocks, links, and emphasis as they are. The only exception is to convert lists separated by commas or semi-colons into numbered lists. -- Do not omit any relevant information. -2. **Localization Adjustments**: -- Adapt text to use regional expressions, units of measurement, and currency specific to the "{idiom}" locale. -- Perform necessary conversions, such as from miles to kilometers or USD to BRL, Fahrenheit to Celsius, using current conversion rates where applicable. -- Translate any non-"{idiom}" text into "{idiom}", considering regional linguistic variations. +1. **Response Integrity:** + - If the initial response meets required standards and fully answers the question, do not modify it. Refinement should only enhance clarity, relevance, and localization without altering the core answer. + - Process the text without modifying any Markdown formatting or structure. Maintain all Markdown elements such as headers, lists, code blocks, links, and emphasis as they are. The only exception is to convert lists separated by commas or semi-colons into numbered lists. + - Do not omit any relevant information. -3. **Detail Inclusion**: -- Ensure that important details are not omitted. This includes file names, folder paths, sizes, line numbers, and other pertinent specifics that could affect the user's understanding or implementation of the response. -- Highlight these details using appropriate Markdown formatting (e.g., code for file paths and names). -- The user's name is "{user}". When addressing him, kindly utilize his name. +2. **Localization Adjustments:** + - Adapt text to use regional expressions, units of measurement, and currency specific to the "{idiom}" locale. + - Perform necessary conversions, such as from miles to kilometers or USD to BRL, Fahrenheit to Celsius, using current conversion rates where applicable. + - Translate any non-"{idiom}" text into "{idiom}", considering regional linguistic variations. + +3. **Detail Inclusion:** + - Ensure that important details such as file names, folder paths, sizes, line numbers, and other pertinent specifics that could affect the user's understanding or implementation of the response are not omitted. + - Highlight these details using appropriate Markdown formatting (e.g., `code` for file paths and names). + - The user's name is "{user}". Address him by his name in responses. + +4. **Use Provided Tips:** + - If available, integrate the provided tips to enhance the final user response: + +--- +{improvements} +--- + +5. **Leave it Untouched**: + - If no improvements are possible, return the response as is without any extraneous explanation or comments. + +6. **Watermark**: + - Add your watermark at the end of the response (do not include the triple quotes): + +""" +--- +> Improved by the Refiner Model +""" Human Question: "{question}" + AI Response: """ {context} """ -Refined Response: + +Begin refining the response! diff --git a/src/main/askai/resources/rag/accuracy.csv b/src/main/askai/resources/rag/accuracy.csv index 668f90bf..bfd705ff 100644 --- a/src/main/askai/resources/rag/accuracy.csv +++ b/src/main/askai/resources/rag/accuracy.csv @@ -19,3 +19,4 @@ Who are you?;Yes he is famous;Red;The AI should assess whether the response is c What is my first task?;You can find out your first task by checking the list of activities above;Red;The AI should refrain from seeking assistance from the user. List my music and let me know if there is any gabiroba song. If so, show me the file name and open it.;The 'gabiroba' song file was not found in the Music folder.;Black;This is a clear negative response indicating that the file does not exist, and therefore, the AI cannot proceed any further. Display the file name of the gabiroba song;The Gabiroba song file name is 'have-a-nice-day.mp3';Red;This is a clear hallucination because the filename does not conform to the required file pattern. +What is my second task;The first task or action to be taken according to the reminder is to 'Thank Rafael Romão for the keyboard' at 17:20 on 04/11/2024.;Red;The AI partially addresses the user's question by providing information about the first task but lacks a direct response to the query about the second task.