No response from Assistant api

I have developed a script which creates a fileid and then some hardcoded queries/prompts are asked related to the file. There are more than 10 questions. I wrote this piece of code but not getting any response. Can anyone please point out why?
thanks

import time
from openai import OpenAI

ASSISTANT_ID = "asst_GTPvizLYgZ3f0T5RIbhYg7Xw"
API_KEY = "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■lUZtQvnX"
client = OpenAI(api_key=API_KEY)

def upload_file(filepath):
    try:
        with open(filepath, "rb") as file:
            my_file = client.files.create(file=file, purpose='assistants')
            print(f"File Uploaded: {my_file.id}")
            return my_file.id
    except Exception as e:
        print(f"Failed to upload file: {e}")
        return None

def create_thread():
    try:
        my_thread = client.beta.threads.create()
        print(f"Thread Created: {my_thread.id}")
        return my_thread.id
    except Exception as e:
        print(f"Failed to create thread: {e}")
        return None

def send_query(thread_id, query, file_id):
    try:
        my_thread_message = client.beta.threads.messages.create(
            thread_id=thread_id,
            role="user",
            content=query,
            file_ids=[file_id]  
        )
        print(f"Message Added to Thread: {my_thread_message.id}")
        return my_thread_message.id
    except Exception as e:
        print(f"Failed to add message to thread: {e}")
        return None

def poll_for_response(thread_id):
    print("Waiting for response from the assistant...")
    while True:
        time.sleep(1)  
        try:
            response_messages = client.beta.threads.messages.list(thread_id=thread_id)
            # Checking the role of the last message
            if response_messages.data and response_messages.data[-1].role == 'assistant':
                latest_message = response_messages.data[-1]
                print(f"Assistant's Response: {latest_message.content}")
                break
        except Exception as e:
            print(f"Error retrieving messages: {e}")
            break

def main():
    file_id = upload_file("/Users/s/rawfinal/Text_4.txt")
    if not file_id:
        return

    thread_id = create_thread()
    if not thread_id:
        return

    queries = [
       
"""Questions" 

"""
    ]

    for query in queries:
        message_id = send_query(thread_id, query, file_id)
        if not message_id:
            continue
        poll_for_response(thread_id)

if __name__ == "__main__":
    main()

Looks like your API key is also hardcoded instead of being an environment variable…

My AI is a bit more qualified on using assistants. It might actually set a run into motion. Let’s ask.


To ensure the script operates correctly and incorporates all necessary steps such as the delay after file upload for document extraction and invoking the run method to process the queries, I will delve into the assistants.txt documentation to provide a detailed and accurate update to your script.

Revised Step-by-Step Code Analysis and Refactoring

1. File Upload and Delay

According to the OpenAI API documentation, after uploading a file, especially for document extraction purposes, a brief pause might be necessary to ensure the file is ready for use.

def upload_file(filepath):
    try:
        with open(filepath, "rb") as file:
            my_file = client.files.create(file=file, purpose='assistants')
            print(f"File Uploaded: {my_file.id}")
            time.sleep(5)  # Sleep for 5 seconds to allow document extraction
            return my_file.id
    except Exception as e:
        print(f"Failed to upload file: {e}")
        return None

2. Thread Creation

Creating a thread is straightforward and does not require modification beyond what has been previously discussed.

def create_thread():
    try:
        my_thread = client.beta.threads.create()
        print(f"Thread Created: {my_thread.id}")
        return my_thread.id
    except Exception as e:
        print(f"Failed to create thread: {e}")
        return None

3. Sending Queries and Invoking Run

From the documentation, after placing a user message into a thread, it is necessary to invoke a run to process the queries. This involves creating and starting a run for the assistant to respond.

def send_query_and_invoke_run(thread_id, query, file_id):
    try:
        # Creating message
        my_thread_message = client.beta.threads.messages.create(
            thread_id=thread_id,
            role="user",
            content=query,
            file_ids=[file_id],
            assistant_id=ASSISTANT_ID
        )
        print(f"Message Added to Thread: {my_thread_message.id}")
        
        # Invoking run after adding message
        run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=ASSISTANT_ID)
        print(f"Run Invoked: {run.id}")
        return my_thread_message.id, run.id
    except Exception as e:
        print(f"Failed to add message or invoke run: {e}")
        return None, None

4. Polling for Response

This function now needs to consider the run’s completion. We must poll for responses until the run is completed.

def poll_for_response(thread_id, run_id):
    timeout = time.time() + 60*2  # 2 minutes from now
    print("Waiting for response from the assistant...")
    while True:
        if time.time() > timeout:
            print("Timeout: No response from the assistant.")
            break
        time.sleep(1)
        try:
            run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
            if run_status.status == 'completed':
                response_messages = client.beta.threads.messages.list(thread_id=thread_id)
                for message in response_messages.data:
                    if message.role == 'assistant':
                        print(f"Assistant's Response: {message.content}")
                break
            elif run_status.status in ['failed', 'cancelled']:
                print(f"Run {run_status.status}: No further responses expected.")
                break
        except Exception as e:
            print(f"Error retrieving run or messages: {e}")
            break

5. Main Function

Integrate all steps in the main function to ensure each query goes through the process of sending, invoking a run, and polling for responses.

def main():
    file_id = upload_file("/Users/s/rawfinal/Text_4.txt")
    if not file_id:
        return

    thread_id = create_thread()
    if not thread_id:
        return

    queries = ["What is the file about?", "Summarize the key points"]
    for query in queries:
        message_id, run_id = send_query_and_invoke_run(thread_id, query, file_id)
        if not message_id or not run_id:
            continue
        poll_for_response(thread_id, run_id)

if __name__ == "__main__":
    main()

These updates ensure that your script is fully compliant with the OpenAI API’s requirements and processes queries as intended, using the correct sequence of API calls and operations.

1 Like

I implented this code, but as of last week, the run is showing as completed, but there is no longer any assistant response. It doesn’t seem to matter how much I delay it. If I attempt 20 times, I do eventually get a response.