Master OpenAI's Assistant API in Just 15 Mins - Streamlit tutorial to build AI Agents

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hi everyone this is going to be a good one custom gpts from open ai's Dev day have been all the Talk of the Town the last two days but open aai just dropped an amazing API it's called the assistance API and that will allow any developer to build chat GPT assistance on their own applications in this video we'll be taking a look at assistance API and even go on to build our own streamlit application and demo it watch on open aai just launched their assistance API this API gives developers ability to build chat GPT level application on their own applications what this means is that they can bring the awesomeness of G GPT 4 and also pair it with amazing tools like knowledge retrieval code interpreter and custom functions all of this is available to developers to build using open AI backend what open AI promises is a seamless experience so things like rag retrieval reverse rag double rag all of that is eliminated because open AI suggests that their technology is smart enough to figure out what is needed and what is not let's jump in take a look at a demo and then look into the assistance API more all right this is our streamlet app that we've built using the assistance API um as you can see this is a streamlet app what I wanted to do was do knowledge retrieval from the assistants API on the sidebar we have a couple of options we can either drop a website scrape it get the data in there and then use that data and do knowledge retrieval on that or you can drop some files and use that data and get knowledge retrieval on that so let's try the uh uh website method out so here I am I'm getting the Wikipedia entry for our President Joe Biden dropping it into our scrape and upload what this is doing is going into the website scraping it making a PDF and dropping it into open AI all right let's start a chat there was J Pyon what this is going to be doing is using the assistance API to go to open aai and grab the entry from the file that we uploaded and give us the answer again all of this is done on the open AIS back end just like how you would do it in chat GPT when you upload a file Etc and so on so forth can we also do where we can add more files to this let's take a look that is a back sheet about the recent executive order that Joe Biden signed so let's see if we can again what we are wanting the API to do is to go fetch the data from our two files that we've where the where the information is not able to distinguish it will be clear in saying that it is not able to find the details so here I've asked it to specifically go in there and use the uploaded data to find the information so let's see if it's able to return to us the details there you go it is able to give us the details on the executive order again executive order was very recent there's no way that the data could have been available in open AI training data so this is a way that we can augment open AI training data so what was announced exactly open aai announced assistance API with retrieval and code interpreter in them uh what this will allow you to do is for users to build more powerful AI agents in their own applications this brings in GPT 4 and all of open AI backend into to users applications the main part about this is that open a has made input and the chat interaction with the chatbot persistent and infinitely long what this means is that we don't need to worry about the context Windows Etc and how to pass the best amount of information opena promises that it'll take care of all of this in the back end let's dive in and take a look at what this assistance API is all about all right I try to get the best visual representation of this assistance API so open AI assistance API has four main components to it tools assistance threads and messages assistants are AI assistants that you can give a custom set of instructions they can also be tagged with using different kind of tools in this situation functions knowledge retrieval code interpreter kind of tools in addition to assistant we also have thread and messages let's look at the concept of threads first threads is your back and forth interaction that the user has with the agent all the back and forth interaction that the user has with the agent is organized in messages that are under the threads messages have assistant or user roles content which is text and also annotations if it is doing knowledge retrieval so that it knows where in which part of the document it's getting details from you can pass files both at the assistant level or at the thread level and that's an important bation what makes this awesome is that the threads is the singular back and forth interaction that you have with the agent this could be infinitely large but open AI promises is that it will have enough context and backend Logic for the latest message a run is where you you are trying to run and execute the latest message in a thread now before you do that you will have to pick the assistant that you want to run that job for assistant 2 is the one we pick here so all the tools associated with assistant 2 come into the picture and any files associated with the thread or the assistant also comes into the picture you can also do a different Run for the new message that has popped up using assistant one so this allows your application to leverage any number of assistants to talk to the same chat interaction or the same user for example if you run a retail app and that person's interactions are all logged on one thread you can have a returns application assistant go do returns and you can have an information gathering assistant to information GA the life cycle of a run is pretty straightforward jobs are queued Jobs go in progress they go to complet it or failed you can keep pulling your job to know the status of your job this in nutshell is all what assistance API is all about hope this has been very interesting let's take a look at building our stream lead up what we're doing here is we are going to be going through our streamlet app that I've built we'll go Section by section we'll take a look at what everything does and how it all comes together before we jump into the code let's take a quick look at how our app is set up all right in our streamlet app as you can see we have a sitebar where we're getting some open AI keys we're going to get a website URL so we can scrape and get some data we're also allowing users to drop their own files obviously there's a lot of State Management going on I can kind of show you how that works first things first we're importing the basics open aai streamlet uh beautiful Soup for the scraping uh requests to make HTTP requests PDF kit and time first things first we're setting up our AI assistant I have hardcoded our AI assistant here and I'll show you why but essentially you can keep this as a user defined thing so you can get a list of all your assistants and pass which one you've selected and so on so forth we're initializing some State variables and this is mostly around State Management so that I am having a sequence of steps if you're new to streamlet you should look at State Management if you're having a sequence of steps you'd want to give incremental State managements so that you don't run all of the code every single time you run it after State Management we are having our functions for the sitebar first will be our scraping the web to give our data to our open uh we're using the beautiful soup as you can see here um then we are converting to PDF I did include and perform the WK HTML to PDF um service you should probably download and set it up on your own computer I'll drop a link on how to set that up so you can do that too after that we are having a couple of new API functions these are open AI files create um and purpose assistance these are new apis and I will give you a link for the description to where these apis are so you can take a look at them but what you're doing here is you're completing this activity and you're uploading that files to open Ai and again I am actually just generating a file ID I'm not attaching it to any assistant yet and I'll show you where I'm doing that after that we get our open AI key that the that you have given and then we go out to to the scrape and upload so once you do the scrape it gets uploaded into openi so here open AI takes in the file yet it is still not attached to an assistant what you're doing then is allowing a way for users to upload their own files this is the function for that once you've kind of completed and uploaded that file this is where we are going to list out all the file IDs that we've just seen and we're going to attach them to the assistants as you can see the assistant API are all classified under beta so make sure you put the beta tag you can remove that in the code once it's out of P this is your new attaching files to your assistance API command so this is how you do it then you would go set up your start chat button once you start the chat it'll then take you to your back and forth with your chat two things that I said threads are persistent so basically a thread that was created in the very beginning that will be the thread that you use in the rest of your chat so we are creating a thread right right here when we click on the start chat button now we go into the chat function there is a extra function here that I've put in with citations I'll explain to you when we get to there after we look at the chat button so in your chat application it is pretty straightforward you're giving your this is very straightforward this is the new streamlet chat functionality so I'm using that to go back and forth with my chat agent the important API calls here are around adding messages to your thread again important point is your thread is already set up so all you're doing is adding messages to it with either a role of user or a role of assistant so first up we are going to add a user role we're going to take the input from the uh uh chat part we're going to take the input from the streamlet chat and send it to our threads as you can see this is the new API call for creating a message on a thread thread ID is the thread ID that I set up for my session and all I'm doing is sending the prompt as content at this point you're ready to run your job now remember what the process for the run is for the run you would need a thread ID and you would need the assistant ID here we're giving an assistant ID and a thread ID and we're creating the run the beautiful part about this is for every run you can give your own instructions for example sometimes I may want to give a run instruction based on a special promotion I'm running or not that's up to the developer and how they want to custom once the run is kicked off and this is the kickoff for the Run runs. create once the run is kicked off you are pulling to see if the run is completed that's where I'm using the time function just to check if um the run is completed once the run is completed I will then take all the messages that are in my thread and and perform some Logic on it so remember you your remember your inputs are role of user so you expect the run to have generated a role of assistant same way how you use your regular GPT 4 API in this situation we're taking the Run ID that just completed and we're grabbing all the messages that came with the role of assistant that could be one that could be two so we want to grab all of them so once you've grabbed all of them them you're sending it to another function called process message with citations this is where I wanted to kind of talk through what that was in the messages like I mentioned in the annotations there's going to be some details sometimes annotations will be return returning data sometimes maybe not but if they do this is the suggested method of how you can kind of display those annotations let's go back to our chat window once you've cleaned up the annotations and described them you can then just display the full response that includes the assistant response out there in your chatbot this whole process goes back up and down back and forth and the thread keeps building now here's where the assistance API is really awesome for the developers you can access your assistant in your playground what this means is that you can check logs and you can update the assistant you can update the instructions for the assistant um and watch how your calls are being generated from the logs and that is important for you to like play around to see what the best instruction set is so that your AI assistant will be working perfectly when it's just deployed on your application again big hitting items here is that threads are infinite you don't need to worry about rag you don't need to worry about chunk size you don't need to worry about context length API takes care of all of that and gives you the best result results I've personally checked that open AI assistant API does better than gp4 calls through a vector database using rag so take it for what it is please try it on your own and get back to me with some results so there you have it folks that is the assistance API in a nutshell open AI expects this to be used in a wide ranging of applications for people building their their own AI assistants and I believe it is going to usher in a new age of very smart AI agents that are Outsourcing their back end to a more powerful model you can grab all of the code in my GitHub the links are below and the medium article where have described this entire process the links are below for that as well I'm going to leave you all to do more building and uh talk to you later [Music]
Info
Channel: The AI Lifestyle
Views: 13,388
Rating: undefined out of 5
Keywords:
Id: TCYgN6R0-RU
Channel Id: undefined
Length: 16min 35sec (995 seconds)
Published: Fri Nov 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.