LangChain + OpenAI Function Calling - Create powerful chains with tools and functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video I want to show you the new function calling capabilities of opening eyes models and also want to show you how you can integrate this new feature with a link chain functioncalling provides a new way to connect gpt's capabilities with external tools and apis I will show you how this works in detail you can get the notebooks with the code in the description and this notebook is part of my complete link chain course which are also provided here on YouTube so I'm here at vs code inside the functions IPython notebook and I will just walk you through the code to make this work on your own computer you need an API key for open Ai and you need to save it in a way like this where you have got this dot end file and the key is open AI API key and then you have to provide here your own API key so first to make this work we have to install some packages we have to install link chain python.nf to load the environment file and also open my eye to make a connection to the openai API so after installing the packages you have to make sure you use the current version of length chain you have to use at least the version 0 0 200 because otherwise functions are not supported yet as you can see I've got 201 which works fine you should have a similar version like this okay then we load the API key and set the API key property of the open AI module to the environment variables value so now we can make a connection to OPM Ai and first to make functions work we have to define a function we mock a little API call here get pizza info and you can pass in a parameter which is a string it's the name for pizza for example Hawaii salami or whatever and then you get back a fixed price which is always 10.99 in this case and then you just return the value as Json so as string not this dictionary so besides defining the function you have to provide a description of the function and in this example we could have multiple functions this is just a list and here every dictionary provides the information about this function as you can see this name parameter matches the function name provided here then you have to provide a description which is important for the llm for the language model to identify if the function is suited to answer the user's request inside the parameters dictionary you have to provide multiple information for example like in properties dictionary we have to provide information about the pizza name which is the argument name here and we have to provide the type and also the description like I said this is important for the llm to get the information about what you can do with this function and then we also have to set this pizza name argument to require because we don't have any default values here so this is the information for the llm2 know what to do with the function okay after providing this kind of information we have to use it and I defined a little helper function here called chat which takes one argument called query this will be a string and this will be the request of the user first we have to define the API call which we make by the chat completion class from opmai and we call the create function here and we have to provide the model it's very important that you use the newest models here with all end with 0613 for example GPT 3.5 turbo 0613 and there's also an equivalent of gpt4 with this kind of ending older version do not support functions okay then you have to provide messages and this will be a dictionary here with the role and the role is currently user and we provide the content which will be passed inside this chat function here and then as additional parameter here you have to provide these functions argument so and by calling this we will get a response and the responses are more or less complex object and we can get the actual message from the AI by calling the response and then there's a dictionary called choices and this is a list which we try to retrieve the first element and this will be another dictionary and we will retrieve the message so there's a lot of stuff like the core tokens and so on which we don't need to demonstrate this example okay then we Define a function so okay now we can ask the model what is the capital of France and if you run that we get an answer and as you can see this will get us back an open AI object with content and the answer is here the capital of Francis Paris and the role is assistant a system is always the role of the AI and user is always the role of the user who makes the request what's pretty nice about this is that the API already provides a Json object which makes it easier to convert this kind of information to a dictionary before it was just a string and you had to retrieve the information for example via an output parser so if you used to open my eye before I think in general the output here should still look pretty familiar to you so now what happens when we ask how much does pizza's alarming cost this is clearly something related to Pitzer and we provided the LML the information that it should use the get bitter info function if the description here has to do something with pizza and if you run this now it should look different so and now the output looks quite different as you can see we don't have anything in the content this is just null but we have got this additional function call object which is a dictionary and has got arguments and the pizza name and also the value of the pizza name so this is the parameter for all function and this is the value we want to pass to our function and the name of the function we want to pass it to is the get pizza info function so this information we want to use now and we can first check if actually we've got this function called object here and if yes we know that the llm ones that we call the function and we extract from this object here the function name um like this we have got this function called object and then we want to retrieve the name so this will be the function name we want to call and we also want the pizza name salami which will be the argument we want to pass to our function and this is a little bit more complicated we get the function call and the arguments and then we've got this object here and we convert this from a Json object with Json loads to a dictionary and then we extract the key with pizza name and then we get the value of salami and this is the name we want to pass to our function and then we could make normally the API call and retrieve the information and this is the function response this is also a Json object which we retrieve from this function here and with this result we want to make another call to the API again we use the newest model here and we also pass in the messages before we also pass in here the message which we get back from the response and in addition to this we pass in another object with a role called function and we pass in the function name here which we extracted here so this function response is the pizza info with the name and the price we pass it here and then we make the second response and if we print this we get a complex object here and we can find the content here the cost of bitter salami is 10.99 so the function response will be used to create a human-like answer from with the information from our API so this is what function calling is all about to let the llm decide if you want to use additional information or external information or just let the llm itself answer the question so this was the vanilla way to do it and now we will take a look at how it works with slang chain currently it feels a little bit hacky to be honest but um for example agents have been implemented yesterday and this works pretty nice but normal chains don't work as I would normally expect but I will show you how to make this workaround work so first we import the chat open AI class and human message in AI message and also chat message class these classes help us to create this kind of functionality with the role user and so on and we only have to provide the content here as you can see we've got this human message just provided content and here we create a model the llm with instantiating chat open my eye and we also pass in the news model here then we run the predict messages function and we can provide an additional keyword argument here which is called functions and we will pass in our functions which were defined above and now we can see a iMessage and the con content the capital of France is Paris so far so standard and now we run again the query how much does pizza salami cost in the restaurant okay and now we would expect that we don't get content back which we don't but we get back additional quarks here and the additional quarks include this function call dictionary with the name and the name is the pizza info so we can see the llm suggests that we should call our pizza info function first and the arguments here are Pizza name and salami so this looks pretty similar to what we've done before without length chain so now if we take a look at the additional quarks from the First Response here they are empty this is just an empty dictionary and if we get a function call suggested we can get it here with the additional quarks here we've got our function call the name and also the arguments so we can now use this kind of functionality to Define if we want to make an additional call or not okay to get the pizza name we do the same like before we just extract the additional arguments here and convert this to a dictionary with Json loads and get in get the picture Name by calling the get method from from the dictionary then we get back the pizza name which is salami and we call it here again like this and then again we call the function like this again and we get back name salami and the price is 10.99 so now we can use this API response and create our new API call with the predict messages function and here we have just the human message which we will provide our query and the AI message we will just provide a string of the additional keyword arguments here and then instead of providing this function here we will just use the chat messages class provide the role function here and then as additional quarks we will provide the name which will be the function name and also as content we will provide the response from our so-called or mocked API so if you run this again we should get the similar response like before a iMessage content depicts across 10.99 in the restaurant so pretty easy to use but working with these kind of additional quarks still feels like some kind of hectomy Lang chain already provides another standardized way to work with the outside world to make requests or whatever and these are called tools tools are classes provided by link chain you can also create your own tool and I will show you how to do this first we have to import the base to class and you can create a custom class or Custom Tool by inheriting from base tool and then you have to provide a name for your tool and also a description this is a very simple and non-functional Tool just to provide you a little bit of the syntax here you have to Define an underscore run function and an underscore a run function this provides async support and this is for synchronous support so I actually returned just here it is funny because AIS so no real functionality you could of course query databases here or whatever you want but I will just show you what you can do with it and if you've got your own tool with the class here and you can easily convert your own class with the format tool to open AI function function to an open my eye function I also imported here the move file tool which allows you to move files on your computer and I create a tool list here where I instantiate my own class and also the move file tool class so I've got two tools here and I will just iterate over the tools and call the format tool to open AI function on all of my classes and now I don't have classes here but I have functions I will show you how they look like just print them here and if we take a look at them they've got a name like stupid joke tool they've got a description this is taken directly here from these class parameters and here the properties are the underscore underscore arcs and we can provide a title here which will be also underscore underscore Arc we can also see what arguments are required and what type they are of so this looks exactly the same like in a function definition I provided Above So now again we can use it like this for example why does chicken cross the road to get to the other side some kind of joke often the AI does and now I can run the predict messages function and we will provide the two functions here and let the l and m decide if they want to use a tool or not so as we can see we don't get any content back so the I wants us to make a function call and the function call is our stupid joke tool class or the function which we converted the class to so now we don't actually have a function we just convert the tool itself to a function definition but not to a function itself so we have to run still the class and here in our tool list we have got the first tool the first tool is our stupid joke tool and we have to call the run method on the question here and we get the question here with Json loads we extract from the additional key arguments the function call and then the arguments and since this is called Arc one underscore underscore Arc one we have to extract extract it like this and then we get to the other side so we pass this to our function and then we get back our tool response and our tool response is it is funny because AI so this is just a static response from our Custom Tool and now we can use these two response again to make an additional request again the same pattern like before we pass in a chat message here with a role called function and the content is all auto response so this might not even work as expected because yeah it doesn't work as expected because um yeah our tool doesn't provide any functionality but I think you'll see the pattern make an initial call and if the llm wants you to call a function you call a function and provide the output of the function to another llm call so this is how it currently works with the normal llm chain to be honest with agents it's already implemented much better I will show you how this works and first we import some kind of chains for example the llm math chain and also a normal chat and chain chat model here chat open AI create our llm and we also create an llm math chain which will be used as tool for our agent so this agent is able to answer normal questions and also do some calculations so for example we use it like this we use the initialize agent function and now we use it here with our tools the llm and now we have got this new agent type which is of kind openai functions so if you run this we don't have to pass any kind of keyword arguments or additional quarks here we just use it like this so now we run what is the capital of France and we get back the capital of France Paris and if we want to know what is 100 divided by 25 here the calculator gets invoked and we get our final answer 100 divided by 25 is equal to 4. so with agents it works pretty Flawless and I think it is only a matter of time until it works with normal llm chains too okay that's it if you liked the video feel free to subscribe to my channel and give the video a thumb up thank you very much bye bye
Info
Channel: Coding Crashcourses
Views: 7,332
Rating: undefined out of 5
Keywords: langchain, chatgpt, gpt-4, openai, function calling, open ai functions
Id: vJot6WCUlGE
Channel Id: undefined
Length: 16min 28sec (988 seconds)
Published: Fri Jun 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.