Lagt till svaren till chat-historiken. Hanterar och delar variabler enhetligt med global_state, inte miljövariabler.

This commit is contained in:
2024-07-30 23:33:00 +02:00
parent c3d7f3ba4f
commit 0adbb9c222
+13 -48
View File
@@ -1,7 +1,7 @@
# 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, send_from_directory, render_template, session from flask import Flask, request, jsonify, send_from_directory, render_template, session, make_response
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 json import json
@@ -41,8 +41,9 @@ def index():
session['chat_history'] = [] # The session object (actually, a dictonary) holds the chat session session['chat_history'] = [] # The session object (actually, a dictonary) holds the chat session
logger.debug("Entering route '/'") logger.debug("Entering route '/'")
api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable # api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable
logger.debug("API endpoint: %s", api_endpoint) api_endpoint = global_state.get_backend_api_ep() # Retrieve the environment variable
logger.debug("Backend API endpoint: %s", api_endpoint)
use_model = global_state.get_llm() use_model = global_state.get_llm()
with open('smartassist/src/html/client.html', 'r') as f: with open('smartassist/src/html/client.html', 'r') as f:
client_html = f.read() client_html = f.read()
@@ -51,6 +52,12 @@ def index():
return render_template('index.html', api_endpoint=api_endpoint, use_model = use_model, client_content=client_html) return render_template('index.html', api_endpoint=api_endpoint, use_model = use_model, client_content=client_html)
@app.route('/set_session')
def set_session():
resp = make_response()
resp.set_cookie('session', 'some-value', samesite='None', secure=True) # Add SameSite attribute here
return resp
@app.route('/profile') @app.route('/profile')
def profile(): def profile():
# Retrieve data from the session # Retrieve data from the session
@@ -79,49 +86,6 @@ CORS(app, resources={
"origins": "*" "origins": "*"
} }
}) })
# @app.route('/api/memfree_chat', methods=['POST'])
# def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"):
# """
# This function handles the chat. The frontend client (web browser) calls the
# backend server through this endpoint (/api/chat) that manage queries
# to the LLM (Large Language Model) server and it also manages the response
# from the LLM server.
# """
# # Get the message from the JSON in the request body
# data = request.get_json()
# message = data.get('query')
# url_server = data.get('url_server', url_server) # Use provided URL or default
# model = data.get('model', model) # Use provided model or default
# logger.debug("data = %s\nmessage = %s", str(data), str(message))
# try:
# url = url_server
# model_to_use = model
# 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 response.json()
# except requests.exceptions.RequestException as e:
# logger.error("Request Exception: %s", str(e))
# return jsonify({'error': 'Failed to process request'}), 500
# except json.JSONDecodeError as e:
# logger.error("JSON Decode Error: %s", str(e)) # Corresponds to print(f"JSON Decode Error: {e}")
# return jsonify({'error': 'Invalid JSON response from server'}), 500
@app.route('/api/chat', methods=['POST']) @app.route('/api/chat', methods=['POST'])
@@ -143,7 +107,6 @@ def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"
# Add the new message to the chat history # Add the new message to the chat history
chat_history.append({'role': 'user', 'message': message}) chat_history.append({'role': 'user', 'message': message})
logger.debug(f"Chat History: {chat_history}")
# Update the session with the new chat history # Update the session with the new chat history
session['chat_history'] = chat_history session['chat_history'] = chat_history
@@ -165,7 +128,9 @@ def chat(url_server = "http://localhost:11434/api/generate", model = "phi3:mini"
headers=headers, headers=headers,
data=json.dumps(data_to_send)) data=json.dumps(data_to_send))
response.raise_for_status() # Raise an exception for bad status codes response.raise_for_status() # Raise an exception for bad status codes
llm_response = response.json()['response'] # Assuming the LLM's response is under 'response' key
chat_history.append({'role': 'assistant', 'message': llm_response}) # Add assistant response to chat history
logger.debug(f"Chat History: {chat_history}")
return response.json() return response.json()
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
logger.error("Request Exception: %s", str(e)) logger.error("Request Exception: %s", str(e))