Creating A Basic OpenAI Assistant Notebook

This article walks through the most basic OpenAI Assistant framework you can create, with all the application elements required to build the Assistant out.

Cobus Greyling
5 min readNov 9, 2023

--

In this article I put together the simplest OpenAI Assistant possible, comprising of only a few lines of code you can copy and paste into a Notebook.

Introduction

The OpenAI assistant can be described as a prompt-less autonomous agent which is defined in natural language. The initial feel of the assistant dashboard (which is an extension of the playground) is clean and easy to setup and manage.

But, like most things, challenges will occur when application complexity needs to be introduced. After prototyping for a short while, a few challenges become clear.

Firstly, the OpenAI dashboard will most probably work well for viewing the logs, but for creating, listing, managing and developing an assistant, the APIs will need to be used.

The run object will become very important, as the run status (there are four statuses) will have to be determined in order for the assistant to know which next step to take.

So levels of granularity will have to be introduced to inspect various stages of the user interaction. OpenAI is really competing with a host of build tools which range from autonomous agents, to prompt chaining in no-code to low-code environments.

Assistant Objects

The OpenAI assistant agent is comprised of five objects, each of these objects need to be defined in the Python code to create the simplest of applications.

Installation and Imports

Install OpenAI, import a few modules and define the API Key.

pip install openai

import os
import openai
import requests
import json
from openai import OpenAI

api_key = "your api key goes here"

Create Assistant

Next the assistant needs to be created, the assistant has a name, which will show up in the OpenAI dashboard. The instructions element is used to describe the assistant. And lastly the model is defined.

#Create Assisant
assistant = client.beta.assistants.create(
name="General Knowledge Bot",
instructions="You answer general knowledge questions as accurately as possible.",
model="gpt-4-1106-preview"
)

Create A Thread

Conversation and dialog management is performed via threads. Threads are persistent and stores message history. Truncation of conversations is also performed when conversations get too long for the models context length.

A thread is created once, and messages are appended to the threat as the conversations ensue.

#Create Thread
thread = client.beta.threads.create()

Create A Message

A Message contains the user’s text or files the user uploads. Image files are not currently supported, but OpenAI states that images will be supported in the coming months.

#Create Message
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="What is the weather like in general in the city of Cape Town?"
)

Run

In order for the assistant to respond to the user messages, run needs to be created. This allows the agent to read a thread and interpret which actions need to be taken. Messages are simply appended to the thread.

#
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Answer the question as accurately as possible."
)

Runs can be queried, and at certain stages the run can be retrieved to check its status if it has moved to completed.

This creates a Run in a queued status. You can periodically retrieve the Run to check on its status to see if it has moved to completed.

Source
#
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)

Retrieve Messages

When a Run is completed, you can retrieve the Messages added by the Assistant to the Thread.

#
messages = client.beta.threads.messages.list(
thread_id=thread.id
)

print (messages)

Below the complete program:

pip install openai

import os
import openai
import requests
import json
from openai import OpenAI

api_key = "Your API Key Goes Here"

client = OpenAI(api_key=api_key)

#Create Assistant
assistant = client.beta.assistants.create(
name="General Knowledge Bot",
instructions="You answer general knowledge questions as accurately as possible.",
model="gpt-4-1106-preview"
)

#Create Thread
thread = client.beta.threads.create()

#Create Message
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="What is the general weather like in Cape Town?"
)

#
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Answer the question as accurately as possible."
)

#
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)

#
messages = client.beta.threads.messages.list(
thread_id=thread.id
)

print (messages)

⭐️ Follow me on LinkedIn for updates on Large Language Models ⭐️

I’m currently the Chief Evangelist @ Kore AI. I explore & write about all things at the intersection of AI & language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces & more.

LinkedIn

--

--

Cobus Greyling

I explore and write about all things at the intersection of AI & language; LLMs/NLP/NLU, Chat/Voicebots, CCAI. www.cobusgreyling.com