From 9ea88d693d5070d7cf9c82fc08b048bb5740899d Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Tue, 16 Jul 2024 17:16:31 +0200 Subject: [PATCH] Bytt namn. --- smartassist/src/backend.py | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 smartassist/src/backend.py diff --git a/smartassist/src/backend.py b/smartassist/src/backend.py new file mode 100644 index 0000000..5c14d8c --- /dev/null +++ b/smartassist/src/backend.py @@ -0,0 +1,57 @@ + +# Import the necessary functions from ollama, Flask, requests, threading +from ollama import Client +from flask import Flask, request, jsonify +#import requests +#import threading + +# Initialize a Flask application +app = Flask(__name__) + +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(): + # Flask endpoint for user interaction + app.run(port=5000, debug=True) + +# def start_flask_as_thread(): +# # Start the Flask app in a separate thread +# threading.Thread(target=run_flask).start() + + + +@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}) + + +@app.route('/api/chat', methods=['POST']) +def chat(): + data = request.get_json() + query = data['query'] + response = requests.post('http://ollama-server/api/v1/chat', json={'prompt': query}) + return jsonify({'response': response.json().get('result')}) + + +if __name__ == '__main__': + # Run the Flask application + run_flask() + +