Create Your First AI Assistant — OpenAI Assistant API

Shweta Lodha
4 min readNov 16, 2023

Last week OpenAI announced a whole bunch of new features. Out of those, some are game-changing ones like extremely high context length with GPT-4 turbo, reduced prices of existing models, Assistant API, Vision API with GPT 4 turbo, Dall.E API and, lot more. So, out of all those, let’s have a closer look at the Assistant API today and see how we can create our own assistant in just a few lines of code.

Using Assistant API, we can build AI assistants in our own applications with just few sets of instructions.

Before we get started, make sure that you have installed the OpenAI, a python library. Let’s get started by grabbing the keys.

Get OpenAI API Key And Organization ID

To get the keys, you need to go to https://openai.com/, login and then grab the keys using highlighted way.

Here we need to grab 2 things — API key and Organization ID

Then set these values in an environment variable:

import os
os.environ["ORGANIZATION_ID"] = "ORG_ID_HERE"
os.environ["OPENAI_API_KEY"] = "API_KEY_HERE"

Import Required Packages And Create Client Object

Do install the dependent library OpenAI and import the below package:

from openai import OpenAI

Once package is installed, we are good to go ahead and create client object for OpenAI, which takes 2 parameters:

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"],organization=os.environ["ORGANIZATION_ID"])

Add Documents

Next, we need to associate all the files which we want to use as our doc store and that can be done as shown below:

file = client.files.create(
file=open("Homelessness.txt","rb"),
purpose="assistants")

client.files.list()

Here, I’m taking just one file, but you have more than one, feel free to add those as well. Once all the files are added, you can list them out using list(). Output would look like this:

SyncPage[FileObject](data=[FileObject(id=’file-JBQqsCgW7xoMbYhzU4DvXnii’, bytes=11822, created_at=1700080318

--

--