Extraherar nu svaret från ollama och skickar detta till frontend
This commit is contained in:
+23
-13
@@ -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,13 +31,29 @@ 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
|
||||
@@ -44,13 +61,6 @@ def chat():
|
||||
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"])
|
||||
def smartassist():
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user