Power Automate - How to access Open AI API chat completion via Power Automate?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video we will have a look at how to access open AI API via power automate so here is a site for the open AI first thing you need to do is create a create an account with open AI then select the API uh menu then here under your profile you can see View APS here okay using the create new secret key you should be able to create a new key here at the first first time when you sign in you will get a $5 credit then afterwards you know you need to buy the credits so here I have already got a key which I created it let's go back to Power autate now so for for the demonstration purpose I'm going to use the manual trigger okay the first thing I need to do here is to define the API key okay this is my open API key okay the next thing I need to do here is I'm going to delete this AP after the demo okay so uh the next thing I need to do here is I need to go and find the documentation of the open AI okay so under here we I'm am looking the completions so let's go back and find the completions documentation here okay so for creating a chat completion you need to access uh the API using this link as a post HTTP api. open.com slver one chat completions okay so let me copy this I'm going to use the http action step here right now under the header to access the open AI what we want to do here is we need to uh pass the authentication here so for that under the header we need authoriz authorization then the bearer token we need to pass b e a r e r be space then the APA key which is the output of of that compost so either you can hard code it here or you can pass via a compost action step or a string variable okay make sure there is a space between that the next thing we want to do here is the content type okay the content type is application SL Json right now we need to construct the construct the body then so the scenario I'm going to talk about here is I have got an XML file I'll show you the XML file how it's going to look like so the XML file is something like this adding a comos action before the HTT HTTP step just called the name as XML data here okay so that's XML um data I got so in in the real scenario this data might be coming from another API and point or a database or from your Excel sheet or from a flat file you know so depends so here I have hardcoded that XML file what I want to do is I need to I need to pass this to chat the open a and then have a conversation with the openi saying that I need to get this data in adjacent format okay so for that first thing we need to do here is we need to construct the model here okay so for communication to the open AI we need to say what model we we are you know using so here I'm going to use GPT D 3.5 Das Turbo - 0613 you can look the documentation it will say what are the models out there okay the next thing I need to do here is the messages okay so the messages what I need to do here is that's an array messages is an array so there are two parties under the messages here okay one is I'll put this um U the body uh the formats under my video description so the first one is the role so role of the system so here I'm saying to this is defining the the chat GPT system actually here okay and the content also so content okay saying that you are my my assistant or you can name it name the you know uh the system so that's what this is so you need to define the role as a system there the next thing is the RO for the user actually okay so that's the user uh then content okay so in the content as a user we are going to specify that hey I got an XML file I want to convert that into a an XM uh into a Json format right okay so for that we I'm going to put two double Cotes inside that I'm going to put the chat text basically so I'm going to say hi could you please format this data into Json format okay that's the first thing we to do but how does it know what data it is so we need to pass the data here okay so for that I'm going to say here here is the data then I'm going to use a start tag here right square bracket start text then pass the XML data that's uh you know this data we need to pass it here so I'm going to use that output here it is then end oops put it in the wrong place okay end uh space text full stop okay so when you you know try to um give this um plain text try to give it more elaborate so it that that means you'll get more accurate result actually so let's see this and let's see this result what we are going to get okay let me save this oh it says enter valid Json so something not quite right here I think yeah I know what it is double quotes here save it again no ah okay I'm for I got addition one additional double quotes here okay let me save it all right so let's test this oh that's failed so uh I think I messed up the model have a look GPT Dash 3.5 Das oh it's turbo spelling is wrong there all right okay so let's see what we get right okay see the results then click on the show row output okay so here you can see here it says certainly here is the format data and then it said uh it says okay let me copy this in a notepad that will be easier to identify it okay here it is um yeah so this is the result see that under the choice under the body then we get we get choices and that's an array then index is zero then the role is an assistant then the content it says certainly here is a data formatted and then we got persons we got the data there but also it said please note that I assume that the data representation a list of persons so have encapsulated it in Array within the just object so that means it did some assumptions there okay so the best thing we need to do here is I'm going to fine tune that text again so this testt text uh you know the ideally what I want to do here is I'm going to use a sample output and show to chat GPT saying that hey this is a sample output I would like to have it okay let's construct that sample output here what we want so another compost here rename this to sample Json output and I I would I would like to have that person name I be h65 5 I don't age 48 it should something it should come up something like this actually yeah this name can be anything here you know I can put here something like this you know it doesn't matter so that's kind of a format I want the person that's an array then name and age uh you know name and Age Again within that person tack yeah that's Json format I would I would like to have it okay so for that again we need to fine tune this so what we need to do here is I'm going to say here I'm going to say something like when when you return the message content I only need the Json format result and then at the end also I'm going to say like I like I mentioned about only return the Json result with no additional text remember we got that additional text earlier here see that these are the additional text which I don't want my plan is I could then directly get the content as a Json format then I can use in the next step of the flow then yeah to trade those values straight away otherwise I need to do some work here to remove this uh these extra lines which I have no control over yeah okay uh with no additional text then at the end I'm going to say here is is the sample Json output I want then I'm going to put a square bracket format then get the sample Json output format there and then again close the format remove this double codes here here it is let me save this so see so I'm giving maximum information here to the open AI saying that hey I got an XML file and this is my start tag and and and text between that that's my XML file then I set my clear instruction saying that I don't need any additional text uh when I get the result and this is kind of a format I'm expecting it all right right let's run this and uh see the result okay so click on the show row output here and again I [Music] go again I got here sure here is a data formatted into Json okay so that's I don't want this I don't want sure here is a data format into Json here again okay let me go back and see what we could do there all right let me run it again and see yeah it's gone now see it's pretty weird actually but I was suspecting that should work at the first time so I don't know what happened there so it's worked see that I got the right result I want so let me copy this schema here I copied everything under the body then going to the new step and um say pass Json map the body of the previous output of the h TTP generate from the sample paste the record here that's where I can generate my schema then then under the comos action step so let me take you back to this here so under that see the body here then I got Choice that's an array and that's my first index is zero then I want the message and the content so I'm going to use the Expression here for that so the expression I'm going to use here is this explain that body of the past Json choices is my array the first index that is zero then the message and the content there we go so rename this to you know content this is a final content we got yeah let me run this all right okay let's see the content then here it is see because I given the sample format exactly the sample format where it did given me the data which is fantastic now edit again oh let me go back again here and take that schema so that I can iterate those values and show you okay I'm going to copy that so use another pass Json in the last step and iterating the name and the age again okay that's output coming from the previous content now this is the record we copied so okay that's generated the schema now then finally I can use a compost to get those values which is name I don't want to add another combos I'm going to put some dot dot dot and then AG here it is so let me run it okay let's see the apply to H what we got see that that name and the age the second one I got that also I got the data in the format which I specified it so that's my sample data which I converted that into the Json format yeah of course you know power automate can do this uh you know in different techniques you can do an XML to converting that into a Json format and that's not the objective here the objective here is how to access the open AI using power automate so a quick refresh all we did here is put the post then the chat completion object with the authorization as a bearer and the token then the model you know to use that so another thing what you could do here is I'll quickly show you there is another reference here like image Generations here so if you look the images you can see oh you can create an image using this so let's see this thing so I'm going to copy this going back here now and um use well I don't need to run this now so let me uh yeah let me do it here now after the API key so I'm going to add it here HTTP this is another this is another method I'm showing okay and uh for that it's a post then it say that's a request URL and let's look the documentation here then it says authorization and the be token we need to copy it so let's go back to here we already have those um let's click on the T sign here we can copy everything easily that yeah let me go back here and click on the T again and paste it here under the header click back here here we go that's it and then going back here and see what else we need oh we need a prompt that's in the body it goes see that okay let's copy this and there are other parameters also here like um you need you can pass the N equals 2 means you get two pictures and also the size you can Define it with the different resolutions the maximum resolution the default one is 1024 and the maximum image it can generate at a time is between 1 to 10 things like that okay um so let me go back to the body and copy that there it is okay let's try this now okay let's try a different picture then cute baby tiger let's see and I'm I don't want to use um two two then I'm going to use one there only need one okay let's see that let me cancel that I don't need to run the below step so what I'm going to use here is I'm going to use a terminate action step make it succeed okay so that this won't run then okay so let's see what we get right so according to the documentation we get a URL with the image URL that's what we should get right let's see then okay here it is so there's a URL here I'm going to copy that go to new tab press enter here it is so that's the picture that's the image I asked and you know that's the way you can generate it then of course you know you can use another HTTP to download that image and store it in your database and all that I'm not going to demonstrate that so here under the API reference you know there are other things also so have a look on fine tuning which is quite useful to know and then the files which you can upload I I I need to make some more videos about this so um stay tuned I will make more videos about how to you know upload a file how to retrieve a file and what are the datas you can get from that uh there quite a lot of things you can do with this at the same time you know there are stages like um in power automate there are some of the things you can't do it then you can think like okay I might be able to achieve this through open a platform and that's a whole idea about you know showing this how to access open AI via power automate thank you for watching
Info
Channel: abm abm
Views: 836
Rating: undefined out of 5
Keywords: Chat GPT, Flow, Image Generation, JSON format, Microsoft, Open AI, Power Automate, XML format, Chat Completion, GPT
Id: WYzsF0z7cLs
Channel Id: undefined
Length: 22min 52sec (1372 seconds)
Published: Tue Oct 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.