Accessing Threads to start Run API

Hey y’all,

I am trying to build my own veterinarian assistant that talks to the user back and forth about their pet. Right now I am building API’s for each function so that I can call them via an API client like insomnia. Right now I am trying to pass threads to my createRun function which takes the thread_id and assistant_id. I keep getting the error saying “thread_id” not found. I also created a retrieveThread function but that is also saying the same thing. I was reading the other forums and I don’t understand if we must store the thread_id into a database and call it from there? I don’t understand if we store that thread_id somewhere, where is the thread itself? If we can’t find the ID where would the actual thread itself be? Here is my code for nodeJS and ExpressJs:

export async function createThread (request: Request, response: Response){
const openai = new OpenAI({ apiKey });

try {
    const thread = await openai.beta.threads.create();
    console.log(thread);

    return response.json({ thread: thread });
} catch (e) {
    console.log(e);
    return response.json({ error: e });
}

}

export async function retrieveThread (request: Request, response: Response){

const threadId = request.params.threadId;
if (!threadId)
    return response.status(400).json({ error: "No id provided" });

const openai = new OpenAI({ apiKey });

try {
    if (typeof threadId === "string") {
        const thread = await openai.beta.threads.retrieve(threadId);
        console.log(thread);

        return response.json({ thread: thread });
    }


} catch (e) {
    console.log(e);
    return response.json({ error: e });
}

}

This createRun isn’t working as the thread_id is not there.

export async function createRun(request: Request, response: Response) {
const { threadId, assistantId } = request.params;

if(!threadId)
    return response.status(400).json({error: "Missing threadId"})
if(!assistantId)
    return response.status(400).json({error: "Missing assistantId"})


const openai = new OpenAI({ apiKey });
try {
    if (typeof threadId === "string") {
        const run = await openai.beta.threads.runs.create(threadId, {
            assistant_id: assistantId
        });
        console.log({ run: run });
        return response.json({ run });
    }
} catch (e) {
    console.log(e);
    return response.status(500).json({ error: e});
}

}

Any help is greatly appreciated!

While it is true that natively you don’t have access to list threads, utilizing the metadata, you can certainly list threads WITHOUT an external database.

openairetro/examples/listablethreads at main · icdev2dev/openairetro · GitHub illustrates the solution

The solution comes with certain limitations; but see for yourself .

That said, on your code: where are you creating the thread?

Edit: nm i see it now

I think we need to see the logic that calls these functions to really help.

But it is likely that you’re not persisting the response from createThread correctly on the front-end and reusing it well for later calls. If you’re using the session for example you want to make sure that it is persisted on localStorage or similar, so that if a user reloads the page or comes back later, you can still use an existing threadId.

Happy building!

So for now im just testing each function via an API call

as such:

POST http://localhost:1337/ai/run/create

{
“threadId”: “thread_sKbRcSlbVh5atkTaZDC2z4ay”,
“assistantId”: “asst_VNwdPqiaRSF58WdeWePGUVvw”
}

then I get

{
“error”: “Missing threadId”
}

Are you certain you’re setting threadId correctly in the request body params?

Try just logging the complete request, inspect it, and see where the threadId is, then addapt your code to take it from there or set it in the place you expect it.

Cheers!