Quick Start Guide: OpenAI Assistants API
OpenAI Assistants API provides a quick way to create smart agents that support features like Code Interpreter, Retrieval, and Function calling.
It offers an easy way to set up a generative AI agent that can be pre-loaded with a custom knowledge-base which can be used when users query information.
This is a quick start guide on working with the Assistants API. For a deeper understanding of Assistants, we recommend reviewing the OpenAI Documentation HERE.
npm install openai
pip install openai
import OpenAI from "openai"; const openai = new OpenAI({ baseURL: {BASE_URL}, apiKey: {YOUR_KEY} });
from openai import OpenAI client = OpenAI( base_url={BASE_URL}, api_key={YOUR_KEY} )
Assistants can be either created programmatically via the SDK or by using the Open AI Assistants UI, which provides an easy-to-use interface for configuring the assistant.
For our projects, Assistants have already been created for you. However, if you wish to explore the creation flow, we recommend creating an account on OpenAI.
const thread = await openai.beta.threads.create({ messages: [{ "role": "user", "content": user_content }] });
thread = client.beta.threads.create( messages=[{ "role": "user", "content": user_content }] )
openai.beta.threads.runs.create(thread_id, { assistant_id: assistant_id })
client.beta.threads.runs.create( thread_id=thread_id, assistant_id=assistant_id, )
Since the run is executed asynchronously, you will have to periodically poll to get status updates of the run. Once the run status is completed
, you can get the generated message.
openai.beta.threads.runs.retrieve(thread_id, run_id)
client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
client.beta.threads.messages.list(thread_id)