From 527a3e029f1e60e4b00f5b81434eab1f87b97d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=20Vinh=20LUONG=20=28L=C6=AF=C6=A0NG=20Th=E1=BA=BF=20Vi?= =?UTF-8?q?nh=29?= Date: Thu, 19 Sep 2024 23:14:48 -0700 Subject: [PATCH] remove openssa.deprecated.slm submodule --- openssa/deprecated/slm/abstract_slm.py | 38 ------ openssa/deprecated/slm/base_slm.py | 109 ------------------ .../deprecated/slm/memory/conversation_db.py | 24 ---- .../slm/memory/sqlite_conversation_db.py | 47 -------- 4 files changed, 218 deletions(-) delete mode 100644 openssa/deprecated/slm/abstract_slm.py delete mode 100644 openssa/deprecated/slm/base_slm.py delete mode 100644 openssa/deprecated/slm/memory/conversation_db.py delete mode 100644 openssa/deprecated/slm/memory/sqlite_conversation_db.py diff --git a/openssa/deprecated/slm/abstract_slm.py b/openssa/deprecated/slm/abstract_slm.py deleted file mode 100644 index b8011865f..000000000 --- a/openssa/deprecated/slm/abstract_slm.py +++ /dev/null @@ -1,38 +0,0 @@ -from abc import ABC, abstractmethod -from dataclasses import dataclass - -from openssa.deprecated.adapter.abstract_adapter import AbstractAdapter - - -@dataclass -class AbstractSLM(ABC): - """ - The AbstractSLM serves as the base for all concrete Small Language Models - (SLMs). It provides an interface for natural language communication and - structured API interactions. - """ - - @property - @abstractmethod - def adapter(self) -> AbstractAdapter: - """Returns our adapter""" - - @adapter.setter - @abstractmethod - def adapter(self, adapter: AbstractAdapter): - """Sets our adapter""" - - @abstractmethod - def do_discuss(self, user_input: list[dict], conversation: list[dict]) -> dict: - """ - Processes a natural language conversation input - and returns a dict of the reply. Not intended for direct use. - """ - - @abstractmethod - def save(self, storage_dir: str): - """Saves to the specified directory.""" - - @abstractmethod - def load(self, storage_dir: str): - """Loads from the specified directory.""" diff --git a/openssa/deprecated/slm/base_slm.py b/openssa/deprecated/slm/base_slm.py deleted file mode 100644 index 98c0eacd7..000000000 --- a/openssa/deprecated/slm/base_slm.py +++ /dev/null @@ -1,109 +0,0 @@ -import json - -from openssa.deprecated.adapter.abstract_adapter import AbstractAdapter -from openssa.deprecated.adapter.base_adapter import BaseAdapter -from openssa.deprecated.prompts import Prompts -from openssa.deprecated.slm.abstract_slm import AbstractSLM -from openssa.deprecated.utils.utils import Utils -from openssa.deprecated.utils.logs import Logs - - -class BaseSLM(AbstractSLM): - def __init__(self, adapter: AbstractAdapter = None): - """ - self.conversations is initialized as a dictionary of conversations, - where each conversation is a list of user inputs and model replies. - """ - self._adapter = adapter - - @property - def adapter(self) -> AbstractAdapter: - """ - Return the previous assigned Adapter, - or a default Adapter if none was assigned. - """ - if self._adapter is None: - self._adapter = BaseAdapter() - return self._adapter - - @adapter.setter - def adapter(self, adapter: AbstractAdapter): - self._adapter = adapter - - def do_discuss(self, user_input: list[dict], conversation: list[dict]) -> dict: - """ - Add the user_input to the conversation, sends the whole conversation - to the language model, and returns the reply. - """ - conversation.extend(user_input) - result = self._call_lm_api(conversation) - conversation.pop() - return result - - # pylint: disable=unused-argument - def _call_lm_api(self, conversation: list[dict]) -> dict: - """ - Send conversation to the language model’s API - and return the reply. Should be overridden by subclasses. - """ - return {"role": "assistant", "content": "Hello, as the base implementation of SLM, this is all I can say."} - - # - # Helper functions for GPT-like completion models - # - @Logs.do_log_entry_and_exit() - def _make_completion_prompt(self, conversation: list[dict]) -> str: - prompt = Prompts() - content = prompt.make_prompt(__name__, "completion") - print(f"Content .... {content}") - system = {'role': 'system', 'content': content} - return str([system] + conversation) - - def _parse_llm_response(self, response) -> dict: - response = response.strip() - - if response.startswith('{') and not response.endswith('}'): - response += '}' - - if response.endswith('}') and not response.startswith('{'): - response += '{' - - if '{' not in response: - response = json.dumps({"role": "assistant", "content": response}) - - parsed_data = [] - start_indices = [i for i, c in enumerate(response) if c == '{'] - - for start in start_indices: - for end in range(start + 2, len(response) + 1): - try: - json_str = response[start:end] - data = json.loads(json_str) - if isinstance(data, dict): - parsed_data.append(data) - break - except json.JSONDecodeError: - continue - - return parsed_data[0] - - def save(self, storage_dir: str): - """Saves to the specified directory.""" - pass - - def load(self, storage_dir: str): - """Loads from the specified directory.""" - pass - - -class PassthroughSLM(BaseSLM): - """ - The PassthroughSLM is a barebones SLM that simply passes - all queries to the adapter. - """ - @Utils.do_canonicalize_user_input_and_discuss_result('user_input') - def do_discuss(self, user_input: list[dict], conversation: list[dict]) -> dict: - """ - Pass through user input to the adapter and return the replies - """ - return self.adapter.query_all(user_input, conversation) diff --git a/openssa/deprecated/slm/memory/conversation_db.py b/openssa/deprecated/slm/memory/conversation_db.py deleted file mode 100644 index 4f8b26162..000000000 --- a/openssa/deprecated/slm/memory/conversation_db.py +++ /dev/null @@ -1,24 +0,0 @@ -from abc import ABC, abstractmethod - - -# pylint disable=wduplicate-code -class ConversationDB(ABC): - @abstractmethod - def connect(self): - pass - - @abstractmethod - def create_table(self): - pass - - @abstractmethod - def append_conversation(self, conversation_id, user_input): - pass - - @abstractmethod - def get_conversation(self, conversation_id): - pass - - @abstractmethod - def close(self): - pass diff --git a/openssa/deprecated/slm/memory/sqlite_conversation_db.py b/openssa/deprecated/slm/memory/sqlite_conversation_db.py deleted file mode 100644 index e8ef8e891..000000000 --- a/openssa/deprecated/slm/memory/sqlite_conversation_db.py +++ /dev/null @@ -1,47 +0,0 @@ -import sqlite3 - -from openssa.deprecated.slm.memory.conversation_db import ConversationDB - - -class SQLiteConversationDB(ConversationDB): - def __init__(self, db_name): - self.db_name = db_name - self.conversation_db = None - self.cursor = None - - def connect(self): - self.conversation_db = sqlite3.connect(self.db_name) - self.cursor = self.conversation_db.cursor() - - def create_table(self): - self.cursor.execute('''CREATE TABLE IF NOT EXISTS conversations - (id text PRIMARY KEY, history text)''') - self.conversation_db.commit() - - def append_conversation(self, conversation_id, user_input): - self.cursor.execute( - 'SELECT * FROM conversations WHERE id=?', - (conversation_id,)) - conversation = self.cursor.fetchone() - if conversation is None: - self.cursor.execute( - "INSERT INTO conversations VALUES (?,?)", - (conversation_id, user_input)) - else: - updated_conversation = conversation[1] + "\n" + user_input - self.cursor.execute( - "UPDATE conversations SET history = ? WHERE id = ?", - (updated_conversation, conversation_id)) - self.conversation_db.commit() - - def get_conversation(self, conversation_id): - self.cursor.execute( - "SELECT * FROM conversations WHERE id=?", - (conversation_id,)) - conversation = self.cursor.fetchone() - if conversation is not None: - return conversation[1] - return None - - def close(self): - self.conversation_db.close()