2024-08-19 12:54:13 -05:00
|
|
|
import importlib
|
2024-08-19 11:34:31 -05:00
|
|
|
import os
|
|
|
|
|
import json
|
2024-08-19 12:54:13 -05:00
|
|
|
import inspect
|
2025-06-02 14:55:45 -05:00
|
|
|
import logging
|
2024-08-19 11:34:31 -05:00
|
|
|
from abc import ABC, abstractmethod
|
2024-08-19 12:54:13 -05:00
|
|
|
from tools.base_tool import BaseTool
|
2024-08-19 11:34:31 -05:00
|
|
|
|
|
|
|
|
class BaseTelegramInferenceBot(ABC):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.conversation_history = {}
|
|
|
|
|
self.processing_status = {}
|
|
|
|
|
self.system_prompt = self.load_system_prompt()
|
2024-08-19 12:54:13 -05:00
|
|
|
self.tools, self.functions = self.load_functions()
|
2025-06-02 14:55:45 -05:00
|
|
|
logging.info(f'System Prompt: {os.environ.get("SYSTEM_PROMPT_PATH")}')
|
|
|
|
|
logging.info(f'Github Repository: {os.environ.get("GITHUB_REPOSITORY")}')
|
2024-08-19 11:34:31 -05:00
|
|
|
|
2025-06-02 14:55:45 -05:00
|
|
|
def load_system_prompt(self):
|
2024-08-21 08:09:47 -05:00
|
|
|
system_prompt_path = os.getenv("SYSTEM_PROMPT_PATH")
|
|
|
|
|
if system_prompt_path and os.path.isfile(system_prompt_path):
|
2025-06-02 14:55:45 -05:00
|
|
|
try:
|
|
|
|
|
with open(system_prompt_path, "r", encoding="utf-8") as file:
|
|
|
|
|
return file.read().strip()
|
|
|
|
|
except IOError as e:
|
|
|
|
|
logging.warning(f"Could not read system prompt file {system_prompt_path}: {e}")
|
|
|
|
|
return "You are a helpful AI assistant."
|
2024-08-21 08:09:47 -05:00
|
|
|
else:
|
2025-06-02 14:55:45 -05:00
|
|
|
logging.warning("SYSTEM_PROMPT_PATH is not set or file does not exist. Using default system prompt.")
|
|
|
|
|
return "You are a helpful AI assistant."
|
2024-08-19 11:34:31 -05:00
|
|
|
|
2025-06-02 14:55:45 -05:00
|
|
|
def load_functions(self):
|
2024-08-19 12:54:13 -05:00
|
|
|
tools = []
|
2025-06-02 14:55:45 -05:00
|
|
|
functions = []
|
2024-08-19 12:54:13 -05:00
|
|
|
tools_dir = os.path.join(os.path.dirname(__file__), 'tools')
|
2025-06-02 14:55:45 -05:00
|
|
|
if not os.path.exists(tools_dir):
|
|
|
|
|
logging.warning(f"Tools directory not found: {tools_dir}")
|
|
|
|
|
return [], []
|
|
|
|
|
|
2024-08-19 12:54:13 -05:00
|
|
|
for filename in os.listdir(tools_dir):
|
|
|
|
|
if filename.endswith('.py') and filename != '__init__.py' and filename != 'base_tool.py':
|
|
|
|
|
module_name = f'tools.{filename[:-3]}'
|
2025-06-02 14:55:45 -05:00
|
|
|
try:
|
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
|
for name, obj in inspect.getmembers(module):
|
|
|
|
|
if inspect.isclass(obj) and issubclass(obj, BaseTool) and obj != BaseTool:
|
|
|
|
|
try:
|
|
|
|
|
tools.append(obj())
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(f"Error instantiating tool {name} from {filename}: {e}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(f"Error importing module {module_name}: {e}")
|
2024-08-19 12:54:13 -05:00
|
|
|
|
|
|
|
|
for tool in tools:
|
|
|
|
|
functions.extend(tool.get_functions())
|
|
|
|
|
return tools, functions
|
2024-08-19 11:34:31 -05:00
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def get_chat_response(self, messages):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
async def handle_message(self, user_id, user_message):
|
|
|
|
|
pass
|
|
|
|
|
|
2025-06-02 14:55:45 -05:00
|
|
|
def clear_conversation_history(self, user_id):
|
2024-08-19 11:34:31 -05:00
|
|
|
if user_id in self.conversation_history:
|
|
|
|
|
del self.conversation_history[user_id]
|
2024-08-19 13:38:39 -05:00
|
|
|
|
|
|
|
|
for tool in self.tools:
|
|
|
|
|
tool.clear()
|
2024-08-19 11:34:31 -05:00
|
|
|
|
2025-06-02 14:55:45 -05:00
|
|
|
def set_processing_status(self, user_id: int, message_id: int):
|
|
|
|
|
self.processing_status[user_id] = {"processing": True, "message_id": message_id}
|
|
|
|
|
|
|
|
|
|
def clear_processing_status(self, user_id: int):
|
|
|
|
|
if user_id in self.processing_status:
|
|
|
|
|
del self.processing_status[user_id]
|
|
|
|
|
|
2024-08-19 12:54:13 -05:00
|
|
|
def call_tool(self, function_call_name, function_call_arguments):
|
|
|
|
|
function_name = function_call_name
|
2025-06-02 14:55:45 -05:00
|
|
|
try:
|
|
|
|
|
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}")
|
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
|
logging.error(f"Error decoding function call arguments for {function_call_name}: {e}. Arguments: {function_call_arguments}")
|
|
|
|
|
return f"Error: Malformed arguments for tool call: {e}"
|
|
|
|
|
|
2024-08-19 11:34:31 -05:00
|
|
|
for tool in self.tools:
|
2025-06-01 11:50:12 -05:00
|
|
|
for function in tool.get_functions():
|
|
|
|
|
if function["function"]["name"] == function_name:
|
2025-06-02 14:55:45 -05:00
|
|
|
try:
|
|
|
|
|
return tool.execute(function_name, **function_args)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(f"Error executing tool {function_name} with args {function_args}: {e}")
|
|
|
|
|
return f"Error executing tool {function_name}: {e}"
|
|
|
|
|
logging.warning(f"Tool function {function_name} not found.")
|
|
|
|
|
return f"Error: Tool function {function_name} not found."
|
2025-06-02 14:30:08 -05:00
|
|
|
|
|
|
|
|
def get_system_prompt_description(self) -> str:
|
|
|
|
|
"""Returns a description of the system prompt being used."""
|
2025-06-02 14:55:45 -05:00
|
|
|
return f"System Prompt: {'Custom' if os.getenv('SYSTEM_PROMPT_PATH') else 'Default'}"
|
2024-08-19 11:34:31 -05:00
|
|
|
|
|
|
|
|
@abstractmethod
|
2025-06-02 14:30:08 -05:00
|
|
|
def get_llm_description(self) -> str:
|
|
|
|
|
"""Returns a description of the LLM being used."""
|
2024-08-19 11:34:31 -05:00
|
|
|
pass
|
|
|
|
|
|
2025-06-02 14:55:45 -05:00
|
|
|
async def get_bot_status(self) -> str:
|
2025-06-02 14:30:08 -05:00
|
|
|
"""Provides a status message including prompt and LLM information."""
|
|
|
|
|
prompt_desc = self.get_system_prompt_description()
|
|
|
|
|
llm_desc = self.get_llm_description()
|
|
|
|
|
return f"{prompt_desc}\n{llm_desc}"
|
|
|
|
|
|
2024-08-19 11:34:31 -05:00
|
|
|
@abstractmethod
|
2025-06-02 14:30:08 -05:00
|
|
|
async def start(self):
|
2024-08-19 11:34:31 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
2025-06-02 14:55:45 -05:00
|
|
|
async def abort_processing(self, user_id):
|
2024-08-19 11:34:31 -05:00
|
|
|
pass
|
2025-06-02 14:55:45 -05:00
|
|
|
|
2024-08-19 11:34:31 -05:00
|
|
|
@abstractmethod
|
2025-06-02 14:55:45 -05:00
|
|
|
async def switch_model(self):
|
|
|
|
|
"""Switches the underlying model if supported by the bot."""
|
2025-06-02 14:30:08 -05:00
|
|
|
pass
|