Retriever Assistant API not giving answer but playground gives

from openai import OpenAI

client = OpenAI(
    organization=<org>,
    api_key=<api_key>
)

# Upload a file with an "assistants" purpose
file = client.files.create(
  file=open("filename.txt", "rb"),
  purpose='assistants'
)


# Add the file to the assistant
assistant = client.beta.assistants.create(
  instructions="You are a customer support chatbot. Use your knowledge base to best respond to customer queries.",
  model="gpt-4-1106-preview",
  tools=[{"type": "retrieval"}],
  file_ids=[file.id]
)

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content=<User question>
    file_ids=[file.id]
)


run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions="Address user as <User name>"
)

messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages)

The above code uploads file. Also registers the user question but when I print messages the assistant has not given any reply. What am I doing wrong ?

1 Like

You have to wait for the run.status to be at completed and then fetch all messages.

1 Like

How to do that ? I tried putting “run” into a while loop that finished once run.status does not equal iin_progress. But it immediately, exits the loop and the status is still “in_progress”

2 Likes
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant.id,
        instructions="Whatever"
    )
    while run.status != 'completed':
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id
        )

    messages = client.beta.threads.messages.list(
        thread_id=thread.id
    )

    print(messages)

This works

4 Likes

@Popeyee ,
Thankyou your solution worked.

1 Like

Weird, i did something similar. The only thing I am wondering how error handling would work where the final status is not ‘completed’

        counter = 0   
        while run.status != "completed" and counter < 10:
            time.sleep(5)
            run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
            counter += 1
            
        if counter == 10:
            st.write("Run timed out")
1 Like

I think
while run.status != 'completed':
should be:
while run.status in ['queued', 'in_progress']:
Since a run could be cancelled or failed or expired. if like that program will never get out of while loop.

1 Like

If you use a paginated listing, use “asc” order for the messages. At least it works for me:

    
let cursor: string|undefined = undefined;
async function getMessages(): Promise<void> {
        const list: ThreadMessagesPage = await openAi.beta.threads.messages.list(
            thread.id,
            {after: cursor, order: "asc"}
        );
        for await (const page of list.iterPages()) {
            page.getPaginatedItems().forEach((message) => {
                cursor = message.id;
                message.content.forEach((content) => {
                    switch (content.type) {
                        case "image_file":
                            console.log("Assistant: [IMAGE]");
                            break;
                        case "text":
                            console.log(`Assistant: ${content.text.value}`);
                            break;
                    }
                });
            });
        }
    }

With the default order options I don’t get new assistant messages after run is complete