OpenAI Assistants API Tutorial - Playground & NodeJS

Leon van Zyl
10 Nov 202317:47

Summary

TLDRThe video script offers a comprehensive guide on creating and implementing an AI assistant using Open AI's assistance API. It demonstrates the process of setting up a math tutor assistant, selecting the appropriate model, and utilizing tools such as code interpreter and knowledge retrieval. The tutorial further explains how to manage conversations through threads, create and retrieve messages, and execute the assistant's functions within a Node.js project environment.

Takeaways

  • πŸš€ Creating an AI assistant involves selecting a name, providing instructions, and choosing the tools and model for the assistant.
  • πŸ“ The process starts by navigating to a platform and selecting the 'Create' option to define the assistant's purpose and capabilities.
  • 🧠 Models like GPT-4110 or GPT-3.5 turbo are recommended for building assistants due to their advanced capabilities.
  • πŸ› οΈ Tools such as code interpreter and knowledge base retrieval can be enabled to enhance the assistant's functionalities.
  • πŸ”„ Threads represent unique conversations that can be retrieved and continued using a unique thread ID.
  • πŸ“Š The assistant's dashboard allows for the management of created assistants, including making changes and testing the assistant's capabilities.
  • πŸ”§ In a node project, the OpenAI package is used to interact with the OpenAI assistance API, and the API key is stored securely in an .env file.
  • πŸ” The 'create', 'retrieve', 'threads', 'messages', and 'runs' methods are key functions for managing and interacting with the AI assistant.
  • πŸ“Œ Storing the assistant ID and thread ID is crucial for managing and continuing conversations within the application.
  • πŸ“ˆ The status of a 'run' indicates the progress of the assistant processing messages and generating responses.
  • πŸ”Ž Logs provide insights into the steps taken by the assistant to process messages and can be used for troubleshooting.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is about creating and implementing an AI assistant using Open AI's assistance API, specifically for a math tutor application.

  • How does one get started with creating an AI assistant on the Open AI platform?

    -To get started, one should visit platform.open.com, click on 'assistants' on the left-hand side, and then click on 'create' to enter the details for the new assistant.

  • What are the instructions entered for the math tutor assistant in the video?

    -The instructions entered for the math tutor assistant were 'You are a personal math tutor, write and run code to answer math questions.'

  • Which models are recommended for use with assistants at the time of recording the video?

    -The recommended models for use with assistants at the time of recording are GPT-41106 preview and GPT-3.5 turbo 11106.

  • What tools can be enabled for the assistant, and what is their purpose?

    -Tools such as 'code interpreter' and 'retrieval' can be enabled. The 'code interpreter' allows the assistant to write and execute code, while 'retrieval' allows the assistant to answer questions from a knowledge base.

  • How are threads used in the AI assistant application?

    -Threads represent individual conversations in the application. Each new conversation generates a unique thread ID, which can be used to retrieve the conversation history and allow users to continue past interactions.

  • What is the process for adding a message to an existing thread?

    -To add a message to an existing thread, one can use the 'openai.beta.threads.messages.create' method, specifying the thread ID and an object containing the user's message.

  • How can the AI assistant's response be retrieved after running the assistant?

    -The AI assistant's response can be retrieved by using the 'openai.beta.threads.runs.create' method with the thread ID and assistant ID, and then checking the status of the run. Once the status is 'completed', the response can be fetched.

  • What is the purpose of the 'logs' in the AI assistant application?

    -The 'logs' provide a record of the individual calls to the assistant's API that make up the conversation. They are useful for troubleshooting and understanding the steps taken to generate the assistant's response.

  • How is the Open AI package used in a Node.js project?

    -In a Node.js project, the Open AI package is used to interact with the Open AI assistance API. It requires installation via npm and the use of the API key, which is then utilized to perform various actions on the assistant.

  • What is the significance of the assistant ID and thread ID in the application?

    -The assistant ID uniquely identifies the created assistant, while the thread ID uniquely identifies individual conversations. Both are crucial for managing and retrieving information related to the assistant and its interactions.

Outlines

00:00

πŸš€ Introduction to Creating an AI Assistant

The video begins with an introduction to Open AI's assistance API, highlighting its capabilities and how it can be utilized to create a personalized AI assistant. The creator demonstrates the process of setting up an account on the platform, selecting the desired model (GPT-41106 Preview or GPT 3.5 turbo), and configuring the assistant with specific instructions and tools, such as a code interpreter for a math tutor demo. The video emphasizes the importance of threads, which represent unique conversations, and how they can be used to continue past interactions with the AI assistant.

05:02

πŸ“ Setting Up the Node Project and Assistant

In this segment, the focus shifts to setting up a Node project to interact with the AI assistant. The creator guides the viewer through the process of initializing a new Node project, installing necessary packages, and configuring environment variables. The video details the creation of a new assistant named 'economics tutor' with specific instructions and tools, and demonstrates how to retrieve an existing assistant using its ID. It also explains the concept of threads as conversations and how they can be managed within a production application.

10:02

πŸ’¬ Managing Conversation Threads and Messages

This part of the video script delves into the management of conversation threads and messages. The creator illustrates how to create a new thread, add messages to it, and run the assistant to process these messages. The video emphasizes the use of unique thread and run IDs for tracking and managing conversations. It also covers the retrieval of messages and logs from a thread, providing insights into how the AI assistant processes user inputs and generates responses.

15:04

πŸ” Reviewing Logs and Enhancing the AI Assistant

The final paragraph focuses on reviewing logs and enhancing the AI assistant's functionality. The creator demonstrates how to fetch and analyze logs for a specific thread, providing a deeper understanding of the assistant's operations. The video also suggests that there are many other topics that could be explored to improve the AI assistant, inviting viewers to provide feedback for future content. The segment concludes with a call to action for users to engage more with the assistance API and explore its potential further.

Mindmap

Keywords

πŸ’‘API

API, or Application Programming Interface, is a set of rules and protocols for building and interacting with software applications. In the context of the video, the API is a crucial component that allows the user to create and interact with AI assistants, enabling functionalities such as math tutoring and code execution.

πŸ’‘Assistant

An AI assistant is a software program that uses artificial intelligence to perform tasks or services for users. In the video, the creation of an 'Assistant math tutor' is a central theme, showcasing the process of setting up an AI to provide personalized math tutoring services.

πŸ’‘Node Project

A Node project refers to a software development project that utilizes Node.js, a runtime environment for executing JavaScript code outside of a web browser. In the video, the node project is used to implement and interact with the AI assistant within a development environment.

πŸ’‘Code Interpreter

A code interpreter is a program that executes code written in a programming language, allowing users to run their code and see the results. In the context of the video, the code interpreter is a tool enabled for the AI assistant, enabling it to write and execute code to solve mathematical problems.

πŸ’‘Thread

In the context of the video, a thread represents a conversation or interaction sequence between the user and the AI assistant. Each thread has a unique ID that allows for the tracking and continuation of the conversation, enabling the AI to reference past interactions and provide contextually relevant responses.

πŸ’‘Knowledge Base

A knowledge base is a collection of information, data, or knowledge that is used for reference or support in various contexts. In the video, the knowledge base is used by the AI assistant to answer questions, providing a source of information that the assistant can draw from to generate responses.

πŸ’‘Model

In the context of AI, a model refers to the underlying algorithm or structure that the AI system uses to process input and generate output. The choice of model can significantly impact the AI's performance and capabilities. In the video, different models like GPT-4 and GPT-3.5 turbo are mentioned as options for the AI assistant.

πŸ’‘Environment Variables

Environment variables are values that can be set and used by software programs to store configuration details or settings. They are used to control the behavior of the program without modifying the program's source code. In the video, environment variables are used to store sensitive information like the Open AI API key.

πŸ’‘Retrieval

In the context of AI assistants, retrieval refers to the process of accessing and bringing back relevant information from a knowledge base or previous interactions. This functionality allows the AI to provide contextually relevant responses and continue ongoing conversations.

πŸ’‘Logs

Logs are records of events or actions that occur during the operation of a system or application. They are used for debugging, monitoring, and analyzing system performance. In the video, logs are used to understand the steps taken by the AI assistant when processing user messages and executing code.

πŸ’‘Run ID

A Run ID is a unique identifier for a specific instance or session of an AI assistant's operation. It is used to track and manage the execution of tasks, allowing developers or users to monitor the progress and outcomes of the AI's actions.

Highlights

The video provides a comprehensive guide on creating an AI assistant using the Open AI's assistance API.

The process begins by visiting the Open AI platform and selecting the 'Assistants' feature.

A new assistant can be created by entering a name and instructions for the assistant's function.

The assistant can be tailored with specific tools, such as a code interpreter, for enhanced capabilities.

The model selection is crucial, with GPT-41106 Preview or GPT 3.5 Turbo being recommended options.

Once created, the assistant can be managed and tested from the 'Assistants' dashboard.

Threads represent unique conversations and can be used to continue past dialogues.

The video demonstrates solving a math equation using the assistant's code interpreter function.

The solution to the equation is displayed, showcasing the assistant's ability to process and answer questions.

Logs can be accessed for troubleshooting and understanding the steps taken by the assistant.

The video then transitions to implementing the assistant in a Node.js project.

It explains how to set up a new Node project and install the necessary Open AI package.

The process of fetching the Open AI API key and configuring the project is detailed.

Creating and retrieving assistants through code is demonstrated, including handling the assistant ID.

The video shows how to create a new thread and message for a conversation.

Running the assistant involves creating a 'run' with a thread ID and optional instructions.

The status of a 'run' can be checked, and the final response is retrieved from the thread.

The assistant's response, along with user messages, can be logged and reviewed.

The video concludes by discussing the potential for further exploration of the assistance API.

Transcripts

play00:00

open ai's assistance API is a game Cher

play00:03

and in this video I will show you

play00:05

everything you need to know to create

play00:07

your very own assistant and we will also

play00:10

have a look at implementing these

play00:11

assistant in a node project to get

play00:14

started go over to platform. open.com

play00:17

and then on the left hand side click on

play00:20

assistants here you will see a list of

play00:22

available assistants to create an

play00:25

assistant click on create we can now

play00:27

enter a name for our assistant for this

play00:29

demo I would like to create a math tutor

play00:32

so let's call this Assistant math tutor

play00:35

and for the instructions I will enter

play00:38

you are a personal math tutor write and

play00:41

run code to answer math questions and we

play00:43

can use this to Prime our model we now

play00:46

need to select the model that we would

play00:48

like to use at the time of recording the

play00:51

best models to use with assistance is

play00:54

GPT

play00:55

41106 preview or GPT 3.5 terbo

play01:00

11106 now we can select the tools that

play01:03

we will make available to our bot for

play01:05

example we can add functions if we click

play01:08

on ADD we can see examples of these

play01:11

functions for instance a function to

play01:13

fetch the current weather code

play01:15

interpreter allows the assistant to

play01:17

write and execute code for this demo

play01:20

that's exactly what I want so I'll

play01:22

enable this retrieval allows the

play01:24

assistant to answer questions from a

play01:27

knowledge base so if you want you can

play01:29

enable retrieval and then upload files

play01:32

for the knowledge base these files could

play01:34

be text files PDFs Word documents CSV

play01:39

and more to create the assistant all we

play01:41

need to do is click on Save now in the

play01:44

assistants dashboard we can see our new

play01:47

assistant we can click on this assistant

play01:49

to make additional changes or to run

play01:52

this assistant we can simply click on

play01:54

test in the playground we can make

play01:56

changes to our assistant but in the

play01:59

middle of the page we can see this

play02:01

heading called thread threads are

play02:03

conversations whenever we start a new

play02:06

conversation a unique thread ID will be

play02:09

generated and we can then use these

play02:12

threads in very unique ways for instance

play02:16

in our code we can retrieve an existing

play02:19

thread and that will pull in the

play02:21

conversation history and that will allow

play02:23

the user to continue with a past

play02:26

conversation we will see that in action

play02:28

in the note section of this video in

play02:30

order to add messages to a thread we can

play02:33

simply add a message here and since this

play02:36

is a math tutor let's ask it to solve

play02:38

this equation we now have the option of

play02:41

adding this message to the thread and

play02:44

this will add the message without

play02:46

executing the assistance and it will

play02:48

allow us to add multiple messages to

play02:50

this thread I'll simply click on ADD and

play02:53

run to execute this immediately and

play02:56

after a second we can see that the code

play02:58

interpreter was triggered and as a

play03:00

result we can see that the solution to

play03:03

the equation is x = 1 because in our

play03:07

instructions we asked our assistant to

play03:09

write and run code to answer these

play03:11

questions this triggered the code

play03:14

interpreter we can actually click Within

play03:16

These brackets to view the python code

play03:19

that was written to solve this problem

play03:21

and we can simply click on code

play03:23

interpreter to close this secondly we

play03:25

can click on this little arrow over here

play03:28

to view the result which was one to see

play03:31

what happened behind the scenes we can

play03:33

now click on logs and in logs we can see

play03:36

the individual calls to the assistant's

play03:39

API that make up this conversation and

play03:42

this is useful for troubleshooting the

play03:45

assistant now let's have a look at this

play03:47

in our note project in vs code I've

play03:49

simply opened up a new folder so let's

play03:52

open up the terminal to initiate a new

play03:54

node project by entering npm in it- y

play03:59

now I'm just going to make one small

play04:01

change to the package file under main

play04:03

I'm going to add a new property called

play04:06

type with a value of module then back in

play04:10

the terminal let's install a few

play04:12

packages by running npm install open AI

play04:17

andv we will use the open AI package to

play04:21

interact with the open AI assistance API

play04:24

andv will contain our open AI API key in

play04:29

the root of our project create a new

play04:31

file called EnV and in this file create

play04:34

a new variable called open AI _ API

play04:38

uncore key now we just have to fetch our

play04:41

open AI API key we can get our API Key

play04:45

by going to open Ai and then in the menu

play04:48

click on API Keys here click on create

play04:52

new secret key give it a name I'll just

play04:55

call mine math tutor then copy the key

play04:58

and then back in vs code paste in that

play05:01

key now let's create a new file and

play05:04

let's call it app.js to get started

play05:07

let's import a few things at the top of

play05:10

this file first I want to import these

play05:12

environment variables from the EnV file

play05:15

so let's type import all

play05:19

ASV from EnV then second let's import

play05:23

open AI from the open AI package by

play05:26

typing import open AI from open AI then

play05:31

let's type

play05:33

env. config and this will make these

play05:35

environment variables available to our

play05:38

application now let's create an instance

play05:40

of open AI by typing const open AI is

play05:45

equal to new open Ai and this takes an

play05:49

object as input with the property of API

play05:52

key and this API key is equal to

play05:56

process. env. openai API key now we are

play06:01

able to perform quite a few actions on

play06:04

the assistant and we will go through

play06:06

each of these methods in detail first we

play06:09

have the option of creating a new

play06:12

assistant or we can retrieve an existing

play06:15

agent like our math tutor we can create

play06:18

a new assistant by calling open ai. Beta

play06:23

And do take note that at the time of

play06:25

this recording these functions are all

play06:27

sitting in the beta package in open AI

play06:31

on beta we can now go to assistance and

play06:34

within assistance we can call create

play06:37

create takes an object as input and the

play06:39

first parameter that we need to pass is

play06:42

name for the name let's call this

play06:45

economics tutor then secondly we can

play06:48

pass in an instructions parameter with a

play06:51

value of something like you are an

play06:54

economics tutor then we also need to

play06:57

specify a property called tools which is

play07:00

an array of objects this list of tools

play07:04

is the same as this list of tools on the

play07:08

open AI website so let's say for our

play07:10

economics tutor we also want to make the

play07:13

code interpreter available so in our

play07:15

code we can add an object to this array

play07:18

this object has a property called type

play07:21

and for the type we can specify the tool

play07:24

that we want to add like code

play07:26

interpreter then lastly we also have to

play07:29

speci specify a property called model

play07:31

and for model we can specify any of

play07:34

these available models so I'll just

play07:36

enter GPT

play07:39

41106 preview like so let's test this

play07:42

out by running our application in the

play07:44

terminal by typing node space app and

play07:48

let's run this after this is executed we

play07:51

can go back to open Ai and let's refresh

play07:54

this page and we can now see our

play07:57

economics tutor and when we click on

play07:59

this we can see all the information that

play08:02

be passed in through the code and we can

play08:04

also see that the code interpreter is

play08:07

enabled now in order for us to continue

play08:10

with this code we need to store the

play08:13

assistant ID in a variable the assistant

play08:16

ID is this value that we can see over

play08:19

here so after creating a new assistant

play08:22

this ID is generated so in our code we

play08:26

can simply retrieve an assistant by

play08:28

using its ID in order to get this

play08:31

assistant ID after creating a new

play08:34

assistant we can simply await this

play08:36

response as the create method is a

play08:39

promise and we can assign this to a

play08:41

variable like so and this assistant

play08:44

object will now contain that ID of the

play08:47

assistant that we created but let's now

play08:49

have a look at retrieving an existing

play08:52

agent I'll comment out this code and

play08:54

then let's create a new variable called

play08:57

assistant which is equal to open ai.

play09:01

beta. assistance. retrieve now we can

play09:05

simply pass in the assistant ID and that

play09:08

will fit the existing assistant and

play09:11

assign it to this variable let's

play09:13

retrieve this math tutor assistant after

play09:16

clicking on this we can simply copy this

play09:19

ID and then in the code pass that ID

play09:22

into this retrieve function as a string

play09:25

this retrieve method is a promise which

play09:28

we need to await so we can do that

play09:30

simply by adding a weight in front of

play09:32

the SC and let's go ahead and console

play09:35

log this assistant variable in the

play09:38

console let's go ahead and run this app

play09:40

again and we can see that we did indeed

play09:42

fetch the math tutor assistant and now

play09:46

that we have our assistant assigned to a

play09:48

variable whether that was done after

play09:50

creating the assistant or fetching an

play09:53

existing assistant we can now go ahead

play09:55

and create a thread now let's move on to

play09:59

to threads basically threads are

play10:02

conversations in a production app

play10:04

whenever a user starts a new

play10:06

conversation you would typically go and

play10:09

create a new thread or a new

play10:11

conversation and then store the ID of

play10:14

that thread in a database somewhere to

play10:17

associate it with that user and that

play10:19

will then allow you to fetch that

play10:21

existing conversation and allow the user

play10:24

to continue with a conversation in your

play10:27

application in order to create cre the

play10:29

thread we can simply do the following

play10:32

first I will create a new variable for

play10:34

that thread which is equal to await open

play10:38

ai. beta. threads. create and let's go

play10:43

ahead and console log This Thread and

play10:45

let's run our app after executing this

play10:48

we can see the unique ID of this new

play10:51

thread or conversation and we can now

play10:54

use this thread ID to add new messages

play10:57

to this thread so let's go ahead and

play10:59

create a new message in order to add a

play11:02

message to the thread we can create a

play11:04

new variable called message which is

play11:06

equal to o wait open ai. beta. threads.

play11:12

messages. create and all we have to do

play11:15

now is specify the thread ID that we

play11:17

would like to add this message to by

play11:19

typing thread. ID then secondly we need

play11:24

to pass in an object and this object

play11:27

takes in two properties the first one

play11:30

being a role property with a value of

play11:33

user and the second property is content

play11:37

and this property contains the user

play11:39

message for example what is 500 * 500

play11:44

now that we've added a message to the

play11:45

thread All That Remains is to run this

play11:49

assistant to run the assistant we can

play11:51

create a new variable called run which

play11:54

is equal to 08 open ai. beta do threads.

play12:01

runs. create this takes in the thread ID

play12:04

as input and also we need to pass in an

play12:08

object and in this object we need to

play12:10

specify the assistant ID which we can

play12:13

get from our assistant variable. ID and

play12:18

optionally we can specify an instruction

play12:21

for instance address the user as Leon

play12:24

let's go ahead and console log this run

play12:27

variable like so in the term Al let's go

play12:29

ahead and execute our application after

play12:32

executing this code we get this unique

play12:35

run ID but what's very important is this

play12:38

status the Run could take some time to

play12:41

complete and at the moment the status is

play12:44

cued we can get the latest status of a

play12:47

run by doing the following let's

play12:49

actually comment out this run statement

play12:52

over here and let's also comment out

play12:54

this logic to create a new message and

play12:57

let's also remove this code to to create

play12:59

a new thread in order to get the latest

play13:01

status of a run we can do the following

play13:04

let's create a new variable called run

play13:07

and then let's await open ai. beta.

play13:12

threads. runs. retrieve this method

play13:16

takes in two properties the thread ID

play13:19

and the Run ID let's simply grab this

play13:21

thread ID from here and pass it in as a

play13:25

string and secondly we need to provide

play13:27

the Run ID which simply grab from over

play13:30

here in a real world application you

play13:33

would typically store this information

play13:35

in a database which is associated with

play13:38

your user in the terminal let's execute

play13:41

this application and we can now see that

play13:44

for this run ID the status is completed

play13:48

this completed status means that the

play13:51

assistant processed our messages and

play13:54

came back with a response in order to

play13:57

retrieve that response we need to call a

play13:59

special method in the open AI package

play14:02

let's go ahead and comment this out in

play14:04

order to retrieve messages from a thread

play14:07

we can do the following I'll simply

play14:09

create a new variable called messages

play14:13

which is equal to O wa open

play14:16

ai. beta. threads. messages. list and

play14:22

this takes in the thread ID I'll simply

play14:24

copy the thread ID from the console

play14:27

again this should be retrieved from a

play14:30

database in a real application this

play14:32

should now return the latest list of

play14:35

messages assigned to this thread let's

play14:37

console log these messages like so let's

play14:40

go ahead and run our application again

play14:43

in the response if we go down we can see

play14:46

this data property which is an array of

play14:49

values it is this data property that

play14:53

contains our messages and in this data

play14:55

property there are two messages at the

play14:58

moment the first one having the role of

play15:00

user and the second having the role of

play15:03

assistant in order to see the contents

play15:06

of this object we can do the following

play15:08

in the code let's type messages. body.

play15:13

data. for each for each will give us the

play15:17

message and then for each message we

play15:19

will simply console log the output so

play15:22

I'll just move this up into the for each

play15:24

statement and for the console log we

play15:26

will output message do content let's go

play15:30

ahead and run this again and this time

play15:32

we can see the actual values from this

play15:34

thread first we can see our message to

play15:37

the assistant and then we can see the

play15:39

response from the assistant lastly we

play15:42

are also able to fetch the logs for the

play15:45

thread and we can use that to see what

play15:48

happened behind the scenes these logs

play15:50

are the same logs that we saw earlier in

play15:53

this demo to fetch the logs all we have

play15:55

to do is create a new variable like logs

play15:59

which is equal to await open ai. beta.

play16:04

threads. runs. steps. list and this

play16:09

takes in the thread ID which will simply

play16:12

copy from the console and pass it in as

play16:15

a string value and then for the second

play16:18

parameter we need to provide the Run ID

play16:21

which I'll simply copy from the console

play16:23

as well and then pass it in as a string

play16:26

like so I'm actually going to comment

play16:28

out this code as well so now we are only

play16:31

left with the logs let's console log

play16:33

these logs and let's run our application

play16:36

again within the body of these logs we

play16:39

can see this data property which

play16:41

contains two objects and within data we

play16:45

can see those two objects with step IDs

play16:49

and for each object we can see a status

play16:52

for these individual steps in order to

play16:54

see the details of these logs we'll

play16:57

simply write a for each on these again

play17:00

so on logs. body. data we can run for

play17:06

each which will give us the log and for

play17:08

each log we'll perform this console log

play17:11

so let's move this up and then on log we

play17:14

can simply view stepore detail Let's

play17:17

test this in the terminal let's run node

play17:20

app and now in the terminal we can see

play17:23

the details for each of those steps the

play17:26

first step was the message creation

play17:28

which was the message we passed to the

play17:30

assistant and secondly we can see in the

play17:33

logs that the code interpreter was used

play17:35

to come up with the solution there are

play17:37

plenty of other topics that we could

play17:39

cover so please let me know down in the

play17:42

comments if you would like to see more

play17:44

videos on assistance API

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
AI CreationNode.js IntegrationMath TutorCode ExecutionAPI ImplementationThread ManagementConversational UIOpenAI BetaTech TutorialSoftware Development