Skip to content

OpenAI Assistants API Guide (With Practical Python Example)

Table of Contents

OpenAI Released a New feature called “Assistants“.

It is really a game-changer if you understand the power behind it.

OpenAI Assistants Guide
Generated By Dall E 3 – Something that looks like me 😅

In this guide, we will cover the following:

  1. What are OpenAI Assistants?
  2. How To Create an Assistant?
  3. How To Use The Assistants API with a Practical Real Example (Python Script Included)
  4. 3 Methods To Make Money With Assistants.

What are OpenAI Assistants?

Before this new update, OpenAI had two main products: ChatGPT and the API.

ChatGPT is the AI ChatBot that you can interact with through the UI. And the API is for developers who want to use AI-language models like ChatGPT in their apps and codes.

A few months ago, OpenAI added new features and plugins for ChatGPT pro users, such as the code interpreter and file uploads.

These features were only accessible through the UI and not to developers.

But, with the new Assistants API feature, you can now use the new features through the API!

Think of Assistants as a custom GPT that you can build with some extra powers available through the API. This is really super powerful.

You will feel this power after finishing this guide.

How To Create an Assistant?

You can create assistants using the OpenAI Playground (UI) and the API.

1- Using the UI

Let’s start with the simpler approach, which is the UI.

Go to your OpenAI Playground, click on assistants, and then create.

Then, you need to fill in the assistant’s parameters and options.

Give it a name, select the model you want to use. and add your instructions.

Think about instructions as prompts instructing the assistant on what to do.

In my example, I am creating a “YouTube Title Generator” Assitant that helps me generate catchy YouTube Titles for my videos based on a dataset of successful titles and formulas I collected.

So my instruction prompt would be something like:

Generate 5 YouTube titles that get the best CTR based on the title ideas provided in the CSV file. Suggest titles that best fit the given [topic].  

IMPORTANT: The output should be a simple JSON array of 5 titles without field names and without anything else!

Example Output:
[
    "Title 1",
    "Title 2",
    "Title 3",
    "Title 4",
    "Title 5"
]

You can see in my prompt there are three main things that you should pay attention to.

  1. The Task: “Generate 5 YouTube titles
  2. Refer to my CSV: “based on the title ideas provided in the CSV file.
  3. Output format and example: “simple JSON array

Remember, we will use this in our Python code and build Apps later, so returning a JSON is very important.

Of course, as we saw in the Function chains guide, this doesn’t guarantee a valid JSON, and we should follow some more techniques to ensure getting a valid JSON response.

But for now, let’s keep things simple to focus on our concept.

Another important thing you should pay attention to is to check the “code interpreter” to allow the assistant to read data from the CSV file. And, of course, you should upload your dataset file.

That’s it! You can now test your assistant in the playground.

2- Using the API

Let’s build the same example through the API.

First, you need to update the Openai SDK:

pip install --upgrade openai

To Create an Assistant, run this code:

from openai import OpenAI

client = OpenAI()

assistant = client.beta.assistants.create(
    name="YouTube Title Generator",
    instructions="Generate 5 YouTube titles that get the best CTR based on the title ideas provided in the csv file....",
    model="gpt-3.5-turbo-16k",
)

To update with code interpreter and file upload, use the following code snippet:

# Upload the file
file = client.files.create(
    file=open(
        "data/yt_data.csv",
        "rb",
    ),
    purpose="assistants",
)
# Update Assistant
assistant = client.beta.assistants.update(
    ASSISTANT_ID,
    tools=[{"type": "code_interpreter"}],
    file_ids=[file.id],
)

And that’s it! We now have an assistant to call and use anywhere in our code.

Using The Assistant API (Python Example)

To simplify things, I prepared a simple Python script that shows you how to connect and call the assistant in your Python code.

You can copy, replace your openAI api key and assistant ID, and run.

Here it is:

import openai
import time
openai.api_key = "sk-xxx"

assistant_id = "asst_M5Of0iAdupbVsOIO"

def create_thread(ass_id,prompt):
    #Get Assitant
    #assistant = openai.beta.assistants.retrieve(ass_id)

    #create a thread
    thread = openai.beta.threads.create()
    my_thread_id = thread.id


    #create a message
    message = openai.beta.threads.messages.create(
        thread_id=my_thread_id,
        role="user",
        content=prompt
    )

    #run
    run = openai.beta.threads.runs.create(
        thread_id=my_thread_id,
        assistant_id=ass_id,
    ) 

    return run.id, thread.id


def check_status(run_id,thread_id):
    run = openai.beta.threads.runs.retrieve(
        thread_id=thread_id,
        run_id=run_id,
    )
    return run.status

my_run_id, my_thread_id = create_thread(assistant_id,"[topic]: make money with chatgpt")

status = check_status(my_run_id,my_thread_id)

while (status != "completed"):
    status = check_status(my_run_id,my_thread_id)
    time.sleep(2)

response = openai.beta.threads.messages.list(
  thread_id=my_thread_id
)

if response.data:
    print(response.data[0].content[0].text.value)

This is a very basic example to help you get started. Please optimize and edit as you like.

Now let’s see the real power of using the assistants API.

3 Methods To Make Money With Assistants

Let me share with you 3 Real-world examples of how to turn the assistant APIs into an income stream!

1- Build an AI Tool

Building online AI tools and monetizing them through monthly memberships or credit systems.

For example, I created the YouTube Title Generator Tool with a set of other AI tools and monetized it with the power points system. It is like a mini-SaaS on my WordPress site.

Here is a full guide on how I built the SAAS-Points system on WordPress.

2- Build an API

A simpler approach is to create an API and sell on marketplaces like RapidAPI.

This way, you don’t need to worry about the UI or payment systems. Just publish the API, and sell directly on marketplaces.

You can learn more about Building and Selling APIs in my full course.

3- AI Automation Services

Or what we call AI automation agencies or AI freelancing services.

Now you can build AI assistants for businesses, helping them automate and grow their business with AI.

You can start freelancing, scale later, and create your AI Automation agency.


Whether you want to create AI tools, APIs, or provide automation services. You must first consider developing your skills, like Python programming, data collection, scraping, building automation scripts, and even marketing.

This is why you must dedicate at least 1-hour hour daily to learning these skills.

Otherwise, there will be nothing unique about your work, and you will not be able to compete with others.

I am here to help. You can get started for FREE with the following guides and courses:

  1. Prompt Engineering Series
  2. The RJP technique
  3. Function Chains
  4. How To Use AI in Marketing (11 Practical Examples)
  5. Join our Free Public Forum

And, if you want to move to the next level and join our private support community, you can check my premium services.

Good Luck!

2 thoughts on “OpenAI Assistants API Guide (With Practical Python Example)”

  1. Hi Hassan, in your first step “using the UI”, can the assistant be created using UI in a python code? I’m trying to build a system that can extract data from an annontated receipt in pdf format, the system must be able to deal with many types of receipt for different type of companies, and return a dataframe with rows and columns in excel format, then the returned dataframe will now be be standardized based on certain conditions.
    I want to implement this using openai. I recently realized that I need python to achieve this. So I want to start learning python, please I need advice and recommendation.

Leave a Reply

Your email address will not be published. Required fields are marked *