Skip to content

Commit

Permalink
Minor fixes and prompt cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Oct 31, 2024
1 parent 6ab84cd commit f8503ed
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 130 deletions.
27 changes: 21 additions & 6 deletions src/main/askai/core/askai_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/askai/core/askai_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/main/askai/core/processors/splitter/splitter_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _:
Expand All @@ -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,
Expand Down
48 changes: 35 additions & 13 deletions src/main/askai/core/processors/splitter/splitter_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -139,45 +141,61 @@ 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:
if plan.is_direct:
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):
log.info(f"Executing task '{action}'...")
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.
Expand All @@ -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))
Expand All @@ -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
10 changes: 1 addition & 9 deletions src/main/askai/resources/personas/taius-jarvis.txt
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/main/askai/resources/personas/taius.txt
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 0 additions & 8 deletions src/main/askai/resources/prompts/acc-report.txt

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/askai/resources/prompts/analysis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ When enumerating is necessary, use numbered lists.

If you have an answer, format like:

"""

Analysing the provided data:

<your thoughts and conclusion>

---
Summary: <a brief concise reasoning>
"""


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).

Expand Down
37 changes: 0 additions & 37 deletions src/main/askai/resources/prompts/refine-response.txt

This file was deleted.

35 changes: 33 additions & 2 deletions src/main/askai/resources/prompts/taius/taius-refiner.txt
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -15,6 +42,7 @@ Chat History and Context:
{context}
```


AI-Response:

```
Expand All @@ -23,3 +51,6 @@ AI-Response:


Human Question: "{question}"


Begin refining the response!
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
Loading

0 comments on commit f8503ed

Please sign in to comment.