Hi, I’m having a bit of a nightmare. When trying to connect to the Assistants API my Thread.id is being treated as an object. I have checked and they are all String types.
I know i’m doing something stupid just not 100 what it is as I am new to the API.
Can someone give me a gentle nudge in the right way either in the docs or code example. I did ask GPT as well XD.
require('dotenv').config();
const { OpenAI } = require('openai');
const pino = require('pino');
const logger = pino({ level: 'info' });
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function interactWithAssistant(assistantId, userQuery) {
try {
// Throttle requests needs to be updated to dynamic throttling
await new Promise(resolve => setTimeout(resolve, 1000));
// Create empty Thread and get ID
const threadResponse = await openai.beta.threads.create();
const thread = threadResponse.id;
// Add a Message to the Thread
await openai.beta.threads.messages.create(thread, {
role: "user",
content: userQuery
});
// Run the Assistant
const run = await openai.beta.threads.runs.create(thread, { assistant_id: assistantId });
let completedRun;
do {
// Polling every 1 second before checking the run status again.
await new Promise(resolve => setTimeout(resolve, 1000));
completedRun = await openai.beta.threads.runs.retrieve(thread, run.id);
} while (completedRun.status === 'in_progress');
if (completedRun.status !== 'completed') {
throw new Error(`Run did not complete successfully. Status: ${completedRun.status}`);
}
// Retrieve the Assistant's response
const messages = await openai.beta.threads.messages.list({
thread_id: thread
});
return messages.data.filter(msg => msg.role === 'assistant');
} catch (error) {
logger.error({ err: error }, 'An error occurred');
return null;
}
}
// Example Usage
const userQuery = "Why are you not working!";
interactWithAssistant(process.env.ASSISTANT_ID, userQuery)
.then(messages => {
if (messages) {
messages.forEach(msg => {
logger.info('Assistant:', msg.content.text.value);
});
}
});
Error:
“error”:{“type”:“Object”,“message”:“No thread found with id ‘function toString() { [native code] }’.”,“stack”:“”,“param”:null,“code”:null},“code”:null,“param”:null},“msg”:“An error occurred”}
Not quite sure what im doing wrong. All help appreciated!