I am creating a chatbot where I using assistant api following is the code for the thread creation and chat messages but the issues is the chatbot don’t know about the previous messages what is wrong with the code. how thread works to manages chat history please guide.
# get routing
@app.route('/get')
def chat():
"""Handles user messages and interacts with the assistant."""
user_question = str(request.args.get('msg'))
# data = request.json
data = {'content':user_question}
print(data)
# Check if session and thread ID exist for the current user
if 'thread_id' not in session:
# If session and thread ID do not exist, initialize a new session and thread
thread = client.beta.threads.create()
session['thread_id'] = thread.id
session['modified'] = datetime.utcnow()
# return jsonify({"message": "Session started", "thread_id": session['thread_id']}), 200
# Add user message to the thread
client.beta.threads.messages.create(
thread_id=session['thread_id'],
role="user",
content=data['content']
)
# Run the assistant on the thread and wait for the response
run = client.beta.threads.runs.create_and_poll(
assistant_id=assistant_id,
thread_id=session['thread_id']
)
if run.status == 'completed':
# Retrieve messages from the thread
messages = client.beta.threads.messages.list(
thread_id=session['thread_id']
)
session['modified'] = datetime.utcnow()
# print(messages.data[0].content[0].text.value)
return messages.data[0].content[0].text.value
# return messages.json(), 200
return jsonify({"status": run['status']}), 202