igbot-base 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- igbot_base-0.0.1/PKG-INFO +7 -0
- igbot_base-0.0.1/igbot_base/__init__.py +0 -0
- igbot_base-0.0.1/igbot_base/agent.py +10 -0
- igbot_base-0.0.1/igbot_base/agent_response.py +43 -0
- igbot_base-0.0.1/igbot_base/exception_handler.py +20 -0
- igbot_base-0.0.1/igbot_base/llm.py +47 -0
- igbot_base-0.0.1/igbot_base/llmmemory.py +52 -0
- igbot_base-0.0.1/igbot_base/models.py +22 -0
- igbot_base-0.0.1/igbot_base/persistable_memory.py +9 -0
- igbot_base-0.0.1/igbot_base/prompt_template.py +38 -0
- igbot_base-0.0.1/igbot_base/response_formats.py +5 -0
- igbot_base-0.0.1/igbot_base/retriever.py +11 -0
- igbot_base-0.0.1/igbot_base/tool.py +20 -0
- igbot_base-0.0.1/igbot_base/vectorstore.py +45 -0
- igbot_base-0.0.1/igbot_base.egg-info/PKG-INFO +7 -0
- igbot_base-0.0.1/igbot_base.egg-info/SOURCES.txt +18 -0
- igbot_base-0.0.1/igbot_base.egg-info/dependency_links.txt +1 -0
- igbot_base-0.0.1/igbot_base.egg-info/top_level.txt +1 -0
- igbot_base-0.0.1/pyproject.toml +11 -0
- igbot_base-0.0.1/setup.cfg +4 -0
File without changes
|
@@ -0,0 +1,43 @@
|
|
1
|
+
from enum import Enum
|
2
|
+
from typing import Optional
|
3
|
+
|
4
|
+
|
5
|
+
class Type(Enum):
|
6
|
+
SUCCESS = 1,
|
7
|
+
NO_CONTENT = 2,
|
8
|
+
ERROR = 3
|
9
|
+
|
10
|
+
|
11
|
+
class AgentResponse:
|
12
|
+
|
13
|
+
def __init__(self, response: any, response_type: Type, exception: Optional[Exception]):
|
14
|
+
self.__response = response
|
15
|
+
self.__type = response_type
|
16
|
+
self.__exception = exception
|
17
|
+
|
18
|
+
def is_successful(self):
|
19
|
+
return self.__type == Type.SUCCESS
|
20
|
+
|
21
|
+
def is_error(self):
|
22
|
+
return self.__type == Type.ERROR
|
23
|
+
|
24
|
+
def is_no_content(self):
|
25
|
+
return self.__type == Type.NO_CONTENT
|
26
|
+
|
27
|
+
def get_response(self):
|
28
|
+
return self.__response
|
29
|
+
|
30
|
+
@staticmethod
|
31
|
+
def error(error_message: str, exception: Optional[Exception]):
|
32
|
+
return AgentResponse(error_message, Type.ERROR, exception)
|
33
|
+
|
34
|
+
@staticmethod
|
35
|
+
def success(response: str):
|
36
|
+
return AgentResponse(response, Type.SUCCESS, None)
|
37
|
+
|
38
|
+
@staticmethod
|
39
|
+
def no_content():
|
40
|
+
return AgentResponse("", Type.NO_CONTENT, None)
|
41
|
+
|
42
|
+
def get_exception(self):
|
43
|
+
return self.__exception
|
@@ -0,0 +1,20 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
|
3
|
+
|
4
|
+
class ExceptionHandler(ABC):
|
5
|
+
|
6
|
+
@abstractmethod
|
7
|
+
def handle(self, e: Exception):
|
8
|
+
pass
|
9
|
+
|
10
|
+
|
11
|
+
class NoopExceptionHandler(ExceptionHandler):
|
12
|
+
|
13
|
+
def handle(self, e: Exception):
|
14
|
+
pass
|
15
|
+
|
16
|
+
|
17
|
+
class PrintingExceptionHandler(ExceptionHandler):
|
18
|
+
|
19
|
+
def handle(self, e: Exception):
|
20
|
+
print(e)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
from builtins import dict
|
3
|
+
from typing import Optional
|
4
|
+
|
5
|
+
from igbot_base.igbot_base.exception_handler import ExceptionHandler, NoopExceptionHandler
|
6
|
+
from igbot_base.igbot_base.llmmemory import LlmMemory
|
7
|
+
from igbot_base.igbot_base.models import Model
|
8
|
+
|
9
|
+
|
10
|
+
class Llm(ABC):
|
11
|
+
|
12
|
+
def __init__(
|
13
|
+
self,
|
14
|
+
name: str,
|
15
|
+
model: Model,
|
16
|
+
temperature: float,
|
17
|
+
response_format: Optional[dict],
|
18
|
+
llm_exception_handler: ExceptionHandler = NoopExceptionHandler()):
|
19
|
+
self._name = name
|
20
|
+
self._model = model
|
21
|
+
self._temperature = temperature
|
22
|
+
self._format = response_format
|
23
|
+
self._exception_handler = llm_exception_handler
|
24
|
+
|
25
|
+
@abstractmethod
|
26
|
+
def _call(self, user_query: str, history: LlmMemory, params: dict) -> str:
|
27
|
+
pass
|
28
|
+
|
29
|
+
def _revert_memory(self, history: LlmMemory):
|
30
|
+
history.revert_to_snapshot()
|
31
|
+
|
32
|
+
def call(self, user_query: str, history: LlmMemory, params: dict) -> str:
|
33
|
+
history.set_snapshot()
|
34
|
+
try:
|
35
|
+
return self._call(user_query, history, params)
|
36
|
+
except Exception as e:
|
37
|
+
self._revert_memory(history)
|
38
|
+
self._exception_handler.handle(e)
|
39
|
+
|
40
|
+
def get_additional_llm_args(self):
|
41
|
+
args = {}
|
42
|
+
if self._temperature is not None:
|
43
|
+
args["temperature"] = self._temperature
|
44
|
+
if self._format is not None:
|
45
|
+
args["response_format"] = self._format
|
46
|
+
|
47
|
+
return args
|
@@ -0,0 +1,52 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
|
3
|
+
|
4
|
+
class LlmMemory(ABC):
|
5
|
+
# todo: capacity of memory
|
6
|
+
@abstractmethod
|
7
|
+
def retrieve(self):
|
8
|
+
pass
|
9
|
+
|
10
|
+
@abstractmethod
|
11
|
+
def append_user(self, content: str):
|
12
|
+
pass
|
13
|
+
|
14
|
+
@abstractmethod
|
15
|
+
def append_assistant(self, content: str):
|
16
|
+
pass
|
17
|
+
|
18
|
+
@abstractmethod
|
19
|
+
def append_system(self, content: str):
|
20
|
+
pass
|
21
|
+
|
22
|
+
@abstractmethod
|
23
|
+
def append_tool_request(self, message):
|
24
|
+
pass
|
25
|
+
|
26
|
+
@abstractmethod
|
27
|
+
def append_tool(self, tool_call_id: str, content: str):
|
28
|
+
pass
|
29
|
+
|
30
|
+
@abstractmethod
|
31
|
+
def clean_conversation(self):
|
32
|
+
pass
|
33
|
+
|
34
|
+
@abstractmethod
|
35
|
+
def delete_last_user_message(self):
|
36
|
+
pass
|
37
|
+
|
38
|
+
@abstractmethod
|
39
|
+
def delete_last_tool_message(self):
|
40
|
+
pass
|
41
|
+
|
42
|
+
@abstractmethod
|
43
|
+
def delete_last_assistant_message(self):
|
44
|
+
pass
|
45
|
+
|
46
|
+
@abstractmethod
|
47
|
+
def revert_to_snapshot(self):
|
48
|
+
pass
|
49
|
+
|
50
|
+
@abstractmethod
|
51
|
+
def set_snapshot(self):
|
52
|
+
pass
|
@@ -0,0 +1,22 @@
|
|
1
|
+
from enum import Enum
|
2
|
+
from openai import OpenAI
|
3
|
+
|
4
|
+
|
5
|
+
class ModelInfo:
|
6
|
+
|
7
|
+
def __init__(self, name, client):
|
8
|
+
self.__name = name
|
9
|
+
self.__client = client
|
10
|
+
|
11
|
+
def get_name(self):
|
12
|
+
return self.__name
|
13
|
+
|
14
|
+
def get_client(self):
|
15
|
+
return self.__client()
|
16
|
+
|
17
|
+
|
18
|
+
class Model(Enum):
|
19
|
+
OLLAMA_3_2_LOCAL = ModelInfo("llama3.2", lambda: OpenAI(base_url="http://localhost:11434/v1", api_key='ollama'))
|
20
|
+
OPENAI_GPT_4o_MINI = ModelInfo("gpt-4o-mini", lambda: OpenAI())
|
21
|
+
OPENAI_GPT_4o = ModelInfo("gpt-4o", lambda: OpenAI())
|
22
|
+
OPENAI_GPT_4o_MINI_JSON = ModelInfo("gpt-4o-mini-2024-07-18", lambda: OpenAI())
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import re
|
2
|
+
from typing import Optional
|
3
|
+
|
4
|
+
from igbot_base.igbot_base.exception_handler import ExceptionHandler, PrintingExceptionHandler
|
5
|
+
|
6
|
+
|
7
|
+
class Prompt:
|
8
|
+
|
9
|
+
def __init__(
|
10
|
+
self,
|
11
|
+
content: str,
|
12
|
+
variables,
|
13
|
+
exception_handler: ExceptionHandler = PrintingExceptionHandler()):
|
14
|
+
self.__content = content
|
15
|
+
self.__variables = variables
|
16
|
+
self.__exception_handler = exception_handler
|
17
|
+
|
18
|
+
@staticmethod
|
19
|
+
def replace_placeholders(text: str, values: dict) -> str:
|
20
|
+
def replacer(match):
|
21
|
+
key = match.group(1)
|
22
|
+
return str(values.get(key, match.group(0)))
|
23
|
+
|
24
|
+
return re.sub(r"\{\{(\w+)\}\}", replacer, text)
|
25
|
+
|
26
|
+
def parse(self, params: Optional[dict]) -> str:
|
27
|
+
if len(self.__variables) == 0:
|
28
|
+
return self.__content
|
29
|
+
|
30
|
+
try:
|
31
|
+
return self.replace_placeholders(self.__content, params)
|
32
|
+
except KeyError as e:
|
33
|
+
self.__exception_handler.handle(e)
|
34
|
+
|
35
|
+
raise Exception(f"Error while substituting values for prompt")
|
36
|
+
|
37
|
+
def get_content(self) -> str:
|
38
|
+
return self.__content
|
@@ -0,0 +1,20 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
|
3
|
+
|
4
|
+
class Tool(ABC):
|
5
|
+
|
6
|
+
def __init__(
|
7
|
+
self,
|
8
|
+
name: str):
|
9
|
+
self._name = name
|
10
|
+
|
11
|
+
def get_name(self):
|
12
|
+
return self._name
|
13
|
+
|
14
|
+
@abstractmethod
|
15
|
+
def get_function(self):
|
16
|
+
pass
|
17
|
+
|
18
|
+
@abstractmethod
|
19
|
+
def get_definition(self):
|
20
|
+
pass
|
@@ -0,0 +1,45 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
|
3
|
+
from igbot_base.igbot_base.retriever import Retriever
|
4
|
+
|
5
|
+
|
6
|
+
class Metadata:
|
7
|
+
|
8
|
+
def __init__(self):
|
9
|
+
self.metadata = {}
|
10
|
+
|
11
|
+
def append(self, key, value):
|
12
|
+
if key not in self.metadata:
|
13
|
+
self.metadata[key] = set()
|
14
|
+
|
15
|
+
self.metadata[key].add(value)
|
16
|
+
|
17
|
+
def get(self):
|
18
|
+
return self.metadata.copy()
|
19
|
+
|
20
|
+
|
21
|
+
class Vectorstore(ABC):
|
22
|
+
|
23
|
+
@abstractmethod
|
24
|
+
def remove_db(self):
|
25
|
+
pass
|
26
|
+
|
27
|
+
@abstractmethod
|
28
|
+
def get_dimensions_number(self):
|
29
|
+
pass
|
30
|
+
|
31
|
+
@abstractmethod
|
32
|
+
def get_retriever(self, load_number_of_chunks) -> Retriever:
|
33
|
+
pass
|
34
|
+
|
35
|
+
@abstractmethod
|
36
|
+
def get_legacy_retriever(self, load_number_of_chunks):
|
37
|
+
pass
|
38
|
+
|
39
|
+
@abstractmethod
|
40
|
+
def append(self, chunks):
|
41
|
+
pass
|
42
|
+
|
43
|
+
@abstractmethod
|
44
|
+
def get_metadata(self) -> Metadata:
|
45
|
+
pass
|
@@ -0,0 +1,18 @@
|
|
1
|
+
pyproject.toml
|
2
|
+
igbot_base/__init__.py
|
3
|
+
igbot_base/agent.py
|
4
|
+
igbot_base/agent_response.py
|
5
|
+
igbot_base/exception_handler.py
|
6
|
+
igbot_base/llm.py
|
7
|
+
igbot_base/llmmemory.py
|
8
|
+
igbot_base/models.py
|
9
|
+
igbot_base/persistable_memory.py
|
10
|
+
igbot_base/prompt_template.py
|
11
|
+
igbot_base/response_formats.py
|
12
|
+
igbot_base/retriever.py
|
13
|
+
igbot_base/tool.py
|
14
|
+
igbot_base/vectorstore.py
|
15
|
+
igbot_base.egg-info/PKG-INFO
|
16
|
+
igbot_base.egg-info/SOURCES.txt
|
17
|
+
igbot_base.egg-info/dependency_links.txt
|
18
|
+
igbot_base.egg-info/top_level.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
igbot_base
|
@@ -0,0 +1,11 @@
|
|
1
|
+
[build-system]
|
2
|
+
requires = ["setuptools", "wheel"]
|
3
|
+
build-backend = "setuptools.build_meta"
|
4
|
+
|
5
|
+
[project]
|
6
|
+
name = "igbot_base"
|
7
|
+
version = "0.0.1"
|
8
|
+
description = "Base classes for igbot"
|
9
|
+
authors = [{name = "Igor Kopeć", email = "igor.kopec95@gmail.com"}]
|
10
|
+
license = "LGPL-3.0-or-later"
|
11
|
+
requires-python = ">=3.12"
|