Recieving empty messages.data

Sorry for my lack of knowledge. I have successfully updated my code with a helpful response earlier today. I am now successfully sending messages to my assistant(I think). I don’t see my test message in the createAndRun() functions data but I don’t see it in the documentation either. I am getting a 200 code and status complete. The message object is being returned but I am not getting the message back from the assistant. Actually looking the response everything is either 0 or null.

Not sure what I am still doing wrong.

Code:

require('dotenv').config();
const { OpenAI } = require('openai');
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function interactWithAssistant(assistantId, userQuery) {
    
    // Create empty thread
    const thread = await openai.beta.threads.create();
    
    // Create and Run assistant. Pass Ass ID, thread and messages. Think I may have a problem here. 
    const run = await openai.beta.threads.createAndRun({
        assistant_id: assistantId,
        thread: {
            messages: [
            { role: "user", content: userQuery },
            ],
        },
    });
    
    //retrieve run using thread id and run id. 
    completedRun = await openai.beta.threads.runs.retrieve(run.thread_id, run.id);

    //while status is in progress or queued continue to attempt to retrieve the run / sleep for 1 second between tries. 
    while(completedRun.status === 'in_progress' || completedRun.status === 'queued'){
        
        completedRun = await openai.beta.threads.runs.retrieve(run.thread_id, run.id);
        await new Promise(resolve => setTimeout(resolve, 1000));
    }
    
    //If status is not completed log the status
    if (completedRun.status !== 'completed') {
        console.log(completeRun.status);
    }
    

    // Retrieve the Assistant's response Returning empty messages.data
    const messages = await openai.beta.threads.messages.list(
        thread.id
    );

    return messages;
    
}

// Example Usage
const userQuery = "Why are you not working!"; 

interactWithAssistant(process.env.ASSISTANT_ID, userQuery)
    .then(messages => {    
        console.log(messages)
});

Response
ThreadMessagesPage {
options: {
method: ‘get’,
path: ‘/threads/thread_84UeIP3vFjRAwGzdwzPSM9pB/messages’,
query: {},
headers: { ‘OpenAI-Beta’: ‘assistants=v1’ }
},
response: Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: { body: [Gunzip], disturbed: true, error: null },
[Symbol(Response internals)]: {
url: ‘https://api.openai.com/v1/threads/thread_84UeIP3vFjRAwGzdwzPSM9pB/messages’,
status: 200,
statusText: ‘OK’,
headers: [Headers],
counter: 0
}
},
body: {
object: ‘list’,
data: ,
first_id: null,
last_id: null,
has_more: false
},
data:
}

I figured this one out. I split my createAndRun function up and created the message before creating a run and it now works. I kind of wanted to use createAndRun as its more concise. However, I just couldn’t get it to run. Lack of understanding on my part. Need to read into the docs a lot further.

require('dotenv').config();
const { OpenAI } = require('openai');
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function interactWithAssistant(assistantId, userQuery) {
    
    // Create empty thread
    const thread = (await openai.beta.threads.create());
    const message = await openai.beta.threads.messages.create(
        thread.id,
        { role: "user", content: userQuery }
    );
    const run = await openai.beta.threads.runs.create(thread.id, {assistant_id : assistantId});
    
    /*
    // Create and Run assistant. Pass Ass ID, thread and messages. Think I may have a problem here. 
    const run = await openai.beta.threads.createAndRun({
        assistant_id: assistantId,
        thread: {
            messages: [
            { role: "user", content: "test" },
            ],
        },
    });
    */


    //retrieve run using thread id and run id. 
    completedRun = await openai.beta.threads.runs.retrieve(run.thread_id, run.id);

    //while status is in progress or queued continue to attempt to retrieve the run / sleep for 1 second between tries. 
    while(completedRun.status === 'in_progress' || completedRun.status === 'queued'){
        
        completedRun = await openai.beta.threads.runs.retrieve(run.thread_id, run.id);
        await new Promise(resolve => setTimeout(resolve, 2000));
    }
    
    //If status is not completed log the status
    if (completedRun.status !== 'completed') {
        console.log(completeRun.status);
    }
    

    // Retrieve the Assistant's response Returning empty messages.data
    const messages = await openai.beta.threads.messages.list(
        thread.id
    );

    return messages;
    
}

// Example Usage
const userQuery = "Please say hello back!"; 

interactWithAssistant(process.env.ASSISTANT_ID, userQuery)
    .then(messages => {    
        console.log(messages.data[0].content[0].text.value);
});

Anyway thought I would post this just in case anyone go stuck like me.

2 Likes