Files
python_test/smartassist/src/backend.py
T
2024-07-24 08:54:40 +02:00

119 lines
3.9 KiB
Python

# Import the necessary functions from ollama, Flask, requests, threading
from ollama import Client
from flask import Flask, request, jsonify, send_from_directory
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 logging
from utils import set_local_logger
logger = logging.getLogger(__name__) # Separate logger for this module
set_local_logger(logger) # Set log level for logger
logger.debug("Logging level of backend logger has been configured")
# Initialize a Flask application
app = Flask(__name__)
app.config['STATIC_FOLDER'] = 'static' # Adjust if needed
@app.route('/<path:filename>')
def serve_static(filename):
return send_from_directory(app.config['STATIC_FOLDER'], filename)
# CORS(app, resources={
# r"/api/chat": {
# "origins": "*",
# "headers": ["Origin", "Content-Type", "Authorization"],
# }
# })
CORS(app, resources={
r"/api/chat": {
"origins": "*"
}
})
@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')
logger.debug("data = %s\nmessage = %s", str(data), str(message))
try:
url = url_server
model_to_use = model
data = {
"model": model_to_use,
'prompt': message,
"stream": False
}
headers = {
"Content-Type": "application/json",
}
# With API key
# headers = {
# "Content-Type": "application/json",
# "Authorization": "Bearer YOUR_API_KEY" # Replace with your API key
# }
response = requests.post(url,
headers=headers,
data=json.dumps(data))
response.raise_for_status() # Raise an exception for bad status codes
return response.json()
except requests.exceptions.RequestException as e:
logger.error("Request Exception: %s", str(e))
return jsonify({'error': 'Failed to process request'}), 500
except json.JSONDecodeError as 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
@app.route('/smartassist', methods=["POST"])
def smartassist():
# Extract the query from the incoming JSON data
data = request.json
user_query = data['query']
# Get the response from the OLLAMA API based on the user's query
# NOTE: Should we append message history here? Maybe interact with SQLlite?
response = get_response(user_query)
# Return the response as a JSON object in the HTTP response
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
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()