Mer korrekt beteende och bättre felhantering för /api/chat

This commit is contained in:
2024-07-17 23:49:13 +02:00
parent 2aa9aae282
commit f1fe8f0bfc
+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"])