AI Experiment #1 Building a Machine Learning Assistant with GPT-4o model

This article is part of my series on experiments using Large Language Models (LLMs) and Generative Artificial Intelligence (GenAI) to create fun and valuable projects to help expand my experience with AI technologies.

Yadia
7 min readMay 16, 2024
Generated with Creative AI, an image of a dragon interacting with a computer

What is an AI Assistant?

An AI Assistant or Agent is an application designed to help you perform specific tasks, answer questions, or provide information. Think of them as personal assistants who know everything. They can be used for tutoring, chatbots for product guides, and customer support with a better understanding of Natural Language and assist you with your coding. The benefit of these AI assistants is their capability of quick information retrieval, which can be built to provide detailed step-by-step or concise instructions for the tasks they are designed for.

For this experiment, I wanted to create an assistant that would help enhance my technical skills in machine learning concepts. You can change my instructions to focus on a new learning topic that fits your preference.

What do you need to know beforehand?

  • Experience working with REST APIs [1]
  • Intermediate knowledge of Python [2]
  • Familiar with AI/ML concepts, particularly prompting engineering and model tunning.

Required Material

  • OpenAI API key https://platform.openai.com/ . This will be bound to your developer account. Make sure to create a dedicated key for this project.
  • Add a $5 budget to your OpenAI Account.
  • Read the Assistants API documentation. [3]

Model Selection and Pricing

OpenAI offers a variety of LLM models to choose from. A good rule of thumb for selecting models is to consider what features and functions you will need for your project and choose the model with the best functionality, enough context window, and cost sensitivity.

You can refer to the documentation on OpenAI [4]for the list of models and their most recent update on the training data and cost.

For this assistant, I want a model that is trainable with good instruction following, has JSON mode, and has an option for future development of image recognition features. Thus, I selected gpt-4o for this. If you would like to be cost-effective, you could choose gpt-3.5 turbo.

List of models available to choose from for the assistant

Method 1: Using OpenAI Platform Web GUI

The OpenAI platform now has the Assistant v2 tab available in their developer playground. This is a fast and easy way to create and deploy your assistant within your account. Note that any assistant you create can be used by any other member of your OpenAI’s organization.

The Assistant playground simplifies the deployment of the assistant allowing you to focus more on building what you want it to do.

My instructions

“””

You are an expert tutor specializing in machine learning and AI technologies. You are required to:

1. Help educate and enhance your student’s technical capabilities to help them master complex topics and demystify them.

2. Provide concise answers but include explanations as to why.

3. Whenever possible, always include links to materials for students further reading.

4. If the user asks about topics other than machine learning or AI, tell them that the tutor assistant does not perform this function.

“””

Tip for writing instructions:

  • You want to be clear and specific with your assistant.
  • Create exception cases to catch questions asked by the user that may be out of scope with the assistant’s use case.

Tools

  • File Search: ON, as we would like it to be able to search from the files we upload to your ML learning material.
  • Code Interpreter: Off, since this assistant focuses on theory for the first step and not on coding and cost, as it is billed at $0.03 per session. However, in the future, we could update this to write and run Python code.
  • Functions: Off, this would allow us to call other custom functions and applications via JSON Objects to extend the assistant’s functionality.

Model configuration

This configuration modifies the response output of our assistant

  • JSON Object: It allows the response to be formatted in a JSON Object.
  • Temperature: It impacts how creative or predictable our assistant’s responses will be. The lower the value, the more predictable and the more creative it is. By default, it’s set to 1.
  • Top P: it is used by our assistant to choose and filter the relevant words when generating a response. Its value is between 0.1–1. By default it is 1.
  • API Version: currently, by default, the assistants on the playground call the v2 API.

Interacting with the Assistant

Once you have defined all the assistants’ configurations, you can go to the playground and enter your message. This will show how a user interacts with the assistants and their replies. It will utilize threads, which will allow the assistant to keep track of the previous message inputted by the user.

I tested out our out-of-scope question by asking it about cats. It very politely informed me that it specializes in ML and AI technologies, so it can not provide information on topics like cats. On the other hand, when asked about examples of unstructured data it provided a list of 5 examples with information on how this data can provide insights.

OpenAI Playground user interaction with our ML tutor

Method 2: Using Assistant APIs

Setting up our environment

I prefer using Visual Studio Code, which is already prepared to run Python environments. I have included a reference at the bottom of the article if you need help setting up your VS Code environment.

Install the following items via pip:

  1. python3
  2. openai
pip install python3 
pip install openai

Step 1: Import the openai package and your API key to your python script

I have also imported time to be used for steps below on our while loop.

from openai import OpenAI
import time

#set up keys
client = OpenAI(api_key="sk-abc........")

Step 2: Declare your assistant ID

You can obtain this key if you have done method 1 or go into the Platform and create an assistant to generate the new id.

Assistant ID from OpenAI Platform
# import asssistant id if already generated on playground
assistant_id = "asst_abcs....."

Step 3: Create a new Thread

A Thread in the OpenAI API is a collection of messages with a unique ID. This allows us to maintain context and utilize previous message history for tailored responses. In this thread, you will create the prompt from the student’s perspective. This is what you can use on your applications to send the user input to your assistant.

chat = client.beta.threads.create(
messages=[
{
"role":"user",
"content": "Provide me two examples of structured data" #user input message
}
]
)

Step 4: Create a Run

Runs represent execution of the thread. This runs sends the user prompt to the assistant for preocessing giving it a unique thread id and run id.

run = client.beta.threads.runs.create(thread_id=chat.id, assistant_id=assistant_id_1)
print(f"Run created:{run.id}")

Step 5: Obtain the status of the run

Since the run requires execution time, you must obtain the status. For debugging purposes, I print the “in progress” status to inform you that the request is running. Once it is completed, the status will change to this.

while run.status != "completed":
run = client.beta.threads.runs.retrieve(
thread_id=chat.id,
run_id=run.id
)
print(f"RUN STATUS: {run.status}")
time.sleep(0.5) #here I use the time to retry this while loop.
else:
print(f"RUN COMPLETTED! : {run.status}")

Step 6: Filter the Response message for text only

You will need to call the thread id and obtain the last message. Since the response provides you with a set of information you will need to filter for the student to only see the text. Once you have done you are able to show the responses only. Make sure to add a function to check that you are accessing only the last message from this thread. For debugging purposes I included an error to print if there is no latest message or response.

response = client.beta.threads.messages.list(thread_id=chat.id)
messages = response.data
# Ensure there are messages before attempting to access the last message
if messages:
last_message = messages[0] # Get the latest message
# Ensure there are content blocks before attempting to access the first one
if last_message.content:
print(f"AI: {last_message.content[0].text.value}")
else:
print("No content blocks found in the latest message.")
else:
print("No messages found in the response.")

Further developments

Creating an assistant is faster with the OpenAI portal. The OpenAI team has provided an intuitive interface for creating the assistant. However, the true power is unlocked when embedding these assistant functions into new applications with the API. In my future experiments, I will dig deeper into projects that could benefit from obtaining data by user input or calling an external API to provide up-to-date information. Nevertheless, this assistant unlocks future opportunities

References and Resources

  1. FreeCodeCamp has an excellent series on REST APIs
  2. I recommend DataCamp’s Python course or CodeCademy Learn Python 3 for practicing python skills
  3. OpenAI Assistant creation tab https://platform.openai.com/docs/assistants/overview
  4. OpenAI list of updated models and details https://platform.openai.com/docs/models/continuous-model-upgrades
  5. Visual Studio Code Tutorial on getting started with Python https://code.visualstudio.com/docs/python/python-tutorial

--

--

Yadia

IoT |AI & Data aficionado | Automation Junkie | 🇹🇼🇭🇳 | All comments, code, and opinions by this profile represent my views only.