ChatGPT API answers twice and I don´t know why :(

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:

Welcome to the community!

I dunno bro

is it possible that

  1. you’re retrieving the list of messages in a thread
  2. whatever the list is, if there’s an assistant message, you just return the first assistant message

:thinking:

if that’s the case, it’s obvious why you keep getting the first message.

at least your response time should be super fast! :rofl:

1 Like

What you should be polling for in your loop is not messages, but the status of a run. That will tell you when the status has stepped all the way from “queued” to “completed”

Run object:

{
  "id": "run_abc123",
  "object": "thread.run",
  "created_at": 1699063290,
  "assistant_id": "asst_abc123",
  "thread_id": "thread_abc123",
  "status": "completed",
  "started_at": 1699063290,

Then there will be an assistant reply waiting for you, either first or last in the messages depending on if you request asc or desc order.

1 Like

There’s no “ChatGPT API”. It’s the Open AI API. ChatGPT is their web product and is not an API.

1 Like

Thank you, @Diet and @_j , for your helpful answers regarding the issue and respondig to the topic. It works now. :slight_smile: