4 Commits

4 changed files with 59 additions and 31 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ backend:
api: "/api/chat" api: "/api/chat"
models: endpoints:
- model: "AUTODETECT" - model: "AUTODETECT"
title: "Ollama" title: "Ollama"
url: "http://localhost:11434" url: "http://localhost:11434"
+14 -1
View File
@@ -87,6 +87,19 @@ CORS(app, resources={
} }
}) })
@app.route('/api/tags', methods=['GET'])
def tag(url = "http://localhost:11434/api/tags", headers = None):
# def tag(url = "http://localhost:11434/api/tags", headers = {"Content-Type": "application/json"}):
"""Get a list of models for the server located at url."""
try:
logger.debug(f"url: {url} headers: {headers}")
response = requests.get(url, headers=headers)
return response.json()
# return response
except requests.exceptions.RequestException as e:
logger.error("Request Exception: %s", str(e))
return {'error': 'Failed to process request'}
@app.route('/api/chat', methods=['POST']) @app.route('/api/chat', methods=['POST'])
def chat(model = "phi3:mini"): def chat(model = "phi3:mini"):
@@ -101,7 +114,7 @@ def chat(model = "phi3:mini"):
data = request.get_json() data = request.get_json()
message = data.get('query') message = data.get('query')
url_server = data.get('url_server', "https://ollama-test.wara-ops.org/api/generate") # Use provided URL or default url_server = data.get('url_server', "https://ollama-test.wara-ops.org/api/generate") # Use provided URL or default
model = data.get('model', model) # Use provided model or default model = data.get('model', model) # Use provided model or default if not provided
# Get chat history from session storage (e.g., a dictionary) # Get chat history from session storage (e.g., a dictionary)
chat_history = session.get('chat_history', []) chat_history = session.get('chat_history', [])
+33 -18
View File
@@ -5,8 +5,9 @@ import yaml
import json import json
import socket import socket
import urllib.parse import urllib.parse
from backend import run_flask from backend import run_flask, tag
import logging import logging
import requests
import utils import utils
from utils import GlobalState from utils import GlobalState
@@ -58,20 +59,6 @@ def configure():
global_state.set_log_level(logging_config['level']) global_state.set_log_level(logging_config['level'])
logger.debug("configure(): This logger now has effective log level %s", logger.getEffectiveLevel()) 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) # 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("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()}") 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 if isinstance(updated_config.get('endpoints'),list): # Extract info on endpoint, model, url, provider et cetera from list
global_state.set_models(models=updated_config.get('models')) global_state.set_endpoints(endpoints=updated_config.get('endpoints')) # Extract and set list of endpoints
logger.debug("models = \n{}".format(json.dumps(global_state.get_models(), indent=4))) 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 # TODO: Remove this section when not needed anymore
if isinstance(updated_config.get('ollama'), dict): # Look for 'ollama' key if isinstance(updated_config.get('ollama'), dict): # Look for 'ollama' key
+11 -11
View File
@@ -27,8 +27,8 @@ class GlobalState:
cls._instance.llm = "phi3:mini" # Default LLM for queries. TODO: Check with ollama server that it actually exists 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 # 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 # Try making things more aligned with the outline of the yaml file
cls._instance.backend = dict() # Holds info on which server the clients connect to cls._instance.backend = dict() # A dictionary that holds info on which server the clients connect to
cls._instance.models = [] # A list that holds info on which models are available for use (server url, model name, provider et cetera) cls._instance.endpoints = [] # A list that holds info on which endpoints are available for use (server url, model name, provider et cetera)
# logging - already done in __new__, perhaps change layout later # logging - already done in __new__, perhaps change layout later
return cls._instance return cls._instance
@@ -82,13 +82,13 @@ class GlobalState:
"""Getter for backend API endpoint""" """Getter for backend API endpoint"""
return self.backend["url"]+self.backend["api"] return self.backend["url"]+self.backend["api"]
def set_models(self, models=None): def set_endpoints(self, endpoints=None):
"""Set the list of models.""" """Set the list of endpoints."""
if models is not None: if endpoints is not None:
if not isinstance(models, list): if not isinstance(endpoints, list):
raise ValueError("Models must be a list, even if there is just one model") raise ValueError("Endpoints must be a list, even if there is just one model")
self.models = models self.endpoints = endpoints
def get_models(self): def get_endpoints(self):
"""Return the list of models""" """Return the list of endpoints"""
return self.models return self.endpoints