OpenAI Assistants API Tutorial - Playground & NodeJS
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
π 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.
π 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.
π¬ 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.
π 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
π‘Assistant
π‘Node Project
π‘Code Interpreter
π‘Thread
π‘Knowledge Base
π‘Model
π‘Environment Variables
π‘Retrieval
π‘Logs
π‘Run ID
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
open ai's assistance API is a game Cher
and in this video I will show you
everything you need to know to create
your very own assistant and we will also
have a look at implementing these
assistant in a node project to get
started go over to platform. open.com
and then on the left hand side click on
assistants here you will see a list of
available assistants to create an
assistant click on create we can now
enter a name for our assistant for this
demo I would like to create a math tutor
so let's call this Assistant math tutor
and for the instructions I will enter
you are a personal math tutor write and
run code to answer math questions and we
can use this to Prime our model we now
need to select the model that we would
like to use at the time of recording the
best models to use with assistance is
GPT
41106 preview or GPT 3.5 terbo
11106 now we can select the tools that
we will make available to our bot for
example we can add functions if we click
on ADD we can see examples of these
functions for instance a function to
fetch the current weather code
interpreter allows the assistant to
write and execute code for this demo
that's exactly what I want so I'll
enable this retrieval allows the
assistant to answer questions from a
knowledge base so if you want you can
enable retrieval and then upload files
for the knowledge base these files could
be text files PDFs Word documents CSV
and more to create the assistant all we
need to do is click on Save now in the
assistants dashboard we can see our new
assistant we can click on this assistant
to make additional changes or to run
this assistant we can simply click on
test in the playground we can make
changes to our assistant but in the
middle of the page we can see this
heading called thread threads are
conversations whenever we start a new
conversation a unique thread ID will be
generated and we can then use these
threads in very unique ways for instance
in our code we can retrieve an existing
thread and that will pull in the
conversation history and that will allow
the user to continue with a past
conversation we will see that in action
in the note section of this video in
order to add messages to a thread we can
simply add a message here and since this
is a math tutor let's ask it to solve
this equation we now have the option of
adding this message to the thread and
this will add the message without
executing the assistance and it will
allow us to add multiple messages to
this thread I'll simply click on ADD and
run to execute this immediately and
after a second we can see that the code
interpreter was triggered and as a
result we can see that the solution to
the equation is x = 1 because in our
instructions we asked our assistant to
write and run code to answer these
questions this triggered the code
interpreter we can actually click Within
These brackets to view the python code
that was written to solve this problem
and we can simply click on code
interpreter to close this secondly we
can click on this little arrow over here
to view the result which was one to see
what happened behind the scenes we can
now click on logs and in logs we can see
the individual calls to the assistant's
API that make up this conversation and
this is useful for troubleshooting the
assistant now let's have a look at this
in our note project in vs code I've
simply opened up a new folder so let's
open up the terminal to initiate a new
node project by entering npm in it- y
now I'm just going to make one small
change to the package file under main
I'm going to add a new property called
type with a value of module then back in
the terminal let's install a few
packages by running npm install open AI
andv we will use the open AI package to
interact with the open AI assistance API
andv will contain our open AI API key in
the root of our project create a new
file called EnV and in this file create
a new variable called open AI _ API
uncore key now we just have to fetch our
open AI API key we can get our API Key
by going to open Ai and then in the menu
click on API Keys here click on create
new secret key give it a name I'll just
call mine math tutor then copy the key
and then back in vs code paste in that
key now let's create a new file and
let's call it app.js to get started
let's import a few things at the top of
this file first I want to import these
environment variables from the EnV file
so let's type import all
ASV from EnV then second let's import
open AI from the open AI package by
typing import open AI from open AI then
let's type
env. config and this will make these
environment variables available to our
application now let's create an instance
of open AI by typing const open AI is
equal to new open Ai and this takes an
object as input with the property of API
key and this API key is equal to
process. env. openai API key now we are
able to perform quite a few actions on
the assistant and we will go through
each of these methods in detail first we
have the option of creating a new
assistant or we can retrieve an existing
agent like our math tutor we can create
a new assistant by calling open ai. Beta
And do take note that at the time of
this recording these functions are all
sitting in the beta package in open AI
on beta we can now go to assistance and
within assistance we can call create
create takes an object as input and the
first parameter that we need to pass is
name for the name let's call this
economics tutor then secondly we can
pass in an instructions parameter with a
value of something like you are an
economics tutor then we also need to
specify a property called tools which is
an array of objects this list of tools
is the same as this list of tools on the
open AI website so let's say for our
economics tutor we also want to make the
code interpreter available so in our
code we can add an object to this array
this object has a property called type
and for the type we can specify the tool
that we want to add like code
interpreter then lastly we also have to
speci specify a property called model
and for model we can specify any of
these available models so I'll just
enter GPT
41106 preview like so let's test this
out by running our application in the
terminal by typing node space app and
let's run this after this is executed we
can go back to open Ai and let's refresh
this page and we can now see our
economics tutor and when we click on
this we can see all the information that
be passed in through the code and we can
also see that the code interpreter is
enabled now in order for us to continue
with this code we need to store the
assistant ID in a variable the assistant
ID is this value that we can see over
here so after creating a new assistant
this ID is generated so in our code we
can simply retrieve an assistant by
using its ID in order to get this
assistant ID after creating a new
assistant we can simply await this
response as the create method is a
promise and we can assign this to a
variable like so and this assistant
object will now contain that ID of the
assistant that we created but let's now
have a look at retrieving an existing
agent I'll comment out this code and
then let's create a new variable called
assistant which is equal to open ai.
beta. assistance. retrieve now we can
simply pass in the assistant ID and that
will fit the existing assistant and
assign it to this variable let's
retrieve this math tutor assistant after
clicking on this we can simply copy this
ID and then in the code pass that ID
into this retrieve function as a string
this retrieve method is a promise which
we need to await so we can do that
simply by adding a weight in front of
the SC and let's go ahead and console
log this assistant variable in the
console let's go ahead and run this app
again and we can see that we did indeed
fetch the math tutor assistant and now
that we have our assistant assigned to a
variable whether that was done after
creating the assistant or fetching an
existing assistant we can now go ahead
and create a thread now let's move on to
to threads basically threads are
conversations in a production app
whenever a user starts a new
conversation you would typically go and
create a new thread or a new
conversation and then store the ID of
that thread in a database somewhere to
associate it with that user and that
will then allow you to fetch that
existing conversation and allow the user
to continue with a conversation in your
application in order to create cre the
thread we can simply do the following
first I will create a new variable for
that thread which is equal to await open
ai. beta. threads. create and let's go
ahead and console log This Thread and
let's run our app after executing this
we can see the unique ID of this new
thread or conversation and we can now
use this thread ID to add new messages
to this thread so let's go ahead and
create a new message in order to add a
message to the thread we can create a
new variable called message which is
equal to o wait open ai. beta. threads.
messages. create and all we have to do
now is specify the thread ID that we
would like to add this message to by
typing thread. ID then secondly we need
to pass in an object and this object
takes in two properties the first one
being a role property with a value of
user and the second property is content
and this property contains the user
message for example what is 500 * 500
now that we've added a message to the
thread All That Remains is to run this
assistant to run the assistant we can
create a new variable called run which
is equal to 08 open ai. beta do threads.
runs. create this takes in the thread ID
as input and also we need to pass in an
object and in this object we need to
specify the assistant ID which we can
get from our assistant variable. ID and
optionally we can specify an instruction
for instance address the user as Leon
let's go ahead and console log this run
variable like so in the term Al let's go
ahead and execute our application after
executing this code we get this unique
run ID but what's very important is this
status the Run could take some time to
complete and at the moment the status is
cued we can get the latest status of a
run by doing the following let's
actually comment out this run statement
over here and let's also comment out
this logic to create a new message and
let's also remove this code to to create
a new thread in order to get the latest
status of a run we can do the following
let's create a new variable called run
and then let's await open ai. beta.
threads. runs. retrieve this method
takes in two properties the thread ID
and the Run ID let's simply grab this
thread ID from here and pass it in as a
string and secondly we need to provide
the Run ID which simply grab from over
here in a real world application you
would typically store this information
in a database which is associated with
your user in the terminal let's execute
this application and we can now see that
for this run ID the status is completed
this completed status means that the
assistant processed our messages and
came back with a response in order to
retrieve that response we need to call a
special method in the open AI package
let's go ahead and comment this out in
order to retrieve messages from a thread
we can do the following I'll simply
create a new variable called messages
which is equal to O wa open
ai. beta. threads. messages. list and
this takes in the thread ID I'll simply
copy the thread ID from the console
again this should be retrieved from a
database in a real application this
should now return the latest list of
messages assigned to this thread let's
console log these messages like so let's
go ahead and run our application again
in the response if we go down we can see
this data property which is an array of
values it is this data property that
contains our messages and in this data
property there are two messages at the
moment the first one having the role of
user and the second having the role of
assistant in order to see the contents
of this object we can do the following
in the code let's type messages. body.
data. for each for each will give us the
message and then for each message we
will simply console log the output so
I'll just move this up into the for each
statement and for the console log we
will output message do content let's go
ahead and run this again and this time
we can see the actual values from this
thread first we can see our message to
the assistant and then we can see the
response from the assistant lastly we
are also able to fetch the logs for the
thread and we can use that to see what
happened behind the scenes these logs
are the same logs that we saw earlier in
this demo to fetch the logs all we have
to do is create a new variable like logs
which is equal to await open ai. beta.
threads. runs. steps. list and this
takes in the thread ID which will simply
copy from the console and pass it in as
a string value and then for the second
parameter we need to provide the Run ID
which I'll simply copy from the console
as well and then pass it in as a string
like so I'm actually going to comment
out this code as well so now we are only
left with the logs let's console log
these logs and let's run our application
again within the body of these logs we
can see this data property which
contains two objects and within data we
can see those two objects with step IDs
and for each object we can see a status
for these individual steps in order to
see the details of these logs we'll
simply write a for each on these again
so on logs. body. data we can run for
each which will give us the log and for
each log we'll perform this console log
so let's move this up and then on log we
can simply view stepore detail Let's
test this in the terminal let's run node
app and now in the terminal we can see
the details for each of those steps the
first step was the message creation
which was the message we passed to the
assistant and secondly we can see in the
logs that the code interpreter was used
to come up with the solution there are
plenty of other topics that we could
cover so please let me know down in the
comments if you would like to see more
videos on assistance API
Browse More Related Video
OpenAI Assistants Tutorial for Beginners
How to Build Custom AI Chatbots π₯(No Code)
AutoGen Quickstart π€ Build POWERFUL AI Applications in MINUTES
How to Get ChatGPT to Make Phone Calls For You (Custom GPT Tool)
Realtime Powerful RAG Pipeline using Neo4j(Knowledge Graph Db) and Langchain #rag
ANDROID com KOTLIN 2023 - #01 - Instalação e Primeira App
5.0 / 5 (0 votes)