Alex Holt
Alex Holt

Reputation: 23

OpenAI Assistants API: How do I pass the assistant to the API?

I am trying to pass my assistant to the OpenAI Assistants API, but I get the following error when doing so:

message: 'The model my Model ID does not exist or you do not have access to it.', type: 'invalid_request_error', param: null, code: 'model_not_found'

Code:

try {
    const fetch = await import('node-fetch').then(mod => mod.default);

    const response = await fetch('https://api.openai.com/v1/chat/completions', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${apiKey}`
        },
        body: JSON.stringify({
            model: 'My model ID',
            messages: conversation
        })
    });
}

If I change the model parameter to gpt-3.5-turbo it works, and my app can interface with the standard model.

I know the API key is correct and it has all permissions. The assistant ID I'm trying to pass to the model parameter is also correct. For testing purposes, both are defined in the same file.

Upvotes: 2

Views: 809

Answers (1)

Rok Benko
Rok Benko

Reputation: 23088

Problem

The OpenAI Assistants API doesn't use the Chat Completions API endpoint (i.e., https://api.openai.com/v1/chat/completions). Using the OpenAI Assistants API is fundamentally different (i.e., more complex) than using any other APIs, like the Completions API or Chat Completions API.

Solution

You don't pass the assistant by using the model parameter. You pass the assistant by using the assistant_id parameter (see Step 4 below).

Here are the steps you need to follow to get a response from the assistant:

Step 1: Create an assistant

POST https://api.openai.com/v1/assistants

Step 2: Create a thread

POST https://api.openai.com/v1/threads

Step 3: Add the user's question to the thread

POST https://api.openai.com/v1/threads/{thread_id}/messages

Step 4: Run the assistant

POST https://api.openai.com/v1/threads/{thread_id}/runs

Step 5: Periodically retrieve the run to check its status to see if it has moved to completed

GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}

Step 6: Retrieve the assistant's answer

GET https://api.openai.com/v1/threads/{thread_id}/messages

Also, I've made a YouTube tutorial on how to use the Assistants API and posted the code on my GitHub profile.

Upvotes: 0

Related Questions