Ersatt utskrifter med loggning
This commit is contained in:
+28
-16
@@ -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.
|
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 requests
|
||||||
import json
|
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
|
# Initialize a Flask application
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -19,12 +25,18 @@ CORS(app, resources={
|
|||||||
|
|
||||||
@app.route('/api/chat', methods=['POST'])
|
@app.route('/api/chat', methods=['POST'])
|
||||||
def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"):
|
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
|
# Get the message from the JSON in the request body
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
message = data.get('query')
|
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:
|
try:
|
||||||
# Alternative LLM: "model": "mannix/llama3-8b-ablitered-v3:latest",
|
# Alternative LLM: "model": "mannix/llama3-8b-ablitered-v3:latest",
|
||||||
url = url_server
|
url = url_server
|
||||||
@@ -47,13 +59,12 @@ def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"
|
|||||||
headers=headers,
|
headers=headers,
|
||||||
data=json.dumps(data))
|
data=json.dumps(data))
|
||||||
response.raise_for_status() # Raise an exception for bad status codes
|
response.raise_for_status() # Raise an exception for bad status codes
|
||||||
# print(json.dumps(response.json(), indent=4))
|
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.exceptions.RequestException as e:
|
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
|
return jsonify({'error': 'Failed to process request'}), 500
|
||||||
except json.JSONDecodeError as e:
|
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
|
return jsonify({'error': 'Invalid JSON response from server'}), 500
|
||||||
|
|
||||||
|
|
||||||
@@ -73,24 +84,25 @@ def smartassist():
|
|||||||
def get_response(user_query):
|
def get_response(user_query):
|
||||||
# Create a client object for interacting with OLLAMA API
|
# Create a client object for interacting with OLLAMA API
|
||||||
client = Client()
|
client = Client()
|
||||||
|
|
||||||
# Generate and retrieve the response based on user's query
|
# Generate and retrieve the response based on user's query
|
||||||
response = client.generate_response(user_query)
|
response = client.generate_response(user_query)
|
||||||
|
|
||||||
# Return the generated response
|
# Return the generated response
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def run_flask(fport=5005):
|
# def run_flask(fport=5005):
|
||||||
# Flask endpoint for user interaction
|
# """
|
||||||
print(f"Entering run_flask()")
|
# Starts the Flask server
|
||||||
# app.run(port = str(str(fport)), debug=False)
|
# """
|
||||||
app.run(port = str(str(fport)), debug=True)
|
# # Flask endpoint for user interaction
|
||||||
# app.run(port=5000, debug=True, use_reloader=False)
|
# logger.debug("Entering run_flask()")
|
||||||
print(f"Exiting 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__':
|
if __name__ == '__main__':
|
||||||
# Run the Flask application
|
# Run the Flask application
|
||||||
run_flask()
|
run_flask(app)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user