igbot-base 0.0.1__py3-none-any.whl

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/__init__.py ADDED
File without changes
igbot_base/agent.py ADDED
@@ -0,0 +1,10 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from igbot_base.igbot_base.agent_response import AgentResponse
4
+
5
+
6
+ class Agent(ABC):
7
+
8
+ @abstractmethod
9
+ def invoke(self, query) -> AgentResponse:
10
+ pass
@@ -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)
igbot_base/llm.py ADDED
@@ -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
igbot_base/models.py ADDED
@@ -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,9 @@
1
+ from abc import abstractmethod
2
+ from igbot_base.igbot_base.llmmemory import LlmMemory
3
+
4
+
5
+ class PersistableMemory(LlmMemory):
6
+
7
+ @abstractmethod
8
+ def save(self):
9
+ pass
@@ -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,5 @@
1
+ from enum import Enum
2
+
3
+
4
+ class ResponseFormat(Enum):
5
+ JSON_OBJECT = {"type": "json_object"}
@@ -0,0 +1,11 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+
4
+ class Retriever(ABC):
5
+
6
+ def __init__(self):
7
+ pass
8
+
9
+ @abstractmethod
10
+ def get_relevant_data(self, query: str):
11
+ pass
igbot_base/tool.py ADDED
@@ -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,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: igbot_base
3
+ Version: 0.0.1
4
+ Summary: Base classes for igbot
5
+ Author-email: Igor Kopeć <igor.kopec95@gmail.com>
6
+ License-Expression: LGPL-3.0-or-later
7
+ Requires-Python: >=3.12
@@ -0,0 +1,17 @@
1
+ igbot_base/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ igbot_base/agent.py,sha256=12ubgE_Vr7gtkRCwRQsE2pRHPyFHut8Ib1bN6FkUayU,199
3
+ igbot_base/agent_response.py,sha256=eAps47djL_aWYn8P_BMpVGkPwO6m8nhltWWyh8qSG6E,1042
4
+ igbot_base/exception_handler.py,sha256=BMzQpgP4jnnRuTcEItx0oMljTZmKgMRCHzpybHn88Ig,341
5
+ igbot_base/llm.py,sha256=cdaGQchUcPI4E84u4Rc9HcmFpOfXvptjpR-wO9dBWs4,1494
6
+ igbot_base/llmmemory.py,sha256=KslOeTSZwvHVyFglbFhPcb6_GCCXZOqHz5YBTzJ2mew,977
7
+ igbot_base/models.py,sha256=15bitUZy4Z4JhZbLd_SMx5pD7m8YuZiIq-wYGumf0ws,622
8
+ igbot_base/persistable_memory.py,sha256=h9UHpnS7vlPriNIL-Giout2R9Pfc_OmGlq-WRWzZ9Vs,177
9
+ igbot_base/prompt_template.py,sha256=kvbGEuKimY5-Lj024CvgBZSgnZCjaVd_RrTw8PxN0dQ,1117
10
+ igbot_base/response_formats.py,sha256=Sp679VfkcyxsR19ddFEHTOM7Ox0u2WBhdji_Y9pVc2M,94
11
+ igbot_base/retriever.py,sha256=lwQhJbPtlWxiRMoRzqz2VI6WFv-60B8jPvKQc8hdeeY,177
12
+ igbot_base/tool.py,sha256=8QwzQuokkVSITCjZuZJeEbCvJU-n2yh9D-QauQex4Ko,319
13
+ igbot_base/vectorstore.py,sha256=mQ-zoWDz6MiHDrJR5ZqB8HIxHAOrBtTBaj69XS-WoDw,853
14
+ igbot_base-0.0.1.dist-info/METADATA,sha256=qSJWNcEtTOQmu6QqkoGxD-Oo765uVI_A6H-w8LwjcI8,199
15
+ igbot_base-0.0.1.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
16
+ igbot_base-0.0.1.dist-info/top_level.txt,sha256=RGpEN0pLsnfNLoLfpw1D_nLSyZwi3ggSaDq07o4ZEAI,11
17
+ igbot_base-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (78.1.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ igbot_base