Still getting the same issue, just for context here is the updated code. I am making a flask app so that may be part of the issue?
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
import openai
from dotenv import load_dotenv
import os
import time
# Load environment variables
load_dotenv()
app = Flask(__name__)
# Retrieve environment variables
openai.api_key = os.getenv('OPENAI_API_KEY')
@app.route("/whatsapp", methods=['POST'])
def whatsapp_reply():
body = request.values.get('Body', None)
response = MessagingResponse()
assistant_response = get_assistant_response(body)
response.message(assistant_response)
return str(response)
def get_assistant_response(user_message):
try:
# Initialize the client
client = openai.OpenAI(api_key=openai.api_key)
# Step 1: Create an Assistant using the beta namespace
assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-1106-preview"
)
# Step 2: Create a Thread using the beta namespace
thread = client.beta.threads.create(assistant_id=assistant.id)
# Step 3: Add a Message to a Thread using the beta namespace
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=user_message
)
# Step 4: Run the Assistant using the beta namespace
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account."
)
# Blocking call to wait for the run to complete; not recommended for production environments
while True:
run_status = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run_status.status == 'completed':
break
time.sleep(0.5) # Adjust the timing as needed for your use case
# Retrieve all messages from the thread using the beta namespace
messages = client.beta.threads.messages.list(thread_id=thread.id)
assistant_messages = [msg.content for msg in messages.data if msg.role == 'assistant']
return assistant_messages[-1] if assistant_messages else "No response from the assistant."
except Exception as e:
print(f"An error occurred: {e}")
return "I am unable to process your request at the moment."
if __name__ == "__main__":
app.run(debug=True)
Thx for your reply btw.