OpenAI Assistants Tutorial for Beginners - Build Bots

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this tutorial you will have a complete boiler plate with python code to work with the assistant API and start building your own bots so if you're a developer and you're interested in working with open a eyes models then make sure to stick around I'm not only going to show you everything that you need to know about the assistance API right now but I'm also really going to provide you with a framework a boiler plate in Python that you can use to build applications and Bots using the assistance API so let's first start high level with what we're actually going to create so I'm going to walk you through the assistance API docs but really what I found is if you follow all of the instructions over here which are pretty clear to follow but then when you want to actually put this into an application and make all of the various pieces communicate with each other that's really where you need some custom logic so I've created some functions uh for us that we can go through and that can really help you to put this all together like I said to create a fra framework to reuse this so I'm going to walk you through this project which I will make available via GitHub link will be in the description and this is part of a larger project that I'm working on and mainly building a Airbnb WhatsApp bot that can assist host uh by answering frequently asked questions which we have over here and we'll get into but for this part we are only going to focus on the AI and the retrieval part of the data so really a common use case for the assistance API now the rest of this project so building out the whole bot uh will be covered in Future YouTube videos so if you're interested in that make sure to subscribe so you can stick around for that so if you want to follow along make sure you have an active python environment running and you have your open AI API key ready you can clone the repository or start from scratch create your own there are requirements in here uh for this tutorial you only need the open AI Library flask won't be needed for this one and then we can get into it so I start with a simple load over here let's quickly get this up and running simple load to uh load the open aai client for us to interact with so this is really the basis and it's the same as you would normally use the open AI client and the open AI API to to work with these models but now let's look into the assistance that we can now create and you can either do this through the UI so I can follow this process click create all of that but we are going to do this from code obviously but first real quick to get some context on how these assistants are different from the regular openingi let's quickly go to the playground and show you what's the difference between just the regular chat which is for example what you would use like one month ago before assistance and how that compares to if you use the assistance so this is all from the UI from the playground but this it works the same behind the scenes when you're using the API let's start from the example of the WhatsApp B that we're trying to create and I'm going to fast forward a little here but if I ask regular C GPT turbo what is the checking time it answers over here a checking time is typically between 3: and 4: so this is just randomly based on the available knowledge within open AI models now let's quickly show if I switch to the Airbnb bot that we're going to create and let's ask what is the check-in time then we can see what's going on over here it's making various requests it's thinking and then ultimately it will provide us with the message saying the check-in time for the RB for the Airbnb is 300 p.m. and then we can reference that in here check time is after 3:00 p.m. this is the fundamental difference that you have to understand over here when you use just the regular API GPT 3 and2 Turbo or the new GPT 4 Turbo model you're interacting directly with open ai's models without giving it any context so essentially the answer that you will get from the API is the same as you would uh get using chat GPT for example but now with the assistance we can provide tools namely functions code interpreter and retrieval and in this tutorial we'll focus on the retrieval part and see how we can provide our bot our our assistant with this data in this case the PDF document so that is a really important concept to understand and if we then look at this from the API side of things what I was just doing in the playground so for example doing a simple completion with jet GPT or GPT 3 and a half turbo we can hear see the same response checkin time generally Etc so before the assistance API this was the only way to interact with open AI models and if you wanted to provide extra uh data extra context to these models then you would have to build your own solutions for that you would have to set up a vector database um use open AIS embeddings for example and then use a retrieval augmented generation process for example which you could build using Lang chain that was just a lot of of custom uh code and and development that you had to put in place in order to to work with these models using your own data and I wanted to show you this because you first have to understand how it previously worked to fully understand the benefits but also the downsides of using the assistance API all right so now let's continue with the creation of our first assistant and here you can see the code for that so we can use client beta assistance create and then we have various parameters that we can put in name instruction tools models Etc one important thing to note is we can also put a file in here so here you can see we're giving it a name Etc and some instructions on on how to behave so this is a simple prompt but then we specify that we want to enable retrieval for this agent and if we enable retrieval we should also provide it with some data that is how retrieval works it decides with every question okay is this a question that I should search my own data for and the API also has functionality to upload your own data and we can do that with the following so we can have uh client files create and then file and then we open the following file and we also give it a purpose so if you look at the project and go to the data folder you can see the Airbnb fhq PDF document in here and that is exactly the one that I link to Let's store that and then we can come over here and we can say upload file and we're going to put in the data path but then let's see we are over here and let's see let's store the file and now we can upload that and make also also make sure to store that okay so now behind the scenes we're uploading data to open Ai and we now have a file which also has an ID so we can use that right now with the creation of our assistant and and for context this is something that you would do for example using UI coming over here and then uploading a file Let's see we can come over here to files and you can now see I just uploaded this one and I also click on this and I can see my file here so that is data management and how you can do it through the UI and through the API so now that we have that over here we can come to our create assistant so let's look at what's going on over here and for the sake of demonstration create a number two because I already have this one and what we now have to do is we can run the create assistant and then put the file we've just created put that in here and this should still be in memory let's see file we still have it in memory and create the assistant so within the code this is uh a one-time process and that's why I'm doing it kind of like flexible over here so later on we we don't need that as we start to interact with this so this is something you can either do like in a notebook or in a separate file normally but let's run all of this and see what's happening so we are now creating our assistant and now we have WhatsApp Airbnb bu assistant to all right so that is really the first step creating your assistant and providing it with data and now if you're new to AI or data projects or if you're a developer that want to get a better understanding of these AI applications then a good resource that you can check out is Project Pro so project Pro is a curated library of endtoend projects and they have more than 250 completed projects in here all related to data and artificial intelligence so we have machine learning data science deep learning NLP projects and for example here is one llm project to build and find you large language models here one on Lang chain so with these projects you not only get the complete code written by industry experts but you also get video walkthroughs documentations and even architecture diagrams so this is really one of the best resources to go through if you want to get a better deeper understanding of data science and artificial intelligence tutorials on YouTube will only get you so far if you really want to get in depth follow these complete end to-end projects then make sure to check out project Pro link will be in the description all right so now we've created an assistant but how do we interact with it and that brings us to the following concept and that is threats and messages and this is important to understand if you want to dive deeper into this make sure to to check out the docs but what open AI has done is they have allowed developers to very easily keep track of different conversations and the messages within all of that and a threat is basically an a whole history of of messages and I think the easiest way to describe this is if you go to chat GPT you have your account but on that you also have the different conversations that you have right so each conversation there is a different threat which is different messages and by separating threats using an ID a unique ID WIS developers can very easily keep track of that not only between different conversations with uh the same user but also of course between different users that are on our application so for example in the context of the WhatsApp bot when I send a message to the bot using my number we of course want to keep that separate from the messages you are sending to the bot so let's see how to do all of that so we're going to work on a function that is called generate response and we're going to start pretty simple and we're going to build that out so initially we cannot generate a response with this because we first have to create the threat for every new user and then add a message to it so it's pretty straightforward we start with a client beta threats create that will create a threat so this is now created and we can look at that and it has an ID created at and and it has some metad data we can also get the U uh get the thread ID by calling the ID and this will allow us to now have a variable that just has this threat ID all right so now we've created essentially a single conversation that we can use to send messages to all right and now next we are going to add a message to that threat so that is using beta threats message create so we'll start with a user role so whenever someone sends a message to the bot this is added as a user roll and then we put in the message so we can do that over here and it takes in the message body so let's actually uh store that and take again let's take the example of the check-in time where is it over here so we have a a body and we are going to run this so let's do that and we now have a message object which we can look at and this is a threat message it also has an idea um but here you can see it does not have an assistant yet yet or nor does it have an answer and that is because this is just a way to prepare the message before sending it to the AI model so and now if we actually want to run this assistant let's get to the next steps we get into runs and run steps so I have a follow-up function let's look at this and this is called run assistant it's a lot of code so let's really go through it step by step so you understand what's going on because this is probably the most complex part because this is where everything comes together so remember that we have created a threat also with a threat ID so that is the to keep track of our conversations and our messages we also have a message what's the checkin time and now we have to send this to the assistant that has context of our data so access to the PDF document and then it should reply based on that so let's see what we have to do so first we have to retrieve our assistant again and we use that using the retrieve function and calling the ID into this so we can load that and then you can see we have the assistance so how do you get the ID well you either get it from the assistant variable that we created in the previous step or you go to your assistants and you can see the ID over here so two ways to capture that information so that's the first step then we're going to retrieve our threat and this also requires an ID that we put in here how do you get that well also from the previous step so we get an assistant and we get a threat and again to check uh that that is correctly stored we can see assistance ID threat ID all right and now this is where we can bring everything together so we can run all of this and put in the threat ID and the assistance ID and now since we've added the message to this unique threat ID now whenever we run this that is what at when everything starts to come together and when the interaction with the AI model will be made so this is pretty complex there are actually a lot of things going on behind the scenes to bring all of this together so I really want to go step by step here and show it with a with a real world example all right and we're not done yet because the thing is if you just run this we can see nothing happens in terms there is no response I can look at this and it just has a uh an ID it has the assistance ID canceled it has all the information completed at created at and that is because how they handle this this run object and if we then come back to how this actually works it explains what's going on over here and again you should read through this to to get a better understanding but this piece is very important in order to keep status of your run date you have to periodically retrieve the the Run object so you have to check this object if I come over here without retrieving this one more time and looking at the status it still says cute because this is when you initiate the run but now and this is where this piece of logic comes in it's a little while loop to itly check the status of the Run object so this has run for a couple seconds already right now I'm now going to skip to this line and run just this part so not in the while loop we'll check that in a bit but if I retrieve this one more time just visualize the run I the the Run object we can see going all the way over here all right so now we have status completed and that is something we can access by calling run. status you can see completed so this is something weird right now in the in the API and it's definitely because it's still in beta It also says uh we're plan to add support for streaming to make this simpler in the near future so right now you literally have to build in some mechanism and I right now put in a time. sleep to check every like half second whether it's already completed using this while loop so this while run status is not completed it keeps checking the Run status so this is a little workaround that you have to do right now that I've not seen in any other tutorials yet so now let's see what we got over here so uh the run is completed which would cause it to break out of the while loop and now we can get the messages so how does that work well we have beta threats messages list and then we put in our same thread ID again so remember the threat stores all of the messages so let's get all of them see what's in there and then we have a message object and here is all the information that is stored within uh the messages so this could be a lot and to get the latest value you have to parse it like this so this is all all Json dat data and we get the data first element then context and then if we print a new message we can see over here we get the same answer the checking time for the Airbnb is after 300 p.m. based on the time over here so that works out awesome all right so that was the full loop from creating an assistant giving it tools and data and then getting to a user response but now let's take that one step further and see how we can bring all of this together in an application framework all right and I'm going a little FAS now because there's a lot to cover and you can also check out the code yourself follow the comments and it should all be pretty straightforward but what we got going on over here we have separate pieces of functions first of all for threat management for generating responses and then for running assistance and we want to do this to enable the following so let's think of this example again from the WhatsApp B and let's say we have John and we have Sarah and they uh they both have their unique WhatsApp uh phone number which is a unique identifier for them so in this case 1 2 3 4 5 6 for Sarah and they have different questions now we want to ensure that the the threats are handled correctly and this is something that out of the box is not possible because when you create a threat it uses a a uniquely generated ID so if you later want to reference that and say for example this threat belongs to John you have to create some kind of a a database mechanism to keep track of that so when the the next time when John with his WhatsApp number sends a message you know which unique threat ID to retrieve from your database so that's really what we're going to to focus on and what I'm going to show you right now so that is basically the only thing that that's added functionality for that but that really is what Bridges the gap between just a demo from the quickstop start and turning this into something useful and for uh the database solution we're going to use a very simple python Library called shelf and that is a uh persistent dictionary like object and it's a very easy way to store a diction dictionary that that we can uh use for later so I will show you what that looks like but you can just as well use any kind of data storage that you would like so we have two functions that we're going to store that is check if the threat exists and also to store it and we're going to base this on the uh WhatsApp ID in this case so we're going to reference whenever John sends a message we're going to check hey does this uh WhatsApp ID this WhatsApp number is it already present in our database if not we'll create a un unique thread for it and that is what is handled over here so you can see uh if a thread does not exist create it and store it so we'll check that so uh if thread ID is none we'll first call this function check if thread ID exists if not we'll create it and we'll store it and you can see here store threat and how we're doing it uh we open threats _ DB which will show up over here in this in in our project once we start to run this all right so let's now see what we got going on over here so let's quickly store all of this and now let's say we are John and John is is new to he just booked the Airbnb and he wants to know what is the check-in time so let's see I've included some print statements to also show the name and now let's run this to see what's going on and here we can see creating new threat for John with WhatsApp ID 123 so it correctly identified that there is no database which there is right now you can see threats DB is created and then it generated the message so here's the message and then finally it says to John checking time for rbb is after 300 p.m. and it also links to the source so that's pretty awesome but now let's see what happens if Sarah sends a message let's run that and again it correctly says creating new threat for Sarah and again we get uh the response and this is now sent to Sarah but now the cool thing over here is is if we take another message and let's say John asks like hey what uh was my previous question so it says now retreat reving the existing threat for John because it's already in a database and to John your qu your previous question was asking about the check-in time okay awesome but now what if Sarah asks that same question and then to Sarah your previous question was about the PIN for the lock box so this is pretty sick right and if you have some development experience you know probably what's going on behind the scenes and how much work it would take to properly set that up that that user management to keep track of all of the messages in the cover conversations and Inter integrating all of the AI models with the data and we now have that in about 100 lines of code so this is complete open AI surface that you can use to interact with the assistants and this is something I haven't seen in other tutorials yet so most people will just cover the quick API steps over here but this is just the basis and you really need functionality like this to bring it all together but now an important question for you as a developer should you go all in on this assistance API and use this to build your AI applications and to answer that I'm not sure I would say I'm a bit hesitant right now to convert my existing and ongoing applications that I'm building for clients to this new framework to this new API and my current method is to build most of this functionality that I've just uh showed to you all custom so mostly using length chain Vector databases performing the similarity search and the embeddings all of that so it's a lot more work but I already have uh the code and the Frameworks for that in place so once you got that up and running it's pretty easy to replicate and the the upside is that you're uh you have a lot more control over what's going on so with this approach you're uploading your data to open Ai and using it from here for retrieval so for I know that for most companies most clients this is a nogo and also from a data management perspective this is definitely not ideal that combined with the fact that this is still in beta uh version of it and while testing it I have found some some scenarios where it did some some weird things where it it said things like hey I cannot answer that or I don't I don't have information on that where it was literally in the PDF document that I provided it with and this is something that has never happened to me with my pine cone setup for example and it was found in some some logs that they apparently used the the quadrant Factor data datase so there are definitely some things going on behind the scenes that you don't have full control over and of course when you build custom software when you build custom applications that that's one thing you do want you you do want that full control and as of right now I feel like it's really a step in the right direction to make things easier for developers but right now I feel like it's still not there yet that said I think it's important to keep a close eye on this this API and at least understand how it works finding your assistance with functions and en code interpreter and retrieval and potentially other tools it's definitely the direction that we're heading and the concepts that are integrated within this API so effectively managing threats and man and messages those are all Concepts that you have to understand so I would still recommend dive into this play around with this but for full scale applications like I said I would still be hesitant and I would still revert to for example looking into Lang chain and and choosing your own Factor database and build it out like that all right so now you have a good understanding of how the assistance API works and if you want to learn more about this and want to learn more about working with data then make sure to check out my free group data Alchemy link will be in the description we have 4.5k members in here the community is very active we discuss all things AI but also there is a classroom in here in where I teach you the Alchemy codex Which is my unique way of working with data and python especially good for beginners but even for season developers switching to uh data science and artificial intelligence so make sure to check that out it's completely free like I've said Link in the description and then that will be uh it for this video thank you for watching as always uh I would really appreciate it if you like this video and also make sure to subscribe to stick around and make sure you don't miss future updates for example the complete WhatsApp Bot video that I have in the pipeline and for those of you that have been following me for a while my apologies for being away for some time from YouTube it's been some weeks since I've uploaded um I did some traveling I did some moving my actually my girlfriend moved in with me and it was just a lot combined with all the client projects that I'm currently working on with my business data Lumina so there was a lot of stuff going on and unfortunately I didn't have enough time to put out YouTube videos but I freed up some time again I'm back at it so hopefully we'll get back to some some consistent uploads because there's just so much going on in the world of AI and it's really exciting also sometimes hard to keep up with you know one one week you learn this specific library and this tool and then the next week it's all obsolete by some some new update but yeah that's just the tradeoff uh of being on really at the Forefront of this AI Revolution and yeah like I've said it's really exting again thank you all for watching and see you in the next one
Info
Channel: Dave Ebbelaar
Views: 41,666
Rating: undefined out of 5
Keywords: data science, python, machine learning, vscode, data analytics, data science tips, data science 2023, artificial intelligence, ai, tutorial, how to, openai, bots, assistants, assistants api, gpt-4 turbo, airbnb bot, whatsapp bot, custom gpts, bots with assistants api, vector databases, openai dev day, openai assistant, openai assistant api, openai news, build ai bots, ai chatbots, openai threads, gpt 4, gpt chat, open ai, ai automation agency, chatgpt plugins, ai automation
Id: 0h1ry-SqINc
Channel Id: undefined
Length: 27min 3sec (1623 seconds)
Published: Thu Nov 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.