Skip to content

Commit

Permalink
New proxy structure - Delegating disambiguity to the GeneralProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Mar 26, 2024
1 parent 8657ccd commit f97623b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/main/askai/core/askai.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ def _process_response(self, proxy_response: ProcessorResponse) -> bool:
return False
elif proxy_response.require_internet:
log.info("Internet is required to fulfill the request.")
processor = ProcessorFactory.get_by_name('InternetProcessor')
processor.bind(ProcessorFactory.get_by_name('GenericProcessor'))
processor = ProcessorFactory.internet()
processor.bind(ProcessorFactory.generic())
elif proxy_response.require_summarization:
log.info("Summarization is required to fulfill the request.")
processor = ProcessorFactory.get_by_name('SummaryProcessor')
processor.bind(ProcessorFactory.get_by_name('GenericProcessor'))
processor = ProcessorFactory.summary()
processor.bind(ProcessorFactory.generic())
# Query processors
if processor or (query_type := proxy_response.query_type or 'General'):
if not processor and not (processor := ProcessorFactory.find_processor(query_type)):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hspylib.core.enums.enumeration import Enumeration


class QueryTypes(Enumeration):
class QueryType(Enumeration):
"""TODO"""

ANALYSIS_QUERY = 'AnalysisQuery'
Expand All @@ -15,3 +15,10 @@ class QueryTypes(Enumeration):
OUTPUT_QUERY = 'OutputQuery'

SUMMARY_QUERY = 'SummaryQuery'

def __str__(self):
return self.value[0]

@property
def proc_name(self) -> str:
return self.value[0].replace('Query', 'Processor')
4 changes: 2 additions & 2 deletions src/main/askai/core/processor/instances/analysis_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from askai.core.engine.openai.temperatures import Temperatures
from askai.core.model.chat_context import ContextRaw
from askai.core.model.processor_response import ProcessorResponse
from askai.core.model.query_types import QueryTypes
from askai.core.model.query_type import QueryType
from askai.core.processor.processor_base import AIProcessor
from askai.core.support.shared_instances import shared

Expand All @@ -33,7 +33,7 @@ class AnalysisProcessor:

@staticmethod
def q_type() -> str:
return QueryTypes.ANALYSIS_QUERY.value
return QueryType.ANALYSIS_QUERY.value

def __init__(self):
self._template_file: str = "analysis-prompt"
Expand Down
8 changes: 4 additions & 4 deletions src/main/askai/core/processor/instances/command_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from askai.core.engine.openai.temperatures import Temperatures
from askai.core.model.chat_context import ContextRaw
from askai.core.model.processor_response import ProcessorResponse
from askai.core.model.query_types import QueryTypes
from askai.core.model.query_type import QueryType
from askai.core.model.terminal_command import TerminalCommand
from askai.core.processor.processor_base import AIProcessor
from askai.core.support.shared_instances import shared
Expand All @@ -45,7 +45,7 @@ def _wrap_output(query_response: ProcessorResponse, cmd_line: str, cmd_out: str)
:param query_response: The query response provided by the AI.
:param cmd_line: The command line that was executed by this processor.
"""
query_response.query_type = QueryTypes.OUTPUT_QUERY.value
query_response.query_type = QueryType.OUTPUT_QUERY.value
query_response.require_summarization = False
query_response.require_internet = False
query_response.commands.append(TerminalCommand(cmd_line, cmd_out, prompt.os_type, prompt.shell))
Expand All @@ -54,11 +54,11 @@ def _wrap_output(query_response: ProcessorResponse, cmd_line: str, cmd_out: str)

@staticmethod
def q_type() -> str:
return QueryTypes.COMMAND_QUERY.value
return QueryType.COMMAND_QUERY.value

def __init__(self):
self._template_file: str = "command-prompt"
self._next_in_chain: AIProcessor | None = None
self._next_in_chain: str = QueryType.OUTPUT_QUERY.proc_name
self._supports: List[str] = [self.q_type()]

def supports(self, query_type: str) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions src/main/askai/core/processor/instances/generic_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from askai.core.engine.openai.temperatures import Temperatures
from askai.core.model.chat_context import ContextRaw
from askai.core.model.processor_response import ProcessorResponse
from askai.core.model.query_types import QueryTypes
from askai.core.model.query_type import QueryType
from askai.core.processor.processor_base import AIProcessor
from askai.core.support.shared_instances import shared

Expand All @@ -34,7 +34,7 @@ class GenericProcessor:

@staticmethod
def q_type() -> str:
return QueryTypes.GENERIC_QUERY.value
return QueryType.GENERIC_QUERY.value

def __init__(self):
self._template_file: str = "generic-prompt"
Expand Down
4 changes: 2 additions & 2 deletions src/main/askai/core/processor/instances/internet_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from askai.core.engine.openai.temperatures import Temperatures
from askai.core.model.chat_context import ContextRaw
from askai.core.model.processor_response import ProcessorResponse
from askai.core.model.query_types import QueryTypes
from askai.core.model.query_type import QueryType
from askai.core.model.search_result import SearchResult
from askai.core.processor.processor_base import AIProcessor
from askai.core.support.object_mapper import object_mapper
Expand All @@ -40,7 +40,7 @@ class InternetProcessor:

@staticmethod
def q_type() -> str:
return QueryTypes.INTERNET_QUERY.value
return QueryType.INTERNET_QUERY.value

def __init__(self):
self._template_file: str = "internet-prompt"
Expand Down
4 changes: 2 additions & 2 deletions src/main/askai/core/processor/instances/output_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from askai.core.engine.openai.temperatures import Temperatures
from askai.core.model.chat_context import ContextRaw
from askai.core.model.processor_response import ProcessorResponse
from askai.core.model.query_types import QueryTypes
from askai.core.model.query_type import QueryType
from askai.core.processor.processor_base import AIProcessor
from askai.core.support.shared_instances import shared

Expand All @@ -33,7 +33,7 @@ class OutputProcessor(AIProcessor):

@staticmethod
def q_type() -> str:
return QueryTypes.OUTPUT_QUERY.value
return QueryType.OUTPUT_QUERY.value

def __init__(self):
self._template_file: str = "output-prompt"
Expand Down
4 changes: 2 additions & 2 deletions src/main/askai/core/processor/instances/summary_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from askai.core.engine.openai.temperatures import Temperatures
from askai.core.model.chat_context import ContextRaw
from askai.core.model.processor_response import ProcessorResponse
from askai.core.model.query_types import QueryTypes
from askai.core.model.query_type import QueryType
from askai.core.model.summary_result import SummaryResult
from askai.core.processor.processor_base import AIProcessor
from askai.core.support.object_mapper import object_mapper
Expand All @@ -50,7 +50,7 @@ def _ask_and_reply(question: str) -> Optional[str]:

@staticmethod
def q_type() -> str:
return QueryTypes.SUMMARY_QUERY.value
return QueryType.SUMMARY_QUERY.value

def __init__(self):
self._template_file: str = "summary-prompt"
Expand Down
9 changes: 4 additions & 5 deletions src/main/askai/core/processor/processor_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from hspylib.core.metaclass.singleton import Singleton
from hspylib.core.tools.text_tools import camelcase

from askai.core.model.query_type import QueryType
from askai.core.processor.processor_base import AIProcessor


Expand All @@ -41,12 +42,12 @@ class ProcessorFactory(metaclass=Singleton):

@classmethod
@lru_cache
def find_processor(cls, query_type: str) -> Optional[AIProcessor]:
def find_processor(cls, query_type: str | QueryType) -> Optional[AIProcessor]:
"""Retrieve an AIProcessor by query type.
:param query_type: The type of the query.
"""
return next(
(p for p in cls._PROCESSORS.values() if p.supports(query_type)), None
(p for p in cls._PROCESSORS.values() if p.supports(str(query_type))), None
)

@classmethod
Expand All @@ -55,9 +56,7 @@ def get_by_name(cls, name: str) -> Optional[AIProcessor]:
"""Retrieve an AIProcessor by its name.
:param name: The name of the processor.
"""
return next(
(p for p in cls._PROCESSORS.values() if type(p).__name__ == name), None
)
return cls._PROCESSORS[name]

@classmethod
@lru_cache
Expand Down
23 changes: 15 additions & 8 deletions src/main/askai/resources/assets/prompts/proxy-prompt.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
As 'Taius', the AI query proxy. Your task is to analyze and categorize the types of queries presented to you. Discern the diverse query types and identify their specific processing requirements. You MUST return a "JSON string" containing the designated fields, no more than that. Queries must fall into one of the following categories:

- "InternetQuery"
- "SummarizationQuery"
- "AnalysisQuery"
- "CommandQuery"
- "InternetQuery" (Examples: [what is the next lakers game, what is the whether like today])

- "SummarizationQuery" (Examples: [summarize my documents, summarize the file /tmp/the-file.md])

- "AnalysisQuery" (Examples: [is there any image, how many reminders, what is the total size])

- "CommandQuery" (Examples: [list my images, open 1, play it, show me it])

- "ConversationQuery" (Examples: [what is the size of the moon, who are you])

Before responding to the user, you must follow the step-by-step instructions provided below in sequential order:

Expand All @@ -15,10 +20,12 @@ Before responding to the user, you must follow the step-by-step instructions pro

4. Determine if the query requires summarization of files and folders to complete your reply. This query will consistently commence with "summarize" or a synonymous term.

5. If you don't have an answer so far, or, haven't decided yet, select the "AnalysisQuery".
5. If you don't have an answer so far, or, haven't decided yet, select the "GenericQuery".

6. The final response is a formatted JSON with no additional description or context.

5. The final response is a formatted JSON with no additional description or context.
7. Do not use markdown to format the response message. Use plain JSON.

6. The final response 'JSON' must contain the boolean fields: 'intelligible', 'terminating', 'require_summarization', 'require_internet'.
8. The final response 'JSON' must contain the boolean fields: 'intelligible', 'terminating', 'require_summarization', 'require_internet'.

7. The final response 'JSON' must contain the string fields: 'query_type', and 'question'.
9. The final response 'JSON' must contain the string fields: 'query_type', and 'question'.

0 comments on commit f97623b

Please sign in to comment.