Ersatt utskrifter med loggning

This commit is contained in:
2024-07-21 23:15:09 +02:00
parent b102309d7b
commit 81f2352291
+28 -16
View File
@@ -5,7 +5,13 @@ from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin # CORS stands for Cross-Origin Resource Sharing. This is necessary to allow the frontend to make requests to our backend.
import requests
import json
#import threading
import logging
import utils
from utils import set_local_logger, run_flask
global log_level # Global variable to store the log level that is set in startservices.py
logger = logging.getLogger(__name__) # Separate logger for this module
set_local_logger(logger) # Set log level for logger based on log_level
# Initialize a Flask application
app = Flask(__name__)
@@ -19,12 +25,18 @@ CORS(app, resources={
@app.route('/api/chat', methods=['POST'])
def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"):
"""
This function handles the chat. The frontend client (web browser) calls the
backend server through this endpoint (/api/chat) that manage queries
to the LLM (Large Language Model) server and it also manages the response
from the LLM server.
"""
# Get the message from the JSON in the request body
data = request.get_json()
message = data.get('query')
print(f"data = {data}\nmessage = {message}")
# print(f"data = {data}\nmessage = {message}")
logger.debug("data = %s\nmessage = %s", str(data), str(message))
try:
# Alternative LLM: "model": "mannix/llama3-8b-ablitered-v3:latest",
url = url_server
@@ -47,13 +59,12 @@ def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"
headers=headers,
data=json.dumps(data))
response.raise_for_status() # Raise an exception for bad status codes
# print(json.dumps(response.json(), indent=4))
return response.json()
except requests.exceptions.RequestException as e:
print(f"Request Exception: {e}")
logger.error("Request Exception: %s", str(e))
return jsonify({'error': 'Failed to process request'}), 500
except json.JSONDecodeError as e:
print(f"JSON Decode Error: {e}")
logger.error("JSON Decode Error: %s", str(e)) # Corresponds to print(f"JSON Decode Error: {e}")
return jsonify({'error': 'Invalid JSON response from server'}), 500
@@ -73,24 +84,25 @@ def smartassist():
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
def run_flask(fport=5005):
# Flask endpoint for user interaction
print(f"Entering run_flask()")
# app.run(port = str(str(fport)), debug=False)
app.run(port = str(str(fport)), debug=True)
# app.run(port=5000, debug=True, use_reloader=False)
print(f"Exiting run_flask()")
# def run_flask(fport=5005):
# """
# Starts the Flask server
# """
# # Flask endpoint for user interaction
# logger.debug("Entering run_flask()")
# # app.run(port = str(str(fport)), debug=False)
# app.run(port = str(str(fport)), debug=True)
# # app.run(port=5000, debug=True, use_reloader=False)
# logger.debug("Exiting run_flask()")
if __name__ == '__main__':
# Run the Flask application
run_flask()
run_flask(app)