Skip to content

Commit

Permalink
Improving processors and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Saporetti Junior committed Mar 14, 2024
1 parent 50ec9ff commit 4c9cd6f
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 49 deletions.
4 changes: 4 additions & 0 deletions src/main/askai/core/engine/openai/temperatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Temperatures(Enumeration):

# fmt: off

ZERO = 0.0, 0.0

# Generates code that adheres to established patterns and conventions. Output is more deterministic and focused.
# Useful for generating syntactically correct code.
CODE_GENERATION = 0.2, 0.1
Expand All @@ -42,4 +44,6 @@ class Temperatures(Enumeration):
# established patterns.
EXPLORATORY_CODE_WRITING = 0.6, 0.7

HOTTEST = 1.0, 1.0

# fmt: on
9 changes: 5 additions & 4 deletions src/main/askai/core/model/chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ def join(self, *keys: str) -> ContextRaw:
"""Join contexts specified by keys."""
context: ContextRaw = []
token_length = 0
for key in set(keys):
content = ' '.join([t['content'] for t in self.get(key)])
for key in keys:
ctx = self.get(key)
content = ' '.join([t['content'] for t in ctx])
token_length += len(content or '')
if token_length > self._token_limit:
raise TokenLengthExceeded(f"Required token length={token_length}k limit={self._token_limit}k")
if content:
context.extend(self.get(key))
if content and ctx not in context:
context.extend(ctx)
return context

def clear(self, *keys: str) -> int:
Expand Down
2 changes: 1 addition & 1 deletion src/main/askai/core/processor/analysis_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def process(self, query_response: QueryResponse) -> Tuple[bool, Optional[str]]:
context: ContextRaw = shared.context.join("CONTEXT", "SETUP", "QUESTION")
log.info("Analysis::[QUESTION] '%s' context=%s", query_response.question, context)

if (response := shared.engine.ask(context, *Temperatures.DATA_ANALYSIS.value)) and response.is_success:
if (response := shared.engine.ask(context, *Temperatures.CODE_GENERATION.value)) and response.is_success:
log.debug("Analysis::[RESPONSE] Received from AI: %s.", response)
if output := response.message:
shared.context.push("CONTEXT", query_response.question)
Expand Down
2 changes: 1 addition & 1 deletion src/main/askai/core/processor/command_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _process_command(self, query_response: QueryResponse, cmd_line: str) -> Tupl
if not cmd_out:
cmd_out = msg.cmd_no_output()
else:
shared.context.push("CONTEXT", f"Command `{cmd_line}' output:\n\n```\n{cmd_out}\n```")
shared.context.set("CONTEXT", f"Command `{cmd_line}' output:\n\n```\n{cmd_out}\n```")
cmd_out = self._wrap_output(query_response, cmd_line, cmd_out)
else:
log.error("Command failed.\nCODE=%s \nPATH=%s \nCMD=%s ", exit_code, os.getcwd(), cmd_line)
Expand Down
3 changes: 1 addition & 2 deletions src/main/askai/core/processor/generic_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def process(self, query_response: QueryResponse) -> Tuple[bool, Optional[str]]:
final_prompt: str = msg.translate(template.format(user=prompt.user))
shared.context.set("SETUP", final_prompt, "system")
shared.context.set("QUESTION", query_response.question)
context: ContextRaw = shared.context.join("GENERAL", "INTERNET", "SUMMARY", "SETUP", "QUESTION")
context: ContextRaw = shared.context.join("GENERAL", "CONTEXT", "SETUP", "QUESTION")
log.info("Setup::[GENERIC] '%s' context=%s", query_response.question, context)

if (response := shared.engine.ask(context, *Temperatures.CREATIVE_WRITING.value)) and response.is_success:
Expand All @@ -54,7 +54,6 @@ def process(self, query_response: QueryResponse) -> Tuple[bool, Optional[str]]:
cache.save_reply(query_response.question, output)
cache.save_query_history()
status = True
shared.context.clear("INTERNET", "SUMMARY")
else:
output = msg.llm_error(response.message)

Expand Down
34 changes: 11 additions & 23 deletions src/main/askai/core/processor/internet_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
Copyright·(c)·2024,·HSPyLib
"""
import logging as log
from functools import partial
from typing import Optional, Tuple

from hspylib.core.zoned_datetime import now
Expand Down Expand Up @@ -49,34 +48,23 @@ def process(self, query_response: QueryResponse) -> Tuple[bool, Optional[str]]:
if not (response := cache.read_reply(query_response.question)):
if (response := shared.engine.ask(context, *Temperatures.CHATBOT_RESPONSES.value)) and response.is_success:
search: SearchResult = object_mapper.of_json(response.message, SearchResult)
query = " + ".join(search.keywords)
fc_call = partial(internet.scrap_sites, query) \
if query_response.require_summarization \
else partial(internet.search_google, query)
if results := fc_call(*search.sites):
search.results = results
output = self._wrap_output(query_response, search)
shared.context.set("INTERNET", output, "assistant")
cache.save_reply(query_response.question, output)
status = True
if not isinstance(search, SearchResult):
log.error(msg.invalid_response(search))
output = response.message
else:
output = msg.search_empty()
query = " + ".join(search.keywords)
if output := internet.search_google(query, *search.sites):
shared.context.set("CONTEXT", output, "assistant")
cache.save_reply(query_response.question, output)
else:
output = msg.search_empty()
status = True
else:
output = msg.llm_error(response.message)
else:
log.debug('Reply found for "%s" in cache.', query_response.question)
output = response
shared.context.set("INTERNET", output, "assistant")
shared.context.set("CONTEXT", output, "assistant")
status = True

return status, output

def _wrap_output(self, query_response: QueryResponse, search_result: SearchResult) -> str:
"""Wrap the output into a new string to be forwarded to the next processor.
:param query_response: The query response provided by the AI.
:param search_result: The internet search results.
"""
query_response.query_type = self.next_in_chain().query_type()
query_response.require_internet = False
query_response.response = str(search_result)
return str(query_response)
2 changes: 1 addition & 1 deletion src/main/askai/core/processor/output_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def process(self, query_response: QueryResponse) -> Tuple[bool, Optional[str]]:
context: ContextRaw = shared.context.join("CONTEXT", "SETUP")
log.info("Output::[COMMAND] '%s' context=%s", commands, context)

if (response := shared.engine.ask(context, *Temperatures.DATA_ANALYSIS.value)) and response.is_success:
if (response := shared.engine.ask(context, *Temperatures.CODE_GENERATION.value)) and response.is_success:
log.debug("Output::[RESPONSE] Received from AI: %s.", response)
if output := response.message:
shared.context.push("CONTEXT", output, "assistant")
Expand Down
2 changes: 1 addition & 1 deletion src/main/askai/core/processor/processor_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def process(self, question: str) -> Tuple[bool, QueryResponse]:
final_prompt = msg.translate(template.format(query_types=self.query_types))
shared.context.set("SETUP", final_prompt, "system")
shared.context.set("QUESTION", question)
context: ContextRaw = shared.context.join("INTERNET", "SUMMARY", "CONTEXT", "SETUP", "QUESTION")
context: ContextRaw = shared.context.join("CONTEXT", "SETUP", "QUESTION")
log.info("Ask::[QUESTION] '%s' context=%s", question, context)

if (response := shared.engine.ask(context, *Temperatures.CODE_GENERATION.value)) and response.is_success:
Expand Down
20 changes: 12 additions & 8 deletions src/main/askai/core/processor/summary_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ def process(self, query_response: QueryResponse) -> Tuple[bool, Optional[str]]:

try:
if (response := shared.engine.ask(context, *Temperatures.CHATBOT_RESPONSES.value)) and response.is_success:
summary_result: SummaryResult = object_mapper.of_json(response.message, SummaryResult)
summarizer.generate(summary_result.folder, summary_result.glob)
if results := summarizer.query('Give me an overview of all the summarized content'):
summary_result.results = results
output = self._wrap_output(query_response, summary_result)
shared.context.set("SUMMARY", output, "assistant")
cache.save_reply(query_response.question, output)
status = True
summary: SummaryResult = object_mapper.of_json(response.message, SummaryResult)
if not isinstance(summary, SummaryResult):
log.error(msg.invalid_response(SummaryResult))
output = response.message
else:
summarizer.generate(summary.folder, summary.glob)
if results := summarizer.query('Give me an overview of all the summarized content'):
summary.results = results
output = self._wrap_output(query_response, summary)
shared.context.set("CONTEXT", output, "assistant")
cache.save_reply(query_response.question, output)
status = True
else:
output = msg.llm_error(response.message)
except (FileNotFoundError, ValueError, DocumentsNotFound) as err:
Expand Down
16 changes: 8 additions & 8 deletions src/main/askai/core/support/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
from askai.language.language import Language

ASKAI_CHAT_ICONS = {
"": "%RED%\n\n",
"": "%BLUE%\n\n",
"": "%BLUE%\n\n",
"": "%BLUE%\n\n",
"": "%YELLOW%\n\n",
"": "%YELLOW%\n\n",
"": "%ORANGE%\n\n",
"": "%RED%",
"": "%BLUE%",
"": "%BLUE%",
"": "%BLUE%",
"": "%YELLOW%",
"": "%YELLOW%",
"": "%ORANGE%",
}


Expand All @@ -59,7 +59,7 @@ def beautify(text: Any) -> str:
text = re.sub(r"[Aa]dvice[-:\s][ \n\t]*", f"{ASKAI_CHAT_ICONS['']}{''} Advice: ", text)
text = re.sub(r"Errors?[-:\s][ \n\t]*", f"%EL1%{ASKAI_CHAT_ICONS['']}{''} Error: ", text)
text = re.sub(r"^\n+", '', text, re.MULTILINE)
text = re.sub(r"\n{2,}", '\n', text, re.MULTILINE)
text = re.sub(r"\n{2,}", '\n\n', text, re.MULTILINE)
text = re.sub(re_url, r'%CYAN% \1%GREEN%', text)
# fmt: on

Expand Down

0 comments on commit 4c9cd6f

Please sign in to comment.