Compare commits
2 Commits
a3d9d07ead
...
f1fe8f0bfc
| Author | SHA1 | Date | |
|---|---|---|---|
| f1fe8f0bfc | |||
| 2aa9aae282 |
@@ -3,7 +3,7 @@
|
|||||||
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 # CORS stands for Cross-Origin Resource Sharing. This is necessary to allow the frontend to make requests to our backend.
|
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
|
#import threading
|
||||||
|
|
||||||
# Initialize a Flask application
|
# Initialize a Flask application
|
||||||
@@ -27,12 +27,29 @@ def chat():
|
|||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
message = data.get('query')
|
message = data.get('query')
|
||||||
|
|
||||||
if message:
|
print(f"data = {data}\nmessage = {message}")
|
||||||
# response = requests.post('http://localhost:11434', json={"model": "llama3",'prompt': message})
|
|
||||||
response = request.post('http://localhost:11434', json={"model": "llama3",'prompt': 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')})
|
return jsonify({'response': response.json().get('result')})
|
||||||
else:
|
except requests.exceptions.RequestException as e:
|
||||||
return jsonify({'error': 'No query provided'}), 400
|
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"])
|
@app.route('/smartassist', methods=["POST"])
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ 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() {
|
function sendMessage() {
|
||||||
// Get the user's input message and trim any whitespace
|
// 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
|
// Check if the message is not empty
|
||||||
if (message !== '') {
|
if (query !== '') {
|
||||||
// Send a POST request to the /api/chat endpoint with the message
|
// Send a POST request to the /api/chat endpoint with the message
|
||||||
fetch('http://localhost:5005/api/chat', {
|
fetch('http://localhost:5005/api/chat', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ message }),
|
body: JSON.stringify({ query }),
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@@ -22,7 +22,7 @@ function sendMessage() {
|
|||||||
const aiResponse = data.response;
|
const aiResponse = data.response;
|
||||||
|
|
||||||
// Render the user's original message in the chatbox
|
// 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
|
// Render the AI's response in the chatbox
|
||||||
renderMessage(aiResponse, 'ai-response');
|
renderMessage(aiResponse, 'ai-response');
|
||||||
|
|||||||
Reference in New Issue
Block a user