From 5aa47d11ea64b26bb8e9422d8fbfd44e1fc3bc27 Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Fri, 2 Aug 2024 23:10:37 +0200 Subject: [PATCH] =?UTF-8?q?H=C3=A4mtar=20lista=20=C3=B6ver=20tillg=C3=A4ng?= =?UTF-8?q?liga=20modeller=20fr=C3=A5n=20ollama-servrarna=20i=20yaml-konfi?= =?UTF-8?q?gurationen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smartassist/src/startservices.py | 51 +++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/smartassist/src/startservices.py b/smartassist/src/startservices.py index 1d64643..6c8cf8c 100644 --- a/smartassist/src/startservices.py +++ b/smartassist/src/startservices.py @@ -5,8 +5,9 @@ import yaml import json import socket import urllib.parse -from backend import run_flask +from backend import run_flask, tag import logging +import requests import utils from utils import GlobalState @@ -58,20 +59,6 @@ def configure(): global_state.set_log_level(logging_config['level']) logger.debug("configure(): This logger now has effective log level %s", logger.getEffectiveLevel()) - #################################### - # Extract and export backend API - # endpoint as global state variable - #################################### - # if isinstance(updated_config.get('backend'), dict): # Look for 'backend' key - # if isinstance(updated_config['backend'].get('url'), str): # Look for 'url' key - # url = updated_config['backend'].get('url') - # if isinstance(updated_config['backend'].get('api'), str): # Look for 'api' key - # api = updated_config['backend'].get('api') - # # backend_api_ep = url+api # Extract API endpoint if defined - # logger.debug(f"Constructing endpoint address as url+api: {url+api}") - # global_state.set_backend_api_ep(url+api) # Extract API endpoint if defined and set in global_state - # logger.debug(f"Backend API endpoint is set to {global_state.get_backend_api_ep()}") - #################################### # Extract models (server url, api_key, model, et cetera) #################################### @@ -80,9 +67,37 @@ def configure(): logger.debug("backend = \n{}".format(json.dumps(global_state.get_backend(), indent=4))) logger.debug(f"Backend API endpoint is set to: {global_state.get_backend_api_ep()}") - if isinstance(updated_config.get('models'),list): # Extract info on model, url, provider et cetera from list - global_state.set_models(models=updated_config.get('models')) - logger.debug("models = \n{}".format(json.dumps(global_state.get_models(), indent=4))) + if isinstance(updated_config.get('endpoints'),list): # Extract info on endpoint, model, url, provider et cetera from list + global_state.set_endpoints(endpoints=updated_config.get('endpoints')) # Extract and set list of endpoints + logger.debug("endpoints = \n{}".format(json.dumps(global_state.get_endpoints(), indent=4))) + for endpoint in global_state.get_endpoints(): + if endpoint["provider"] == "ollama": + if "requestOptions" in endpoint: # Check if authentication is needed + # headers = { + # "Content-Type": "application/json", + # "Authorization": endpoint["requestOptions"]["headers"]["Authorization"] + # } + headers = { + "Authorization": endpoint["requestOptions"]["headers"]["Authorization"] + } + else: # otherwise proceed without authentication + # headers = {"Content-Type": "application/json"} + headers = None + # models = tag(url = endpoint["url"], headers = headers) # Ask for models (LLMs) available at endpoint + try: + models = requests.get(endpoint["url"] + "/api/tags", headers=headers).json() + except requests.exceptions.RequestException as e: + print(f"Error: {e}") + + if isinstance(models, dict) and 'error' in models: + logger.error('Error fetching models from backend: %s', models['error']) + else: + endpoint["models"] = models # Update endpoint with detected models + logger.debug("models = \n{}".format(json.dumps(models, indent=4))) + if endpoint["model"] is not "AUTODETECT": # Check if specified model is available + # do something + logger.debug("Asking for specific model") + # TODO: Remove this section when not needed anymore if isinstance(updated_config.get('ollama'), dict): # Look for 'ollama' key