I’m trying to build a web application where I can input a question to our ChatGPT Assistant, which provides information about uploaded documents, and receive responses from ChatGPT. I don’t know why, but the API always responds twice. This issue persists not because I’m using a new thread for every question, but now it occurs even when I use the same thread for all questions. I chose this approach because I want ChatGPT to be able to answer my questions in context.
Do you know what the problem might be?
from django.http import JsonResponse
from django.shortcuts import render
import openai
import json
import time
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
def serialize_thread_messages(messages):
serialized_messages =
for message in messages:
serialized_message = {
“id”: message.id,
“role”: message.role,
“content”: [{“type”: content.type, “text”: content.text.value} for content in message.content]
}
serialized_messages.append(serialized_message)
return serialized_messages
Assistant ID
assistant_id = “–”
Global variable for the thread
thread = None
@csrf_exempt
@require_http_methods([“POST”])
def get_chatgpt_response(request):
global thread
api_key = “–”
client = openai.Client(api_key=api_key)
try:
data = json.loads(request.body.decode(‘utf-8’))
user_input = data[‘user_input’]
print(f"Request: {user_input}") # Print the request
# Check if the thread already exists, and if not, create a new one
if thread is None:
thread = client.beta.threads.create()
# Send the user input to the assistant
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=user_input,
)
client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant_id,
tools=[{"type": "retrieval"}]
)
# Maximum waiting time in seconds
max_wait_time = 240
start_time = time.time()
print(f"Request: {thread.id}")
while True:
messages = client.beta.threads.messages.list(thread_id=thread.id)
for message in messages.data:
if message.role == "assistant":
message_content = message.content
if message_content and isinstance(message_content, list) and message_content[0].type == 'text':
assistant_message_text = message_content[0].text.value
print(f"Response: {assistant_message_text}") # Print the response
return JsonResponse({"assistant_response": assistant_message_text})
# Check if the maximum waiting time has been reached
if time.time() - start_time > max_wait_time:
return JsonResponse({"assistant_response": "No response from the assistant received."})
# Wait for a short time before trying again
time.sleep(2)
except Exception as e:
return JsonResponse({"error": str(e)})
def home(request):
return render(request, ‘–’)
Output example: