2 Commits

2 changed files with 27 additions and 10 deletions
+23 -6
View File
@@ -3,7 +3,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 requests
#import threading
# Initialize a Flask application
@@ -27,12 +27,29 @@ def chat():
data = request.get_json()
message = data.get('query')
if message:
# response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
response = request.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
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})
response.raise_for_status() # Raise an exception for bad status codes
return jsonify({'response': response.json().get('result')})
else:
return jsonify({'error': 'No query provided'}), 400
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"])
+4 -4
View File
@@ -6,15 +6,15 @@ const userInput = document.getElementById('userInput');
// 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();
const query = userInput.value.trim();
// Check if the message is not empty
if (message !== '') {
if (query !== '') {
// Send a POST request to the /api/chat endpoint with the message
fetch('http://localhost:5005/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message }),
body: JSON.stringify({ query }),
})
.then(response => response.json())
.then(data => {
@@ -22,7 +22,7 @@ function sendMessage() {
const aiResponse = data.response;
// Render the user's original message in the chatbox
renderMessage(message, 'user-message');
renderMessage(query, 'user-message');
// Render the AI's response in the chatbox
renderMessage(aiResponse, 'ai-response');