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