3 Commits

6 changed files with 61 additions and 41 deletions
+13 -9
View File
@@ -7,11 +7,16 @@ import requests
import json
import logging
import os
# from utils import set_local_logger
import utils
from utils import GlobalState
# Create a logger for this module
logger = logging.getLogger(__name__) # This logger will be used to log messages from this module
logger.debug("Logging level of backend logger has been configured")
# logger = logging.getLogger(__name__) # This logger will be used to log messages from this module
# logger.debug("Logging level of backend logger has been configured")
global_state = GlobalState() # Import the singleton that holds global states (e.g., logger)
logger = global_state.getLogger(__name__) # Logger for this module, inherit properties of the root logger
# llm = global_state.get_llm()
# Find out the path to current directory according to the Python interpreter (venv)
logger.debug("Current working directory: %s", os.getcwd())
@@ -31,6 +36,7 @@ def index():
logger.debug("Entering route '/'")
api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable
logger.debug("API endpoint: %s", api_endpoint)
use_model = global_state.get_llm()
with open('smartassist/src/html/client.html', 'r') as f:
client_html = f.read()
logger.debug("Client HTML (first few characters): %s", client_html[:50]) # Print to see if it's loading
@@ -114,12 +120,10 @@ def smartassist():
return jsonify({"response": response})
def get_response(user_query):
# Create a client object for interacting with OLLAMA API
client = Client()
# Generate and retrieve the response based on user's query
response = client.generate_response(user_query)
# Return the generated response
return response
client = Client() # Create a client object for interacting with OLLAMA API
response = client.generate_response(user_query) # Generate and retrieve the response based on user's query
return response
def run_flask(fport=5005):
"""
+3 -8
View File
@@ -17,16 +17,11 @@
<!-- Get the apiEndpoint -->
<script>
const apiEndpoint = window.apiEndpoint;
// Debugging log messages
// if (apiEndpoint) {
// console.log("client.html - apiEndpoint: ", apiEndpoint);
// }
// else {
// console.log("client.html - cannot find apiEndpoint");
// }
const useModel = window.useModel;
</script>
<!-- Get the javascript handling communication with the backene -->
<!-- Get the javascript handling communication with the backend -->
<script src="/js/frontend.js"></script>
<script>
+30 -22
View File
@@ -56,38 +56,46 @@ def configure():
# envrionment variable
####################################
backend_api_ep = 'http://localhost:5005/api/chat' # Default API endpoint
if isinstance(updated_config.get('backend'), dict): # Look for 'backend' key in config file
if isinstance(updated_config['backend'].get('url'), str): # Look for 'url' key in config file
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 in config file
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("BE_API_ENDPOINT is set to '{}'".format(backend_api_ep))
os.environ['BE_API_ENDPOINT'] = backend_api_ep # Look into alternative way to share this with backend.py
####################################
# Extract Ollama parameters (url, api_key, model)
####################################
if isinstance(updated_config.get('ollama'), dict): # Look for 'ollama' key
if isinstance(updated_config['ollama'].get('model'), str): # Look for 'model' key
model_to_use = updated_config['ollama'].get('model')
global_state.set_llm(model_to_use)
return updated_config
def start_frontend(config):
parsed_url = urllib.parse.urlparse(config['frontend']['url'])
hostname = parsed_url.netloc.split(':')[0] # Split by ':' and take the first part, i.e., 'localhost', IP, or domain name
port = parsed_url.port # This is the server port
# def start_frontend(config):
# parsed_url = urllib.parse.urlparse(config['frontend']['url'])
# hostname = parsed_url.netloc.split(':')[0] # Split by ':' and take the first part, i.e., 'localhost', IP, or domain name
# port = parsed_url.port # This is the server port
# Use the socket module in Python to check whether a port is in use,
# which would indicate that a server is already running on that port.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((hostname, port))
logger.debug("No server is running on %s -— starting one.", parsed_url.netloc)
# Start frontend (web server) as a separate process
subprocess.Popen(["python", "-m", "http.server", str(port)])
except socket.error as e:
if e.errno == 48:
logger.debug("A server is already running on %s -— will use this.", parsed_url.netloc)
else:
raise # Unexpected error, re-raise it so we can see the traceback
except Exception as e:
logger.error("Failed to start frontend: %s", str(e)) # Corresponds to print(f"Failed to start frontend: {e}")
# # Use the socket module in Python to check whether a port is in use,
# # which would indicate that a server is already running on that port.
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# try:
# s.bind((hostname, port))
# logger.debug("No server is running on %s -— starting one.", parsed_url.netloc)
# # Start frontend (web server) as a separate process
# subprocess.Popen(["python", "-m", "http.server", str(port)])
# except socket.error as e:
# if e.errno == 48:
# logger.debug("A server is already running on %s -— will use this.", parsed_url.netloc)
# else:
# raise # Unexpected error, re-raise it so we can see the traceback
# except Exception as e:
# logger.error("Failed to start frontend: %s", str(e)) # Corresponds to print(f"Failed to start frontend: {e}")
+1 -1
View File
@@ -14,7 +14,7 @@ function sendMessage() {
fetch(`${apiEndpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, model: "phi3:mini" }), // Add these parameters here
body: JSON.stringify({ query, model: `${useModel}` }), // Add these parameters here
// body: JSON.stringify({ query, url_server: "http://your-custom-url", model: "phi3:mini" }), // Add these parameters here
})
.then(response => response.json())
+3
View File
@@ -18,9 +18,12 @@
<script>
// Extract apiEndpoint for use in your frontend code...
const apiEndpoint = '{{ api_endpoint }}'; // Templating syntax (Jinja2)
const useModel = '{{ use_model }}'; // Templating syntax (Jinja2)
// Tell the iframe about the apiEndpoing
document.getElementById('client-frame').contentWindow.apiEndpoint = apiEndpoint;
document.getElementById('client-frame').contentWindow.useModel = useModel;
console.log("index.html - API Endpoint: ", apiEndpoint);
console.log("index.html - use model: ", useModel);
</script>
<!-- Responsive scaling and some padding -->
+11 -1
View File
@@ -24,6 +24,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.llm = "phi3:mini" # Default LLM for queries. TODO: Check with ollama server that it actually exists
return cls._instance
def configure_logging(self, level=None):
@@ -53,4 +54,13 @@ class GlobalState:
if module_name is None:
module_name = __name__
logger = logging.getLogger(module_name)
return logger
return logger
def set_llm(self, model_name="phi3:mini"):
"""Set LLM for queries"""
self.llm = model_name
def get_llm(self):
"""Getter for which LLM is used for queries"""
return self.llm