Added extensions

This commit is contained in:
Saifeddine ALOUI 2023-10-03 01:10:58 +02:00
parent 189fb75139
commit 10fbef35da
4 changed files with 106 additions and 11 deletions

View File

@ -3,13 +3,14 @@ from lollms.helpers import ASCIIColors
from lollms.paths import LollmsPaths
from lollms.personality import PersonalityBuilder
from lollms.binding import LLMBinding, BindingBuilder, ModelBuilder
from lollms.extension import LOLLMSExtension, ExtensionBuilder
from lollms.config import InstallOption
from lollms.helpers import trace_exception
from lollms.terminal import MainMenu
from typing import Callable
import subprocess
import importlib
class LollmsApplication:
def __init__(
self,
@ -33,6 +34,8 @@ class LollmsApplication:
self.mounted_personalities = []
self.personality = None
self.mounted_extensions = []
self.binding=None
self.model=None
@ -91,6 +94,7 @@ class LollmsApplication:
self.mount_personalities()
self.mount_extensions()
def load_binding(self):
try:
@ -125,6 +129,17 @@ class LollmsApplication:
return model
def mount_extension(self, id:int, callback=None):
try:
extension = ExtensionBuilder(self.lollms_paths, self.config, self.model, self, callback=callback).build_extension(self.config["extensions"][id], self.lollms_paths, self)
self.mounted_extensions.append(extension)
return extension
except Exception as ex:
ASCIIColors.error(f"Couldn't load extension. Please verify your configuration file at {self.lollms_paths.personal_configuration_path} or use the next menu to select a valid personality")
trace_exception(ex)
return None
def mount_personality(self, id:int, callback=None):
try:
personality = PersonalityBuilder(self.lollms_paths, self.config, self.model, self, callback=callback).build_personality(id)
@ -167,10 +182,32 @@ class LollmsApplication:
self.mount_personality(0, callback = None)
self.config.active_personality_id = 0
self.personality = self.mounted_personalities[self.config.active_personality_id]
def mount_extensions(self, callback = None):
self.mounted_extensions = []
to_remove = []
for i in range(len(self.config["extensions"])):
p = self.mount_extension(i, callback = None)
if p is None:
to_remove.append(i)
to_remove.sort(reverse=True)
for i in to_remove:
self.unmount_extension(i)
def set_personalities_callbacks(self, callback: Callable[[str, int, dict], bool]=None):
for personality in self.mount_personalities:
personality.setCallback(callback)
def unmount_extension(self, id:int)->bool:
if id<len(self.config.extensions):
del self.config.extensions[id]
del self.mounted_extensions[id]
self.config.save_config()
return True
else:
return False
def unmount_personality(self, id:int)->bool:
if id<len(self.config.personalities):

View File

@ -4,8 +4,11 @@
# This is the main class to be imported by the extension
# it gives your code access to the model, the callback functions, the model conditionning etc
from lollms.helpers import ASCIIColors
from lollms.config import BaseConfig
from lollms.config import InstallOption, TypedConfig, BaseConfig, ConfigTemplate
from lollms.paths import LollmsPaths
from enum import Enum
from pathlib import Path
import importlib
__author__ = "parisneo"
@ -18,23 +21,78 @@ class EXTENSION_TYPE(Enum):
EXTENSION_TYPE_STAND_ALONE = 0 # An extension that has access to the current personality and model but has an independant
class Extension():
def __init__(self, metadata_file_path:str, app) -> None:
class LOLLMSExtension():
def __init__(self, name:str, script_path:str|Path, config:TypedConfig, app) -> None:
self.name = name
self.app = app
self.metadata_file_path = metadata_file_path
self.config = BaseConfig(file_path=metadata_file_path)
self.config.load_config()
self.config = config
self.script_path = script_path
self.card = self.script_path /"card.yaml"
self.configuration_path = app.lollms_paths.personal_configuration_path/"extensions"/f"{name}"
self.configuration_path.mkdir(parents=True, exist_ok=True)
self.configuration_path= self.configuration_path/"config.yaml"
def build_extension(self):
return self
def install(self):
"""
Installation procedure (to be implemented)
"""
ASCIIColors.blue("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
ASCIIColors.red(f"Installing {self.binding_folder_name}")
ASCIIColors.red(f"Installing {self.name}")
ASCIIColors.blue("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
def pre_gen(self, previous_prompt:str, prompt:str):
return previous_prompt, prompt
def in_gen(self, chunk:str)->str:
return chunk
def post_gen(self, ai_output:str):
pass
def get_ui():
"""
Get user interface of the extension
"""
return "<p>This is a ui extension template</p>"
return "<p>This is a ui extension template</p>"
class ExtensionBuilder:
def build_extension(
self,
extension_path:str,
lollms_paths:LollmsPaths,
app
)->LOLLMSExtension:
extension, script_path = self.getBinding(extension_path, lollms_paths, app)
return extension(
extension_path.split[1],
script_path,
lollms_paths=lollms_paths,
app = app
)
def getExtension(
self,
extension_path:str,
lollms_paths:LollmsPaths,
app
)->LOLLMSExtension:
extension_path = lollms_paths.bindings_zoo_path / extension_path
# define the full absolute path to the module
absolute_path = extension_path.resolve()
# infer the module name from the file path
module_name = extension_path.stem
# use importlib to load the module from the file path
loader = importlib.machinery.SourceFileLoader(module_name, str(absolute_path / "__init__.py"))
extension_module = loader.load_module()
extension:LOLLMSExtension = getattr(extension_module, extension_module.extension_name)
return extension, absolute_path

View File

@ -51,7 +51,7 @@ class LollmsPaths:
self.bindings_zoo_path = self.personal_path / "bindings_zoo"
self.personalities_zoo_path = self.personal_path / "personalities_zoo"
self.extensions_zoo_path = self.personal_path / "extensions_zoo_path"
self.extensions_zoo_path = self.personal_path / "extensions_zoo"
ASCIIColors.green("----------------------Paths information-----------------------")
ASCIIColors.yellow("personal_path:",end="")

View File

@ -26,7 +26,7 @@ def get_all_files(path):
setuptools.setup(
name="lollms",
version="5.5.6",
version="5.6.1",
author="Saifeddine ALOUI",
author_email="aloui.saifeddine@gmail.com",
description="A python library for AI personality definition",