Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c82f603d51 | |||
| 4f4d9af67d | |||
| 10469df5be | |||
| 8a9e57be76 |
@@ -1,8 +1,9 @@
|
|||||||
# A requirements file for the project.
|
# A requirements file for the project.
|
||||||
#
|
#
|
||||||
# Run 'pip install -r requirements.txt' to install all required packages.
|
# Run 'pip install -r smartassist/requirements_dev.txt ' to install all required packages.
|
||||||
|
|
||||||
|
|
||||||
ollama
|
ollama
|
||||||
Flask
|
Flask
|
||||||
|
flask_cors
|
||||||
requests
|
requests
|
||||||
+14
-10
@@ -2,25 +2,29 @@
|
|||||||
# Import the necessary functions from ollama, Flask, requests, threading
|
# Import the necessary functions from ollama, Flask, requests, threading
|
||||||
from ollama import Client
|
from ollama import Client
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
|
from flask_cors import CORS, cross_origin
|
||||||
#import requests
|
#import requests
|
||||||
#import threading
|
#import threading
|
||||||
|
|
||||||
# Initialize a Flask application
|
# Initialize a Flask application
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
CORS(app)
|
||||||
|
|
||||||
|
# @app.route('/api/chat', methods=['POST'])
|
||||||
|
# @cross_origin(origin='http://localhost:8000', headers=['Content-Type']) # Allow cross-origin requests from localhost:8000
|
||||||
@app.route('/api/chat', methods=['POST'])
|
@app.route('/api/chat', methods=['POST'])
|
||||||
|
@cross_origin(origin='*', headers=['Content-Type']) # Enable CORS for this route. Probably not necessary.
|
||||||
def chat():
|
def chat():
|
||||||
if request.method == 'POST':
|
# 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')
|
|
||||||
|
|
||||||
if message:
|
if message:
|
||||||
# response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
|
# response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
|
||||||
response = request.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
|
response = request.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
|
||||||
return jsonify({'response': response.json().get('result')})
|
return jsonify({'response': response.json().get('result')})
|
||||||
else:
|
else:
|
||||||
return jsonify({'error': 'No query provided'}), 400
|
return jsonify({'error': 'No query provided'}), 400
|
||||||
|
|
||||||
|
|
||||||
@app.route('/smartassist', methods=["POST"])
|
@app.route('/smartassist', methods=["POST"])
|
||||||
|
|||||||
+49
-49
@@ -4,62 +4,62 @@ const chatbox = document.getElementById('chatbox');
|
|||||||
const userInput = document.getElementById('userInput');
|
const userInput = document.getElementById('userInput');
|
||||||
|
|
||||||
// Define a function to send the user's message to the AI
|
// Define a function to send the user's message to the AI
|
||||||
// function sendMessage() {
|
|
||||||
// // Get the user's input message and trim any whitespace
|
|
||||||
// const message = userInput.value.trim();
|
|
||||||
|
|
||||||
// // Check if the message is not empty
|
|
||||||
// if (message !== '') {
|
|
||||||
// // Send a POST request to the /api/chat endpoint with the message
|
|
||||||
// fetch('/api/chat', {
|
|
||||||
// method: 'POST',
|
|
||||||
// headers: { 'Content-Type': 'application/json' },
|
|
||||||
// body: JSON.stringify({ message }),
|
|
||||||
// })
|
|
||||||
// .then(response => response.json())
|
|
||||||
// .then(data => {
|
|
||||||
// // Get the AI's response from the API data
|
|
||||||
// const aiResponse = data.response;
|
|
||||||
|
|
||||||
// // Render the user's original message in the chatbox
|
|
||||||
// renderMessage(message, 'user-message');
|
|
||||||
|
|
||||||
// // Render the AI's response in the chatbox
|
|
||||||
// renderMessage(aiResponse, 'ai-response');
|
|
||||||
|
|
||||||
// // Clear the user input field for the next message
|
|
||||||
// userInput.value = '';
|
|
||||||
// })
|
|
||||||
// .catch(error => console.error('Error sending message:', error));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
function sendMessage() {
|
function sendMessage() {
|
||||||
const message = userInput.value.trim();
|
// Get the user's input message and trim any whitespace
|
||||||
if (message !== '') {
|
const message = userInput.value.trim();
|
||||||
// Create a new XMLHttpRequest object
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
// Configure the request
|
// Check if the message is not empty
|
||||||
xhr.open('POST', '/api/chat'); // Set method to POST and URL
|
if (message !== '') {
|
||||||
xhr.setRequestHeader('Content-Type', 'application/json'); // Essential for sending JSON
|
// Send a POST request to the /api/chat endpoint with the message
|
||||||
|
fetch('http://localhost:5000/api/chat', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ message }),
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
// Get the AI's response from the API data
|
||||||
|
const aiResponse = data.response;
|
||||||
|
|
||||||
// Handle response data
|
// Render the user's original message in the chatbox
|
||||||
xhr.onload = function() {
|
renderMessage(message, 'user-message');
|
||||||
if (xhr.status >= 200 && xhr.status < 300) { // Successful response
|
|
||||||
const data = JSON.parse(xhr.responseText);
|
|
||||||
renderMessage(data.response, 'ai-response');
|
|
||||||
|
|
||||||
} else {
|
// Render the AI's response in the chatbox
|
||||||
console.error('Request failed. Status:', xhr.status);
|
renderMessage(aiResponse, 'ai-response');
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send the request
|
// Clear the user input field for the next message
|
||||||
xhr.send(JSON.stringify({ message }));
|
userInput.value = '';
|
||||||
}
|
})
|
||||||
|
.catch(error => console.error('Error sending message:', error));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// Define a function to render a message in the chatbox with a specific class name
|
||||||
function renderMessage(text, className) {
|
function renderMessage(text, className) {
|
||||||
|
|||||||
@@ -37,6 +37,6 @@ def start_backend():
|
|||||||
print(f"Failed to start backend: {e}")
|
print(f"Failed to start backend: {e}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
start_frontend()
|
start_frontend() # No need for this as the backend starts a web server on its own.
|
||||||
start_backend()
|
start_backend()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user