From 5c114ad82cee418964bcbcfe6122fc2e91d530aa Mon Sep 17 00:00:00 2001 From: saloui Date: Thu, 25 May 2023 12:51:31 +0200 Subject: [PATCH] upgraded --- api/backend.py | 1 + backends/backend_template/.gitignore | 1 + backends/backend_template/__init__.py | 67 ++++++++++----------------- backends/backend_template/install.py | 7 +-- backends/backend_template/models.yaml | 59 +++-------------------- 5 files changed, 37 insertions(+), 98 deletions(-) create mode 100644 backends/backend_template/.gitignore diff --git a/api/backend.py b/api/backend.py index e765f8b4..43e4d19c 100644 --- a/api/backend.py +++ b/api/backend.py @@ -73,6 +73,7 @@ class LLMBackend: """ models_dir = Path('./models')/config["backend"] # replace with the actual path to the models folder return [f.name for f in models_dir.glob(LLMBackend.file_extension)] + @staticmethod def get_available_models(): # Create the file path relative to the child class's directory diff --git a/backends/backend_template/.gitignore b/backends/backend_template/.gitignore new file mode 100644 index 00000000..5a1d8308 --- /dev/null +++ b/backends/backend_template/.gitignore @@ -0,0 +1 @@ +config_local.yaml \ No newline at end of file diff --git a/backends/backend_template/__init__.py b/backends/backend_template/__init__.py index 657e9ccd..da7ffbd3 100644 --- a/backends/backend_template/__init__.py +++ b/backends/backend_template/__init__.py @@ -17,16 +17,21 @@ from typing import Callable from api.backend import LLMBackend import yaml from ctransformers import AutoModelForCausalLM +from api.config import load_config +import re __author__ = "parisneo" __github__ = "https://github.com/nomic-ai/gpt4all-ui" __copyright__ = "Copyright 2023, " __license__ = "Apache 2.0" -backend_name = "GPTJ" +backend_name = "CustomBackend" -class GPTJ(LLMBackend): - file_extension='*.bin' +class CustomBackend(LLMBackend): + # Define what is the extension of the model files supported by your backend + # Only applicable for local models for remote models like gpt4 and others, you can keep it empty + # and reimplement your own list_models method + file_extension='*.bin' def __init__(self, config:dict) -> None: """Builds a LLAMACPP backend @@ -34,33 +39,19 @@ class GPTJ(LLMBackend): config (dict): The configuration file """ super().__init__(config, False) - if 'gpt2' in self.config['model']: - model_type='gpt2' - elif 'gptj' in self.config['model']: - model_type='gptj' - elif 'gpt_neox' in self.config['model']: - model_type='gpt_neox' - elif 'dolly-v2' in self.config['model']: - model_type='dolly-v2' - elif 'starcoder' in self.config['model']: - model_type='starcoder' - elif 'llama' in self.config['model'] or 'wizardLM' in self.config['model']: - model_type='llama' - elif 'mpt' in self.config['model']: - model_type='mpt' - else: - print("The model you are using is not supported by this backend") - return - - if self.config["use_avx2"]: - self.model = AutoModelForCausalLM.from_pretrained( - f"./models/c_transformers/{self.config['model']}", model_type=model_type - ) + # The local config can be used to store personal information that shouldn't be shared like chatgpt Key + # or other personal information + # This file is never commited to the repository as it is ignored by .gitignore + self._local_config_file_path = Path(__file__).parent/"config_local.yaml" + if self._local_config_file_path.exists: + self.config = load_config(self._local_config_file_path) else: - self.model = AutoModelForCausalLM.from_pretrained( - f"./models/c_transformers/{self.config['model']}", model_type=model_type, lib = "avx" - ) + self.config = { + #Put your default configurations here + } + + # Do your initialization stuff def tokenize(self, prompt): """ @@ -105,19 +96,11 @@ class GPTJ(LLMBackend): self.model.reset() tokens = self.model.tokenize(prompt) count = 0 - for tok in self.model.generate( - tokens, - top_k=self.config['top_k'], - top_p=self.config['top_p'], - temperature=self.config['temperature'], - repetition_penalty=self.config['repeat_penalty'], - seed=self.config['seed'], - batch_size=1, - threads = self.config['n_threads'], - reset=True, - ): - - + generated_text = """ +This is an empty backend that shows how you can build your own backend. +Find it in backends +""" + for tok in re.split(r' |\n', generated_text): if count >= n_predict or self.model.is_eos_token(tok): break word = self.model.detokenize(tok) @@ -126,8 +109,6 @@ class GPTJ(LLMBackend): break output += word count += 1 - - except Exception as ex: print(ex) return output diff --git a/backends/backend_template/install.py b/backends/backend_template/install.py index d19128d6..bc8af3bd 100644 --- a/backends/backend_template/install.py +++ b/backends/backend_template/install.py @@ -13,6 +13,7 @@ class Install: print("-------------- Template backend -------------------------------") print("This is the first time you are using this backend.") print("Installing ...") + # Example of installing py torche """ try: print("Checking pytorch") @@ -31,11 +32,11 @@ class Install: requirements_file = current_dir / "requirements.txt" subprocess.run(["pip", "install", "--upgrade", "--no-cache-dir", "-r", str(requirements_file)]) - # Create ther models folder - models_folder = Path("./models/c_transformers") + # Create the models folder + models_folder = Path(f"./models/{Path(__file__).parent.stem}") models_folder.mkdir(exist_ok=True, parents=True) - #Create the install file + #Create the install file (a file that is used to insure the installation was done correctly) with open(install_file,"w") as f: f.write("ok") print("Installed successfully") diff --git a/backends/backend_template/models.yaml b/backends/backend_template/models.yaml index 68fd7335..83c4a9d9 100644 --- a/backends/backend_template/models.yaml +++ b/backends/backend_template/models.yaml @@ -1,52 +1,7 @@ -- LLAMA: 'true' - description: GGML format model files for the original LLaMa - filename: llama-7b.ggmlv3.q4_0.bin - license: Non commercial - owner_link: https://huggingface.co/TheBloke/ - owner: TheBloke - server: https://huggingface.co/TheBloke/LLaMa-7B-GGML/resolve/main/ - sha256: ec2f2d1f0dfb73b72a4cbac7fa121abbe04c37ab327125a38248f930c0f09ddf -- LLAMA: 'true' - description: GGML format model files for Wizard LM 7B model - filename: wizardLM-7B.ggmlv3.q4_0.bin - license: Non commercial - owner_link: https://huggingface.co/TheBloke/ - owner: TheBloke - server: https://huggingface.co/TheBloke/wizardLM-7B-GGML/resolve/main/ - sha256: ea35e30a7c140485b856d0919284ce59e4ca47c1b8af037ea8b7ba05ef291c43 -- LLAMA: 'true' - description: GGML format model files for Wizard LM 7B model - filename: koala-7b.ggml.unquantized.pr613.bin - license: Non commercial - owner_link: https://huggingface.co/TheBloke/ - owner: TheBloke - server: https://huggingface.co/TheBloke/koala-7b-ggml-unquantized/resolve/main/ - sha256: c478ceced3b38800cb768225b1e759a32c9e89bd33606fb38eeff3b811e28371 - - - - -- MPT-7B: 'true' - description: MPT-7B - filename: mpt-7b.ggmlv3.q5_1.bin - license: Apache-2.0 - owner_link: https://huggingface.co/TheBloke/ - owner: TheBloke - server: https://huggingface.co/TheBloke/MPT-7B-GGML/resolve/main/ - sha256: c947c38405921a199c603fed2ed63486811777ba370bb51c40c3132e5cfe84e5 -- MPT-7B-Instruct: 'true' - description: MPT-7B-Instruct - filename: mpt-7b-instruct.ggmlv3.q5_1.bin - license: Apache-2.0 - owner_link: https://huggingface.co/TheBloke/ - owner: TheBloke - server: https://huggingface.co/TheBloke/MPT-7B-Instruct-GGML/resolve/main/ - sha256: a4d17a39ac277d48a3d55aa74b36a4e6e1b891b58937a838243fad549d26c686 -- MPT-7B-Storywriter: 'true' - description: MPT-7B-Storywriter - filename: mpt-7b-storywriter.ggmlv3.q5_1.bin - license: Apache-2.0 - owner_link: https://huggingface.co/TheBloke/ - owner: TheBloke - server: https://huggingface.co/TheBloke/MPT-7B-Storywriter-GGML/resolve/main/ - sha256: 3b7dd7aa7508cc8cb4e262fe4b93214826f38d18d04059075e05837457f5402 \ No newline at end of file +- description: The description of the model + filename: the file to be loaded + license: The licence + owner_link: https://link_to_the_owner_web_page + owner: Owner_name + server: https://Path_to_the_server_to_load_from + sha256: The Hash code of the file \ No newline at end of file