welcome to this comprehensive course on open AIS assistance API which allows developers to build powerful AI assistance capable of performing a variety of tasks this course covers essential topics like integrating the API for advanced functionalities leveraging its knowledge retrieval and code interpretation capabilities and understanding the fundamentals of large language models through Hands-On projects with streamlet and real world examples you'll gain practical skills to build respon responsive AI enhanced applications by the end you'll be able to use the assistance API to transform your web apps into Dynamic smart platforms hello and welcome to this course my
name is Paulo deson I am a software engineer so I teach people how to become software Engineers developers and build applications and I've also taught over 250,000 students on various skills from programming build apps AI as well as Cloud engineering so welcome to this course the next question is what is it that you're going to be learning in this course well in this course you're going to be learning about the assistant API essentially you're going to learn about functional calls you're going to learn about retrieval mechanisms and of course you're going to be building a
lot of stuff going through this course because that is how I like to introduce certain Concepts to my students so that's part of the what you're going to be expecting in this course and of course I'll be talking about large language models embeddings and all these things that relate to AI specifically okay now let's talk about the prerequisites for this course so I expect you to at least have the basics of programming of course I don't expect to be an expert or anything as long as you have some fundamentals of programming be it on JavaScript
or any other modernish programming language you should be fine going uh through this course and one thing to keep in mind is that unfortunately this is not a one-on-one programming course I'm not going to teach you how to progam program rather I'll focus on the big picture idea of assistance API and large language models so we're going to be using python in this course so if you've never used python you don't even know what python is that's okay that's not the main thing here the main thing is that I want you to understand how this
technology how assistance API and how large language models how AI based applications are built that is the main goal here so don't worry you'll be fine as long as you want to learn and you follow along what I'm going to be doing the other thing also remember that you'll have access to all of that code in the description below okay now let's go ahead and look into python setup and essentially the environment setup so that you can follow along in this course now keep in mind if you already have python set up on your machine
and you have Visual Studio code or any other code editor that you use obviously you don't have to follow along because this is you have all that right so if you don't have that then I'm going to show you right now what you need to do what you need to install on your machine that way you able to follow along in this course now the best place I found online where you are able to go through tutorials on how to set up python specifically on your machine whether it's Windows Mac Linux and so forth is
this link here uh which I'm going to show you right now let's go ahead and take a look all right so if you go to this link here which is kingstar docomo base install python this does everything that you will ever need or at least has all of the instructions you need to install python if you don't have python installed on your machine okay so just go ahead and follow exactly what they have here and they even tell you what is python which is kind of nice and all the things that you need okay so
just follow along and the great thing is that you can go through these different links here so how to install python in Windows if you're on Windows click here that will take you to this setup and how to install python on mic OS that will take you through this whole all setup that you need to go through and of course if you're very special and are on Linux go ahead and click here and just follow the instruction so it's really straightforward I don't see any value in me going through all of this with you so
go ahead and get that setup if you don't have python setup on your machine and we'll go from there so the next thing we will need is an IDE an integrated development environment I use Visual Studio code which is what I'm going to be using in this course so if you have something else that you use that's totally fine go with that but go ahead and download that if you don't have it so it's very simple going to go to visual studio code and depending on your machine for me I'm on Mac it just goes
straight to download Mac universal you can click here and you can see that we have mac o universal window Linux and all of this okay so depending on your situation just go ahead and download the ver version the latest version of vs code and you should be ready to go okay now because of the nature of what we're going to be learning in this course which is open AI assistance API you will need to have an open AI account that is very important for obvious reasons and so particularly what you need to do is you
need to go ahead and create in this case have an API key because that's what will allow us to use the assistance API and and various things we can do with open Ai apis and so forth so that is very important so let me show you real quick here so if you type in open.com as you see here make this a little bit larger you'll be able to say log in so click here if you don't have an open eye account you can go ahead and say sign up if you already have one you can
just put your email and go ahead and sign in also you can go ahead and say continue with Google with Microsoft account or with Apple so I already have an account but in this case you can go ahead and sign up okay so I'm going to say continue with Google I think you can also go that route to create an account and gives you this opportunity here to choose between going to char or going to API we want to go to API and this is what's going to open so this is will take us to
this main view here so there's a lot of things a lot of capabilities uh get started and all that what do we really want to go to is to go to assistance API which actually we're going to do that later but what we want right now is to go to API keys so click here on API Keys you can see that I have one API key which I created a little bit ago so go ahead and say create an API key and you can name this assist API Das key you can name whatever you want
and that API key click there to create this API key and then what you need to do is copy this API Key by clicking here and save this API key for later in this course once we get started coding into all these things so it's very important to not to share this with anyone in fact after I've created this I'm going to delete this so that way you can't use it okay so save it in a very safe place that way you don't lose it okay make sure that once you close that window just like
I said there's no other way for you to access it to get it again so that was that's why I wanted you to save it okay but you can create as many secret keys as you need so if you for some reason didn't copy that you can go ahead and create a new one and go from there so that should be it about setting up your API environment understanding what we're going to be doing in this course as well as having your API Keys set up or created for us to be able to use that
in this course okay so now let's go ahead and look at what is this thing called assistance API the assistance API is a tool built by the opening I folks that allows developers to easily craft and build AI based applications that Leverage The Open AI environment the open AI platform per se so the thing to understand about the assistance API is that it's not its own entity per se although it is but it is attached or extends in this case what we know the open AI API which includes all of the tools and models that
power chbt and many other underlining infrastructure of the open AI environment so that is the important distinction here is that when we talk about the assistance API it extends what already is there in this case the open AI API and so it makes it easier for us to build AI Tools in this case AI assistance that we can use in our applications so essentially allows us gives us this platform this framework this libraries to quickly use these tools to build any application AI based application that we would want to build without having to know too
much about the underlining obstruction of how things actually work so essentially we can go ahead and create an assistant and use the API so that we can connect that assistant in our application and use that assistant that has the capability of connecting with the open AI apis essentially now the next thing we need to look at is for this assistance API what is the problem that it solves now the thing to understand is that building applications in general later alone building AI based applications uh it's a very hard task for developers because it's very complex
there are usually a lot of moving parts that a developer needs to focus on developers need to manage in this case certain infrastructures they need to keep in mind how the data is being used Which models are needed to use in this case to power their applications they have to learn how to craft the best prompts possible in order to communicate with this large language model and they also need to understand or keep track of the application State understand about embeddings which is something totally different but it's part of this realm of AI and large
language models and understand the storage mechanism right how do we upload files how does the large language model use those files to allow us to tap into the main knowledge but also those files retrieval those files knowledge but also retrieve information from those files and so many things right so this is not easy it's a very complex thing to carry as a developer this poses a problem for developers because now developers instead of focusing on building the actual application now they're focusing on how to stitch all these pieces together to make this work so the
benefit of using the assistance API is that is that all of this complexity all of these things that developers need to know know or to deal with it's abstracted out which means they don't have to deal with it anymore because now the assistance API the back end of everything is taken care of all we need to do as developers now is to focus on what matter which is build Our obligation so the assistance API already has all the things that we need as developers but we don't need to think about them because it abstracts out
all of that work so one thing to keep in mind is that when we talk about large language models is that they by themselves they don't have state which means they don't save a context of a conversation per se now with the assistance API we don't have the problem because there is this thing called persistent threading for ongoing conversation what this means is now we're able to save messages and the context of that messages that way let's say if you're creating a bot um you don't have to yourself save that context so that then when
you ask a certain question that the bot needs to know about the previous conversation the bot will actually know what it's referring to because it has a context okay that's huge because now we don't have to worry about that the assistance API has that built in and also we have the retrieval mechanisms for digging through data so what this means is that we can upload files for the models to use for additional knowledge base so you can upload files about something let's say about cryptocurrency so now that piece of knowledge is added to the main
base knowledge right that comes with the large language model that way you can converse with the large language model in addition to the knowledge that we just added by uploading certain files documents and so forth so now we have a more flexible and more knowledgeable if I may say large language model because we are aiding it with retrieval mechanisms it also has code interpreters so it actually writes can analyze code and do all sort of things which is amazing as you will see okay one of the biggest things that we now get with assistance API
is that we can actually do function calling what that means is that when our assistant is solving a problem we ask a question the assistant will be able to call a function that we specify that will be used as a tool to go and fetch certain data that that the large language model needs to solve a certain problem and that is huge because now in addition to uploading file for retrieval mechanisms and so forth we can also add more tools by adding these functions that go serve as agents that can go and do something else
and bring back the results which are going to be used by the large language model in this case the open AI apat per se or the assistant right to solve the problem or to answer accurately what you are asking it for that's pretty awesome mind-blowing let me show you the diagram here real quick so you can see visually the assistant API benefits so if you look at this diagram here right the assistants can go ahead and call the openi models right to uh tune their personality in this case their capabilities also these assistants can also
access multiple tools and parallel so now it's sort of having these agents that can go and do stuff and bring back the results so that we can help the assistant to be a better assistance right so in this case here these are tools the tools that we have here we talked about the code and code interpreter we have the knowledge retrieval right files and documents we can pass upload so that our assistant is more knowledgeable about certain topic as well function calling now the great thing about function calling is that we can create it's a
way for us to create our own tools on top of the tools that the assistant already has access to such as the code interpreter knowledge retrieval and so forth so now with this we can create our own tools right our own functions that go for instance and fetch the current situation in Bulgaria or go and fetch go and fetch some sort of data in the database somewhere you get the idea and along with everything that I said we have these persistent threads the threads we'll talk about in a second here allows us to have these
messages that we are saving or are being saved in our conversation and so those are saved for context which is H huge because remember all large language models they don't have a concept of saving history per se so they're stateless so they don't have a way of saving the context which is problematic when we want to refer to a certain concept that was talked about if they don't not they're not saving that history then it wouldn't know what you're referring to so context is very important and that is all given with the assistance API okay
and of course we can access files in several formats but this is diagram I wanted to show you about the benefits of the assistance API now most like of course you've heard of the chat completion API which is the one that you've been using if you were using the open AI API right so you just send messages and you receive response and it's very useful because the completion API it goes straight to the open AI models and ask questions and the large language models and of course the knowledge base but the thing here is that
this knowledge base is knowledge base that is already there there's no addition meaning if you were to ask certain questions about something that was done or happened after September 20 after September 2023 I believe right anything that was done after that then the large this large language model here wouldn't have that knowledge base so it would just say I don't know because the completion API is just a way for us to get direct response okay that is the difference here between the chat completion API which is useful with the assistance API gu as I have
discussed earlier okay so let's look at this General comparison uh so we can see the differences between the assistance API and the chat completions API okay this just to have some sort of an idea an overall idea so you can see here the aspect column and the assistance API the chat completion API so for assistance API initial setup you have to create an assistant with Define capabilities which we'll see in a little bit for chat completion of course there's there's nothing for you to set up you just call the API and get the results the
answers and so forth for the assistance API we have to initiate and manage in this case a thread for ongoing conversation we'll talk about what a threat is and all that stuff in a little bit and for the chat completion of course we have none of that so this is just to show you the differences between the assistance API and the chat completions API and you can go down here to complexity so more complex setup of course for the assistance API and it's simpler of course for the Chad completions API okay so no need to
memorize this this is just something you can take a screenshot of and then go ahead and you know just take a look to see the differences between the assistance API and the chat completions API okay and there you have it all right so we just finished looking at the benefits of the assistance API and how it works and how it facilitates our lives as developers so that we can focus on actually building those AI based tools right instead of worrying about the underlying infrastructure things that we would have to do if we didn't have assistance
so now let's look at the assistance API the building blocks because we talked a lot about the underlining infrastructure how it works to benefits let's go ahead and look inside and see what are the building blocks of an assistant all right so here is a diagram I like having diagrams because it hopefully helps you conceptualize these ideas the assistance API has about four main building blocks so so first you see that we can have in this case an assistant so the assistant itself is in this case The Entity the thing per se that we actually
build which will then use to help us get information about certain topic and so in this case here we could have an example an assistant who is a travel agent bot that we can use so that is the first component and the second component of second building block is what we call threads a thread is where all of the messages that occur between the assistant and the user because that is the whole idea all those messages are tracked here in the thread so in this case here a users may have a message and say hey
what is the cost for an extra day in Paris okay so that is a message that is going to be on a thread which the assistant will have access to as well as the user and then the assistance message would be the cost is 12,890 for instance right but then you will ask okay okay there is the assistant's message but how does the assistant know well the whole process has to go through the other piece here the other building block which is the Run entity so the Run entity needs to have a few things for
it to be able to run those queries those questions essentially evaluate what's going on so there is this dialogue per se that is happening so in this case here the run as you can see here we have has to have access to the assist in this case you will see later it's just going to be an ID and also has to be have access or has to have a reference to the thread because that's where all the messages are so in this case here the assistant is going to be our travel agent bot and the
thread is trip to Paris for instance and then inside of this run the Run can be triggered to then run different steps to get to that conclusion to get to the result what would happen is if you look at all of this we have in this case the assistant the number one building block we have thread that is the number two building block inside of the thread that's we have the message so messages or message that's the number three building block and number four is the actual run entity okay and so how would all this
work well we have the assistance and inside of the assistant we have and then we have the thread we have a message from the user what is the cost for an extra day in Paris that is going to be sent to run which will have the assistant ID as well as the thread ID because it needs to know where to pull those messages that are coming in from right and then it's going to go through a process steps number one use code interpreter if that's what needed to calculate things and then is going to go
ahead and create a message and if need B this is the beauty is going to be able to go and call any other tool that may be needed in order to get the correct response and the response is going to be as a message which is going to be returned back again to our thread does it make sense so the thread is where all the action really is being dumped into the history per se right and there we go and now the thread will have the assistance message because all the steps are done we have
the response and goes back as a message assistance message which then we can use that to show in our application these are the assistance API building blocks that's very important here because it can be a little bit tricky at first to really conceptualize the idea the assistant itself needs thread a thread that's where all the messages go just think of a bucket of messages right and then the entity run here this is where the running of things to go and fetch things go through the steps this is where it happened the Run needs to have
reference to the assistant because needs to know who is this assistant because remember the assistant will have all of the specifications that it needs so it knows what it specializes in in this case is a travel agent bot so the Run needs to have a piece in this case reference to these assistant so when it goes and starts running the steps it knows what knowledge base it needs as well as other tools to indeed create those message back so the assistance messages are all here so that we can then use those messages to show in
our applications so forth all right so very simple but can be complex to wrap your head around understand and conceptualize these building blocks then what's coming next is not going to be too complicated okay so rewatch this part if you need to but I think you should be good so what we'll do next is we're going to go in the dashboard in the playground and look at create creating an assistant manually and later of course we're going to do that using Code okay let's go ahead and do that so now let's go and log in
into the open AI dashboard so you should have an account and everything now when you log in this is more or less what you're going to be seeing so what we want to do is you want to hover over here if you don't see that so you click here this is what you're going to be seeing going to hover over and we're going to go to assistance but this is where we need to go so click here and sometimes takes some time looks like uh incorrect let's go ahead and refresh this okay there we go
so today we've been having some issues but just refresh if you're having some issues okay so once you here let's go ahead and create our first assistant so we can go ahead and say create here or from here doesn't matter going and create and first thing we need to do is to add a name so let's say this is going to be personal trainer as such and then we need to pass or add the instructions so because it's an assistance we need to say okay you are X Y and Z you're knowledgeable in this area
and so forth okay I have the instructions here so I'm just going to paste it in here and I'm going to click here to make it a little bit larger so this says you are the best personal trainer and nutritionist who knows how to get clients to build lean muscles you've trained High Caliber athletes and movie stars because that is exact what we are all are striving to be okay so there we go so these are the instructions of course you will want to put as many instructions as a prompt really as you need for
your assistance so if specify exactly what you want your assistance to help you with okay I'm going to go back and next we're going to choose the model right openi model that we're going to be using there are a few and as a recording of this video we have gpg for preview and of course these numbers will change depending on when you're going to be watching this video okay but just imagine just remember that the concepts of how this worked that's what really matters instead of things that may change here okay so I'm just going
to go ahead and use the jpt 3.5 turo that should be fine for now and it's a little bit cheaper as well so I'm going to you are more you are more than welcome to go to the higher so I'm going to just use that one really that's fine and then we have the tools we talked about the tools so these allow us to attach certain things that they we want this assistance to be able to do if it doesn't already have those tools right so we have here retrieval we have the code interpreter and
we can add functions as well okay so we don't need anything I'm just going to go ahead and toggle the code interpreter just because why not okay and anything else we can just live as is and I'm going to say save as such so now the assistant was created and if I get rid of this and get rid of that and we can see that we do have our personal trainer so there is the instructions and the ID so this is very important because remember the diagram I was showing you earlier was that um when
things start running when we have that fourth component or building block which is the run it needs to know this personal trainer or in this case the assistant so this is an ID that will be used internally later we're going to be able to generate or create a system using Code which I'm going to show you but I just want to give you an overview right now so very important these IDs are going to be used and from here you can see we can go and say test so if you click here this will open
a different tab as you see here and then you can test it out to see if this actually works for instance you can enter a message remember this is the message in this case will be the user message if I show you exactly these assistance API building blocks this is where we've set up so we have the assistant which is in this case we said it's travel agent bot but in this case is our trainer personal trainer and we have thread which we haven't created yet hasn't been created but we will create all of this
is going to be created in the back end once we start running things and then we have the messages which is now a user message is exactly what we pass here okay so say I want to build muscles what foods should I eat I'm going to go and say run haha run what you think it's supposed to do well what's going to happen now when we run first of all that message the moment we say run the message will be added to thread Aha and you can see it even says thread here right so the
thread right now is empty if I say run it's going to go ahead and add that message and the instructions is showing here and the there's a lot of things that happening here so let me go ahead and go through everything I'm going to just make this probably can't make that smaller that's okay and one thing you see here this is the thread and each thread that is created because it is indeed a simple message right and each thread that is created has also an ID this will make sense later so you can see here
um we have all the information so the thread is where all the messages are going to be in from this case our user the question that we ask as well as when we run and come back we should have the personal trainer the message as well just like what I showed you here this run went ahead and pulled in the assistant ID internally and then the thread ID internally and then went through the steps and sped out the assistance message which now we can see here is the message right this is the assistant say personal
trainer that's the name of our assistant and says to build muscles lean muscles that is it's important to focus on balanced diet that includes blah blah blah protein rich foods whole grains fruits and vegetables healthy fats dairy products and all this great stuff and even gives us a little recommendation here and note it's important to consult with healthc care professional and all that great stuff very cool this is what's happening right essentially goes back to what I showed you earlier going back here you can also see that we have logs now this is really cool
because it allows us to look at exactly what happens so you can see here we can hide the logs we don't want to hide the logs right now because I want to show you something so for our logs here look at what happened the moment we said run what happens that a thread was created indeed because we need a thread where all the messages are going to be in just like again aha what I showed you here okay okayy this is this is good okay so if we open here you can see exactly what happens
so the thread was created and it we have a response which is indeed the thread ID and this ID here is exactly the same as this this is the thread ID that was created okay and we have other some other metadata that's okay and then we added a message to that thread the message was the one that we wrote here in fact you can see all that happens here on the thread visually it is what being shown here as a log but I want you to understand this so you know where things are in case
when things don't work you understand where to find things okay so we have our add message here the message was added the role is user because I am a user in this case because I enter the message here right so going back it is this message here which is going to be sent to run very good so that happens let me go ahead and close this we added message and then we run the thread right it just did a post request aha it has to have the assistant ID why did I tell you because if
you look at this diagram here first it said assistant the name but internally it has to have the actual ID as well as the thread ID which is exactly what's happening in the back end if I close this thread here let's see you can see that has the response and the response each response has of course an ID the object is run and when it was created the assistant ID as well as the thread ID voila and all of this other metadata as well as the model that was used the instructions all of that was
sent along in the response okay in the thread and then of course get runs status because when we hit run when hit run what happens at the back end things are happening meaning these steps are going to go do you see maybe it has to use the code interpreter create a message and do all these other things it's not until all these steps are done which means an answer a message in this case an answer from our run okay the our assistant in this case uh it's not until then once that is done that the
message is actually created and sent back so all of that is happening here all right this is the log and run step let's see what happens first it has this object here this data if I open this data here and keep opening it has the ID the object right these are the steps right in fact that's that's ex that's exactly what I'm showing you there's the status and this is the steps going through the whole process of creating crafting this message for the assistant message which is going to be sent and then we can see
the results all right let's continue so you can see it has a Json here response it comes in as list an object list and we have the data field which has an ID the object thread step a run step when was created and all the great stuff and continue the other step and as many steps as possible and then the status run status to tell us whether the running is over everything is set up now we have the response the message which can be passed back to the assistant right so that we can then see
let's see continue completed you can see here that the status now is completed which mean all is good we should have a formulated uh message okay if I go to tools you can see what tools were used code interpreter right in this case even though it wasn't really needed but it's okay and then get runs you can see now it returns data which has an ID has an object and the status and everything and then we go to the message response now comes in as object list and data there and you can see now we
have the ID the thread message okay the first one there and then this is for the assistant aha if you open here you will see that assistant will should have let me click here to open aha there we go and there we go we have the the actual value which is exactly this what we see here says to build muscles all in muscles all of that information which weed back so the assistant in this case went through this whole process that I showed you earlier and the return the assistant message in this case back to
our thread which then we can see in this case we're able to see that the information the message is here so this is a very good representation of the assistance in all the building blocks but this is real cool the log is because it sees we can actually see all the whole process of what's happening so we understand exactly what's happening so it's not this Mystery Box per se uh what you've learned here is that to create an assistant here manually you have to easily just go to the assistant API or playground ideas and just
hit create and you have to pass the name of your assistant as well as the instructions right so specify what do you want your assistant to be good at or to what knowledge these assistants has to have right what they're going to help you with and then of course that will generate the ID as you see here and of course as you create a assistance you have to of course as you as I showed you you have to make sure that you attach the right model and the tools that you want to attach to so
what I want you to do is to create for instance a code assistant okay so essentially uh come up with instructions for for an assistant that can help you with code issues right you can say you are a very helpful Java developer assistant blah blah blah you're knowledgeable about the basic of java intermediate and advanced and so you help developers to do pair programming whatever it is so that and test it out and see if it works and make sure because it's code based make sure of course in this case you really would need to
have this code interpreter because then you can paste in as a message a user message a pie of code and ask the assistant if this code will compile if there's something you can do they can do to improve it and so forth so you have some ideas on how this works okay do that and save changes and create the assistant and then test it out and see if it works all right go ahead and do that and I'll see you next all right so now we're going to actually do the same thing we did before
but in code which is very exciting the first thing we need to do is let's go back to our playground here assistance playground login and everything and I hope you were able to do that little exercise earlier if not well what can I say all right so what we'll do here is we're going to do the same thing but we're going to be using code right so I'm going to go ahead and delete this yes I know it's very sad but it got to go okay now it's empty so we're going to do all that
using Code so you should have Visual Studio code installed or any other rde that you may use that's fine and it should also have python everything set up so everything should be good right so what I'm going to do here is I'm going to create a new folder for our project so personal trainer as such going to CD to our personal should be personal but Persona that's fine too okay so it's empty there so I'm going to say I'm going to say code to open this exact here just going to going to close that one
and then there we go so we don't have much going on here so I'm going to go ahead and create a new file let's call this main.py there we go and also I'm going to go ahead and create in this case requirements.txt requirements txt such this is where we're going to put all of our dependencies and libraries that we need for our Python and for our project I should say and next what I'm going to do let's go ahead and create create a file this is going to be our environment variable file so EnV this
is where we're going to put our environment variable so the idea is we're going to have our open AI environment variable let me close this down like that and we're going to put your open AI key remember that key that I showed you earlier so essentially you're going to come here and go to API keys and if you don't have an API key go ahead and create one just like I showed earlier and take that key and that's what we're going to put inside this here okay so go ahead and do that and for requirements
here I'm actually going to pass in a few ones I'm going to say python. EnV and I also need open AI package like that very good so that's inside of our requirements.txt I'm going to create another file here called thatg ignore so that's for me to hide a few things once I push this to the repository GitHub repository so that you don't have things that you don't need okay so in this case here I'm going to make sure that our git it's going to bypass the environmental variable file so that it's not pushed I'm going
to create a python environment that way we can run pip install to install everything in this environment specific for this project to do so uh remember I'm on Mac here here Python 3 and then it's going to be I'm going to say Das m v andv as such and then the name of the environment so I'm going to just say my EnV like that and what will happen now is just going to go ahead and create I'm going to say yes because you notice a new environment has been created say yes going to use that
and you will see now we have this my EnV uh with binaries include libraries and everything this is our python environment that we can use then to activate so to activate it I'm going to say Source my EnV and then I'm going to go to Binary and then activate hit enter you can see now my EnV is environment is activate so that's really good which means then I can say pip install d r and pass the requirements that text which has all of these dependencies or or packages or libraries that we need so I'm going
to hit enter so he's going to go ahead ahead and get all that information for us okay so all the packages have been downloaded that's really good let me just clear all out of this and then now if you look at my M of course I'm going to go to binaries you can see that we have Envy we have opening I which is exactly what we added very good so we should have everything set up now I'm going to get rid of this so let's go ahead and do some importing here so I'm going to
import open AI library and I'm going to set up the m so that we are able to get the environment variables and do all the great stuff so I have all that information here just going to copy that and paste in here okay important findm load an M Fromm package all right and I'm going to call this load M there so that it all set up and then I'm going to create a client here which I'm going to be using the client is going to be open ai. open AI as such okay and now now
with this client we're going to be able to call the openi API assistant and anything else that we want because we have created that now you could have also done something like this open AI right and then say API key and then go to os. environment. get and pass the open AI key which is the key in this case the value that we put inside of our environment variable okay so that's another way to do that but since I'm loading all the environment variable this to work just say client open ey. open Ai and also
if you want you can pass the open AI key directly from here but it seems to work just like this uh because it gets all the environment varable if not just go ahead and add the openi key directly here or just like what I showed you uh let me just do that again I'm going to put it here I'm going to leave it there all like that okay whatever works for you go ahead and do that or the other way too I'm going to put it all here there's another way you could have done said
client open AI just like I said pass the API key and go ahead into the custom environment variable if you change the names okay but if you are using open AI _ aior key then this should just work as it is all right I'm going to leave that there just in case you need that all right very good so next what I'm going to do let's go ahead and say model this is just a variable that we're going to be using and the model we're going to be using it's going to be gpt3 for turbo
and I'm going to put it here for now okay now we may change this you may want to use GPT for preview depending on situations but so far so good okay so now that we have everything set up let's see how can we create in this case a an assistant right in this case we're going to create the trainer the personal trainer assistance very simple all we do I'm going to just put here all we do really is just say the assistant I'm going say personal and then I'm going to use our client right which
is which has instantiated our open AI there and say client and it's going to go to Beta remember that because as a recording of this video the assistance API it's actually in beta which means they're still working on it and so maybe by the time you watch this video you watch this course maybe this you won't need to say beta which will be great so just keep that in mind for now we just have to go to Beta and then we go to assistance and once we get to assistance we go ahead and say create
very conveniently and then we can pass certain parameters here okay so what parameters do we pass here well first of all we need to pass the name that we want to name our personal assistant in this case I'm going to call this personal trainer as such and then we pass instructions which are very very important as you remember and I'm going to pass the instructions here real quick so these are the same instructions that we used before so you are the best personal trainer nutritionist and you've trained High Caliber athletes and movie stars okay there
we go we have all of that information let me get rid of that don't know where that came from there we go all right and the next thing we need to pass here is the actual model because we need to remember this is the same thing we've done uh manually so now we are passing the model that we're going to be using or the assistant it's going to be relying on and it's going to be the model that we pass here just put in this variable so it's easier to change in one place so just
like that we created our assistant and can go ahead and say print in this case I'm going to say personal trainer. ID right this will give us the actual ID of this personal trainer assistant that we just created okay and I'm going to go and comment that out for now the next thing we need to do is to create yes the thread because this is where all of the messages as you know uh will be so to create a thread again you see that's very easy I'm going to say thread and use client again beta
and I'm going to go ahead and say threads and. create again for our thread we need to pass a few thing now the cool thing here is that for our thread we passed messages which is going to be a list so this messages here I'm going to pass an object in this case going to start with the role okay and the role is going to be user and for the user in this case the content is going to be what well I'm going to just put something I'm going to say how do I get started
working out to lose fat and build muscles something like this so this will be the first message it's going to be sent for as the user okay we could have created a thread with no message at all and then add those messages later but we can go ahead and add a message a user message right away as well okay I'm going to go ahead and print actually let's call This Thread ID and get that thread ID so just thread. ID okay because those are needed and I can go ahead and print thread ID let's say
so we see and I can do the same thing here assistant ID and personal. ID like that and then here I can just go ahead and when we need just going to go like that very good so now that we have these two things what will happen now if we run this well we are going to be able to create an assistant right and also in this case create a threat and get those IDs which you will see in a second here so let's go ahead and run this real quick and see if this work
all right so I know my environment is active so I'm going to say Python 3 so make sure on Windows I think it's just Python and I'm going to say main that py and let's go ahead and make sure everything is good I think let's go run voila and you can see that we have our assistant ID because we want to create that assistant ID we created here personal trainer and we're printing it out and we also have the thread let's see if this is correct because as you saw we deleted everything in our back
end in our playground and now if we go back let's see let's go to our assistants we should see our personal trainer assistant which was created just now all right and this ID 7 yt5 you can see 7 yt5 that is exactly what we have here you can see we have that personal uh trainer name instructions and the model that we're using that's what we specified and so forth and internally we do have a thread which is this thread here that is ready to be used which will have a message so now this is not
working we're not seeing anything right now but it's okay because we're going to be using that okay so we have our assistant and we have a thread where all the messages are going to be in and so going back to code here we understand that if we were to run this again that would kind of deit the purpose because we have the assistant created already it would go ahead and create yet another assistant which is not what we want and this is the reason why in this case here we were able to get those in
this case here you can see we have the assistant ID as well the thread ID which we're going to be using to proceed uh creating and conversing in this case with our assistant so what I'm going to do here I'm copy these two real quick and I'm going to just go ahead and hardcode our IDs so that we can use them okay so I'm going to do like that so this is going to be assist put that into variables that we can actually use okay let's not give spaces so we don't want into issues so
now that we have that we can go ahead and comment all of this out here okay I'm going to leave this as is so we don't need to you will have access to all this code anyway so now we have what we need so in this case the assistant ID when we use that it's going to be um created right in this case it's going we're going to be referenced that in fact I'm going to move all of this uh to the bottom here because we already have all of that so I'm going to also
comment this out because we don't need that anywhere so now we have the information that we need which is the thread ID and the assistant ID because they have been created as you saw in the back end okay so we'll do next is we're going to go go ahead and create the actual message another message here so we can start conversing with our API assistance API say create a message so I'm going to say message going to set that to uh let's say say what are the best exercises for lean muscles and getting strong that's
our message we're going to then add to the thread to add to the thread well I'm just going to say message again and I'm going to say client that better and threads and I'm going to go to messages right and then create in this case a message and for the message well in this case here the thread will need we need to pass the thread ID which is going to going to be the thread ID and we need to pass the role which is going to be the user role right because we are conversing we
are asking a question to them and then the content in this case is going to be the message which we created at the top here and next we are going to then run the thread or in this case run the assistant how do we do that well we need to create that entity first to run our assistant and to do so I'm going to say run and then I'm going to use client again beta threads that runs right that create so we need to create that runs entity which is attached to the thread and you
can see the beauty here because we said earlier that run needs to know the assistant ID that's why we kept it as well as the thread ID so that they can get message and added that back to that thread so in this case I'm going to pass the thread ID it's going to be thread ID and the assistant ID it's going to be the assistant ID and in this case instructions we need to pass the instructions for our run okay so in this case I just going to say please address the user as James Bond
and there we go so now we have our run here now we have to remember that our run has to go through different steps it could go and find a tool that needs to be able to assist the user well right to create to give the assistant more power things that he needs to do right go through all the steps that we saw before uh you may need to then formulate of course that message back right it's going to be the assistant message with the result that the user would want to receive okay so that
takes time because it's a process so what I'll do here is I'm going to put together a helper function that will help help us deal with the waiting the stepping that the Run has to do before it gets to the result right the message is written back the response Etc okay so for the sake of time here I do have this wait for run completion so essentially we're passing the client and passing the third ID run ID and then sleep interval is going to be five now for this to work I'm going to have to
import a few other things here real quick so because it's going to calculate in this case the time the starting time and all of that to give that elapsed time so I'm going to just go ahead and paste in the time logging and daytime from daytime okay so these are the packages that we need so essentially what this is doing here is we're going to wait so while true we're going to try we're going to go ahead and uh say client threads retrieve in this case we're going to pass the thread ID and the Run
ID and then it's going to check if run completed at and it's going to go through and get the laps time we're going to get that and we're going to subtract so do all the calculation just to tell us how long this whole process took it's not necessary but it's fun to know and then we're going to print run completed at the elapsed formatted elaps time and we're going to log that okay but most importantly this is where we're actually going to get the message messages that is once the run is completed because that's where
also the message that we need the assistant is going to be spitting out sent to us that's it's going to be because all that is going to be put back into the thread okay so what we do here is we go through our threads again remember threads that's where all the messages are the user's messages as well as the assistant messages live we're going to go through that because it's a list and then we need to pass the thread ID so we can get those messages that's very important because you could have multiple threads going
right but on the same assistant so it's important to understand that we need to pass the actual ual thread ID so that we know we're looking at the right bucket in this case where all the messages are so we can repreve all those messages and then we're going to get the last message okay that is there because that's going to be our response in this case and then we're going to go to the text and get the value okay this is how we get the response so the response here is going to go to last
which is the messages go to content and then the text and the value and we're going to retrieve that and now here this is where I'm going to actually run by just calling the waight for completion and pass what needs to be passed which is the client I'm going to call client okay I could have just because the names are the same I could just pass client but let's just say client like this and then I'm going to pass the thread ID it's going to be thread ID I'm need to pass what else uh it
says thread ID run ID so run ID in this case how do we get the Run ID well we know that we have a run here which is being passed along right so we're going to pass that run. ID so I'm going to say run. ID so we get that for this to work and there we have it so when we run this we should see some sort of result let's see if this works I'm going to go ahead and open here and let's say Python 3 main that Pi in your case if your windows
it's got to be something else let's run if all goes well it will take a little bit because it's thinking and we should see once all is done we should see some results all right there we go now we have a result so if you go up here we can see the Run completed in what would be 24 seconds right and then the assistant response well James Bond aha because we told it to refer as as James Bond uh when it comes to getting lean and all that stuff it's essential to focus on a combination
of resistance training and cardiovascular exercises there's squats deadlifts bench press pull-ups chain UPS lunges planks and all of that great stuff so we got the response so you can see that this indeed works right and the other thing you can do also here is we noticed earlier that whenever we run something let's go to test here you can see that we have have logs so we can actually go and look at all the logs if you want to in code to do so I'm going to in fact it's going to be the steps right of
whatever it's happening all the steps when we say run the steps goes through so I can do that as well real quick here to do so I'm going to say run steps putting a variable and I'm going to use client better again threads and runs and steps look at that and then in this case it's going to come in as a list so I'm going to say that list and I'm need to pass again you guess it the thread ID because it needs to know where I'm getting the steps from or for in this case
and the Run ID which is going to be run. ID okay and remember this run here it is what we created earlier here very very important all right so what I can do now is I'm going to change the message here that the user and I can say how much water should I drink in a day to get healthier okay there we go so let's go back and open our terminal and I'm going to run this again okay so there we go we have a result here took took about 15 seconds and to maintain a
good health James Bond it's generally recommended to to aim for intake of eight glasses roughly 2 lit of water per day however this is General guideline blah blah blah and all that informations very very good ah look at this stay hydrated 007 prioritize your health and well-being very cool very cool indeed so this is very exciting notice here that now we have this power of creating different assistance apis right and create threads and different threads actually even within that one assistant API and we can have all this conversation and get response so you can see
how easy this is and um this is a very simple example and later I'm going to show you how to create an actual bot or a how to create a bot a chat bot where you can go ahead and start chatting and then the assistant will will respond and you can get information that way as well which is really interesting and now you can see the advantages here right the advantages that not only are are we just going to come here in the playground and create assistance and having to interact with it here we can
obviously but that's not scalable right because we want to build application that will leverage this assistance API and this what we're doing here is indeed what it's the leverage that we're given by the assistance API because now we can use the API the actual assist API to create uh in code and attach this to a website or any application that we may be building ourselves and then we can also go steps and look at all the steps right in fact oh I did not there we go so I didn't I didn't print the steps let's
go ahead and print the steps oh you probably saw that and I'm going to put inside of F string here I'm going to say steps and say run steps and that data that's how you get to the steps data and and then it's a list okay and voila okay let's go ahead and run this again of course I didn't do what I wanted to do because I didn't print it out but that's okay let's give a new message how many reps do I need to do to build lean muscles okay let's see if this works
and run again all right and you can see uh we we know of course this works but what I want to show you is that we can see these steps look at this object here they got ID step ID the assistant ID and all this information but most importantly it also has all the things that were that happened so step details this message creation details let's just print that as an object itself just the data and let's see if we can get more okay so you can see we have thread the Run steps and all
other this metadata and when it was completed the assistant IDs and all this other information here that was generated type message creation and voila now I really hope that you're seeing the whole value the whole um leverage that we get with this okay so go ahead and play around this concept of course as always thank you so much and I'll see you next okay so we making real good progress here so now what we'll do is we're going to create yet another very awesome application here called the current news summarizer assist so essentially it's going
to be a summarizer a new summarizer that will allow us to learn a few new things what we're going to be using in this case is going to be the function calling tool now if we go back to what we saw before when we talked about the assistance API in the beginning we noticed that we have the assistance API which Taps into API models and then in this case it calls with specific instructions to the open I model and also can access multiple Tools in parallel which is amazing because in this tool list we can
have different things different tools right so we can have code interpreter can have knowledge retrieval which we'll see later but the cool thing also but the amazing thing is that we also have access to function calling which is great because it sort of gives us wings per se which allows us to create our own tools right meaning we can create functions that will go be able to fetch data somewhere or do something and then retrieve that information and push it back to the large language model in this case openi models and the assistants can then
do what it needs to do so this is the part that we are going to be look into um in these upcoming lessons let's go ahead and take a look at what are we're going to be building this summarizer application at least a little demo so you understand what's going on all right so this is the app it's called news summarizer so so essentially this will allow you to enter any topic I could say for instance vaccine and then I can run the assistant so what will happen here in the back end it's going to
go and tap into a function a call a function calling a function that was created which is going to be called by assistant and that function is actually going to get an API a news API pull the news that have this topic vaccine and then summarize everything for us and as you can see we have here here are some news articles on the topic of vaccines and then we have this other headline here and great thing is that we also can click here to go to see more of those news about whatever is that is
being talked about so it's very cool and so it gives you some sort of a summary and at the end of course I added his run steps which just logs out the uh run Steps From the assistant as we've seen before okay it's very cool so this is exactly what we're going to be building I can also enter another topic here let's say Bitcoin why not and it's going to go ahead and run this is actually a streamlit application which I'll show you uh how to put together it's very simple if you've never heard of
or never even done anything with stream late it's very cool okay so it's running as you can see here and it takes a bit of course because it's pulling all that information calling that function and then retrieving that information and then pull it back to the AI okay so there we go we have here here are some articles on the topic Bitcoin it tells us exactly what's going on give some descriptions so this is what we're going to be building and let's go ahead and get started so we need to go ahead and get an
API key in order for us to be able to get this application going so let's go to newsapi.org as you see here this is where you are going to go ahead to either log in if you already have a log in or go ahead and say get API from here here or from down here but if you want to learn a little bit more about what it's happening here it's a very simple articles or news API that allows you to fetch data for instance if you add a topic such as Tesla as you can see
here in this URL then you get something like this so a Json that allows us to go in probing and get information pertaining to that topic so which is exactly what we want in this application so it's very simple go ahead and say get API and the thing you need to do is to register for to in order for you to get an API so you put your first name your last name and then choose a password make sure you are if you are an individual or a business I'll just go with an individual I
think that's the easiest and of course you have the capture here agree and submit so I'm not going to do that because I already have that API so once you have the API of course as we did before go ahead and save that API because we're going to use it uh shortly in our application I have created already this news summarizer so I've got environment going I have everything I also have re the requirements.txt with python. EnV and openi of course and I'm going to say pip install - R pass the requirement of text and
go ahead make sure that we have all that installed okay again if you go to my M bin you can see we should have uhv as well as opening ey setup for us so the main folder or the main file as you see here it is essentially the same setup we had before okay so just copy and paste we have the client instantiated our open AI here and pass into model of course remember we could change this model here to perhaps GPT 4 depending on when you're watching this video I'm using the 3.5 turbo 16k
because that's all we need anyway but feel free to change that okay okay so we have all of this imported nothing new here so if you click here you can see that I have the open API key and what we'll do next is you need to add the other key which is going to be the news API key so here then you can put like that and put your API key that the API key that you created so once we have all of that let's go back to our main in this case to our main.py
or app.py and and I'm going to put here this code so it's going to be news API OS environment of course we need to make sure that we import OS for that as well and then it should be good so here we are getting the news API we just created put that into a news API key so we can use and set things up in a little bit okay so once you have all that we should be able to get going now what I'm going to do next here I'm going to create a class that
will be our manager say our assistant manager which will allow us to call it and then get the assistant or create the assistant the thread ID and to all the things that we've seen before okay so I think that's the better way to do it of course we could do this in a more modular way uh create functions and do what we did before but I think this is kind of fun and also keep in mind that you will have access to all of this code so it's all good so in the bottom here I'm
going to put this piece of code which which allows us of course to pick up the main function I'm going to create a here and then run whatever is here so for now I'm just going to say pass so that we don't have any issues so let's go ahead and create a function which is going to allow us to tap into the API and get some result that is the base of this entire application so that is the first thing we need to do so I'm going to say get news say Define or def for
to Define and then I'm going to pass here the topic which is going to be the topic we're going to pass so first first I'm going to put the URL here so the URL that we need to tap into essentially it's our API so I'm going to I have it already and I'm going to put inside of an F string and just go ahead and grab it so essentially what I'm doing here I have the htps newsapi.org and in this case here the end point that I want is everything okay there are different end points
but we can just focus on everything and you can go back to the API page which I will recommend you do to read a little bit more about all this end points and everything but this should suffice and then you have a query here and pass the topic which is going to be passed along here and then the API key which is the news API key that we put together right at the top here and of course we're saying page size about five because we going want to get more than that but that's just a
little bit too much all right and you can play around with the numbers and everything that's totally fine and up to you now the next thing we need to do here is go back to to our requirements txt and I need to pass request package here because we're going to need that to hit our API do the request to our API um and then get the information okay so let's go ahead and say pip install oops let's say pip install d r requirement text voila and we should be good go back here now I can
go ahead and say try put all that inside of a try and I'm going to put say accept at the bottom here require a request oh I actually have to import requests so import requests as such the package and now I can use the requests and then acception class exception as e this is just to say gave us uh this make some requests and I'm just going to say print or rather say error occurred during API request something like that okay and then pass the E so don't worry too much just an exception here uh
because we're going to put put some code inside of the try here so that if something happens at least we have a way of handling our errors right probably there are better ways of doing this but this is fine all right so now inside here we are going to do the fetching of the response from our API I'm going to put in a response variable and I'm going to say request. getet in this case and pass the URL that's how easy uh really it is to use request to go and fetch some data um from
the API so now that we have our response here I'm going to put some if statements here just to make sure things are okay so I'm going to say if response status because the response will have the status code in this case if status code is okay in this case it's just 200 which means think okay and then I'm going to be able to parse through our Json so I'm going to say news and then I'm going to transform that into Json which means we need to import Json as such and say Json dumps and
I'm going to pass pass our response Json such and of course I can say indentation about four as the Json comes in so to make the actual Json looks a little bit better okay we have our object here which is going to be our payload with all the news so the next thing we will do here just to make things simpler is we are going to transform this Json here we say Json dumps which if you have over what it says here it says serialize object to ad Json format string so this is is just
a format Json string now we need to actually convert that string into a python dictionary that way we that way we're going to be able to access some of the fields easily so I'm just going to say J news Json um and then use Json package again and say loads and then pass our new uh this should be news not new but either way works okay pass our news here all right there we go so now this is easy for us to work with which which is exactly what we want and then I'm going to
say data I'm going to say use uh Json here just to pass things around a little bit and then I'm going to go ahead and uh here I'm going to go ahead and access all the fields essentially Loop through right Loop through the Json and extract all the things that we want okay so if we go back to the news API of course we have to get started documentation everything but if you look here you can see the payload how it looks right so all the payload will more or less look like this okay which
is really cool and also you can scroll left and down you can scoll left to see different end points top headlines and we have here top headlines still and there we go this is the everything end point that we are using and with some modification to get the data that we're looking for and so it will look essentially the same the payload you can see here we have status it's an object Json object so in this case it's just an object we have the status okay results 254 and then we have this articles array which
then we have the source and the author so each article is an object itself with all information and has author title description and url url image and all of these fields okay so we're going to extrapolate some of these fields so that we can get the information that we need because we don't need everything from these uh from the payload so essentially we're going to Loop through our payload and get the and access the fields and get the information that we actually need so before we even go through the for Loop um let's go ahead
and get some of the most important Fields so I'm going to start with status and this status is going to be getting this status we're going to get from data and go to status field here so you can see we have the object go to status and get the status field this case you're just going to say okay okay we got that and let's get the total results in this case I'm it's going to be data and then I believe it's just total we going down here and get the total result which will say how
many articles are in this payload and then we're going to go to articles to get the actual articles so we going to go through data again we're going back to get this array which will contain all of the Articles as objects or dictionary in Python okay so we just we just say articles so this is exactly what we want make sure that these names are exactly the same because these are our fields which are our IDs uh for us to be able to get what we need I think I got everything right so I get
all the Articles so now is when I'm going to Loop through all the loop through the Articles and get the information that I want so first thing I'm going to say for article in articles for simple Loop here so what we want to do is again go and get the information that we want from each article what we want is the source and get the name and then we may need the author the title of the article of course and the description uh we may need some other things here so and we may need the
content on other things so we're just going to go ahead and extrapolate a few of these fields name in this case going to be going to articles or article in this casee for each article and I'm going to go to source and go through and get the name all right do the same to others so I'm going to get the author so article author and title there we go and what else I need this description which going to be article and description and what else let's get the URL because we may need that the URL
to the article itself and it's going to be URL okay let's go ahead and get the content okay what else so that's good then here at the bottom I'm going to put all that into a string so it's easy for us to use it so I'm going to say call this title description I'm going to put all that into an F string just to facilitate everything really and then here's what what's going to happen so I'm going to go ahead and concatenate all of these different pieces that we put together here we received into a
long nice string so first I'm going to put the title and then put a text title that says title first and then I'm going to say author so put get the author field that we just got from here and what else let's go ahead and get let's say source source name in this case and description description and let's go ahead and get url url okay so now we have this long string that has all this information that we pulled from the API looking good and the next thing we'll do here is that I'm going to
actually put that into an array because each one of these string that contains title author Source name description URL we know that we inside of for Loop we want to do that for each one of these articles that we're getting and so I'm going to go ahead and create an empty list here say final news that way I can pass all of that each time will Loop through okay so right below here I'm going to say final news I'm going to pend uh our description so title description maybe I should have named it better but
that's okay okay so each time we do that we are going to pull in all of the Tittle description which we are getting all of the title author Source name description URL for each article as we Loop through okay all of that so that way we can actually uh return that array so we see all of the information in this case all of the news that we want so at the bottom here I'm going to go ahead and just say return the final news okay let me go ahead and move this because this is an
if statement I can say return else in this case if indeed response if response once status code is not 200 meaning we have some issues we're just going to go ahead and return in this case an empty array Or List like this all right very good so what we'll do now is let's go ahead and call our get news and pass a topic here that says Bitcoin just to see what will happen let's go ahead and quickly run this so I'm going to say Python 3 I am on Mac so on Windows or Linux you
have to do something something else I believe and let's call Main let's give it a run hopefully everything works looks like it's running but it's not showing anything that is because I need to actually put that into a variable say news and then say print news like this okay let's see if this works let's run again and as you see we're getting some information and the great thing is because if it's an array that we should be getting yes a list it keep saying array it's a list in Python but same thing you can see
we have the square brackets there and of course I can go ahead and get just the first item in our list if I wish to do so and let's run again we should get just one so we can see exactly that we have a title and description author source and all that great stuff and URL and that's exactly what we want so there we go so we have all the articles that we want we just got the pieces that we want and created one long string and put that in Array each time we call get
news so this is good news the thing this this says that this tells us that we're able to fetch news from our API and get the data that we want for what we're going to be doing next so now that we know that the get news works by the way we'll see later this get news this is the function that we're going to be using you will see in a bit here um as the function calling functionality we talk about with assistance API which is pretty cool okay so now we have that working we know
we're able to pass in in this case a topic and then we receive the news right and then we formatted everything we got what we need and all is good so we're going to use that as our function calling very cool all right so next what we'll do here is as I said before we are going to actually create a class this is going to be be a manager assistant manager class where we're going to put everything in that one class okay so now we could have done this uh just putting functions you know modularly
but this is I think is the best way okay to do it this is probably the better way for us to have everything in one place in one class so we can instantiate that class as an object and then get what we want from that let's do that so I'm going to create a class here so say class and call this assistant manager s all right very good and so I'm going to add a few fields for our class here the first field that we need is going to be the thread ID and for now
I'm going to say none and then I need the assistant ID which is also going to be none like this have a Constructor or initializer depending on which programming language coming from so we're going to say Define and this is going to be the init and it pass the self and it's returning in this case um nothing um just to simplify things of course I'm going to pass a few parameters here so it's going to self I'm going to pass also the model which is going to be a string okay and just in case I'm
just going to go ahead and pass our model that we passed at the top here we created at the top here we have a default value which is going to be the model gpt3 or whatever model you added here and I'm just is going to remove that and next I am going to then set up a few things for our class here in our Constructor in our initializer okay so I'm going to say self and I'm going to call the client because I want the client to be in this case open AI that open AI
as such just to instantiate all of that or I could have just called our client here that would work as well so either way works I'm just going to pass client like this which is going to be in this case our client and then of course self. model in this case I'm just going to go ahead and say model whatever we're passing along here and then self what else that assistant is going to be none in the beginning and self. thread also is going to be none and self. run right is none as well the
other thing I'm going to add here because I want to be able to have the actual summary because that's the whole goal is that this class is going to be able to give us the summary of the news from the topic that we paste in or enter so I'm going to create yes another field here called summary okay which in the beginning is going to be equal to none like that so now we're going to do some if statements here to actually check if some of these um Fields like assistant and thread but but more
particularly if this um static Fields here thread ID and uh assistant IDs if they're existent that way we can then look at that and see whether we need to create a new assistant or just use existing ID because the idea is that as we run this the first time as you saw before we will have the assistant ID at least as well as a third ID and so that we don't run this many times and then recreate the same things in the back end in the console or in the playground we want to make sure
that those things are set up so that we don't have copies of the assistant and IDs and so forth so still inside of our init here I'm going to say if assistant manager. thread in this case let's start with assistant ID so if we have an assistant ID that means we have an assistant in that case we are going to set our assistant to the assistant that we already have so I'm going to say self that client that beta assistants and then I'm going to retrieve that assistant and to retrieve that assistant we need to
say pass of course the ID of that assistant so we're just going to say assistant ID is equal to manager assistant manager and get that assistant ID as such and also we're going to check for our thread ID if we have a thread ID so assistant manager. thread ID if that's the case we're going to do the same thing we did which is going to go which is we're going to go ahead and get the thread ID so self assistant or thread ID I should say and set that up with the self. client uh beta
threads and retrieve this case third ID it's going to be to assistant manager or I could have said self but that's okay thread ID as such okay all right very good so now we are making sure that indeed if we have an assistant ID then we don't create a new assistant ID in this case we just go ahead and retrieve the assistant that we have okay the same thing with the thread ID okay so all of that that is inside of our init function here our Constructor next we're going to define a new method here
called uh create assistant so this will allow us to create an actual assistant so I'm going to pass self name and instructions as well as tools because those are the things that any assistant as you know by now may need when we create an assistant okay so first of all I'm going to go ahead and put an if statement here so if not self that assistant right so if there's no assistant of course then what I'm going to do I'm going to create an assistant let's say assistant object essentially and which is going to be
self. client. beta that uh assistants and then I'm going to create a new assistance essentially so to do that we need to pass name which is going to be name we need to pass the instructions which is going to be instructions that we are passing along right and then we're going to pass also tools and pass the tools as such and for completion we also going to pass model and we're going to say self. model so to get the model that we already have in our Constructor in our class at the top there okay very
good so that's how we would create an assistant if we didn't have an assistance okay so now that we know that we've created the assistant object here next I'm going to say assistant manager that assistant ID I'm going to grab that ID and then I'm going to get that from our assistant object that ID like that and then I'm going to make sure that the class field has that ID okay the assistant's ID so I'm going to use self assistant is going to and then I'm going to set up the assistant field which is usually
none in the beginning to the assistant that we just created here which is going to be this assistant object right so ass so self that assistant now is equal to our assistant object very good so if you want we can just print things out here so that we can debug and see what's going on so I'm going to say assist ID just put something more there so we can see what's going on and then I'm going to put say self. assist ID oh I actually could just go assistant. ID like this right to get the
actual assistant ID when that is created that why that's why we're saying this all right very good and that's it for create assistant okay so next what we'll do is we are going to then Define another function or method in this case uh this is going to be called create thread of course I'm going to pass self so we have that reference as such so you can see really nothing is out of this world it's the process is still the same it's just that I've changed a few things so that we have an actual class
that helps do all the things in one place so we can just instantiate that class and get what we need all right so the same thing really if not uh in this case self. thread so if there's nothing there then that means we need to create a thread object okay so sing self. client and then threads and then I'm want to create a thread and as you know now to create a thread we just uh need to pass we don't actually need to pass anything things which actually I'm going to just leave as it so
we're going to create an object thread and we can attach messages and whatever we need later all right so now that we have that thread we're going to use assist manager just like where we did before and Say thread ID it's going to be equal to thread object. ID okay set that thing that up and then we're going to set the field the class field self that thread okay to be equal to the thread object again we're going to go ahead and print F string here and Can Say thread ID just to make things better
and just pass here self. thread. ID all right so just print it out so we have it and make sure that we are getting what we need so now we have created the create assistant method we also have in this case the create thread method now we need to add messages to that thread so I'll Define another function here say add message to thread okay so we're going to pass here first of all self reference and roll as well as the content all right in order to create that message because we know messages are added
into a thread so we need to make sure that we indeed have a thread ID so we going say if self. thread we have that then we're going to go ahead and self. uh client beta. threads messages and we're going to create that message all right to create a message we need a thread ID which is going to be in this case self. thread. ID and then we need the rooll which is going to be the roll okay which is going to be passing when we call this create a message or add message to thread
as well as the content okay and pass the content as such which has been passed along that's it all right so we have that function or in this case a method and next we need of course a way for us to run the assistant so I'm going to find another method here so this is going to be run assistant and we're going to pass self- reference and instructions as well okay so how do we do that again we're going to check It's always important to check because in order to run an assistant we need of
course to check if there's a thread ID as well as the assistant okay so if self. thread and self do assistant if they are existent then we are safe to run this thread so self run in this case I'm going to set that to self. client go to Beta say threads and runs create okay so we have create here so what we need to do we need to pass a few things obviously we need to pass the thread ID how do we get that we get that by saying self again right the reference of this
class and we go and get the thread id thread. id as such and then we need to pass the assistant ID do the same thing and can pass the instructions which are the instructions that we passing as parameters okay so our class is actually coming along nicely next we going to Define yet another method here called process messages because we need a way as you know to process those messages going through all that stuff and retrieve what we need all right so I'm going to pass here just the self reference okay let's go ahead and
work on this bad boy so what we need now here and process message where first of all I'm going to check if there is a thread because we can't because we know that messages are inside of our thread so we need to check if there is indeed a working thread in order for us to go in and get that information which are the messages so if thread or self I guess or self. thread if that's existent if that exists then I'm going to go say messages I'm going to retrieve those messages so I'm going to
go client better for now that may change by the time you watch this video this lessons and I'm going to say threads messages and we know that comes in a list so let's go to the list and in this case we need to pass the thread ID which is going to be self. thread. ID very good I'm going to create a list an empty list but that's where we're going to add all of the messages or summary in this case right so I'm going to say summary an empty list is it an empty list as
such and I'm going to just go ahead and get the last message of the thread because that's all that we need so I'm going to say last message and how do we get that we say messages but this is indeed the list right and then get uh go to data right that's the object and then uh get the first one zero and I need to get the role we get the role from the message in this case payload which is a a dictionary really that we're receiving uh we say messages again actually since we already
have the last message here we just just probe in into that and say content okay and of course get the first index and go to text and value as such oh value okay so now we get the actually my this is not R this is going to be the response all right but I want the roll which is going to be I'm going to do the same thing so last message and add content I believe get to zero and then I need no actually I can get that straight to roll I think yeah there we
go so we can see intellisense is helping me very good so we got the last message the big object because we looping we know that the messages come in in a list so we're probing in and getting what exactly what we need so we need the last message in the list in the message list that we got from our thread. messages and then we get the role just need the roll so we can use it in a bit here and the response that we're getting all right so that we can formulate and create a nice
response all right so so we have the response now I'm going to go ahead and say print in this case put a inside of an F string here just for us say summary and put a lot of information like that that way we know exactly what's what and I'm going to say rooll I'm actually going to go ahead and capitalize the RO right response like that so we just printing out the summary and at top I'm going to put the role so it says for instance user or assistant in this case going to be assistant
right and then we pay and then we have our response there okay so what are we're going to do before even that we have this summary here right which is an empty array so what I'm going to do is I'm going to say summary. append and pass in our response so now our summary when we return because this is going to return something I'm going to return real quick here this is going to return our summary so now that our summary will have the response which is exactly what we need this is going to be
the assistant response right we processing messages so what we'll do next well you remember at the top of our class here we have this field called summary that we instantiating initializing here to none so now we can fill that summary into with the summary that we got here so all I'm going to do here now I can say self that summary and I'm going to just then say summary like that okay but I want actually to make it a little bit better which means I'm going to which means we're going to format things a little
bit so first I'm going to put a new line every time we add before we add the actual summary the actual text in this case this summary that we get as a response and then I'm going to call the join and then pass the the summary as such okay which is what we have here a list which will contain in this case U the summary which is which will contain the assistant response okay so now our field summary in this class should have whatever we getting when we process and get all of the messages one
other thing we could have done we could have looped through these messages here because it's a list right we could have said something like this for MSG in messages right we could have say roll set that to MSG that roll okay and then content set that to MSG go to content say text and get the value as such and then we're going to print in this case I'm just going to copy this something like that say summary and said roll pass in the roll and also then pass in the content like this so this also
would have work but because this is a method and we want something else to happen um I prefer to do what I just did here but I'm just going to comment this out so you have access to that if you wish to just look at things in this perspective next we are going to yet create another function another method here real quick so we still in inside of our class it's hard to see everything because I needed to make the text a little bit larger so you're able to see I'm going to say depth to
Define this is going to be wait for completed and pass self as well get this function here or method uh will as the name imply wait for completed and do what needs to be done so essentially we're going to make sure that as we invoke the run and to run our assistant and do all the things that need to be done and as you remember uh when we run we have to go usually we have to go through certain steps right those steps may take a little bit so we're going to configure things along that
way we can wait for a few milliseconds or seconds until we get the response and then we do something for it okay so it's just more of a helper function in this case method which we we saw before but now I'm putting that inside of this class so I'm going to check of course to see if there is a thread and also if there is a run entity that was created so if self that thread and self that run okay I'm going to put in a while loop here while true then we're going to go
ahead and going say time. sleep for about 5 Seconds okay and then run status I'm going to retrieve the Run status to see I'm going to retrieve the Run status to see where we are by saying self again go to client and go to threads and runs and retrieve okay so in this case here I need to pass a few things first of all we need a thread ID which we can get by saying self that thread get the ID and then we also need the Run ID which we can get by self. run. ID
and remember at this point here all of these fields are of course part of our class the run and thread and all that so that's real good that's the beauty of putting everything into a class where we have one entity which we can use to do things so all right so now that we have indeed our run status we can check that for now I'm going just go ahead and print something here put an F string we just say run status and then I'm going to say run status we can use the model we're going
to go ahead and say model dump Json function there and then we can just add some indentation in our Json for readability just say for as such very good so we are going to go ahead and get this run status which is which is going to be a lot of information we getting the model we are invoking the model dump Json which if you have over tells exactly what it does generates Json representation of the model using penics and so forth all right very good and there we go so that's going to be printed out
check the status because remember the status has uh certain booleans that tells us where we are in the process of running these steps right so we want to check if for instance a the status is completed or not so I'm going to say if run status that status is completed as such and that's the case I'm going to say self the process message right we're going to call our function or method that we created here the process message which will know how to process the messages because that means the running is completed we should have
an answer right which is good okay so the process message is called and and then we just going to go ahead and break now one thing I want you to remember or to keep in mind let's go back to our presentation here so when we looked at this diagram here where we have talking essentially talking about the benefits of the assistance API we said that the most exciting thing really is the fact that we have tools so in this case here we have the code interpreter which we saw a little bit we have the knowledge
knowledge retrieval which allows us to upload files and then we can converse with those files or documents in addition to the base knowledge that the models has but the function calling as well okay right this is where we are we are going to implement the function calling how does that work well in this case here if we look at our code here when the status is completed then we know we can we have the messages so we can go ahead and process those messages but there's also another status that we can look for or watch
for which is they requires action because because of the function calling as we go through all the steps we saw before remember this so the steps that the Run goes through you can use code interpreter create message to and then call another tool in this case we're calling a function because it is indeed another tool when that happens what that when we get to that part the Run entity will know that okay something is required so it's going to trigger a flag that says the status now is requires action when there is this this requirement
of required action that means we can then call a function do a function call okay so that is the beauty here so we can actually look for that so I'm going to say else if if run status that status is equal voila you can see it even gives us all of these enams here right so we have the requires action right these are enams that are actually we can get right Flags essentially that we can get from the Run status so there is requires action which is what we need there is cued in progress failed
expired cancel cancel or cancelling or cancel and so forth so all of these are steps or these are states that we can that we can check each time to do certain things so if the status says it's been cancelled or cancelling in the process of counceling then we can tell the user or send out a message say hey now this is being celling so do something else if it's failed of course we can do something else if it expired and so if it's in progress we can also uh maybe shoot maybe show something for the
user interface to say hey things are uh in progress so just give it a minute okay so this is real cool so we want the requires action because this is what will allow us to then do the function calling right which in this case I'll will tell you now it's going to be the function that I told you earlier that we created earlier which is this get news but we'll get there okay so if this is the status so if this is the status requires action what we'll do then we'll do a few things first
of all I'm going to just go ahead and print here so we see what's happening I'm going to say I'm going to say function calling now CU that's what we need to happen what we do next is we are going to go and say self and then we're going to call this call required functions should be function that's fine because you can call more than one function so say functions going to pass self and required actions okay we haven't created that let's go ahead and create this actions that's that and all this will do I'm
going to say if not self. run right if run is not available or so all I'm going to do here if not self that run then I'm just going to ahead and return right because there's nothing really to do and in any other case I'm going to just say tools output and it's going to be an empty array so this tools outputs here is what we're going to use to go through and pull in all of the required Tools in this case required functions that we need for this run so let's work on this little
bit a little bit here okay the thing to remember here because this required action here here it's going to be a list that's why it says actions plural so I'm going to put a for Loop here that's a for Action in required actions and inside of the required actions actually we are going to have tools calls as such and then inside I'm going to go ahead and get the function name so I'm say funk name and then I'm going to say action and then I'm going to go ahead and say function okay and then I'm
going to go and get the actual name so that's what we're doing here so we're going to get through through this required tools but we're going to have a lot of different tools we may have more than one tool of course in this case more than one function okay and then we're going to Loop through and just get all of the names of the functions but in this case we're just going to have one so we get the name and put that in function name so if the function name which is going to be tool
is get news then we're going to get the actual name of the function but that's what we need to pass around to get uh in this case to get it so that the function can run and then get the data pull it back then the assistant knows what to do very good and then I'm going to say arguments I'm going to say Json in this case loads and then I'm going to pass the action so essentially exactly what I have here I'm just copy this change a few things and instead of function name we're going
to get the arguments the function arguments so all we be doing here is going through get the function and get the arguments of that function because we may need they may have some arguments that need to be passed around okay and that's what I'm doing so I'm going to put that into a variable arguments okay so we'll see how it all pans out now I'm going to put an if statement here so I'm going to say if Funk name is equal in this case I know it's going to be get news now how do I
know this well I know this because that's the function I need to be called as a function calling it's called get news if you said happy birthday or James Bond whatever you named here this has to be exactly the same because that's what we're going to be calling right so if function name is get news in this case then what we'll do well we're going to put inside of we're going to say output and must called get news aha now I'm actually calling the function and passing the topic how do I get the topic well
I get the topic because I have the arguments remember when we did this here we're getting the arguments of the function so we need to pass the actual arguments here like that okay now what is the arguments now the cool thing is that when you go back to this get news here the argument name is topic right so in this case here I need to say well the arguments because it's going to come in as just a dictionary it's going to be topic as such very very important so whatever you put inside of get news
function when you call the argument has to be passed along like this very good so that is part of the function calling guys all right so now that we have the output because get news is going to return something or it's going to give us something right and we have that there as an output now I'm going to go ahead and print that just so we have it F string here just say stuff okay to differentiate things and I'm going to say output like this because we know that the get news will return that very
nice string right remember let's go back here can see I'm going to open this so we return this final news which is going to be a a string but in this case it's just going to be a list containing all of those different new news right because we listed everything we put everything in one string and we Loop through so each news that we getting we going to get a title author Source description URL and so forth and put that in final news list right so in this case here what we'll do is we are
going to Loop through and so that we can create the actual final string so I'm going to Loop through and extract everything from the list because that's what's been returned from our get news and then I'm going to put everything in a very long string that way it's easier for the L for the llm for the model in this case to be able to just pick all that stuff and and summarize it and create some sort of a new uh summary of everything if that makes any sense okay so structuring things for us so I'm
going to say final string Str just make it an empty string as such and then I'm going to say four item in output then we're going to do something so I'm just going to append like I said to create that long string so final string plus equal I'm going to just say that join and pass in this case our item okay now we have this final string ready for us to use all [Music] right okay so you notice here we also have what we call this tools output should be tool outputs so our tools output
here is just an empty list so we're going to use that cuz that's where we're going to add all of the tool outputs so I'm going to say tool outputs in this case and append what well I'm going to pend an object or a dictionary that will have a few things first of all let me just put it all together here so you can see first of all I'm going to say tool and call ID like that and then for the call ID so how do I get the call ID well a call ID it's
going to be coming from our action if we say action right what is the action well action is this because we're still inside our for Loop right so it's going to go through all the tool calls so I'm going to get each one of the action which part which is essentially the action part of the function right the function call and I'm going to go ahead and get the ID so I'm going to say like this and say ID so I'm getting the tool call ID in this case we call it's going in this case
essentially it's just the function the function function calling right say output this is going to be the output that comes from our get news so what I'm going to do well I already have this final string here so that's what I'm going to put here so I'm going to say final string like that very good now this tool outputs is very important as you can see in a little bit here so now that we have our tool outputs with stuff that means each time we go through it's going to go ahead and get all of
the tools in this case it's just one function you could have other tools right uh we're doing call uh function calling here it's going to go ahead and get the action ID and get the final string which is what we get from our output when we call the actual function the actual tool this is our tool right very good so next what we do I'm just going ahead and print something here so we see what's happening going say submitting outputs back to the assistant that's what we'll do next right here so now before I do
that actually I'm going to say something like else because we're inside of if statement if the function name is getting news right so if any anything else happens I'm going just say raise value error and I can just say something like an F string here unknown function and then I'm going to just put the funk name as such okay just to save ourselves from a lot of issues so I'm going to get out of our if or for Loop that is here all of that has to be inside of for Loop and now I'm going
to say submitting output put back to the assistant so what we'll do next is ah this is the beauty I'm going to say self client beta and then threads and runs and submit tool so there is such function or such in so there's such function called submit tools output or tool outputs that's why we were doing all of this but now we have to pass along a few things in order for the run in this case here to be able to go and get the tools because at this point it's saying hey we need some
tools right tools required right in order for that to happen then we need to pass a few think it needs to know about the thread ID which we can get from our thread. ID right it needs to know about the Run ID all right so I'm going to say can know about self. run. ID it also need to know about the tools outputs and where do we get those well tool outputs which is exact what we've been working on around here now we have tool outputs we will have tool call ID it has to be
exactly like this that is what's required and the action ID we're passing to like call it same right we have here tool call ID which needs to be passed exactly like this in this dictionary and if it's going to be action ID and output is going to be the final string which is exactly what we have here which is the information coming from our get news very cool very cool all right now indeed if we go back to where we were calling this this will make more sense now I don't need to pass that at
all nor do I need to pass that let's see what do I need to pass so now this makes sense that means now when status is requires when status is requires action we are going to call required functions which is what we just did so it's going to go through and go and get all the tools in this case it's just going to go ahead and say okay so we need a function in this case but because it could be more than one function because it's a tool itself go through all the process we make
sure the function name is exactly the name that we created at the top as I showed you and then we get the output from that function but those results are needed right for the run to process everything and we do all this great work and then we make sure that then we submit those tools as you see here okay to our uh run runs that submit tool function you need to pass the thread ID as well as run ID always all right very cool all right so there we go that is exactly what's happening here
and so now we should be able to have everything that we need to get things going but before we go that we need to pass something else here says here H required actions so we need to pass the required action here what do we how do we do that well it's going to be I'm going to say required actions and it's going to be run status that required action submit uh tool outputs and I'm going to say model say model dump like that so essentially we are just getting the submitter tools and of course we
are going to go ahead and generate in this case and of course we're going to make it so that it is indeed a model Dum so create a a python dictionary which is needed so that the call required function knows how to read all that stuff which is needed that is needed for the function calling to actually work okay so what I want you to do is to if this is not making a lot of sense it's okay uh rewatch this part a few times until you understand it the main thing to keep in mind
here is that all of that is being called inside of wait for completed but that's where all the action is and we are setting up or we are listening right looking for the status if it's completed then we just are good we go ahead and process message in cases which is this case here where status says requires action that's when we call required functions right because this again I'll repeat myself I know but I want you to understand this is where all the getting the function name getting the IDS the action ID and everything and
actually calling our function as a tool this is where everything happens and it's very important to make sure that we get the arguments and pass the actual arguments which are passed along when we call that function which we created the get news and make sure the function name is getting there and everything is good and life is great all right so that's very important to keep in mind now I know it was a little bit of working that needed to be done but that's how you do function calling you with the assistant API next we
are going to create yet another function here or method I should call it because it's inside of a class this is going to be for streamlet and we're going to talk about that in a little bit this is going to be our front end right going to say here for streamlet so essentially we're going to define a method here called get summary pass self and all it will do is this is going to go ahead and return self. summary as such okay that's all this function is doing here and it's going to be more apparent
once we are putting together the front end with streamate so the next method we're going to create here is the run method so run the steps Define this run steps reference self and I'm going to say run steps self. client go to threads runs and steps as such and pass in the list and we've seen this before so we are going to of course go through the Run steps and get all that information for that we need to pass the thread ID as you know so thread self of course get the ID as well as
the Run ID so it's going to be self run. ID okay very good and so we're going to go ahead and just go ahead and print in this case and have string say run steps and pass in the Run steps so we can see what's going on this is just for us okay so I think these are all the methods that we need we have a few here so we have we have the call required functions we have the process message we have the Run assistant these are very important because allow us to actually create
assistants which is the most important thing really and add to thread but before that we have the create thread and create assistant which one was this this was run assistants of course to run an assistant and we have the create assistant this is the one I wanted to talk about and voila so we have our class here assistant manager which we can instantiate as an object and we're going to be able to run and create assistance and do all the things that we've done before but in a different more um objectoriented way of doing things
and most importantly that we know that indeed the get news which we tested earlier here this is a tool that's going to be using in the process in the function calling tool as we discussed so very exciting okay so now I think it's time for us to test this out so let me move this main to the bottom right about here and so I'm going to comment this let's comment this out as well as that as course for testing and first of all we are going to go ahead and say a manager create a variable
manager and then estan shate our assistant manager as such okay very clean and what we'll do next is we are going to be using streamlit which will allow us to create a web page where we can actually see things happening so it's very simple and of course I'm going to go up here and go to requirements and I'm going to go ahead and say stream lit such and make sure that I've run again pip install d r requests requirements a text so we can pull in this streamlet package okay so looks like we've gotten everything
let's make sure it should be fine let's see streamlet is there that's very exciting really good so let clear this and now I can go ahead and we can now use streamlet again if you don't know what streamlet is again streamlet is just a very simple framework that you can use Python firw workk you can use to quickly generate web pages with python it's really beautiful create a streamlet interface so we can actually create a user interface of course that's what it does I'm going to use streamlit first of all I need to go ahead
and import streamlit as St for streamlit okay so now I can set things up here I'm going to start by putting the title so st. title it's going to be news summarizer and then I'm going to add here a form which will allow us to add a field where we can in this case add where we can type where users can type in the topic so I'm going to say with sd. form and for any form we need to pass a key okay so I'm going call it user input form and then I'm going to
pass instructions here I'm going to s that text area so we can actually create a text area or text input I'm just use text area because it's cool and I'm going to say what I want the text to show here is going to be enter topic maybe text error is too much let's just uh say that text input okay that's probably better all right and we're going to also create a submit button such and I'm going to St do form submit button and I'm going to pass a label which is going to be just a
string I'm going to say run assistant and now we need to be able to handle the button U let me see something here I'm going to run this real quick just to see if something's going to happen now in order for us to run a streamlit application we no longer say python we say stream lit run and then the name of the file hit enter and if all is well what will happen a new window is going to open okay so you can see I have have it open now it looks really nice and new
summarizer we can enter something and we have the button here so just like that we're able to create a user interface that looks wonderful next let's go ahead and handle this submit button because we want once the submit button is clicked something needs to happen so I'm going to say if submit button in this case that's how you handle submits we're going to go ahead and create an assistant and thread if they don't exist so I'm going to call the manager because we instantiated here and I'm going to say dot create assistant look how beautiful
this is because now we can just go ahead and call those methods right the function in this case from our manager object class that we created and for that we need to pass a few things I'm going to pass the name the name is going to be news summarizer as such and then I'm going to pass instructions and I already have those instructions I'm just going to copy and add them here so that way we don't have to do all of this okay so essentially it says here your personal article summarizer assistant who knows how
to take a list of Articles title and descriptions and then write a short summary of all the news articles now the most important thing here is that we need to pass the tools list right so the list is going to go in as an object or a dictionary in this case and we need to pass a few things here so now I have all of that already and you'll have access to all of that this is um the dictionary that you need to pass in for our function calling to work so instead of tools you
say this so we have the type is going to be function the function we're going to get the name is get news remember it has to be the same name of the function we created earlier get news very important and then the description we can add a description here for that function we have the parameters and then we have the parameters and and then we have the parameters fied which is an object or dictionary it's going to be an object the property and importantly notice here the property is the topic this is the argument right
so topic is going to be type of string and we need to pass the description of that property which is topic remember we adding topic because that was the argument we're passing has to be exactly the same name this is very important otherwise it's not going to work okay and so we pass the description the topic for the news for example Bitcoin and so forth now important part here is a required because this is going to be the argument parameter as you see this is part of parameters and that is required which is going to
be the topic notice these are the same names as the arguments or parameters we passed in the function okay so when we click the button then we're going to create an assistant and then also we are going to go ahead and create right below that we're going to go ahead and create create the actual thread because that is needed right to create a thread going to use manager again and then invoke the function or run the function or call the function create thread we don't have to pass anything and next we're going to go ahead
and add the message and run the assistant as such how do you do that well we're going to use manager again this is the beauty of having that class right and call the add message to thread function and here we need to pass the role the RO is going to be user content it's going to be I'm going to pass in as an F string so I'm going to copy what I have here already so it's easier so for Content says summarize the news on this topic and passing the instructions now where is this instructions
coming from well this is the instructions we getting from the text input very good once we've added that message this is going to be a message to the thread right what we need to do then is just say manager that run assistant right that method and passing the instructions which is going to be summarize the news okay you can be more elaborate but that's okay and once we have that then we are going to wait for completion and process then the messages okay so I'm going to call manager again say wait for completed it's called
wait for completed it should be wait for completion so I already so we have that so change that so that wait for completion and then we're going to have a summary here which is going to be which then I'm going to call the manager. get summary because at this point we should have a summary and once we have this summary we can then say St for stream lit and write that summary okay into our page and then SD again I'm going to add a text here which is going to be the Run steps and then
I'm going to st. code so this is just going to show the Run steps at the bottom and then I'm going to pass it's called manager that run steps function which we created right is just go ahead and show all the steps and I want this line numbers to be true as such that's about it so we are now calling everything first of all we created the assistant make sure that we pass the tools which is going to be a function function calling tool essentially and then we create the thread that's needed and then once
the thread is created we I'm going to create in this case add call the add message to thread add the user and the summarized news on this topic which is going to be instructions right and we're going to run the assistance past instructions we going to wait for completion right a few seconds and then we get the summary manager get summary and then we write that summary into our into our page and then of course we also get the steps so we can show at the bottom that's it so now if I save this go
ahead and run again I think uh I could go back and always run okay let's go ahead and say open Ai and run all right so after a few moments you can see that we have here here are some recent news articles about openi and goes and gets the title how to stop another open ey meltdown and it's got all the main points the author Source description so what happens now in the back end the assistant was able to go and create this all of this the description as a little summary okay the same thing
with wired so everything related to the description here is indeed showing now it's saying none here maybe perhaps in the code I put something wrong but that's okay we can look at that a little bit later okay so as you can see this is indeed working and if we go back to our command line here or terminal I should say there's a lot that has shown up here and let's see so we see here in one of the Run status we have the assistant ID so I'm going to copy that assistant ID and go back
to our code and for our assistant ID here I'm going to actually paste that in and let's go ahead and find the thread ID there we go to thread ID let's just copy this and I'm going to put there so this is what we're going to be using very well so that means then I'm going to go ahead and stop this all together because now we're going to change gears to make sure that we don't recreate this same um we don't create yet another another assistant when if we were to run this in this case
um adding another something else okay so I stop that and this is stopped now if we go to open AI let's go to our assistance we should see that indeed we have this new summarizer assistance with that ID which is the same ID that we just added hardcoded to make sure that everything works as intended okay so now that we have actually added this thread ID as well as assistance ID we have this code so that it will check if there's already an assistant ID okay if there is an assistant ID we just going to
go ahead and retrieve that assistant right say assistant better assistance and retrieve same thing with the thread okay so this is why we had this code before so now if I save this let's go ahead and rerun again going to refresh this so this time here I'm going to run again let's have a different topic let's say US inflation let's go ahead and write R so we'll take a few moments and there you have it here are some recent news articles about US inflation and we have the title the author and everything and we do
have that description which is really nice and we also have to read more the homelessness in the US hit record high and all this information very good so we still getting none for steps here we can look at the code just to make sure because for steps I saw earlier that we should be getting something but I think I I let's see okay I have run steps let's go to our code okay run steps I'm just run steps self list right I'm printing the Run steps and I should return H that's why run steps like
this and of course I can move this there so this actually works I can actually save this and I'm going to prob in into data to actually get the right information all right there we go so now if I go back here and I'm going to say always rerun or just rerun one time that's okay and I'm going to find something else let's say let's say apple okay after a little bit you can see that indeed this is running here are some recent news articles about Apple and Apple pay Apple card Apple Visa TV and
all this great stuff so you can see oh there we go and of course we have all the Run steps which is exactly what we want to see and this is just for us to debug of course in a final project or product you shouldn't you probably shouldn't have this there but I just want to show you so we have this is working if we go to our assistant we see that we don't have duplicates of assistant we just have one summarizer because now we're able to get the assistant ID and of course we printed
out the thread ID so we're using those for our assistance API that we heating creating here to summarize our news very cool so I hope this is making sense I hope you are seeing the power of using function calling uh with assistance API and it's part of adding more tools to the toolkit so that an assistant is able to run things and get information extra information from somewhere else as you can see we're tapping into a totally different thing per se totally different system uh API in this case to get information and pass that back
through the large language model in this case the model that we're using large language model that we're using here gpt3 we instructed it to create a summary get that information and summarize everything and then pass it back to the thread as the response and that's what we getting here and you can see a lot of different use cases that can come up from this right you can create for instance a newsletter application that what it would do is depending on the topic that you pass in here it will just create General generate a newsletter with
the main points about that topic so the things you can do are literally endless there's so many things you can do with this and I hope you're seeing uh the power that we have here okay so all of this is just using the assistance API okay so so far we've done a lot of things and the last thing we did was to create this summarizer new summarizer with which allows us to look into uh passing tools passing function calls to our assistant that way we can extend even more the toolings that we can pass to
theist to our assistant to do something else right so in this case the assistant now has this function call we just looked at one function call but you can have many function calls and this is very important because that as I said will extend the capability of our model because not only can we rely only on what the large language model knows now we can include out tools so that the large language model the model model in this case will have more context more knowledge base so that's what's all about so it gives us that
flexibility and what I want you to do is to think about use cases that you can Implement at your work or maybe in your projects what else can you do the example that I showed you here was tapping into or calling in this case an API news API but you could tap into uh a database for instance or do all sort of things so the idea is that now you have that capability and you see how function calling is really awesome when it come to creating really powerful assistants so next what we'll do is we're
going to look at a knowledgebase retrieval tool because now we're going to look at how to even give more context more knowledge specific knowledge that we would want to our model so that our assistant is able to have the knowledge to answer certain questions and get us the results that we want okay what would going to be building is what I call a study body essentially will allow us to upload certain PDF files and then we can type and questions in the chat Boot and get answers pertaining to that to those documents or that document
that we have uploaded okay but before we do that let's talk a little bit about the background of how things work how this retrieval thing really works right in the back end so how does it work this knowledge retrieval the way it works is very simple if you look at this we have documents a document could be whatever we upload in this case let's think of it as just a PDF file okay so what happens now is that we pass that or we upload that to openai large language model which has this Lang which has
this massive knowledge base but when we pass these documents in that means those documents or files are now part of these large language model knowledge base that's the whole idea so that way when we query or we ask questions to the model which now understands more than what it knows because we've passed more knowledge per se then we can get the answers so this is what we have in a n shell but there's a lot of things that are happening internally here one of the things is if you look inside right so what happens now
is that we're taking a document and we put it through a document splitting process okay so the documents are split or cut up into smaller chunks and then those are actually saved in what we call a vector store now this is what generally happens when we talk about when we talk about pushing information in this case documents and so forth to the large language model so it has more context has more knowledge okay so the question here is okay what is this Vector store and why is that even important the thing to keep in mind
here is that in a vector store the representation of the document is not is not the same as we started with so this is not even documents chunk this is not even a document that you and I can read anymore now we have this different representation that we call embeddings so what happens that a vector store holds embeddings so embeddings is a vector just a mathematical representation of the text of those documents and that is indeed what is put inside of the vector store a vector store is just a database that holds all of this
mathematical representation of our document now the reason why embeddings are so important is because embeddings are first of all our mathematical representation which means it's really easy for algorithms to be able in this case to search for whatever it's looking for for your text for meaning and so forth so there's a lot more to that but that is is the idea because now we can look for pieces of text in this case we call text but they're not necessarily text they're mathematical representation but let's call them text so now it's easy to look for the
pieces of text and trying to find similarities between those pieces of text which are now embeddings the idea so visually again this is what happens so you have all the documents so I'm putting different kind of documents but right now we're talking about just PDF files but could be anything really in general okay and that has to be transformed into embedding as I just said which are these mathematical representation of those pieces of data the text or the document which we call Vector representation and that is what is pushed into this massive algorithm that will
then know how to do the similarity search to find the right results per se okay so the idea here is as follow the query the questions also have to be transformed into embeddings so that they are able to be understood and be searched for because if the information now is in the vector representation format makes sense that the answer or rather the question the query has to be the same format that way the algorithm is is able to do the searching in the vector store and so forth and get the right information and of course
that gives us the result so essentially this is what how it looks like again okay so we have the vector store loading so this is not necessarily just what happens uh with open AI in this case are we talking about it but I'm talking about in general this is what would happen so in this case here we have the document loading so what happens then we split everything and then all the splittings those are the vector representation of our documents here they put into a storage a vector store okay now the retrieval in this case
everything is going to be indexed right so now we have a question or a query we go through through the vector store the retriever so we retrieve the relevant splits because the information now it's into is in this different format and now the search happens to find what goes with what what makes more sense right to find the most similar pieces of data so that we understand what's going on and then once that is retrieved the most similar pieces of data that's what it's then put into a prompt all right so we have that prompt
the query and we have that what we've received from the retrieval the documents were split and everything and with the prompt and with the help of the large language model in this case just a model that's when we get the answer so this my friends this is how it works in the back end now if we go ahead and look back at the first the open AI you notice that in this case open eye simplifies everything for us when it comes to this whole process because now we have some sort of like a a black
box where all of the things that I've just told you now is happening all we have to be concerned about is to get a document push them into this black box per se and then we can just start conversing with our large L model in this case our model in this case in the form of our assistant API so this is what we're going to be doing next next we're going to learn how to do knowledge retrieval by pushing or by uploading files in this case documents to our assistant's API and then be able to
ask questions about those documents so I went ahead and created a new project called study body and I've set up the environment environment variables and everything and I also have requirement uh. txt we have all of these Let's see we don't need request anymore I don't think that's okay so let's go ahead and run pipe install real quick here R request so we can get all those dependencies all right so we got all those dependencies that's very good and what we'll do next let's get rid of that in our app here everything should be looking
good nothing new I've got a few Imports as well some of them we're not going to use them that's okay we can get rid of them when we need to and one thing I noticed I've changed here is that is that now for a model we have GPT 4 110 six preview now remember these could change depending on when you're watching this you can go to the documentations and see that okay but this should work so the first step we need to do is to is how do we upload files to the assistant API in
the back end okay so let's put number one here upload a file to open AI embeddings essentially that's what we're doing ah embeddings yes okay so to do so first of all we need to get those files and I already have that prepared for us so I'm going to go ahead and get it the one file at least which is going to be in this case this is the understanding cryptocurrency it's very short not nothing too large that's okay and we're going to use that and upload that to the embeddings the open AI embeddings okay
so I'm going to say file path I'm going to create a file path here and because it's literally here so what are we going to do we just going to go ahead and say that for Slash and of course make sure that you know depending on what operating system you're on so this is how we do in on Mac so I know it's cryptocurrency so I'm going to uh crypto currency. PDF that's the name so make sure the name is exactly the same and then I'm going to create an actual file object and I'm going
to say client Cent right beta and I'm going to files let's see no it has to go client files it's not beta which is good so and then let's say I'm going to create and I'm going to pass the file I'm just going to say open and then find uh pass the file file path as such and I'm going to say RV and we need to also pass the purpose why we are uploading this in this case I'm just going to say that purpose is going to be equal to assistant assistance like that okay there
we go so now we have let me put this perhaps in a new line no doesn't work so now we've created the actual file object which is going to be pushed into the open AI embedding in this case the open AI assistance API okay okay and then then step number two very simple is we're going to create an assistant oh we've done this many time so you should know how to do this now so I'm going to just go ahead and do it anyway so assistant I'm going to use our client going I have to
do beta for now assistance. create and we have to pass a few things first of all it's going to be name I'm going to call this study buddy and then instructions and I have the instructions here so I'm not going to bore you so I'm going to just get all of that and show you here okay so I do have the instructions here you are a helpful study assistants who knows a lot about understanding research papers Your Role is to summarize papers and do all these things so you're going to have access to all this
so there's no need for me to go through but it's important to remember that whenever you're creating these instructions these are the prompts so you need to be very thoughtful and very um specific of what you want this assistants to be able to do next the important thing thing here is tools the tools we're going to have to pass here or essentially simple we're going to pass just one tool and the type of this tool I'm going to say it's going to be retrieval because we want to be able to upload a file or files
okay I'm going to say model in this case I'm going to say model pass the model that we created at the top and then we need to pass has also file IDs notice it says ID is not ID that means we can pass more than one file and that's why we have a list here so I'm just going to go ahead and say the file object. ID like this okay this file object again remember that's what we created here and for reasons that you know by now I'm just going to go ahead and get the
assist ID so we can use it later later so I'm going to say assist ID I'm I'm going to use the assistant and get the ID I'm going to go ahead and print that one assist ID and then I'm going to just paste some of the code here this is for later once we've run once so we have the uh the thread ID and assist ID then we're going to add them here so these are old ones and they're not going to work so for now just going to go ahead and uh make them empty
as such so and I can also go ahead and go like this for now and next I'm going to go ahead and create in step three we create a thread again nothing new here same thing okay so first of all I'm going to create a message here and for now I'm just going to say what is mining something like that that's okay so let me go ahead and create a thread here so thread uh just client beta threads and we just go ahead and create a thread need no need to pass pass anything and let's
go ahead and get the thread ID right away that ID let's go ahead and print thread ID li like that say run the assistant so I'm going to say run client threads runs create eight okay and we need to pass the thread ID so it's going to be thread ID as such and assist ID going to be assist ID and instructions I can just put something here that says please address the user as Bond as Bru I'm also going to go ahead and pull in this weight for run completion we've seen this before so no
need for me to go through the idea is that it's going to go ahead and wait for a few moments make sure that everything is running and then it's going to give me the time formatted elapse time to say how long it took to run and everything okay so we've seen this before that's what we're going to be using of course to run right through our uh to run it so now we're going to go ahead and say run it how do we do that I'm going to say wait completion and then I'm going to
pass the client which is going to be our client and let's pass the thread ID it's going to be thread ID and let's pass the Run ID which is going to be run. ID as such okay because we have the run here okay and of course if you want we can check the Run steps logs and this we've seen that before so there we go so very simple um let's go ahead and run this most importantly we know hopefully that we're going to be able to push this cryptocurrency PDF um file into open ey embeddings
right so it's going to go into do all the things that I've showed you before what happens in the back end and we should have an assistant let's go ahead and run this real quick if all goes well we should be able to go so I'm going to say Python 3 that's that and run so we got some things here but let's go ahead first and get the assistant ID so we copy that and add it here let's get the thread ID and put it here okay so that next time we can run things differently
so let's go to open AI here the assistance in the back end let's see if this actually worked if I go ahead and refresh here we should see indeed that we have study buddy this is our new assistant there is the ID and everything is good if I click of course we have all that information and the model that it's using and most importantly we see that the retrieval is ticked as well as the cryptocurrency PDF file was uploaded this is real great we knew this would happen of course but it's always nice to see
and most importantly says assistant response to document understanding C currency by the University of Kentucky blah blah blah so it goes through all the things really and does a a summary of the everything okay which is good and also we have the Run steps and voila so nothing new what we seeing but most importantly we know that things are working because we got the document uploaded and this it's part of the knowledge base now okay let's go close this and now that we have the IDS right so we don't have to go back and recreate
the same assistant uh we're going to hardcode them here this is just for testing purposes and now we no longer need to create this so I'm going to go ahead and comment this out as well as comment this out and what else I'm going to comment this out also cuz we don't need that so now before we run I can then create the actual message so I'm going to say message is equal client beta threads messages and I'm going to create a message which means I have to pass the thread ID pass the thread ID
right and roll it's going to be user and content aha this is where we pass our message which is the query per se what is mining okay so now you should get an answer of what is mining because we have added or pushed into the embeddings right uh the file in this case the PDF file about cry cryptocurrency so it should know how to answer to that let me go and save this make sure everything is good okay so everything seems to be great let's go ahead and run again so that means now we should
be able to see the answer pertaining to what is mining so it's going to go and get look at the document that we uploaded and answer according to that document so assistant response mining is a process through which uh cryptocurrencies can be obtained it involves the creation of block and blah blah blah and everything it tells us that this was actually able to get the information from that PDF file which is wonderful so this is working so there we go it's very simple if you go back to the code here you can see we still
have uh one hopefully just one because we didn't repeat things right we have study body and everything is there and if I click I should have still the file and I can upload as many files as I want of course we'll get there but the basics is here so so you're able to see how to get in this case retrieval the knowledge retrieval how to get this to work by uploading a file a PDF file to enhance the knowledge base for our assistant API all right there you have it now that we know how to
upload files and Converse per se ask questions uh about that file in this case the PDF file that we uploaded in conjunction with our assistance API it is time for us to do something a little bit cooler which is to build our study body so the idea is we're going to upload the file the one that we're uploaded now and maybe another file and then start chatting and asking questions about those files or those PDF files documents let's go ahead and do that so what I'm going to do here I'm going to create yet another
file uh to differentiate a few things so I'm going to say main.py or py and the differentiation is going to be little I would say minimal so let me get all of these code from the app file and pass it over so we have everything here let's see and actually we need to get the IDS because we need them I'm going to copy that and put them here and and in this case we no longer need that so let's just get rid of that all together all right now because the nature of this application this
is going to be a stemulate application there are a few things that we need to do to set everything up so the first thing I'm going to put here is the nature of streamlit is that we need to make sure that we have sessions and any time you hit enter or click a button uh in a stream lad application what it happens that it's going to go ahead and reload the entire application the entire view or the entire window which is not what we want so uh I'm not going to go into this whole session
thing with streamlet just this is going to help us especially when we want to create uh this chatbot using streamlet and the great thing also about streamlet is that all of that is included so there is actually a chat form that we can create quickly with streamlit that's why streamlit is pretty cool okay so first I'm going to go ahead and initialize all the sessions that we need okay okay so I'm going to start by putting an if statement here if file ID list so this is going to hold all of the file IDs because
potentially we can upload more than one file and not in sd. session state if that's the case then we're going to create that session session state. file IDs list and that's going to be just an empty list okay we just have to do this and I'm going to do another one as well so if start chat which is going to be part of a button really not in sd. session State s the session that start chat I'm going to just say it's going to be false what else now I'm going to use the thread if
thread I ID not in session State and then we're going to say St session state DOT thread okay very good those are the sessions we need to set up okay we're going to go straight and start setting up the front end in this case the page so I'm going to go ahead and just say set up our front end page and to do so with it is extremely simple I'm going to say St and then set there's a function called set page config and here we can pass a few things so I'm going to say
page title say study buddy say chat and learn that sounds very good and Page icon we can actually pass an icon here like this books okay so you just put this is the icon representation of a stack of books this is where we're going to start our function definitions Etc so essentially this is where the fun will start so the first thing we need to do is to figure out a way to upload a file to open AI which we know how to do that so I'm going to go and Define the upload AI so
we need a file path okay and and I'm going to go ahead and do that we've seen that before with open and I'm going to find file pass the file path and then say RB and I'm going to say as file and I'm going to say response in this case I'm going to go get the client files create and then pass the file which is going to be just the file uh that we have here we got here right and then I'm going to get the path or read actually because it's going to come in
in this case I want it to come in as bytes okay stream really all right and then of course I'm going to pass the purpose going to be assistance and what I want to happen for this function is I want to return the actual ID so I'm going to say response. ID so this will return the ID of the file that was uploaded very simple and next what we'll do here we're going to create a sidebar okay the Side Bar say where users can upload files so here I'm going to say file upload I'm going
to put that in a fun in a so I'm going to put that in a variable because we want access to that so file upload and to generate a sidebar in streamate again very very simple so we just say St that's sidebar tou and I'm going to say dot file uploader because we want actually a sidebar fly a sidebar with a file uploader and we can pass some text to say upload a file to be transformed into embeddings in a real application obviously you probably don't want to talk about embeddings because most users won't even
care but it's okay we're here and then I need to pass here a key just to say something file upload and that's that now we can also add a button that says when click then they can actually upload the file okay so this is going to be just um the interface where they can upload the file right drop the file and then we need to allow them to click to say actually want to upload the file that we just dropped here upload file button and we can also store the file ID so I'm going to
say if SD that's sidebar as such I'm going to create a button and this is going to be upload file and you going to this will make sense once you start seeing things but I hope you're seeing the pieces coming together I'm going to say if in this case File uploaded this should be file uploaded okay if there's something there if I let's see uploaded there we go we got to go ahead and open file uploaded in this case and get the name and this should probably be F string and we need to pass that
we want to say here we want to write this as a binary that's why we're going to say uh WB read write as binary RBS read as binary as we saw earlier here okay and all of that as F and I'm going to say F that write going to write that file uploaded okay and I'm going to say get buffer and in this case here there are times when also we want to allow users to upload more than one files so I'm going to just say something like maybe another uh file ID and I'm going
to say uploaded oh let's say upload I'm going to call the upload to open Ai and then put an F string here and this I'm going to pass the file uploaded and get the name as such and let's make sure that we the we take care of the session so the session State file ID list let's see file _ id. list doesn't give me intellisense that's okay and I'm going to append let's see file ID list sure I have that file ID list file ID so this be file ID okay there we go and going
to pend our another file ID my such and SD I'm going to create that sidebar and write something to to it which is going to be uh file ID and then I'm going to pass the another file ID as such so essentially we're just writing on the wall on the sidebar the ID of the other file ID that was added so each time we added a file and then we can see all those IDs okay so what I want to do next is I want to uh display play those file IDs so I'm going to
make sure again look at session State file ID list if that's all good then I'm going to go to sidebar and write in this case uploaded file IDs so this is where we're actually showing the thing things really and then at the bottom we say for file ID and S do that session I'll make sure I get the right list okay because it's going to be a list with all those file IDs that have potentially been uploaded and I'm going to write into the sidebar again and I'm going to pass the file ID now once
we have these file IDs need to associate them to the current assistant that's very important because each assistant can have multiple files okay so how do we associate them so I'm going to write here associate each file ID with the current assistant okay so I'm going to say assistant file really and say client beta and assistants. files and I'm going going to go ahead and create to create a file as you know by now we need to pass the assistant ID which we can get by say assist ID right oops assist ID and then we
need the file ID which is going to be file ID that we have there okay so that's how we associate that each file that we add is going to be Associated to the correct assistant okay next we are going to go ahead and SA button to initiate the chat session okay we're getting there okay so I'm going to put in if state so you'll notice that in streamlit to start in this case to initiate something Handler right a buttonclick handle Handler of of some sort you say SD put an if sd. sidebar. button and I
can say start chatting and this is where we handle the click okay so first we need to make sure that uh all the files have been uploaded that's very important so that we can check that by saying STD go to session State and then file id id list okay make sure that we have that session which means we have something and then we're going to make sure that our session state for start chat in this case is going to be true because that means we have everything set up for things to be get going because
we've just clicked so that means we have the file ID session which means we have IDs that means then the session for the start chat has to be true at this point okay so now we're going to create a new thread we could create a new thread can just go ahead and use that same thread that's okay we're just going to and create a new thread uh for this chat session okay okay so again nothing new you know how to create a thread so I'm going to call this chat thread and so I'm going to
say client or yes client go beta uh threads and create a thread okay now we have the thread set the session again of that thread this is very important a lot of sessions here for the reason I said so that's just how stream lit works and uh session State and I'm going to use the thread ID and I'm going to pass the our chat thread. ID so now we have set the session for this whole thing the thread ID session to the thread ID of this newly created thread okay I'm going to write something to
the wall all right so so I'm going to say thread ID and I'm going to pass the chat. ID chat thread. ID notice that we inside of this if statement here that handles the on click and then else if none of that is true I'm going to go ahead and just say something sd. side r. warning right we can just invoke a warning so the text is going to be redish and I can just say something uh no files found please upload a at least one file to get started all right very good so we're
making real good progress here so one thing I want you to keep in mind is that as we pass this documents to the embeddings the open eye embeddings so we are able to attach all that information to enhance as they call it the model so we can converse or chat with it um whatever comes when we ask a question whatever comes the resulted response it also has other metadata such as citations okay to say okay this information that I got it from this place in a document now we have a very simple document but you
can imagine if you have a very large document then this could be very helpful and so what I'm going to do here just in the interest of time I'm going to paste in a helper function that will essentially process message with citations okay so it's very simple as you see here extract content and annotation from the message and format citations as footnotes so it goes through gets the content that we get for as a response right and then that and go goes through and looks for certain annotation looks for certain attributes in the message the
whole payload uh where we see annotations and if it sees annotations of course it creates those annotations goes through creates their annotations and so forth so now there are some limitations because we said that we can pass different files right which we will see but in this case here um for retrieving decided files we're going to do things uh we're going to use some dumi way or dumi data to get that going and I'm hardcoding here just is for testing purposes I hope by the time at some point once this assistance API is mature we
have ways to do certain things such as such as calling the cited files details instead of us having to do all this okay so but this is just to give you some ideas now again this is not perfect code um it just works for what are we trying to do here to give you some ideas okay so in this case here for the file name cited file we are putting in this case the cryptocurrency PDF right hardcoded it again not a good idea but that's what we're doing anyway okay so that's what we're doing and
then we append we have the citations list here and as all of that is happening we retrieving all those annotations and we're painting all of them so that everything is set up okay so that's what we do and after that we create a footnote in form of a a full response and we get that information we put it all together and then we'll return that so that is essentially what we're doing in this process message with citations let's go ahead and continue putting together the front end the API okay or I should say the the
page itself so I'm going to say the so here is where I'm going to put the main interface so we can actually see things okay so I'm going to use streamlit again and I'm going to add a title for our page call it study body now notice the difference here I'm adding the title before we're adding the title to the page configuration which is those are two different things and if you want to learn more about streamlit which I encourage you to do we can just Google real quick or stream lit and then go to
documentations it is the most simplest straightforward way to create prototype applications okay that's very good in Python and I'm going to also St stream L here I'm going to write something here kind of like a subheader and I can say learn fast by chatting with your documents okay good for marketing I guess okay next we are going to again check a few things check sessions to make sure everything is good so we can proceed so first we're going to check that the chat is not already in session so to do so we're going to say
if St go to sessions that chat oh start chat and if there is that session so if there is a chat session that means things have started so now I'm going to go initialize them and this now we're going to go ahead and and now we're going to go ahead and initialize our model so I'm going to say if open AI model not in session okay I'm going to say st. session state. open AI model I'm going to just say this is going to be the chat GPT or GPT 4 like that so we're using
the preview okay and that's the only thing that actually works with what we're trying to do if you try to do jpt 3 Turbo 3.5 turbo it's not going to work and also I'm going to check messages not in St session state then I'm going to say s State messages set that to empty so this is going to help us of course with the bot with the chat bot all right so let me go ahead and show existing messages if any so four messages in st. session that messages and I'm going to now invoke the
chat notice that this chat this is what I talk about notice as I said we don't have to do anything really when it comes to creating a chat a chat box per se um stream lit has that so I can say do chat right it's part it's part of the framework so I can go ahead and say chat message all right look at that and I'm going to pass the message or message in this case and get the role this has to change to message okay singular and then we're going to go ahead and add
that and then we're going to write the content inside of a markdown okay so I'm going to say message and get content of that message okay so now we putting together the actual chat and this will make a lot of sense once we start running this and apologize there's a lot of code but believe me this is going to work well we hope okay so now we're going to chat the user so now we going to go ahead all right now we're going to go ahead and say chat input for the user so this is
where they're going to add something to as a prompt so say if prompt in this case s see that chat prompt look at that we have chat input as they call it that's the beauty of streamlit I'm going to say what's new that's going to be the first thing that will see so what's new all right which will allow them to then add user this so next I'm going to go ahead add user message to the state and display on the screen so I'm going to put session messages and I'm going to pend in this
case I'm going to pass in as a dictionary so first of all do roll and user okay it's going to be user for role and then for the content that's where we going to pass the actual prompt we're going to say chat messages or message and this is we're going to show that this is part where the user so we have the user so this is the user chat message and we're going to write that using a mark down and we're going to pass that prompt so now we're going to go ahead and add the
users message to the existing thread how do we do that well we're going to use client go better and go to threads and messages and create that message and how do we get or what is this me what we need to create a message we need a thread ID so we're going to go through our session again because that's what's driving everything and go get and thread get a thread ID and we need the role in this case going to be user and the content is going to be our message oh actually it's going to
be our prompt right to create that message very good okay we're getting there next we're going to create create and run create a run with additional instructions okay so I'm going to say run again nothing new here beta threads runs and create need to pass the thread ID so I'm going to go go through the session State again get a thread ID and then assistant ID we pass the assist ID and then I'm going to pass instructions I have the instructions or instructions and then let's pass the instructions which I have here so please answer
the questions using the knowledge provided in the files when adding additional information make sure to distinguish it with bold or underlying text most us most likely they won't do that but it's okay just to make sure we can distinguish things so next what I want to do is I want to be able to show a spinner as all this process is happening before we get the actual response so that's good user interface addition so show a spinner while the assistant is thinking okay okay so I'm going to say with spinner so I stay spinner and
it's going to say wait generating response such and then inside here I'm going to put a while loop so I'm going to look at the run right so status we could have done we've done this differently before but that's okay but we can just go straight here status if it's not completed then we're going to sleep for 1 second and then run okay so can say run client beta threads runs and retrieve so we're going to retrieve in this case our run so I'm going to pass the thread ID which we know we need to
get from the session session State and get the thread ID and then pass the Run ID which we can get that from just run. ID okay and outside the while we are going to then retrieve messages added by the assistant okay I'm going to say message or messages in this case I'm going say client get to go to threads where that's where all messages are and messages and go to list and of course we need to pass the thread ID go again to session as such so now we have all the messages and we can
process all of that I'm going to process and display assist messages okay so I'm going to put everything into inside of a list to make things a little bit easier so I'm going say assistant messages for run okay and I'm going to pass the message okay that's very important and I'm going to pass a for and then and then we're going to actually pass a for Loop inside here which is pretty cool for messages or actually for say message in okay say message so we pass message first and then on another index here we're going
to go do a for Loop so it's for message so so what we do here we have a message and then for message in messages so going to make sure that if message that run ID is equal to run. ID and message. roll is also equal to assistant so essentially just making sure to put all of the messages for our from our assistant into this array so that way we can then Loop through them so in this case here I can say and once we have that this list this array list I can say for
message I know a lot of message here and then go to our assistant messages for run because it is indeed a list so we can Loop through it um in this case here I'm going to just say full response and I'm going to say process message with citation this is where we're using that function that helper function we created earlier or just add it there and I'm going to pass message it's going to be equal to messages actually it's going to be equal to message because we inside of a loop so whatever are we getting
there and when that happens we are going to also set the session State messages and append all those messages in so in this case going to create a dictionary here so so for the role it's going to be assistant and again for Content we going to pass the full response so create a dictionary to add into our messages State okay and then here I'm going to go ahead and get the chat message because we need now to use that which is going to be the assistant and then we're going to add that all those responses
onto the screen so I'm going to use mark down again and pass the full response and I'm going to also pass the unsafe allow to True okay and say all out to true for HTML so if you have over um let's go mark down here this will you can scroll down and see that the unsafe allow bullan by default any HTML tags found in body will be escaped and therefore treated as pure attack this Behavior may be turned off by setting this argument to true all right okay so it's hard to show you everything but
we are inside of if in this case to make sure that we chat input for the user so I'm going to come down here go to that level I'm going to say else if none of this is actually happening that means we can prompt use users to start chat so we just say St and say write we can just write something and say please upload at least a file to get started by clicking on the button say on the start chat button and such all right there's a lot of things here but that's okay should
be all good okay again remember you will have access to this code so it's not a big deal so essentially we are creating this chat and there's a lot of sessions going on here but that's important like I said because with streamlit you have to make sure that the sessions are all set up because streamlet whenever you click a button it refreshes the entire page and that means you start from scratch so you lose all the state so we want to make sure we have the state that's why we have all this state thing going
on here want to learn more about that you can check out their documentations okay so we should have everything set up for us um let's see if this works let me go ahead and um make sure that we can say stream stream lit run and make sure we're running the main not the app that Pi or py in enter and you can see we have this very nice usern interface that was created which we can go ahead and use so for now let's see if this works I'm going to go to browse and then I'm
going to go ahead and get the crypto currency again and there we go we have this cryptocurrency it's there now we're going to go ahead and say upload it's uploading and voila so that file was indeed uploaded file ID uploaded file ID is there very good so is it true if we go back to if we go to open Ai and let's go refresh and we can actually look at the files we should hopefully see cryptocurrency one of these files I added a little bit ago is going to show us exactly uh the the purpose
status is ready and how the size of it and of course also the file ID okay we have two there but that's okay now let's go back to the application and let's start chatting so click Start chat and what's up I'm going to say tell me about data mining let's go ahead and see what's going to happen so voila you can see the weight generating response is working and then we have this all this was given to us uh by streamlet that's really if we had to build all of that ourselves that would be pretty
um would take us a long time okay so now it's going to look at the cryptocurrency and look at it and make sure that it gets the information that it needs it appears that there may be some confusion around the term data mining it is it pertains to cryptocurrency which the uploaded files seem to disguise aha this is good because it's saying the uploaded files seems to disguise so he looking at those two files that one I uploaded before and that's okay usually you should probably just get one there uh but also it's probably from
the previous upload that we did when we first ran this it's okay so in this case they're mining in the context of cryptocurrencies usually refers to the process of mining cryptocurrencies themselves and blah blah blah and there we go so uh we can say can you tell me how many crypto currencies are the are discussed are being discussed in the document hit enter and voila there we go okay there we go apologize in convenience but seems there is an issue accepting the content document title Cryptor PDF the page has had no contents uh when I
tried to open it which means means I cannot determine how many C okay all right that's okay uh what I can do again remember this is still in beta sometimes happens so what I can do let me see if I can delete one of these perhaps maybe that's going to uh it shouldn't that shouldn't be a thing anyway but sometimes this happens and sometimes it does weird stuff so it's okay we're just going to try again maybe this time it will work okay so now it worked say based on the search results document mentions several
cryptocurrencies including Bitcoin Litecoin I think that's what it called ether and potential others are not named explicitly and blah blah blah that's really good I can also ask now the other thing also I can go ahead and upload a different file or another file okay so I'm going to say upload oh shoot so this is a problem now that I uploaded I uploaded two files so if I refresh this this is exactly what I didn't want to do I should have this file just delete a few here and it's very strange because it shows all
of these different files have been uploaded let me just delete some of them some of them says upload others says uploaded I'm not sure why but okay let's go ahead and delete some of them and go back go back here and I'm going to browse to go get yet another one so I have another one called lipids this is is a biology file and that's the one I'm going to go ahead and upload try again this sometimes doesn't work let me upload that one okay that file was uploaded and let's see if I can upload
another one issue because we have that let's upload this one okay so now we have two files that were uploaded let's go back to opening I and I'm going to refresh here okay so we have a few files that were uploaded but one thing I can do I can go to assistant so I think this will be better and go here and I can see the files that were uploaded wow there's a lot of files that were uploaded so it looks like when you first upload something they change their names from upload first and then
they the IDS are created I think that's what's happening I'm not sure it's very strange so it's a little bit buggy but it's okay so uh let's see if this works so now we're going to ahead and start chatting okay let's talk about because we have two files let me go ahead and talk about biology I'm going say what is a lipid okay so now it should be able to go and fetch the file in this case the lipid the biology file that has information about lipids and so forth hopefully all right this worked so
lipids are biological molecules that have limited solubility in water and are soluble in nonpolar organic solvents okay so this is really good because it went and got the information from the document because we have even the source and everything here that is showing very good so I can also go back and ask about this case um cryptocurrency what is crypto okay so what comes here with four resources crypto such as Bitcoin litcoin and all this stuff and explains everything and very awesome okay so this is actually working which is very exciting now remember these are
just examples that I assembled really to Showcase how to use assistance API and so that you can take this to the next level okay so this is no production code obviously for many reasons but at least gives you some ideas the other thing also to remember is that the assistance API is still in beta which means they're still working on it and maybe some apis will change by the time you're watching this but I hope you understand at least what's happening the basics of how assistance API work I give you a little bit the rundown
of the vector store embedding so how things work in the background and how assistance API in so many ways allow us developers to really not think too much about what's in the background which is really exciting but also I need to let you know that with that we get a lot of some disadvantages meaning that we don't have a lot of control but with all of that flexibility all of that abstraction we also lose a few things one of the things you will notice even though assist is API it's most likely going to get keep
getting better um it's important to also know that there are other tools that you can use that perhaps give you a little bit more flexibility ability but also that means that you have to do a little bit more work per se not too much uh there's this tool called Lang chain which I'm just going to let you know about it's a wonderful framework that allows you to really assemble many other many other models in this case many other large language models because there's not just um opening eye chat gbts and gpts there are many others
as well for this framework you are able to actually see most of the things we talked about most of the things that open a I have abstracted things out for us which is a good thing but you're able to see or be able to do more in the backand in terms of choosing your own Vector stores indexing all your documents so the process that I showed you in the back end that's happening um you will actually be able to see more it becomes more apparent if I may say that all right so that is a
different topic but just something to keep in mind and I hope this course has given you something at least to think about some good starting points when it comes to the assist and API and you can keep learning of course watching for the documentation because things will change that's just a known um fact and so but I hope this course was able to give you the tools that you need to at least start thinking about how to use the assistance API to build all sort of things and now we didn't touch on the on using
the code interpreter as a tool directly really but that's something you can do as an exercise so you can create a bot or you can create a tool that will analyze code in fact I think we talked a little bit as an exercise earlier but now you have the tools you know stream lit or at least you have some ideas you can use that to create a code body something like this right or a uh pair programming buddy or something like that that will help you debug things and go from there right so a lot
of things that you can do here a lot lot of things but I've just scratched the surface and I hope you enjoy this course I hope you like this style that I use to to teach this course and I hope this was useful thank you so much for your time and I'll see you next