Reputation: 31
I need to save chat history for an LLM in a session variable to avoid having to do it in a data-table per user. A session variable would just work wonderfully here. However, I have a generator function that yields the Chatbot LLM stream back to a JavaScript reader in the users view and it seems that saving session variables within generator functions just doesn't work easily.
I did a lot 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.
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
and using a separate function to save the session variable. but to no avail. How can I save a session variable in Flask?
Upvotes: 0
Views: 94
Reputation: 11
Can you share the front end code that you're using to hit the flask endpoints? Your session functionality is a combination of both the front end and back end code and there may be something there that is causing your issue.
Upvotes: 1