What Are GPT Assistants?

Codecademy Team
Learn how to create and use GPT Assistants.

Introduction

OpenAI is best known for the development of ChatGPT, the most common natural language chatbot available on the internet. Interestingly, OpenAI developed the ability for other developers to create their own Generative Pre-Trained Transformers (GPTs) via the GPT Store and GPT Assistants.

OpenAI’s ChatGPT is one example of a GPT. Recently, OpenAI created a platform for the community to create GPTs focused on a specific topic. For example, OpenAI developed CanvaGPT, a ChatGPT variant further trained on Canva’s functionalities and capabilities. OpenAI has released a GPT store for GPT creators to share profits in their creations.

A GPT Assistant is specifically oriented for developers. It requires some programming knowledge to format the required parameters to assist users. A GPT Assistant allows a developer to create custom queries, provide files, and most notably, can be integrated into their applications.

In this article, we’ll focus on GPT Assistants. We’ll discuss when to use GPT Assistants, what’s available with GPT Assistants, and lastly, walk through an example of creating a GPT Assistant.

What is a GPT Assistant?

GPT Assistants are specialized GPTs that allow developers access to the parameters of the GPT. Well, why not just build a GPT? The reality is that building a GPT can be time-consuming. Consider the idea that we may want to create multiple GPTs. With GPT Assistants, we can quickly create assistants for a variety of applications. Let’s introduce some of the options available during Assistant creation.

A: Assistant Name. The name of the Assistant.

B: Instructions. These are the overarching rules of the Assistant. A great example is the one on the GPT Assistant’s Webpage. Here, they provide the following instructions: “You are a personal math tutor. Write and run code to answer math questions.” With these instructions, we would be able to provide any math question, creating a math tutor GPT Assistant.

C: Model. Specific models are available for use with GPT Assistants. The following models are available in the Assistants Playground:

    a. gpt-4-1106-preview
    b. gpt-4-0613
    c. gpt-4-0314
    d. gpt-4
    e. gpt-3.5-turbo-16k-0613
    f. gpt-3.5-turbo-16k
    g. gpt-3.5-turbo-1106
    h. gpt-3.5-turbo-0613
    i. gpt-3.5-turbo

D: Tools. There are four specific tools available to GPT Assistants.

  1. Functions: Functions let you describe custom functions of your app or external APIs to the assistant. This allows the assistant to intelligently call those functions by outputting a JSON object containing relevant arguments.
  2. Code Interpreter: Code Interpreter enables the assistant to write and run code. This tool can process files with diverse data and formatting, and generate files such as graphs and data visualizations using data sets.
  3. Retrieval: Retrieval enables the assistant with knowledge from files that you or your users upload. Once a file is uploaded, the assistant automatically decides when to retrieve content based on user requests.
  4. Files: By uploading files, you enable the assistant to use the content from these files for retrieval and code interpreter, if these tools are enabled.

GPT Assistants were specifically created to be placed within applications using OpenAI’s Assistant API. From a purely exploratory perspective, GPT Assistants Playground allows you to create a GPT Assistant without writing any code to understand its capabilities.

Walk-through Using GPT Assistants

Let’s walk through an example of how to use GPT Assistants. We’ll use a standalone GPT Assistant. First, we’ll prepare our environment using OpenAI’s quickstart guide to develop a GPT Assistant in Python. Next, We’ll create out GPT Assistant and prepare the assistant to continue interactions with a user until the user ends the sessions.

To begin, you’ll need to complete step 1 and 2 of OpenAI’s Quickstart guide.

With the setup complete, let’s create a GPT Assistant.

This is NOT free. This will require you to buy credits on OpenAI’s platform. The minimum payment for credits is $5(USD).

  1. Let’s create a GPT Assistant by establishing a connection to the OpenAI API
  2. Let’s establish a thread to contain all the messages for our Assistant
  3. Let’s create a function called ask_gpt(prompt) that returns a response from the GPT Assistant with the provided user prompt
  4. We’ll create a loop to always ask the user for input until they provide the command exit

GPTAssistant.py

import time
from openai import OpenAI
# Establish OpenAI Client
client = OpenAI()
# Method to prompt GPT Assistant. This is called for each time the user prompts the Assistant
def ask_gpt(prompt):
try:
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=user_input
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
complete = False
while(not complete):
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if(run.status == "completed"):
complete = True
else:
time.sleep(5)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
return messages.data[0].content[0].text.value
except Exception as e:
return f"An error occurred: {str(e)}"
assistant = client.beta.assistants.create(
name="Personal Assistant",
instructions="You are a personal assistant,",
model="gpt-4-1106-preview"
)
thread = client.beta.threads.create()
print("Welcome! You can start chatting with the GPT Assistant. Type 'exit' to end the conversation.")
while True:
user_input = input("You: ")
if user_input.lower() == 'exit':
print("Exiting the conversation.")
break
else:
response = ask_gpt(user_input)
print(f"GPT Assistant: {response}")

The script declares a new GPT Assistant using the OpenAI client.beta.assistants.create() function. Next, we begin a thread with the GPT Assistant. Until the user indicates it wants to stop with the exit command, the user can interact with the GPT Assistant. For each interaction, the GPT Assistant will reply with a message. Each message is placed in the thread that we as developers can access. After the user types in a message to the GPT Assistant, we return the response to the user.

Great work! We built our own GPT Assistant using the OpenAI GPT Assistant API. Due to the requirement of checking the run status, the solution uses a sleep(5) to wait five (5) seconds before trying to determine if the GPT Assistant has responded. Once it responds, we set the complete variable to True and provide the message back to the user.

Running this script will begin a dialogue with a GPT Assistant created by the script. Although this GPT Assistant is a standalone script, it could very well be placed in a website, application, or other developer form.

Conclusion

Well done! We first introduced the idea of a GPT Assistant, denoting the different between a GPT and a GPT Assistant. Next, we began our development of a GPT Assistant. Initially, we setup our environment to include our individual OpenAI API key. We then created a GPT Assistant in Python to provide general assistance.

We had to use the OpenAI API to determine how to query the run() function since there is no way to properly wait for a response from a GPT Assistant. A simple sleep(5) combined with a for loop worked for us!

If you’re looking for next steps on how to use GPT Assistants, try moving this script from a standalone to a simple web application. Additionally, you can also try integrating code interpreter, retrieval, or file inclusion tools available to GPT Assistants.

To see what else you can do with generative AI in general, checkout some of the articles located here: AI Articles.