Can get output of the openai assistant

Hi, i’m trying the openai assistant and for some reason i can’t get access to the latest response of the openai assistant. i know there are some topic that talk about that but the problem here is i get the old question and answers of the openai assistant and i even get the last question but i don’t get access to the last answers of the assistant.
Here is my code that i try

client = OpenAI(api_key="")
threadid = "thread_dB9YeZywkPUHwf9BGv65ke6B"

def get_prompt(question):
    try:

        # Send the question to the thread
        client.beta.threads.messages.create(
            thread_id=threadid,
            role="user",
            content=question
        )

        assistant = client.beta.assistants.retrieve(assistant_id=assistantid)

        # Execute the thread
        run = client.beta.threads.runs.create(
            thread_id=threadid,
            assistant_id=assistant.id,
        )

        # Retrieve the run result
        run = client.beta.threads.runs.retrieve(
            thread_id=threadid,
            run_id=run.id,
        )

        # Get the last message from the thread which is assumed to be the answer
        messages = client.beta.threads.messages.list(
            thread_id=threadid
        )

        last_message = messages.data[0]
        response = last_message.content[0].text.value
        i = 0
        for message in messages.data:
            print(i)
            print(message.role[1], " : " , message.content[0].text.value)
            print("---------------------------------------")
            i+=1

    except Exception as e:
        print(f"An error occurred: {e}")
        return None

# Example usage of the get_prompt function
answer = get_prompt("What is the best time to visit Nigeria?")
print (answer)

Here is the answers i got with all the trying i did :

0
s  :  What is the best time to visit Nigeria?
---------------------------------------
1
s  :  The best time to visit Mali is during the cooler and drier months, typically from November to February. During this period, temperatures are more moderate, and the weather conditions are generally more suitable for outdoor activities and sightseeing. It's important to consider the climate and seasonal variations when planning a trip to Mali.
---------------------------------------
2
s  :  What is the best time to visit Mali?
---------------------------------------
3
s  :  The best time to visit Mali is during the cooler and drier months, typically from November to February. During this period, temperatures are more moderate, and the weather conditions are generally more suitable for outdoor activities and sightseeing. It's important to consider the climate and seasonal variations when planning a trip to Mali.
---------------------------------------
4
s  :  What is the best time to visit Mali?
---------------------------------------
5
s  :  The best time to visit Mali is during the cooler and drier months, typically from November to February. During this period, temperatures are more moderate, and the weather conditions are generally more suitable for outdoor activities and sightseeing. It's important to consider the climate and seasonal variations when planning a trip to Mali.
---------------------------------------
6
s  :  What is the best time to visit Mali?
---------------------------------------
7
s  :  The best time to visit Mali is during the cooler and drier months, typically from November to February. During this period, temperatures are more moderate, and the weather conditions are generally more suitable for outdoor activities and sightseeing. It's important to consider the climate and seasonal variations when planning a trip to Mali.
---------------------------------------
8
s  :  What is the best time to visit Mali?
---------------------------------------
9
s  :  The best time to visit Mali is during the cooler and drier months, typically from November to February. During this period, temperatures are more moderate, and the weather conditions are generally more suitable for outdoor activities and sightseeing. It's important to consider the climate and seasonal variations when planning a trip to Mali.
---------------------------------------
10
s  :  What is the best time to visit Mali?
---------------------------------------
11
s  :  What is the best time to visit Burkina?
---------------------------------------
12
s  :  What is the best time to visit Burkina?
---------------------------------------
13
s  :  What is the best time to visit Mali?
---------------------------------------
14
s  :  The best time to visit Mali is during the cooler and drier months, which typically occur between November and February. During this time, temperatures are more moderate, and the weather conditions are generally more comfortable for outdoor activities and sightseeing. It's important to note that Mali experiences a hot, dry season from March to May and a rainy season from June to October, so planning a visit during the cooler, drier months is recommended for a more enjoyable experience.
---------------------------------------
15
s  :  What is the best time to visit Mali?
---------------------------------------
16
s  :  What is the best time to visit Mali?
---------------------------------------
17
s  :  The best time to visit Mali is during the cooler and drier months, which typically occur between November and February. During this time, temperatures are more moderate, and the weather conditions are generally more comfortable for outdoor activities and sightseeing. It's important to note that Mali experiences a hot, dry season from March to May and a rainy season from June to October, so planning a visit during the cooler, drier months is recommended for a more enjoyable experience.
---------------------------------------
18
s  :  What is the best time to visit Mali?
---------------------------------------
19
s  :  The best time to visit London is typically during the spring (March to May) or the early fall (September to October). During these months, the weather is generally mild, and there are fewer tourists compared to the peak summer months. However, London is a vibrant city with events, attractions, and activities year-round, so the best time to visit may also depend on your specific interests and the type of experience you are seeking.
---------------------------------------
None
1 Like

You forgot the part where you have to allow a reasonable time for a response to be formed by the language AI, then continue polling for the run status until it is completed, to only then grab the assistant response that has been generated.

2 Likes

Thanks you for your reply, i have managed to do it

1 Like

I’m not familiar with the python library, I wrote a C# wrapper over the rest calls. On my Run object, I included a wait method that includes an optional timeout. In the background, there’s a thread that’s managing the status of the Run and executing call backs to the main code when status changes (i.e. for requires_action status). The wait returns when it completes either by success, error or cancellation. My point here isn’t that I did anything particularly insightful, its that I would expect that the python API would have a wait like function as well. Using this you wouldn’t need to code at the application level at least, the polling and waiting for a response.

1 Like