OpenAI Assistants API: How to get response from the /v1/assistants API endpoint in Postman?

930 views Asked by At

I created an assistant using platform.openai.com and am trying to access it from Postman.

POST: https://api.openai.com/v1/assistants/{assistant-id}/completions

Body:

{
  "prompt": "Tell me a funny daddy joke about computers.",
  "max_tokens": 60
}

Headers:

Authorization : Bearer API_KEY
Content-Type : application/json
OpenAI-Beta : assistans=V1

And getting response 404 Not found:

{
    "error": {
        "message": "Invalid URL (POST /v1/assistants/asst_EKb5utNB3oVKBHsZusxKKijO/completions)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

If I try https://api.openai.com/v1/assistants/{assistant-id}

Body : {}

I get all my assistant information. So it means my authorization works, but where is the problem with the first approach?

1

There are 1 answers

2
Rok Benko On

You don't understand how assistants work. It's not that straightforward. You missed a lot of steps.

The following are steps you need to follow to get an answer 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 a Message to a 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 on 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 Messages added by the Assistant to the Thread

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

The following image from the official OpenAI documentation is a nice visual example of how assistants work.

Screenshot


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