How to use ChatGPT on your data - End to End Project | OpenAI Function Calling For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let me tell you the story of Joy Pandey and his brother Peter Pandey Joy Pandey is a student at atlick Commerce college and Peter Pandey is an AI enabled data professional Peter also saved his brother by using the open AI function calling yes he did Joy was depressed as his project got rejected by his Dean Joy successfully integrated chat GPT with the college website for his project but the dean wanted something more he wanted his chatbot to answer specific questions related to college internal knowledge Dean said throw this in the dust bin and you cannot graduate this year but Peter had other plans he wanted to help Joy he decided to build an internal chatgpt in fact he succeeded in building a prototype the first small step towards a giant leap [Music] in this video I'm going to take you along Peter's journey of building qns system for atlick college that can not only answer general questions but questions specific to college database such as student fees marks information Etc this is going to be a perfect project if you want to acquire skills around NLP llm especially using openai and build a career around it faster your seat belts and get ready for the ride let's look at how we are going to build this project at click college has a mySQL database that stores information on students such as marks fees Etc essentially we have to use this database and combine it with chat gpd's knowledge and build atlick qns system which can not only answer general questions which GPT can answer but a student specific questions as well we are going to build this application in streamlit see it is answering all the questions related to atlick college as well now if anything about the project architecture here whenever you are thinking about any llm such as gpd3 GPT 4 which are the llm models are behind applications such as chair GPT these models have two component reasoning and the knowledge you all know as of right now chat GPT has knowledge till September 2021 so when you are asking this question and if it is part of their knowledge and reasoning capability it doesn't have to go anywhere else it can just directly answer that question but when you ask question specific to your organization knowledge it doesn't know it doesn't have that information in the knowledge but it can use its reasoning capabilities okay and it can probably make a call to a database so somehow that knowledge that what is Peter pandey's uh fee payment in the first semester that answer is here in this database we have to somehow make a call to this database maybe using a python function that can accept student name semester Etc and that should return an answer back Now using the reasoning capabilities of llm it can actually translate this question into an appropriate function call and once you have the answer so just three thousand dollar once again you can use llm to translate that number into a human readable answer which is Peter has paid blah blah blah in first semester this functionality is called open AI function calling which is something open AI has released recently it's a very hot technology and we are going to write code using this hot technology today let's first understand open API function calling by going over a simple example that they have given in the documentation for this purpose I have created an empty directory called ethlick College query which I am going to open in my pychamp here I have selected the folder I'll hit OK and it's gonna create an empty project I will create a file to store my secret key I will call it secret e dot Pi where I will create a variable and call it SK something okay and that will be my secret key and then from here uh from Secret tree I will import that particular key which I will use to initialize my open AI model okay we have looked into this in the previous video as well uh for the open AI crash course so this is pretty straightforward and the basic functionality is given by this call called chat completion which which is kind of obvious to everyone where you can ask any question and it will return the answer so I'll right click run and I have by the way updated my open API key so it should work and then we will move to a more complex question so see it is giving you the answer which chat GPT would have given to you otherwise now let's try our next question which is what is the weather like in Boston and obviously it's gonna fail it will say okay I don't have the latest information blah blah blah fine now let's hook up a function which can give you Boston's weather and as per the documentation we'll just create a dummy function usually you will have a function which is connected to our database or some kind of API call and it can give you the latest weather but here just for demo purpose we will use this nummy function and what that dummy function is doing is it is returning you a hard-coded response it takes location and unit as an input unit by default is Fahrenheit you can give degrees as well and it will just return you this uh hard-coded response so here if you check the documentation there is another argument to this function called functions okay and I'm just gonna copy paste here just to save my time on typing and here this functions is a variable which is a list and in the list you provide information on what function open API can call if it doesn't find information in its own knowledge and those functions are so you can say the name of the function is get current weather okay and you can use some other parameters too so once again I'll copy so names could get current weather and I have few other things which I can just copy here okay and here you can see two parameters location and unit which is what you have here location and unit location type string unit type is string but enum it can take only these two values now when you give this mapping to chat completion API it will first detect the question then it will say okay I can't find this in my own knowledge then it will look at in this functions parameter and it will try to map the correct function now see I am showing only one function it this can have 100 functions but since it is a language model it will have that reasoning capability that it can map this question what is the weather like in Boston to this particular function and it will use function name as well as description so from the description it can figure out that this is the right function not only that it can also extract the right entities or write parameters for the function we need location so it will say oh Boston is a city name so that's why I need to put that into location and we don't have unit here if I say what's weather like in Celsius it will map that to a unit so that is how it will work let's try this out and see how it goes so here I am going to just once again copy paste this thing here only this part and let's run this code and see how it goes you saw it actually extracted the right function and right location once again I have one function you can have 100 functions here it is able to figure out that what is the weather like in Boston question corresponds to this function and these many arguments so now we can pull that function name so what we'll do is we will say okay this is the response message now from that response message this is the message actually this is response and this is the message so this is the response and the response message looks something like this response and in bracket this thing right here and from response message you want to pull function call and there will be a name so that will be your function name okay and then function R function this but the arguments parameter now you see this is a string it's a dictionary but dictionary is inside the string so if you want to convert string to dictionary what do you do well you use choose Json dot load string function that way you can have the function arguments okay and once you have that you can directly call you have function name you have function argument so now you are ready to call this function but what we'll do is we will create this map called available function to call and the reason that we we create this map is because you don't need to necessarily have you know same function name here you can have let's say get current weather function here and you can have some other name XYZ so you can do that kind of mapping here okay so let's say if your function name here for example was XYZ then you will put XYZ here so you're getting an idea and from that available function say function name if I do this I will get x y z right because it's a dictionary I'm just calling pulling the element from the dictionary and then you can call a function so this is where you are calling a function and here this function takes two parameters location and the unit so what is the location well location is in function argument remember function argument is a dictionary and here in the dictionary if you want to pull an element from the dictionary you can say get function so this is how you call that function and what you get in return is a function a response so let's print the function response first and see if it is working okay perfect see it call the function this one and it is returning you this hard coded response but this response doesn't look very much human readable if you want to print a human readable message such as a weather in Boston is 72 blah blah blah you can once again use llm and you can call llm to kind of get a nice response so for doing that what you will do is we have this messages parameter in our call we will make a variable out of it and then for messages will pass that and in this messages we will append few things now the way llm's work is if you give some past context it performs better it's almost like a chat history you're giving it back so that is the reason we are taking this messages and we are appending few things so what we are going to append well we are going to open the I think the response message that's what the documentation says okay so we are going to open response message and these two things so let's just copy paste this thing here so we appended a response message and then we also append uh this another element where you are saying role is function see previously the role was only user or assistant now they have introduced a new role for function and there is a function name and there is a function argument and whatever you get here I think we need to call llm again so that will be your second response so we are calling llm here and that response you can print here second response and right now the model you need to use is this uh however I mean they might have upgraded GPD 3.5 I think they did because this thing was already working uh but anyways since this is in the documentation I'll use the same model and when you run this you will get nice human readable message the current weather in Boston MA sunny and windy all right I hope you have a good basic understanding of function if you still have doubt don't worry let's look at our athletic caller database and after that it should be super clear let us now do database setup for our application I have launched my SQL workbench and here I am going to import the database that I have already given to you in the course if you check video description below you should be able to download this code which will have DB directory and this is the SQL file that you can open here and let's create a new schema first let's call it at Lake College DB apply schema is created let's make that uh default schema and let's execute the script this script is creating tables it is inserting all records it is creating stored procedures and so on if you refresh this you will see these tables in store procedure so the first table is fees where your student name the semester that he's studying in let's say six thousand dollar was the fees for Bill Gates for second semester out of which he has paid 6000 so he doesn't have anything pending but Elon Musk because he has money problems he has paid only two thousand dollar he has five thousand dollar pending simple table in reality in real life if you look at College databases they will not have student name as a primary key they will probably have student ID here and student information will be in a separate table but we want to keep things simple that's why we have this kind of simple schema and the second table is marks where there is student name second semester Bill Gates got three GPA out of four so that's a simple schema we have you also have some store procedure so if you execute this stored procedure and if you just say Elon Musk in a third semester how much is the pending fees if you execute that see 5000 if you say total it will say seven thousand if you don't give student name it will give you the total fees for all the students in third semester okay so you can look at the store procedure you will get an idea the other one is get marks so let's say you want to get the marks of a specific student and you can say student name is let's say how much he got in fourth semester operation is a special parameter so we don't need to worry about it Jack ma 4 it should return 3.30 and 3.308 has returned now in the operation the value can be let's say Max okay or average let me let me try Max so what happens is when you don't give student name what it will do is in the fourth semester tell me what was the maximum GPA so maximum GP in fourth semester what was 3.5 and if you look at third semester it was 2.9 so let's confirm that here we can say where semester is equal to 3 and the third semester the maximum was 2.9 which is what we see here now let's think about what kind of questions a user may ask to our q a system we have two tables fees and marks so person can ask free related question or Mark related question field letter question may look like this how much Peter Panda has paid so far the answer of that will be see here Peter Pandey has paid three thousand dollars so far in first semester or how much is spending for Peter Panda so Peter Pan day has to still pay two thousand dollar or what is the total fees for semester one which is what is the total maybe we can say what is the total let's say pending fees for semester one so we'll go to semester one get all the pending fees and sum it up we will write a python function called get fees Maybe which will take an input and it should return a number which is an answer to this question if you look at these three questions the answer to these questions will be single number and in the input what do you need so maybe we can make input a dictionary where you can give a student name which can be let's say Peter Pandey okay and semester is needed of course so we don't support just the student name it is mandatory that you have to supply semester name as well that's just a requirement I mean we can enhance this program and we can remove that restrictions but just to keep things simple we are asking them to enter semester and the third thing is you need to specify are you asking for pending fees or total fees or paid fees there are three fees types that we have we will get that into this parameter called Freeze type it can be pending it can be paid or it can be total okay so that's how the input should look like and return value will be whatever some number let's look at Mark related question you may ask what was Elon musk's GPA in semester two Elon Musk seems to be a bright student let's see what was his GPA oh 2.7 not that great anyways but here you can ask for GPA for a specific student or you can ask for the GPU of the entire semester let's say in semester 3 what was my average GPA or what was my minimum GPA and maximum GPA in that case we can have a function like this get marks where you have student name semester and you can have an operation operation can be Max Min or average whenever student name is blank we will assume you are asking for let's say if it is something like this we'll assume that you are asking for a maximum gpn semester one if it is average you are asking for average GPA in let's say semester three but if there is a name of the student then we will ignore this parameter will say okay you are asking for GPA for this student in this semester similarly here if you keep this thing blank we will say you are asking for total pending fees let's say this this is mainly total pending fees in semester one but if you have a student name here you are asking for that student's pending fees in semester too this is how our database not database but the function python function design is going to look like and then I can go ahead and write python functions which will see this function will call database and get the desired result when you call the database luckily we have all the stored procedures you can directly called a stored procedure and get a number so let's say student name Elon Musk for example or let's say this is a marks table so if you want to get Elon Musk marks in the Elon Musk GPA in semester 3 or let's say you want to get okay let's get this first so Elon Musk GPA in semester 3 is 2.7 you can see two point 2.7 or if you want to get an average in three in the third semester you will make this blank and you will say get me an average GPA of third semester which is also 2.7 for fourth semester it is 3.3 and so on now what do you think do you think I am going to write all this code myself well I have a good friend whose name is chat GPT I asked that friend hey can you write this python code I have these two store procedure get marks and get fees which takes this XYZ input and it should call this database when I wrote all those description it gave me this nice functions and of course it doesn't get things riding for short so you have to give different prompt initially to use Pi minus SQL then I said can you use MySQL connector because I personally like that model better and it wrote me a nice code which I can use with very little modification and if you look at this you realize that the database that you are using in this video that is also generated by child GPT even the stored procedure that we wrote they are generated by cat GPT where say I write a stored procedure called get fees where input name is this when student name is blank return this you just give your requirement it will write the code for you so now let me create a new file called DB helper so DB helper is a python file which will have all those functions get fees and get semester which will interact with the database and return ask the desired results so that's the code that we have and if I have to kind of quickly go over it initially we connect with the database using this MySQL connector object my host name is localhost username password is root database name is this database connection closing code and then get marks and get fees these are the two functions and if you look at the parameters it's a dictionary which has student name semester and operation you can see operation here and all it is doing is calling a store procedure when you say call proc you're calling a store procedure and then you can iterate through the results and get the first result because we'll get just one result back same thing for get fees and then here we are calling those two functions so let me just run these and here I'm missing print here so 2.7 and 3000 so 2.7 is an average GPA in semester three okay how about semester 4 3.3 we already saw that 3.3 is an average GPA so let's say in my fourth semester what is an average GPA this is a SQL query it says 3.3 it's either 3.3 and how much fees is spending for Peter Pandey we already saw it is three thousand dollars so if you look at Peter Panda here see three thousand dollar is pending so this code is working fine our DB helper is ready we are ready to call this function from our open API chat completion call now we are going to make use of open AI function calling to call these functions get marks and get fees for this I will right click create a new file call it open AI helper and here I will put my usual code the the one that we used in that Boston weather example we are importing bunch of things initializing open API key Etc and then I'll go to the documentation and copy paste this code but before that let me create a function called get answer so get answer is the function single function that we'll call which will have question as an input and for this question it will return an answer so here this is the main code to call the open Ai and what is needed as an input is see messages and functions so what is messages is an array it is an array of the question that you are asking which will have a role which is a user role and the message or the content it is called content it will be your question and then for functions this is also an array and it will have all our function mapping so we have two functions the first function is get marks so get marks that is false function second function is get fees and we will also have a description description and that description we need to give a proper description here which can help llm map to a correct function I took some time thought about it and I wrote this description you can pause this video and read it get the GPA for college student or an aggregate GPA blah blah blah similarly we will have a description for the fees as well and that's going to look something like this see llm has a language understanding so when you read this description and when you have an input question it knows how to match that input question to these one of these functions that's why writing a proper description and proper giving proper function name is going to help llm decide the proper function proper parameters too so I will just copy paste parameters here just to save time on recording and these parameters are kind of same what we saw in Boston weather example where you have input parameter is an object it's a dictionary and it has student name semester and operation three types and operation can be Max Min or average and semester can be number between 1 to 4 same thing here I did some bunch of trial errors some some work and then I figured out this is the proper you know properties this is how the property should be defined okay now that our messages and function is defined what we can do is we can maybe we can call this function let's call this function just quickly try it out how much was Peter Pan is total fees in in the first semester that is a question okay so once you get the response in the response there will be let's see how does the response look like first of all so response looks something like this so it will have choices and okay now not this one so it will have choices and within the choices it will have message and within the message it will have content okay so here what I will do is I will just get the message portion in our separate variable called response message and I'll maybe return response message and let's print this to see what happens so just pause the video think about it you are giving a question and open AI is intelligently going to match that question to one of these two functions so let's right click run it it matched this question to function get fees not get marks and it also figured out that first semester see first means one see it is intelligent it is mapping first to number one why it is doing that because I said that this number should be a number between one to four so it is using all this hints that I am giving it to him and also it is mapping it to total so let's say I have this 3 NM total pending paid and my see okay what happens is Peter pandey's new fees so see if I say new fees I don't have a parameter called due but I have parameter called pending let's see if it can match it to pending Newman's pending see that is the whole purpose of language models when you say do it knows based on this that it has to match you with one of these three values and as a language model it understands language it says okay news meaning is what is it paid pending or total it is now it is pending so it will match it to pending and it will generate this kind of dictionary so if I have to explain this using a presentation it will look something like this you have given this configuration to your llm input question comes it will use now its reasoning abilities its language understanding to say which function is appropriate for this question it will say okay this is a fees related question so it has to be get fees and the way it does this is it will compare this question with the description that you have given here and the name of the function and based on that it can figure out okay it's get fees then second thing it needs to do is it needs to extract the entities from this statement see this statement it needs to extract the entities which can serve as an input to this function which is student name semester fees type now it will say Peter Pan day semester one paid see this question can have how much Peter pandas is new then if you if it is new then it will match to paid if you have first fir St then it will match to 1 why it will match to 1 because you are saying in semester the values can be 1 to 4 say here a number between one to four therefore and then once you know the function name and then parameters then it's easy in DB helper we have defined this get fees functions it will call the database MySQL return you this three thousand dollar you can return this as a response back or if you want a human readable response once again call alarm and say hey llm what is the right response for this now llm has a context of all your previous conversation so it will figure it out and it will generate a nice response Peter Panda has three thousand dollars so far same thing if you have GPA related question it will figure out it is get marks function extract entities figure out function argument python code generation answer again llm generates a beautiful human readable answer all right now in response message which looks something like this I will check if there is an element called function call and the way you do that in Python dictionary is by saying dot get and if it exists then we need to map this to our appropriate function so I have created this dictionary where these function calls you know these function calls like get fees Etc are actually mapped towards real functions these two can be different okay name String can be different and then you are getting the function name call arguments this code is same as what we did in the Boston example so you get this and then you get a function response back so let's print a function response or let's return a function response and let's see things are working so far good or not so the question here is we are trying to get the fees of Peter Panda's new fees pending fees and when you run this two thousand dollar so if you just verify in your table see two thousand dollar is pending if you set paid fees it will be written 3000 Etc uh what is the average GPA of semester 3. average GP of semester three so let's say semester three if you run it it is this if you say average the average GPA select average GPA 2.7 C 2.7 so this thing is working fine let me try one more thing average marks now see marks and GP are two different things but as a language model it should understand see so look at it how smart it is it knows marks in GPR same thing therefore it goes and internally see in this function what happens is it will call this function get marks and it will give semester 2 as an input and average as an operation and it will you know call this function get marks where is it get marks this one it will call this function with student name blank semester 2 operation being average so far things are working okay we obviously want to call llm second time so that you can print a human readable answer this answer that we are getting is like 3 2 not very interesting I want to see whole sentence Peter panda is a paid fees is this much and for that also we have seen how to do this before as well so in the messages array that you have what is my messages array well messages array is the original question in that you need to open the response message why because that will give that will help it build the context and it can work better and in the second one you need to apron something called this particular dictionary where your role is function name is the function name and content is whatever is your function response then you will call not yet GPT but open AI again with those messages that you're passing as an input and then the response that you get will be again choices zero message content that's pretty much a standard path that you have in that particular Json response so let's run this again average marks of semester 3. see the average marks of semester 3 is 2.7 so this generation of this nice beautiful sentence is done by the second call and if you say average GPA in third semester C notice this is the same question but the language I'm using is different and it will give me the appropriate answer see previously the average marks of semester 3c3 was number and here was Marx now I'm saying GPA and instead of three number I'm saying third see the average GPA in third semester is 2.7 okay now someone can come and they can ask some stupid question as well or not a stupid question let's say they ask the usual common knowledge question which is what is the purpose of a balance sheet is that going to work maybe not because I'm not handling that scenario because see here and see if it give me none but I wanted to return the proper response so I handle only this if condition where there is a function call but in this question there won't be any function calls so it will go into the else so I need to return from the response message I need to return content okay so what is response message response message is this let's see where is response message yes choice is zero message and after message you need content because if you read the documentation open AI documentation it says choices then zeroth element then message and then content so that's what we are giving here let's rerun it again and now see it is generating an answer of a common general question which activity can also answer and it is generating an answer of a question which is specific to our college or which is specific to our database now comes the easy and fun part which is building the front end or UI in streamlit I am going to import streamlit as St and I'm using the same main.pi file that was created I removed all the previous code here I will create a title of my application which will look something like at Lake college qns system Maybe and your application is ready you can go to terminal and you can run streamlit run main dot pi and it will launch that application in a browser see it's so easy to create applications in streamlit now I am going to import the get answer function from the open AI helper module and I'm also going to put some text input so we'll put some text input which will take an input question and if you type in a question it will come into this variable and if you say if question then you call your function let's run this let's rerun click on rerun and you see this question input and when you type something and hit enter that is the time it comes into this if block where you will call this get answer function and your question is is question so what you get as a result is your answer and then that answer you can display below the question so below the question here you will need a label called answer and then the actual answer so let's create that label first which is simple answer text and then you will write your answer here and that's it this is ready so now you can rerun it and see we are giving some garbage so it's it's not gonna return you anything say it's running I'm sorry but I don't know what you're saying here so you need to give proper question here so I will go and copy paste some of this question what is the how much was Peter Pond is dues fees whatever and while it is a recurring the answer let me copy paste another question which is average GPA in third semester whatever this much then you have another question you can ask n number of questions on your internal at click College database you can also ask general question like what is balance sheet or give me a uh or let's say a write a poem on those Dosa only four lines I love South Indian food especially in southern lands on a sunny day whatever so it can answer all the questions which GPT can answer plus the internal athletic database related questions that's it folks the code is given in a video description if you like this video uh please give it a thumbs up or subscribe to our Channel this might be a smaller thing for you just clicking on a button is like one second work but we are putting a lot of hard work in building these videos so really it will help us in terms of YouTube algorithm promoting our content so I really appreciate if if you are finding any value of our tutorial please make sure you are clicking on Thumbs Up Button sharing the content with your friends who are trying to learn this thing and also subscribe to our Channel if you have any questions there is a comment box below [Music] thank you
Info
Channel: codebasics
Views: 26,923
Rating: undefined out of 5
Keywords: yt:cc=on, openai function calling, openai function calling example, openai function api, openai project ideas, openai api project, openai example projects, openai example python, openai sample project, openai sample, openai easy explanation
Id: tku5zP1VzXA
Channel Id: undefined
Length: 42min 46sec (2566 seconds)
Published: Wed Jul 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.