diff --git a/smartassist/src/backend.py b/smartassist/src/backend.py index 3ea3cf5..07f32f6 100644 --- a/smartassist/src/backend.py +++ b/smartassist/src/backend.py @@ -4,6 +4,7 @@ from ollama import Client 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 # Initialize a Flask application @@ -30,26 +31,35 @@ def chat(): print(f"data = {data}\nmessage = {message}") try: - response = requests.post('http://localhost:11434/api/generate', - json={ - "model": "mannix/llama3-8b-ablitered-v3:latest", - 'prompt': message, - "stream": False}) + # Alternative LLM: "model": "mannix/llama3-8b-ablitered-v3:latest", + url = "http://localhost:11434/api/generate" + model_to_use = "phi3:mini" + 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 jsonify({'response': response.json().get('result')}) + # print(json.dumps(response.json(), indent=4)) + return response.json() except requests.exceptions.RequestException as e: print(f"Request Exception: {e}") return jsonify({'error': 'Failed to process request'}), 500 except json.JSONDecodeError as e: print(f"JSON Decode Error: {e}") return jsonify({'error': 'Invalid JSON response from server'}), 500 - - # if message: - # # response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message}) - # response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message}) - # return jsonify({'response': response.json().get('result')}) - # else: - # return jsonify({'error': 'No query provided'}), 400 @app.route('/smartassist', methods=["POST"]) diff --git a/smartassist/src/frontend.js b/smartassist/src/frontend.js index fc8d764..22a336d 100644 --- a/smartassist/src/frontend.js +++ b/smartassist/src/frontend.js @@ -34,31 +34,7 @@ function sendMessage() { } } -// function sendMessage() { -// const message = userInput.value.trim(); -// if (message !== '') { -// // Create a new XMLHttpRequest object -// const xhr = new XMLHttpRequest(); -// // Configure the request -// xhr.open('POST', 'http://localhost:5000/api/chat'); // Set method to POST and URL -// xhr.setRequestHeader('Content-Type', 'application/json'); // Essential for sending JSON - -// // Handle response data -// xhr.onload = function() { -// if (xhr.status >= 200 && xhr.status < 300) { // Successful response -// const data = JSON.parse(xhr.responseText); -// renderMessage(data.response, 'ai-response'); - -// } else { -// console.error('Request failed. Status:', xhr.status); -// } -// }; - -// // Send the request -// xhr.send(JSON.stringify({ message })); -// } -// } // Define a function to render a message in the chatbox with a specific class name