Saving Previous Conversations With My Chatbot

Hey all,

Im trying to get my chatbot to be able to remember previous message, for example if I ask it something than next ask it what I just said, it will remember.

Please help, here is my code down below. I also have HTML.
Also thanks to those who reported my last post., very dumb mistake.

from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import openai


# Initialize Flask app
app = Flask(__name__)
CORS(app)

# Set OpenAI API key

# Serve HTML file from static directory
@app.route("/")
def index():
    return send_from_directory("static", "html.html")

# Chat endpoint
@app.route("/chat", methods=["POST"])
def chat():
    try:
        # Get user message from JSON payload
        user_message = request.json.get("message")
        if not user_message:
            return jsonify({"error": "No message provided"}), 400


        User: {user_message}
        Assistant:
        """

        client = openai.OpenAI(    
            api_key = "key",
        )

        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": """You are an expert in Finance. Respond to user questions with helpful, concise, and accurate information. Dont talk botty like a ai bot and dont make your response too long
                                                ALSO, do not show the math only show the end numbers. Dont add a bunch of extra characters. If the question is outside this area, politely redirect the user. Help them make the best decisions in finance and show the math as you do so. Be descriptive."""},
                {
                    "role": "user",
                    "content": user_message
                }
            ],
            max_tokens=300,
            temperature=0.7
        )
        print(response)
        # Return OpenAI response
        return jsonify({"response": response.choices[0].message.content})

    except Exception as e:
        # Handle generic errors
        return jsonify({"error": f"Internal server error: {str(e)}"}), 500


if __name__ == "__main__":
    app.run(debug=True)

type or paste code here

type or paste code here

First off, that openai.OpenAI(api_key="key") approach is, shall we say, non-traditional. Typically, you’d do something like openai.api_key = "your-api-key" (or set it up via environment variables).

Secondly, “gpt-4o-mini” is this a specific model you’ve created and is the name correct because if you are attempting to use OpenAi that name doesn’t exist.

Lastly, you have this just sitting there not doing anything.

User: {user_message}
Assistant:

Thank you for your feedback,

Sorry I was getting it working through broken code and forgot to take away a few things that are not needed. Also, I did the api_key approach like that in order to have the key running in the client. For the gpt 4o mini, idk what I was thinking, I thought that was a model in my mind while I was writing the code. Will fix now. Thanks for bringing that up.

Also im sorry for the inconvenience but I got it working now and dont know how to delete posts.

Thank you very much for your help cyount210!

1 Like