Build Chatbot using OpenAI's Latest Assistants API - A Beginner's Guide | Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in today's video we're going to look at assistant API which is the latest API released by open AI to build the chat boot so we're going to go through the concepts like what is assistant how do we create it how do we upload the file and eventually you know because we're going to see that it also supports the you know code interpreter retrieval and the function calling so we're going to focus on maybe the retriever we're going to upload some file and then see whether we can build an assistant which can even you know let's say the answer questions from the particular file right and you could do this thing from the UI also if you go to the you know open AI your dashboard there are a lot of you know you have playground you have assistant and then you can create assistant from the U also you can use it right but today we're going to see you know how do we create from the you know code so it's going to be the beginner friendly but then we're going to also interact via this particular interface right so let me uh go back to this assistant page and let's have a look at you know what exactly this assistant API so first of all you know uh this assistant is capable of using some uh you know internal tools it can use the code interpreter it can use a retrieval retrieval is nothing but this it has inbuilt support for the rag which is you know retrieval augmented generation so earlier let's say we used to upload file on let's say pine cone or a chroma DB and then we going to do some semantic search and we used to pass those context to the GPT using let say our you know typical chart GPT um API right so here we we treat the file upload and the let's say the rack component outside of the chat GPD but what assistant does actually you know we can simply upload file there and then we can ask question it will use the retrieval tool it can just you know do the semantic search R through some context and you know start answering so we're going to see that particular thing right so let's look at the you know the concept so first of all this we're going to say this thing chatbot is going to be called like an assistant right and then we're going to create some threade inside that assistant and threade represent the conversation so if you look at the chat GPT here you know this is the chart GPT whatever the conversation I'm having with here chart GPT this is like one threade actually okay this is one thread and I can create new thread if I click on here this is going to be the creation of the new thread and then I can write some messages inside this thread so it's like a similar concept so we have threade creation you create the threade and then you can create the messages and you put inside that threade so that assistant can start you know replying that thing right so the first step is going to be definitely the creating the assistant right and then we have a endpoint called you know client. beta. create so let's get into the code and let's see how we can do this thing right so the first thing definitely we're going to require to you know install open aai and recently the open a you know whatever the recent 1 Point version whatever it has I think it is one point some version the syntax is also Chang you know so you can go and check what is the recent syntax that we need to use while creating the open a client while uploading the files and all of those things so let's go back so we have let's install the open a the next thing you know definitely we want to import the open AI object from the open a and you know so sorry so we create let's say whatever this module and we create the object of it so we need to pass this open a key for this particular thing you know the ideal way to pass that particular key via environment variable purpose for the tutorial purpose I'm just putting that key here and I'm going to delete this key once you know I'm done with this particular tutorial right so this way we're going to create the uh let's say you know the open a client using which we can do all the functionality what open a offers okay so we got the open air Cent right so as I told you I want to teste the assistant you know the rag functionality or the retrieval capability so in this tutorial what I'm I'm going to do I'm going to just upload one HTML file and and I'm going to ask some questions related to that particular file so what HTML file I'm going to use so let's say I have my website which is not at all updated from the last one and a half year but what I did actually I you know I just saved the you know the um HTML of that particular uh you know website so I just did the control what is this control yes and saved that particular HTML page and I'm going to uh you know upload that page to the assistant and I expect that it will answer me the anything questions I ask with respect to this particular page let's go back to the notebook and you know to upload the file right now so once you have the client object you can go to the files. create and you can create the uh you know file which is nothing but you're like an uploading file where you specify the path of the file and eventually the purpose of the file because we could also send this file for the fine tuning purpose so if you do the GPT 3.5 fine tuning there also you can send this file so here we are specifying the purpose of uploading this file is actually for the assist you know we're going to use so let's upload that particular file which is nothing but here which is simple an HTML page which you see here this is an HTML page that I'm uploading so I think uploading is successful once you upload the file you should get something in a return which is like uploaded file object or some kind of you know reference you could see you get some file object return and uh you can see the purpose of this file was assistant it was already processed this was the file name you know that you could say this was the name of the file and this is the ID because we're going to require the file ID for the further processing now once we have the file uploaded because we're going to require that file for the assistant now let's create the assistant so we again going to use the you know opening ey client saying that you know assistants. create we give the name for the assistant it means you know maybe I think it's pretty good already yeah so we give the name for the assistant let's I'm going to call it like future smart a assistant and I'm going to give some instruction that you know you are an AI assistant that answer any queries related to the Future smart AI right and then here is the list of tools that you can specify that your assistant uh you know can use so again if you go back here uh it supports I guess yeah it can support the code interpreter for example let's say you want to run some python code it supports that thing retrieval is something when you upload the file and you want to get some answer from that particular file function calling is let's say you want to call some internal API or some external API or even your database that can be done using your you know fun calling but what we're going to see is the retrieval uh you know things so I just put one tool that I'm interested which is like retrieval and which model do you want to use so I'm going to use this latest gp4 you know this preview model whatever it is available and while creating the assistant I'm going to give the file IDs that I'm interested so here you see we have one file but you can upload multiple files here because it's going to take the list of file IDs so here in this list I'm just giving the one file ID so let me create the assistant okay and uh now this assistant is created so we can go to the UI actually and see whether that assistant got created or not so here we come here let's refresh this thing and uh you see one assistant got created here so whatever the assistant fees you know created through the code we can see that assistant name here which is the future smart a assistant you also see the instruction that I have given and it got some ID for that assistant you can see it just got created and if you click on it uh you can see you know you are an assistant that answers any queries related to all of these things the model that we selected you see this is the model we selected and we have only activated the retrieval so if you see we only activated the retrieval that's why you see only the retrieval thing here and you can also see the files we uploaded so we got the uploaded file here we can actually test our you know let's say assistant here itself actually if you want to you know taste for this particular uh you know things so let's click on a test I put a couple of queries here so that you know I can ask actually to it so let's say I'm going to ask what is futes AI and I'm going to click here this is now our assistant future smart assistant and let's click on ADD and run and see whether it is able to answer it's actually running we're going to see again through the you know uh the code if you look at it when we start interacting with this is nothing but it created one threade this is the threade ID or whatever it got created right now it is uh you know uh trying to find the specific information about and this is coming from the web page that we have given you know the we do the custom natural language processing these are the couple of things what we use so see it is able to um you know decide to call our file and get some you know text from it you know custom natural language processing Solutions and in all of those things so our let's say our assistant is ready this is what we you know uh just created but we want to see how we can create this the same interface using the code so that we have more flexibility right you can also look at the logs actually here you know whatever things happen you can look at the logs I think this was the threads what we just you know maybe the threade creation happen and all of those things so if you want to go into the detail you can always you know check here let's go back to our code and see whether we can send the same message you know uh let's say uh via code and not like this let's clear this thing maybe and where is the cancel option here I think maybe we go back okay so here is our assistant actually so let's go back to the code and since we created the assistant we know that if you want to start any conversation we going to require the threade because threade represent the conversation so let's create the thread so again the same syntax we're going to create the threads. create and we're going to get the trade similarly we should also get some properties of this particular threade and you know we look at the threade maybe and you know we should get its detail so some ID for this particular threade again this is created and all of this thing maybe similarly just we saw the assistant we should also see the threads here maybe I think I seen like let's go no maybe somewhere else no no no this is the only assistant tab okay I think I had seen that I just don't remember maybe you know um anyway let's go back so we created the threade and we know that if you want to interact with the assistant we need to you know create some message inside this threade so let's create the message the similar way we can use the you know and this all the steps are already mentioned here okay uh so uh here you can see that you know client. assistant. create you can create the empty thread and then now you can you know actually create the message inside the threade that's what we're going to do actually so we have created the threade let's create a message and then we're going to put you know threade ID because we are creating that inside this particular you know threade and the role is user because it's like what we going to ask right and then we ask the same thing what is future smart AI right now once we create the message it goes inside the part of the threade and we can actually retrieve that whole messages which is available inside the threade so at the moment you see that threade has only one message what is future smart AI right so this is the list of thread messages and it has I think only one message right now let's run the assistant because if you look at the documentation when you create the message ultimately you need to run the assistant you know so that you get the response so we're going to run the assistant and while running the assistant openi says that when you run the assistant it actually goes into the cued status so it's going to something like in progress and cued and eventually it will be in the completed States it will be very like I think quickly but this is how the states it goes through so if you want to look at the let's see if you want to display the assistant response you want to make sure the assistant run that we're going to run is actually the completed so what I'm going to do here in this particular thing I'm going to create the run and I'm going to quickly retrieve that same run so so we have this uh whatever the function to retrieve the run which take the thread ID and the Run ID you know and let's see what happens I'm going to run this run and I'm quickly retrieve it and see what status we get so we created this run and then I call the retri and see what happening here so we got this assistant that is fine so there is a status that I'm looking for yeah you see the status is in progress so when I quickly retrieve that same run I see the status is you know um in progress but maybe if I actually uh print again this run it might have completed by this time oh status is in still in progress actually okay status is still in progress so let's close this thing so this what we can't keep doing this right so we need to make sure we are using some while loop or some Loop kind of things right so we're going to run the same thing in the while loop we're going to retrieve that same run we're going to keep retrieving it still we get the status is equal to completed and if we get the status is equal to completed we're going to print the message so let's run this thing I think now by this time it should have completed okay it has completed actually and see what happens since it has completed that's why it went to this particular you know part where it is printing something so once we retrieve the run and we know that it's completed the whatever the message generated by the assistant should be available inside our message list here inside our message list maybe it should be available here now let's click here here yeah now you see we got the more thing here initially we saw only one message which was what is future smart a I hear and now I think we also got the answer that future smart AI this and that you know you can see here we got that now if you see the the latest message what is generated the threade is actually the zero index is the latest message and the old one is that whatever the first Index right so if you want to access the latest message you need to access the zero Index right so let's go back to the code and what we are doing so once we got the you know we are retrieving all the messages in the threade and we know that we want the latest message which is at the Zero Entry of that data here data is nothing but that messages. data is nothing but that trade message list what we just saw this one you know do data so we got that latest message and inside the latest message again you see that that message itself is a one object so the message if you look at that message is called threade message and that thread message has one field called content and that content is again a list so we're going to look at the zero index of that content you know lot of and then we're going to look at finally the text attribute of it and we're going to look at the value so very much like hierarchical kind of a thing so you look at the um get the message you look at the content list we're going to look at the zero index and then finally takes and that the we don't need to buard we don't need to remember anything it's there we just need to see the documentation and do the thing right and this where we actually printed you know this uh it is talking about the custom natural language processing from the file now let's ask one more followup questions inside the same thread right so I'm going to ask what services does it offer but I'm not going to ask I'm actually asking about the future smart AI because since it's like I'm inside the chat GPD I'm expect it should understand that earlier question was related to the Future smart so this is like a followup question that what services does it offer and run this particular message it should go to our message list okay it should be our latest message here somewhere yes you could see this is the latest message now do the same thing we going to now this time we don't need to do these things we know now how it works so let's create the run and once we created The Run let's wait for till the assistant get the you know so it's going to keep running and the once we got the response from the assistant that is the time the run dot status will be completed and by that time it should print what services does you know future smart AI offer so let's wait okay so you see future smart a offers the range of services it actually picked up whatever the you know old uh things I had I think it must be here somewhere yeah that's what it took and it's working pretty good you know so we don't need we see we didn't bother about uh reading the file chunking that file indexing that file in Pine Cone or pra be writing that semantic logic nothing in the assistant we simply uploaded the file and it is able to uh you know do all of these things and it is able to answer the followup questions because we are asking those questions in theide the same thread that's why it is able to answer the uh followup questions and similarly we can keep asking you know uh actually the more and more questions what we have and it should be able to uh answer those questions so let me quickly ask the another questions what I have maybe the text tag that I use we don't need to check that thing run this thing and we're going to run this thing again you know assistant so this is how you can you know create the open a assistant now it's actually pretty much mature very structured you know so this is what the text tack we are using okay it was not that good actually you know there are many things on this but it couldn't answer actually that properly on that thing maybe I ask one more thing oh okay and let's create and let's run okay yeah so this is how you create so I was talking about you know the things are very simplified we don't need to worry about you know chunking the file indexing it in some uh Vector database and do the things you know everything is happening at one place so now I ask what client says and it somehow I think it read this testimonials what we put here right so pretty good I like the opena assistant API now I need to see you know because if you look at the earlier chart GPT API here we need to every time we got the new message either from the user or assistant we need to append that message and send whole conversation to the open all the time but here if you look at what we are doing we are not sending all conversation we are only sending the latest message what I'm typing here and all the state and the conversation inside the thread is open I maintaining so it is more stateful nowadays compared to what we had you know earlier we don't need to worry about sending those all messages all the time it's going to maintain that threade uh you know all of these things I hope you found this you know video useful okay before that maybe we go and check here again we tested that assistant same assistant here right if I ask those same questions here and we know that we should get those same answers here also right what services does okay here there is no followup question inside there is no first question and if I ask this one you know what services does it offer because let's see it has a limited context we haven't seen that we are talking about the futur mod AI but let's see what happens okay because it knows that this assistant is actually about the future smart AI even I'm um simply asking what services does it offer it knows that it actually asking for what services does future smart a offers right so you can play with this thing here also I think uh a very pretty good interface what they have you know uh created so I will be uploading this you know file uh in the geub and then I will put a link inside the description I hope you found this video useful thank you
Info
Channel: Pradip Nichite
Views: 28,003
Rating: undefined out of 5
Keywords: openai, chatgpt, openai assistent, chatgpt tutorial, generative ai, chatbot tutorial
Id: yo0qy7xyd3A
Channel Id: undefined
Length: 20min 26sec (1226 seconds)
Published: Sat Nov 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.