AI

OpenAI Assistant API

OpenAI Assistant API

The OpenAI Assistants API allows developers to build and integrate AI assistants
into the application workflows. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user prompts. Note that Assistant API is in the beta phase at this particular time.

Assistant API

Assistant API is an evolution of Completions API, now considered a legacy. The Assistants API supports the following tools:

  • Code Interpreter
  • Knowledge Retrieval
  • Function calling

This tutorial uses Python language and requires proper OpenAI Python SDK setup.

The best way to get the API flow is to interact with the Assistant using the
Platform Playground. Enable the logs to see the API calls executed
under the hood.

Pre-requisites

Things you need for this tutorial:

  • Python Install
  • OpenAI Python SDK
  • Python IDE
  • OpenAI account
  • OpenAI API Credits

Assistants API Integration

The steps to follow when working with the Assistant API are as follows:

Create an AI Assistant

Steps to create an assistant using API can be found here:

https://www.testingdocs.com/create-assistant-api-request/

This is a POST call on the following API endpoint.

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

Create a Thread

  • POST on the API endpoint  https://api.openai.com/v1/threads

Add User message to Thread

Users and assistants can add messages to the thread. Add User Message to a Thread. This simulates the user, prompting the AI assistant.

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

Run the Assistant on the Thread

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

Get Assistant Message on Thread

Poll the Run to check if the status is completed

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

Retrieve the response messages added by the Assistant to
the Thread

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

 

Assistant API Thread Model Response

 

That’s it. You have successfully interacted with the model using the API.