I am using flask. Below is the relevant lines of code as an example only. I essentially need to save chat history for an LLM in a session variable to avoid having to do it in a data-table per user. Session variable would just work wonderfully here. Problem is, I have a generator function that yeilds the Chatbot LLM stream back to a javascript reader in the users view and it seems that saving session variables within generator functions just doesnt work easily. Did allot of reading and came up with the below that was supposed to fix this issue but still no luck. I am now able to access my session variable in the nested function (stream_with_context fixed that) but I cannot save it. Please help!
from flask import Flask, session, render_template, request, redirect, stream_with_context, Response
from flask_session import Session
app = Flask(__name__)
wsgi_app = app.wsgi_app
app.config['SECRET_KEY'] = "madeupkeyforstackoverflow"
app.config.update(SESSION_COOKIE_NAME="madeupcookieforstackoverflow")
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
@app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(minutes=10)
@app.route('/login', methods=['GET', 'POST'])
def login():
session['chat_history'] = "some chat data in an array"
# Rest of code that logs in user and sends them to a default view
@app.route("/chat", methods=['POST'])
@login_required
def chat():
# Removed code that just collect variables from user view and setup connection with various LLMs
def ask_function():
#THE FOLLOWING SUCCESSFULLY PRINTS THE DATA AS DEFINED IN LOGIN() ABOVE
print (session['chat_history'])
#THE FOLLOWING SUCCESSFULLY APPENDS NEW DATA TO THE SESSION VARIABLE. WHILE IN THIS FUNCTION, THE DATA IS AVAILABLE IN THIS SESSION VARIABLE. BUT ONCE THIS FUNCTION IS FINISHED AND RUNS AGAIN, THE DATA THAT IS APPENDED BELOW IS NOT THERE, JUST THE DATA ORIGINALLY SET ON LOGIN ABOVE. HENCE MY PROBLEM.
session['chat_history'].append({ "role": "user", "parts": "question" })
yield "some random response"
return Response(stream_with_context(ask_function()), mimetype='text/event-stream')
I tried session.modified = True
I tried using a seperate function to save the session variable
I tried a bunch of other suggestions found all over stack overflow as well as gemini code assist within visual studio code. Still no bueno. Could use some help, been stuck on this for hours.
Jami Bailey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.