diff --git a/smartassist/src/utils.py b/smartassist/src/utils.py index d0c5c6d..2fb8fa0 100644 --- a/smartassist/src/utils.py +++ b/smartassist/src/utils.py @@ -2,6 +2,49 @@ # imported to more than one other module. The rational for defining these things here # is that it is easier to avoid circular imports when they are defined in a central location. import logging +import json +import requests +#from backend import GlobalState # Assuming GlobalState is defined there + +def fetch_models_from_endpoints(endpoints, global_state): + """ + Fetch models from endpoints and update the endpoint dictionaries. + + Args: + endpoints (list): List of endpoint dictionaries. + global_state: The global state object. + + Returns: + None + """ + logger = logging.getLogger(__name__) + + for endpoint in endpoints: + if endpoint["provider"] == "ollama": + headers = { + "Content-Type": "application/json", + } + if "requestOptions" in endpoint: # Check if authentication is needed + headers.update({ + "Authorization": endpoint["requestOptions"]["headers"]["Authorization"] + }) + + try: + models_response = requests.get(endpoint["url"] + "/api/tags", headers=headers) + models_response.raise_for_status() # Raise an exception for HTTP errors + models = models_response.json() + except requests.exceptions.RequestException as e: + logger.error("Error fetching models from backend: %s", str(e)) + continue + + 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") + class GlobalState: """