From 352e7045379c1ecae9d345470e14d719faaac9df Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Sun, 4 Aug 2024 01:16:40 +0200 Subject: [PATCH] =?UTF-8?q?Lagt=20till=20hantering=20av=20host=5Furl=20d?= =?UTF-8?q?=C3=A4r=20LLM:er=20k=C3=B6rs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smartassist/src/utils.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/smartassist/src/utils.py b/smartassist/src/utils.py index 2fb8fa0..e959950 100644 --- a/smartassist/src/utils.py +++ b/smartassist/src/utils.py @@ -40,10 +40,8 @@ def fetch_models_from_endpoints(endpoints, global_state): if isinstance(models, dict) and 'error' in models: logger.error('Error fetching models from backend: %s', models['error']) else: - endpoint["models"] = models.get("models", []) # get the list of models directly - logger.debug("models = \n{}".format(json.dumps(models, indent=4))) - if endpoint["model"] is not "AUTODETECT": # Check if specified model is available - logger.debug("Asking for specific model") + endpoint["models"] = models.get("models", []) # Get the list of models directly + # logger.debug("models = \n{}".format(json.dumps(models, indent=4))) class GlobalState: @@ -67,6 +65,7 @@ class GlobalState: cls._instance.logger.addHandler(handler) cls._instance.logger.setLevel(getattr(logging, cls._instance.log_level)) # Initialize root logger level cls._instance.logger.info(" __new__(cls): Logger in GlobalState created: %s", cls._instance.logger) + cls._instance.host_url = None # Currently used LLM host cls._instance.llm = "phi3:mini" # Default LLM for queries. TODO: Check with ollama server that it actually exists # cls._instance.backend_api_ep = "http://localhost:5005/api/chat" # Default backend API endpoint # Try making things more aligned with the outline of the yaml file @@ -105,6 +104,14 @@ class GlobalState: logger = logging.getLogger(module_name) return logger + def set_host_url(self, url="http://localhost:11434"): + """Set the host url to which LLM requests are sent""" + self.host = url + + def get_host_url(self): + """Get the url for the currently used host for LLMs""" + return self.host_url + def set_llm(self, model_name="phi3:mini"): """Set LLM for queries""" self.llm = model_name @@ -114,11 +121,11 @@ class GlobalState: return self.llm def set_backend(self, backend=None): - """Set backend that web clients connect to""" + """Set backend server that web clients connect to""" self.backend = backend def get_backend(self): - """Getter for backend that web clients connect to""" + """Getter for backend server that web clients connect to""" return self.backend def get_backend_api_ep(self): @@ -134,4 +141,11 @@ class GlobalState: def get_endpoints(self): """Return the list of endpoints""" - return self.endpoints \ No newline at end of file + return self.endpoints + + def get_list_of_available_llms(self, endpoint=None): + """Return a sorted list of LLMs available at endpoint""" + llm_list = None + if isinstance(endpoint["models"], list): + llm_list = sorted([list_item['name'] for list_item in endpoint["models"]], key=str.lower) + return llm_list \ No newline at end of file