Function Calling in Azure OpenAI Service

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to the fifth video in the series of azure openai service in this video we're going to look at how function calling works so let's say we have a setup like this we have an AI integrated application that calls Azure openai or open AI apis now the application that you're building it has functions like search for hotels and get weather and things like that it has already built-in functions now when you hear the term function calling what what comes to your mind it involves Azure openai doing something like calling a function so how does it work does it calling an API for us or does it invoke the functions that we have built in application none of this is true actually so let's understand what actually is happening basically when the user interacts with application Visa is using natural language application by itself it can't understand natural language and because of that we are passing that user input to Azure openai service or open AI service and what happens is that we get the response back now with this response open AI service will tell us which function to invoke now let's understand how this works so basically this is how we invoke open AI service we're using the engine or the deployment we're invoking and we are passing the history of messages now in addition to these two now we have to pass in a list of functions and how these functions should be invoked the Behavior now this is the addition that we have to include in the invocation and in the output of this large language model it will have the name of the function and the arguments that we should invoke as I said Azure open AI service it does not invoke any function it just tell us which function to invoke now let's say for example the user is asking find beachfront hotels in San Diego for less than 300 a month with free breakfast now this is what the user wants so what we do is we pass that input to open AI service and it will tell us okay this is the function that you should invoke in your application Asian right because we are passing all the functions in the application so Azure open as service it knows about those functions and the arguments also so it's going to convert this natural language input to a function call like this it just sends us a Json output something like this one and one thing that you should keep in mind is that these functions that we are invoking here these are injected into the system message under the hood so the more functions you have here you will have to pay for more tokens so this is the response and obviously we should have a function in application called search hotels right so that application can invoke this based on the response that you receive and that search hotels function should match the the arguments that we have informed as you can see it should match all these three arguments so that we can invoke it so basically if you're showing the user a natural language output based on on the response of this function call so basically this function call might be invoking another API for example So based on the output of this function call we need to invoke open AI service again as you can see to get the natural language output and show that to the user and there might be cases where we don't have a natural language output like for example when we invoke this function here it will do some some kind of a thing in the application like it will act as a method that will do part of work of a copilot so we'll get into that later so this is the high level flow open AI function calling functionality now this is the official documentation for this feature as you can see it is still in preview so as you can see the latest version of GPT 3.5 turbo and gpt4 have been fine-tuned for this functionality and this contains some of the things that I have shown you and it also includes a python example as well as you can see and it shows us how to define function so as I have shown you earlier we need to Define functions here there is a specific format for doing that let me show you that as well so as you can see this is how you define those functions we have an array of functions and that is why we are passing here and that array contains the name of the function and a natural language description of it and the parameters and the type of the parameters and the the properties that it has for example the location Max price and the features and all those parameters they should have a natural language description like this so based on these natural language inputs Azure open AI will understand which function to invoke so it's basically like we are passing in a open API definition to not open AI by the way open API definition to open AI server so it understands all about our apis and it decides which API to invoke based on the natural language user input and now if I go into Azure portal as you can see we have a open AI service deployed and if I go into the AI studio and deployments I have two deployments as you can see GPT 3.5 turbo 16k version and the the normal version it doesn't matter really for this demo and now let's see how we can run this example code here let me just create a new python file it's really straightforward copy uh the content that we have here and I'm just going to paste it all right we need to pass in the Azure open AI key and an Azure open AI endpoint and those things you can find in here keys and endpoint section of the Azure open AI service let me replace the open AI key and open AI endpoint now the API version matters here we need to use this API version I think that is a relatively new version and it is still in preview for this to work we need to use this API version we have a list of messages like if we didn't have functions let me just delete the functions and let me just try running this let's see whether this works yes as you can see okay so yeah it doesn't really say that it can't all right so let's just go back and include the functions here as you can see we're just defining the functions I mentioned how to define this earlier we are passing that this function here like this so let's just run it let's see how it works now so this is what we're getting since we are asking it to find beachfront hotels it's just invoking the function it just gives us okay you should invoke this function in your application to get this results now let's look at this function call um parameter that you're passing in and if I go into the documentation and if I search for function call as you can see here when the functions are provided by default uh the function call will be set to Auto so basically if we don't have it here it'll still work and let's see how it behaves and if we ask let's see if we pass in none let's see what would happen yeah as you can see it just it is not include now if you compare this to Auto it does not include the function call object so basically it is giving us this uh a little bit random outputs but it's interesting to see that it just gives you giving us this Json here even though we don't ask for a function call the probably as I told you earlier these function definitions these are included in the system message so that's why I believe we're getting this and now we understand how this function calling works and I'm not going to go into um based on this function call like add an if condition and if it is search hotels I'm not going to add a function called search hotels and invoke that I think it's pretty clear to you how this works now but in my next video I'm going to show you how to do this with C sharp language and I'm gonna also go through this document and cover the rest of rest of it in my next video alright this is the end of this video if you have any questions comments or any video suggestions please let me know in the comments down below and don't forget to like this video And subscribe if you learned something new today I will see you in my next video and thanks for watching
Info
Channel: Meet Kamal Today - Cloud Mastery
Views: 2,369
Rating: undefined out of 5
Keywords: azure openai, function calling, openai function calling, function calling openai, azure openai funcion calling, functions, customize openai
Id: rzJERWSa_G8
Channel Id: undefined
Length: 8min 57sec (537 seconds)
Published: Thu Aug 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.