0

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?

1
  • Please read tag descriptions before using tags. From python-3.x: "DO NOT USE UNLESS YOUR QUESTION IS FOR PYTHON 3 ONLY. Always use alongside the standard [python] tag." Commented Jun 17, 2024 at 10:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Will...... no, there is nothing in the view that has anything related to a session in it. This is a known issue within generator functions. Just looking for a way around it. I decided to just store the history in a vectordb instead. It allows me to create long term memory also and minimize context windows by auto labeling older portions of the conversation that can later be retrieved via RAG. - Donkey1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.