diff --git a/doc/server_endpoints.md b/doc/server_endpoints.md index 245fd86..31e04de 100644 --- a/doc/server_endpoints.md +++ b/doc/server_endpoints.md @@ -155,7 +155,7 @@ Actions: - Creates a deep copy of the `self.config` dictionary and assigns it to `self.cp_config` variable. - Updates the `"binding_name"` value in `self.cp_config` with the selected binding name obtained from `data['binding_name']`. - Attempts to build a binding instance using the `self.bindings_path` and `self.cp_config`. -- If successful, updates `self.binding_class` with the created binding instance and updates `self.config` with `self.cp_config`. +- If successful, updates `self.binding` with the created binding instance and updates `self.config` with `self.cp_config`. - Sends a response to the client indicating the success of the binding selection along with the selected binding name. - If an exception occurs during the binding creation process, the exception is printed and a response is sent to the client indicating the failure of the binding selection along with the selected binding name and the error message. @@ -174,12 +174,12 @@ Events generated: - `data['model_name']` (string): The name of the model to select. - Actions: - Extracts the model name from the request data. - - Checks if a binding class is available (`self.binding_class`). + - Checks if a binding class is available (`self.binding`). - If no binding class is available, emits a `'select_model'` event with a failure response, indicating that a binding needs to be selected first. - Returns and exits the function. - Creates a deep copy of the configuration (`self.config`) and assigns it to `self.cp_config`. - Sets the `"model_name"` property of `self.cp_config` to the selected model name. - - Tries to create an instance of the binding class (`self.binding_class`) with `self.cp_config`. + - Tries to create an instance of the binding class (`self.binding`) with `self.cp_config`. - If successful, assigns the created binding instance to `self.current_model`. - Emits a `'select_model'` event with a success response, indicating that the model selection was successful. - Returns and exits the function. diff --git a/examples/document_questions/document_questions.py b/examples/document_questions/document_questions.py index 1046caa..8c0c0bf 100644 --- a/examples/document_questions/document_questions.py +++ b/examples/document_questions/document_questions.py @@ -93,6 +93,7 @@ def test_generate_text(host, port, lollms_paths:LollmsPaths): outputs=lollms_paths.personal_data_path/"outputs.txt" if not docs.exists(): + ASCIIColors.error(f"Documents file {docs} does not exist") sys.exit(0) if not questions_path.exists(): @@ -126,7 +127,7 @@ def test_generate_text(host, port, lollms_paths:LollmsPaths): print(f"Question:{question}") useful_chunks=[] for chunk in file_chunks: - prompt="Document:\n"+chunk+"\n"+question + prompt="Read text chunk, then answer question by yes or no\nText Chunk:\n"+chunk+"\nQ: "+question+"\nA:" # Trigger the 'generate_text' event with the prompt infos["is_ready"]=False print(f"Sending prompt:{prompt}") @@ -148,8 +149,6 @@ def test_generate_text(host, port, lollms_paths:LollmsPaths): @sio.event def text_generated(data): - print("text_generated_ok") - print(data["text"]) infos["answer"]=data["text"] infos["is_ready"]=True diff --git a/lollms/__init__.py b/lollms/__init__.py index 72ab4be..48174ba 100644 --- a/lollms/__init__.py +++ b/lollms/__init__.py @@ -54,18 +54,18 @@ class BindingBuilder: # use importlib to load the module from the file path loader = importlib.machinery.SourceFileLoader(module_name, str(absolute_path / "__init__.py")) binding_module = loader.load_module() - binding_class = getattr(binding_module, binding_module.binding_name) - return binding_class + binding = getattr(binding_module, binding_module.binding_name) + return binding class ModelBuilder: - def __init__(self, binding_class:LLMBinding, config:LOLLMSConfig): - self.binding_class = binding_class + def __init__(self, binding:LLMBinding, config:LOLLMSConfig): + self.binding = binding self.model = None self.build_model(config) def build_model(self, cfg: LOLLMSConfig): - self.model = self.binding_class(cfg) + self.model = self.binding(cfg) def get_model(self): return self.model diff --git a/lollms/console.py b/lollms/console.py index 8387ddd..5ad57ac 100644 --- a/lollms/console.py +++ b/lollms/console.py @@ -106,8 +106,8 @@ class MainMenu: if 1 <= choice <= len(models_list)-3: print(f"You selected model: {ASCIIColors.color_green}{models_list[choice - 1]}{ASCIIColors.color_reset}") self.conversation.config['model_name']=models_list[choice - 1] - self.conversation.load_model() self.conversation.config.save_config() + self.conversation.load_model() elif choice <= len(models_list)-2: self.install_model() elif choice <= len(models_list)-1: @@ -129,7 +129,7 @@ class MainMenu: # Usage example with tqdm(total=100, unit="%", desc="Download Progress", ncols=80) as tqdm_bar: - self.conversation.config.download_model(url,self.conversation.binding_class, progress_callback) + self.conversation.config.download_model(url,self.conversation.binding, progress_callback) self.select_model() elif choice <= len(models_list)-1: path = Path(input("Give a path to the model to be used on your PC:")) @@ -185,7 +185,7 @@ class MainMenu: def reinstall_binding(self): conversation = self.conversation try: - conversation.binding_class = BindingBuilder().build_binding(conversation.lollms_paths.bindings_zoo_path, conversation.config, force_reinstall=True) + conversation.binding = BindingBuilder().build_binding(conversation.lollms_paths.bindings_zoo_path, conversation.config, force_reinstall=True) except Exception as ex: print(ex) print(f"Couldn't find binding. Please verify your configuration file at {conversation.config.file_path} or use the next menu to select a valid binding") @@ -377,17 +377,17 @@ Participating personalities: # cfg.download_model(url) else: try: - self.binding_class = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config) + self.binding = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config) except Exception as ex: print(ex) print(f"Couldn't find binding. Please verify your configuration file at {self.configuration_path} or use the next menu to select a valid binding") print(f"Trying to reinstall binding") - self.binding_class = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config,force_reinstall=True) + self.binding = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config,force_reinstall=True) self.menu.select_binding() def load_model(self): try: - self.model = ModelBuilder(self.binding_class, self.config).get_model() + self.model = ModelBuilder(self.binding, self.config).get_model() except Exception as ex: ASCIIColors.error(f"Couldn't load model. Please verify your configuration file at {self.configuration_path} or use the next menu to select a valid model") ASCIIColors.error(f"Binding returned this exception : {ex}") diff --git a/lollms/server.py b/lollms/server.py index b6da78e..10edc4e 100644 --- a/lollms/server.py +++ b/lollms/server.py @@ -91,12 +91,12 @@ class LoLLMsServer: if self.config.binding_name is None: self.menu.select_binding() else: - self.binding_class = self.build_binding(self.bindings_path, self.config) + self.binding = self.build_binding(self.bindings_path, self.config) if self.config.model_name is None: self.menu.select_model() else: try: - self.current_model = self.binding_class(self.config) + self.current_model = self.binding(self.config) except Exception as ex: print(f"{ASCIIColors.color_red}Couldn't load model Please select a valid model{ASCIIColors.color_reset}") print(f"{ASCIIColors.color_red}{ex}{ASCIIColors.color_reset}") @@ -137,17 +137,17 @@ class LoLLMsServer: # cfg.download_model(url) else: try: - self.binding_class = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config) + self.binding = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config) except Exception as ex: print(ex) print(f"Couldn't find binding. Please verify your configuration file at {self.config.file_path} or use the next menu to select a valid binding") print(f"Trying to reinstall binding") - self.binding_class = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config,force_reinstall=True) + self.binding = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config,force_reinstall=True) self.menu.select_binding() def load_model(self): try: - self.model = ModelBuilder(self.binding_class, self.config).get_model() + self.model = ModelBuilder(self.binding, self.config).get_model() except Exception as ex: ASCIIColors.error(f"Couldn't load model.") ASCIIColors.error(f"Binding returned this exception : {ex}") @@ -264,9 +264,9 @@ class LoLLMsServer: Returns: _type_: _description_ """ - if self.binding_class is None: + if self.binding is None: emit('available_models_list', {'success':False, 'error': "No binding selected"}, room=request.sid) - model_list = self.binding_class.get_available_models() + model_list = self.binding.get_available_models() models = [] for model in model_list: @@ -337,7 +337,7 @@ class LoLLMsServer: self.cp_config = copy.deepcopy(self.config) self.cp_config["binding_name"] = data['binding_name'] try: - self.binding_class = self.build_binding(self.bindings_path, self.cp_config) + self.binding = self.build_binding(self.bindings_path, self.cp_config) self.config = self.cp_config emit('select_binding', {'success':True, 'binding_name': self.cp_config["binding_name"]}, room=request.sid) except Exception as ex: @@ -347,13 +347,13 @@ class LoLLMsServer: @self.socketio.on('select_model') def handle_select_model(data): model_name = data['model_name'] - if self.binding_class is None: + if self.binding is None: emit('select_model', {'success':False, 'model_name': model_name, 'error':f"Please select a binding first"}, room=request.sid) return self.cp_config = copy.deepcopy(self.config) self.cp_config["model_name"] = data['model_name'] try: - self.current_model = self.binding_class(self.cp_config) + self.current_model = self.binding(self.cp_config) emit('select_model', {'success':True, 'model_name': model_name}, room=request.sid) except Exception as ex: print(ex) @@ -427,6 +427,15 @@ class LoLLMsServer: prompt = data['prompt'] personality_id = data['personality'] n_predicts = data["n_predicts"] + parameters = data.get("parameters",{ + "temperature":self.config["temperature"], + "top_k":self.config["top_k"], + "top_p":self.config["top_p"], + "repeat_penalty":self.config["repeat_penalty"], + "repeat_last_n":self.config["repeat_last_n"], + "seed":self.config["seed"] + }) + if personality_id==-1: # Raw text generation self.answer = {"full_text":""} @@ -449,7 +458,14 @@ class LoLLMsServer: fd = model.detokenize(tk[-min(self.config.ctx_size,n_tokens):]) ASCIIColors.print("warm up", ASCIIColors.color_bright_cyan) - generated_text = model.generate(fd, n_predict=n_predicts, callback=callback) + generated_text = model.generate(fd, n_predict=n_predicts, callback=callback, + temperature = parameters["temperature"], + top_k = parameters["top_k"], + top_p = parameters["top_p"], + repeat_penalty = parameters["repeat_penalty"], + repeat_last_n = parameters["repeat_last_n"], + seed = parameters["seed"] + ) ASCIIColors.success(f"\ndone") if client_id in self.clients: if not self.clients[client_id]["requested_stop"]: @@ -510,7 +526,10 @@ class LoLLMsServer: generated_text = personality.processor.run_workflow(prompt, previous_discussion_text=personality.personality_conditioning+fd, callback=callback) else: ASCIIColors.info("generating...", end="", flush=True) - generated_text = personality.model.generate(personality.personality_conditioning+fd, n_predict=personality.model_n_predicts, callback=callback) + generated_text = personality.model.generate( + personality.personality_conditioning+fd, + n_predict=personality.model_n_predicts, + callback=callback) if personality.processor is not None and personality.processor_cfg["process_model_output"]: generated_text = personality.processor.process_model_output(generated_text) @@ -545,8 +564,8 @@ class LoLLMsServer: # use importlib to load the module from the file path loader = importlib.machinery.SourceFileLoader(module_name, str(absolute_path / "__init__.py")) binding_module = loader.load_module() - binding_class = getattr(binding_module, binding_module.binding_name) - return binding_class + binding = getattr(binding_module, binding_module.binding_name) + return binding def run(self, host="localhost", port="9601"): diff --git a/lollms/settings.py b/lollms/settings.py index 34ac36a..a036e3a 100644 --- a/lollms/settings.py +++ b/lollms/settings.py @@ -147,17 +147,17 @@ Participating personalities: # cfg.download_model(url) else: try: - self.binding_class = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config) + self.binding = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config) except Exception as ex: print(ex) print(f"Couldn't find binding. Please verify your configuration file at {self.cfg_path} or use the next menu to select a valid binding") print(f"Trying to reinstall binding") - self.binding_class = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config,force_reinstall=True) + self.binding = BindingBuilder().build_binding(self.lollms_paths.bindings_zoo_path, self.config,force_reinstall=True) self.menu.select_binding() def load_model(self): try: - self.model = ModelBuilder(self.binding_class, self.config).get_model() + self.model = ModelBuilder(self.binding, self.config).get_model() except Exception as ex: ASCIIColors.error(f"Couldn't load model. Please verify your configuration file at {self.cfg_path} or use the next menu to select a valid model") ASCIIColors.error(f"Binding returned this exception : {ex}") diff --git a/setup.py b/setup.py index 412deba..8b073c7 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ def get_all_files(path): setuptools.setup( name="lollms", - version="1.2.9", + version="1.2.10", author="Saifeddine ALOUI", author_email="aloui.saifeddine@gmail.com", description="A python library for AI personality definition",