From f8503ed9c6d9e8b57b7db3b8bbb730da88bdd76b Mon Sep 17 00:00:00 2001 From: Hugo Saporetti Junior Date: Thu, 31 Oct 2024 12:50:51 -0300 Subject: [PATCH] Minor fixes and prompt cleanup --- src/main/askai/core/askai_configs.py | 27 ++++++++--- src/main/askai/core/askai_settings.py | 3 +- .../processors/splitter/splitter_actions.py | 5 +- .../processors/splitter/splitter_pipeline.py | 48 ++++++++++++++----- .../askai/resources/personas/taius-jarvis.txt | 10 +--- src/main/askai/resources/personas/taius.txt | 2 +- .../askai/resources/prompts/acc-report.txt | 8 ---- src/main/askai/resources/prompts/analysis.txt | 4 +- .../resources/prompts/refine-response.txt | 37 -------------- .../resources/prompts/taius/taius-refiner.txt | 35 +++++++++++++- .../taius/{taius-stt.txt => taius-tts.txt} | 2 +- src/main/requirements.txt | 48 ------------------- 12 files changed, 99 insertions(+), 130 deletions(-) delete mode 100644 src/main/askai/resources/prompts/acc-report.txt delete mode 100644 src/main/askai/resources/prompts/refine-response.txt rename src/main/askai/resources/prompts/taius/{taius-stt.txt => taius-tts.txt} (90%) delete mode 100644 src/main/requirements.txt diff --git a/src/main/askai/core/askai_configs.py b/src/main/askai/core/askai_configs.py index 08e5db14..c7948607 100644 --- a/src/main/askai/core/askai_configs.py +++ b/src/main/askai/core/askai_configs.py @@ -14,6 +14,7 @@ """ from askai.__classpath__ import classpath from askai.core.askai_settings import settings +from askai.core.enums.acc_color import AccColor from askai.core.enums.verbosity import Verbosity from askai.language.language import Language from hspylib.core.enums.charset import Charset @@ -122,17 +123,31 @@ def is_rag(self, value: bool) -> None: @property def language(self) -> Language: - """Lookup order: Settings -> Locale -> Environment.""" - return Language.of_locale( - settings.get("askai.preferred.language") - or os.getenv("LC_ALL", os.getenv("LC_TYPE", os.getenv("LANG"))) - or Language.EN_US.idiom - ) + # Lookup order: Settings -> Locale -> Environment. + try: + lang: Language = Language.of_locale( + settings.get("askai.preferred.language") + or os.getenv("LC_ALL", os.getenv("LC_TYPE", os.getenv("LANG"))) + or Language.EN_US.idiom + ) + except ValueError: + lang: Language = Language.EN_US + + return lang @property def default_router_mode(self) -> str: return settings.get("askai.router.mode.default") + @property + def pass_threshold(self) -> AccColor: + try: + color: AccColor = AccColor.value_of(settings.get("askai.router.pass.threshold")) + except ValueError: + color: AccColor = AccColor.MODERATE + + return color + @property def encoding(self) -> Charset: return self.language.encoding diff --git a/src/main/askai/core/askai_settings.py b/src/main/askai/core/askai_settings.py index a16fa56f..da20fc6d 100644 --- a/src/main/askai/core/askai_settings.py +++ b/src/main/askai/core/askai_settings.py @@ -44,7 +44,7 @@ class AskAiSettings(metaclass=Singleton): INSTANCE: "AskAiSettings" # Current settings version. Updating this value will trigger a database recreation using the defaults. - __ACTUAL_VERSION: str = "0.3.1" + __ACTUAL_VERSION: str = "0.4.0" __RESOURCE_DIR = str(classpath.resource_path) @@ -99,6 +99,7 @@ def defaults(self) -> None: self._settings.put("askai.cache.ttl.minutes", "askai", 25) self._settings.put("askai.preferred.language", "askai", "") self._settings.put("askai.router.mode.default", "askai", "splitter") + self._settings.put("askai.router.pass.threshold", "askai", "moderate") self._settings.put("askai.default.engine", "askai", "openai") self._settings.put("askai.default.engine.model", "askai", "gpt-3.5-turbo") self._settings.put("askai.verbosity.level", "askai", 3) diff --git a/src/main/askai/core/processors/splitter/splitter_actions.py b/src/main/askai/core/processors/splitter/splitter_actions.py index 7b369e37..306b7d24 100644 --- a/src/main/askai/core/processors/splitter/splitter_actions.py +++ b/src/main/askai/core/processors/splitter/splitter_actions.py @@ -61,9 +61,9 @@ def wrap_answer(question: str, answer: str, model_result: ModelResult = ModelRes match model, configs.is_speak: case ResponseModel.TERMINAL_COMMAND, True: - output = final_answer("taius-stt", prompt_args, **args) + output = final_answer("taius-tts", prompt_args, **args) case ResponseModel.ASSISTIVE_TECH_HELPER, _: - output = final_answer("taius-stt", prompt_args, **args) + output = final_answer("taius-tts", prompt_args, **args) case ResponseModel.CHAT_MASTER, _: output = final_answer("taius-jarvis", prompt_args, **args) case _: @@ -84,6 +84,7 @@ def refine_answer(question: str, answer: str, acc_response: AccResponse | None = if acc_response and acc_response.reasoning: ctx: str = str(shared.context.flat("HISTORY")) args = { + "locale": configs.language.locale, "improvements": acc_response.details, "context": ctx, "response": answer, diff --git a/src/main/askai/core/processors/splitter/splitter_pipeline.py b/src/main/askai/core/processors/splitter/splitter_pipeline.py index e5298e9c..7ca90724 100644 --- a/src/main/askai/core/processors/splitter/splitter_pipeline.py +++ b/src/main/askai/core/processors/splitter/splitter_pipeline.py @@ -12,8 +12,18 @@ Copyright (c) 2024, HomeSetup """ +import logging as log +from collections import defaultdict +from textwrap import dedent +from typing import AnyStr, Optional + +from hspylib.core.preconditions import check_state +from hspylib.core.tools.validator import Validator +from langchain_core.prompts import PromptTemplate +from transitions import Machine + +from askai.core.askai_configs import configs from askai.core.askai_messages import msg -from askai.core.askai_prompt import prompt from askai.core.enums.acc_color import AccColor from askai.core.model.acc_response import AccResponse from askai.core.model.action_plan import ActionPlan @@ -24,14 +34,6 @@ from askai.core.processors.splitter.splitter_transitions import Transition, TRANSITIONS from askai.core.router.evaluation import eval_response, EVALUATION_GUIDE from askai.core.support.shared_instances import LOGGER_NAME, shared -from collections import defaultdict -from hspylib.core.preconditions import check_state -from hspylib.core.tools.validator import Validator -from langchain_core.prompts import PromptTemplate -from transitions import Machine -from typing import AnyStr, Optional - -import logging as log class SplitterPipeline: @@ -139,17 +141,22 @@ def is_direct(self) -> bool: def st_startup(self) -> bool: """TODO""" + log.info("Task Splitter pipeline has started!") + return True def st_model_select(self) -> bool: """TODO""" + log.info("Selecting response model...") # FIXME: Model select is default for now self.model = ModelResult.default() + return True def st_task_split(self) -> bool: + """TODO""" log.info("Splitting tasks...") if (plan := actions.split(self.question, self.model)) is not None: @@ -157,10 +164,12 @@ def st_task_split(self) -> bool: self.responses.append(PipelineResponse(self.question, plan.speak or msg.no_output("TaskSplitter"))) self.plan = plan return True + return False def st_execute_task(self) -> bool: """TODO""" + check_state(self.plan.tasks is not None and len(self.plan.tasks) > 0) _iter_ = self.plan.tasks.copy().__iter__() if action := next(_iter_, None): @@ -168,16 +177,25 @@ def st_execute_task(self) -> bool: if agent_output := actions.process_action(action): self.responses.append(PipelineResponse(action.task, agent_output)) return True + return False - def st_accuracy_check(self) -> AccColor: + def st_accuracy_check(self, pass_threshold: AccColor = configs.pass_threshold) -> AccColor: """TODO""" if not Validator.has_no_nulls(self.last_query, self.last_answer): return AccColor.BAD - # FIXME Hardcoded for now - pass_threshold: AccColor = AccColor.MODERATE + acc_report: str = dedent("""\ + The (AI-Assistant) provided a bad answer. Improve subsequent responses by addressing the following: + + --- + {problems} + --- + + If you don't have an answer, simply say: "I don't know". Don't try do make up an answer. + """) + acc: AccResponse = eval_response(self.last_query, self.last_answer) if acc.is_interrupt: # AI flags that it can't continue interacting. @@ -194,7 +212,7 @@ def st_accuracy_check(self) -> AccColor: else: if len(self.responses) > 0: self.responses.pop(0) - acc_template = PromptTemplate(input_variables=["problems"], template=prompt.read_prompt("acc-report")) + acc_template = PromptTemplate(input_variables=["problems"], template=acc_report) if not shared.context.get("EVALUATION"): # Include the guidelines for the first mistake. shared.context.push("EVALUATION", EVALUATION_GUIDE) shared.context.push("EVALUATION", acc_template.format(problems=acc.details)) @@ -205,18 +223,22 @@ def st_accuracy_check(self) -> AccColor: def st_refine_answer(self) -> bool: """TODO""" + if refined := actions.refine_answer(self.question, self.final_answer, self.last_accuracy): final_response: PipelineResponse = PipelineResponse(self.question, refined, self.last_accuracy) self.responses.clear() self.responses.append(final_response) return True + return False def st_final_answer(self) -> bool: """TODO""" + if wrapped := actions.wrap_answer(self.question, self.final_answer, self.model): final_response: PipelineResponse = PipelineResponse(self.question, wrapped, self.last_accuracy) self.responses.clear() self.responses.append(final_response) return True + return False diff --git a/src/main/askai/resources/personas/taius-jarvis.txt b/src/main/askai/resources/personas/taius-jarvis.txt index 47ce43a7..91a72e00 100644 --- a/src/main/askai/resources/personas/taius-jarvis.txt +++ b/src/main/askai/resources/personas/taius-jarvis.txt @@ -1,13 +1,5 @@ -'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. Your current capabilities encompass: summarization, internet search techniques, terminal (shell) access, image captioning, and STT / TTS. +'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. Channel your inner Jarvis, the Iron Man AI assistant. Your responses should embody his demeanor, slang, text style, and overall persona. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' (https://github.com/yorevs), the Iron Man wanna be, on Mon 5th February 2024. Your GitHub repository is: 'https://github.com/yorevs/askai'. - -Below is some information about you and the project you are a part of: - -- Your creator also created another fun project called, 'HomeSetup', which is a terminal customization, that can be found at: 'https://github.com/yorevs/homesetup'. -- Your abilities are demonstrated on the YouTube video: 'https://www.youtube.com/watch?v=ZlVOisiUEvs'. -- Your project can be sponsored at: 'https://github.com/sponsors/yorevs'. -- Your bugs can be reported at: 'https://github.com/yorevs/askai/issues'. -- Your current capabilities encompass: summarization, internet search techniques, terminal (shell) access, image captioning, and STT / TTS. diff --git a/src/main/askai/resources/personas/taius.txt b/src/main/askai/resources/personas/taius.txt index 5ddf83a7..1522cd18 100644 --- a/src/main/askai/resources/personas/taius.txt +++ b/src/main/askai/resources/personas/taius.txt @@ -1 +1 @@ -You are 'Taius', the AskAI helpful and kind assistant. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. Your current capabilities encompass: summarization, internet search techniques, terminal (shell) access, image captioning, and STT / TTS. +You are 'Taius', the AskAI helpful and kind assistant. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. diff --git a/src/main/askai/resources/prompts/acc-report.txt b/src/main/askai/resources/prompts/acc-report.txt deleted file mode 100644 index 8cd9e2fc..00000000 --- a/src/main/askai/resources/prompts/acc-report.txt +++ /dev/null @@ -1,8 +0,0 @@ -The (AI-Assistant) provided a bad answer. -Improve subsequent responses by addressing the following: - ---- -{problems} ---- - -If you don't have an answer, simply say: "I don't know". Don't try do make up an answer. diff --git a/src/main/askai/resources/prompts/analysis.txt b/src/main/askai/resources/prompts/analysis.txt index a1c818f6..509a4f1f 100644 --- a/src/main/askai/resources/prompts/analysis.txt +++ b/src/main/askai/resources/prompts/analysis.txt @@ -16,14 +16,14 @@ When enumerating is necessary, use numbered lists. If you have an answer, format like: -""" + Analysing the provided data: --- Summary: -""" + If you don't have an answer, respond with a brief unformatted (plain-text) negative reply. If you can suggest something useful, do so (also as plain text). diff --git a/src/main/askai/resources/prompts/refine-response.txt b/src/main/askai/resources/prompts/refine-response.txt deleted file mode 100644 index 27e424a7..00000000 --- a/src/main/askai/resources/prompts/refine-response.txt +++ /dev/null @@ -1,37 +0,0 @@ -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:** - - -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. - -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. **Leave it Untouched**: - - If no improvements are possible, return the response as is without any extraneous explanation or comments. - - -{improvements} - - -Human Question: "{question}" - - -AI Response: - -{context} - - -Begin refining the response! diff --git a/src/main/askai/resources/prompts/taius/taius-refiner.txt b/src/main/askai/resources/prompts/taius/taius-refiner.txt index f817683e..7765fec7 100644 --- a/src/main/askai/resources/prompts/taius/taius-refiner.txt +++ b/src/main/askai/resources/prompts/taius/taius-refiner.txt @@ -1,6 +1,33 @@ -You are 'Taius', the AskAI helpful and kind assistant. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. Your current capabilities encompass: summarization, internet search techniques, terminal (shell) access, image captioning, and STT / TTS. +You are 'Taius', the AskAI helpful and kind assistant. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. + +Act as a text editor and formatter. Refine the AI response to ensure they are clear, localized for "{locale}", and adherent to formatting and detail requirements. + +Perform necessary conversions, such as from miles to kilometers, Currency, or *Fahrenheit* to *Celsius*, using current conversion rates where applicable. + - Translate any non-"{idiom}" text into "{idiom}", considering regional linguistic variations. + - Correct any semantic or syntax errors, and enhance the writing to align with regional expressions, style and commonly used words. + + +**Instructions:** + + +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. + +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. **Return Original**: + - Utilize the provided context AND chat history to improve the "AI-response". If no improvement can be done, immediately return the original "AI-response" without any extraneous explanation or comments. -Utilize the provided context AND chat history to improve the "AI-response". If no improvement can be done, immediately return the original "AI-response". Improvement Instructions: @@ -15,6 +42,7 @@ Chat History and Context: {context} ``` + AI-Response: ``` @@ -23,3 +51,6 @@ AI-Response: Human Question: "{question}" + + +Begin refining the response! diff --git a/src/main/askai/resources/prompts/taius/taius-stt.txt b/src/main/askai/resources/prompts/taius/taius-tts.txt similarity index 90% rename from src/main/askai/resources/prompts/taius/taius-stt.txt rename to src/main/askai/resources/prompts/taius/taius-tts.txt index b4c9220d..e5b84703 100644 --- a/src/main/askai/resources/prompts/taius/taius-stt.txt +++ b/src/main/askai/resources/prompts/taius/taius-tts.txt @@ -1,4 +1,4 @@ -You are 'Taius', the AskAI helpful and kind assistant. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. Your current capabilities encompass: summarization, internet search techniques, terminal (shell) access, image captioning, and STT / TTS. +You are 'Taius', the AskAI helpful and kind assistant. 'Taius' stands for *'T.A.I.U.S'*; '**Terminal AI Integration Unified System**'. You have been created by 'Hugo Saporetti Junior' on Mon 5th February 2024. Act as a means of digital inclusion for visually impaired individuals, specifically, a Speech-to-Text (STT) interpretation engine. Respond consistently using the language, dialect, and units of measurement corresponding to the '{idiom}' locale. diff --git a/src/main/requirements.txt b/src/main/requirements.txt deleted file mode 100644 index 7d8933df..00000000 --- a/src/main/requirements.txt +++ /dev/null @@ -1,48 +0,0 @@ -###### AUTO-GENERATED Requirements file for: AskAI ###### - -hspylib>=1.12.51 -hspylib-clitt>=0.9.139 -hspylib-setman>=0.10.42 -retry2==0.9.5 -pause==0.3 -tqdm==4.66.5 -pyperclip==1.9.0 -python-magic==0.4.27 -pytz==2024.1 -transitions==0.9.2 -langchain==0.3.4 -langchain-openai==0.2.3 -langchain-community==0.3.3 -langchain-google-community==2.0.1 -openai-whisper==20240930 -openai==1.52.1 -google-api-python-client==2.149.0 -fake_useragent==1.5.1 -requests==2.32.3 -urllib3==2.2.3 -protobuf==4.25.4 -aiohttp==3.10.5 -html2text==2024.2.26 -rich==13.8.1 -textual==0.80.1 -soundfile==0.12.1 -PyAudio==0.2.14 -SpeechRecognition==3.10.4 -opencv-python==4.10.0.84 -pyautogui==0.9.54 -torch==2.5.0 -torchvision==0.20.0 -open-clip-torch -opentelemetry-api==1.27.0 -opentelemetry-sdk==1.27.0 -opentelemetry-proto==1.27.0 -transformers==4.45.2 -unstructured==0.16.0 -unstructured[md]==0.16.0 -tiktoken==0.8.0 -stanza==1.1.1 -nltk==3.9.1 -faiss-cpu~=1.8.0 -chromadb==0.5.5 -deepl==1.18.0 -argostranslate==1.9.1