logo

NJP

ServiceNow and DALL·E 2 - How it works!

Import · Dec 22, 2022 · video

[Music] hey everyone it's Justin thank you for watching and welcome to my house that's Justin's house in this video you finally get to see behind the scenes on how I did those open AI artificial intelligence integration so I'm going to start with the very beginning when you're doing an integration what you want to do is you want to set up a credential so that you can store that securely in service now and uh and not have that kind of hard-coded into your scripts or stuff like that so what you're looking at here is a API key credential now this particular open AI integration uses rest and what they do is they give you a API key for your account there are some other authentication options I of course went with the easiest and that was this so that's that API key in here it's protected you can't see it and then what I did is I have a workflow that is associated with a catalog item so you can see here this one is triggered by the service catalog and what it does is it adds a work node as you saw in the demos we get the variables from the catalog item that someone put in for their prompt of what the image they want to see I'm setting some flow variables but the key thing here in this video I want to talk about is generating the image so this is making the rest call to dolly or open API open AIS Dali and so what I did for that is a custom action okay so each one of these things if you didn't know this already in flow designer are called actions and servicenow has built out a ton of these where you can use them and drag and drop them into your workflows or your flows and flow designer and use them I made my own for Dolly which you can see here this generates image um where to go it went up to the top I'm going to scroll back up here there it is the generate image is one the one I made to make this happen and that action looks like this so we start off with Gathering some inputs and my inputs are either going to be I'm going to set them or I'm going to grab them from that catalog item in this case there's the prompt that someone's going to type into that catalog item and then maybe they want three images instead of one image that's an option they can specify a size according to the API and then I want to know the requested item so I can get that attachment that picture and attach it back to the requested item so as you saw that demo it just shows up in the work notes or in the comments for that so to make the initial rest call all we do is use get that credential Alias load that in memory I have a base URL to talk to that API so you can see their api.openai.com V1 right it doesn't get any simpler than that and I built my request manually to go to this slash image Generations uh prompt or I forget I forget what you call it and rest but basically if you go to their documentation you can see exactly how to use their API and this is a structure you can see there's the prompt there's the number of images there's the size of the image and in this case it's going to respond back with the URL I actually didn't do a URL in mine I did a Json response because some of these are pretty big and I didn't want to have to fool with like going and requesting a URL so I did there's my resource path that is going to get appended to this https API openai.com V1 slash Images slash generations and it's going to be a post and my headers that I need to put in there are the content type which is going to be Json and the authorization is going to be my Bearer token which I just had to add the word bear in here and then I literally dragged and dropped my credential value which is right here uh right it's right behind my head so I just drag and drop that into there so that's where you got the Plug and Play here in servicenow and then my content was I had to build out the a prompt for the artificial intelligence to know what I wanted so my prompt was one of the input variables the number of images one of the input variables the size one of the input variables and then the response format I went ahead and hard coded as hey I want base64 Json back okay so that gets executed I didn't do anything other fancy there and then my next step was to run a script now when I originally tried to do this I tried to use in um when you're doing these actions one of the steps you can do is parse Json and that's really helpful for some basic use cases where you're expecting a small response maybe a couple you know 100 lines to go through and if I just search for Json here you'll see it that's pre-built you don't have to do anything this is Json parser now that didn't work in this case because these this base64 Json string values exceeded the max string value when you're using those complex objects so I went ahead and wrote a script and I actually learned a couple of things here that I wanted to share with you so let's walk through the script first of all I've set up some input variables basically most important is I need that payload from the rest step that I just ran over here so that's me saying hey get that response and put it in a variable for me and then I also want that requested item because I'm going to use that requested item to attach the picture to the next thing I want to do is I want to do my own parsing so you can see there my result is going to be the um the method global.json.parse and I'm going to drop in that response variable which was my rest payload that came back from that rest app next I'm going to Loop through it in the demos and in this way it's configured right now I'm only getting one image back but I've made this so that there's a potential in the future that if I want allow more than one image back I can so that's why this for Loop is here so it's saying for every result in the result data go ahead and set the image number and the image number is going to be the the I variable as We're looping through here plus one I do plus one because it starts off a zero um so I want image zero as my name but then I use that to concatenate with the word image and make my file name so in this case image1.png I know that dolly is going to send back a PNG image so I went ahead and set that content type variable to image slash PNG and then my base64 encoded content this is a variable I just made up is going to be the result data that I'm looping through and then I'm going to drop into that initial zero in this case or one two three if I've Loop through this a couple of times and I'm going to grab the base64 Json that's just putting in the variable Next Step I'm going to create an attachment so new Glide sys attachment and this is this is where the learning came in the attachment method has it or the attachment object has its own method to write base64 which was exactly what I was getting back from Dali so I just needed a pass the record that I'm going to attach it to which is right there attachment record uh which we collected collected at the beginning the file name which I created right here so I've got a file name for it the content type which I created right here the image that slash PNG and then importantly the base64 encoded content that's the actual image itself and so that runs everything it creates the attachment it attaches it to there there's nothing left to do on the output because I did everything in my script step and then it goes on with the flow and so in the flow itself there's another step that I'll go into later where we query The DaVinci model to get that poem and then I Loop through that particular thing add a work note update the requested item and close it and update the request records record and close it but the real action that's going on that is behind the scenes that to me is not that complex I think you can do this is two steps in a custom action send your rest request get that back as a payload then run a quick script I mean look at this this is not this is 20 lines of code it really isn't that complicated and I was able to make something really fun that as you probably saw some people really enjoyed playing around with and I need to get back there and play around with it too but that's it for this video I hope you found this helpful if you did please like Please Subscribe or share it with somebody who you think might be interested in writing their own custom actions in flow designer so they can do Integrations like you saw with openai and artificial intelligence until next time don't forget to always be learning [Music] [Music] thank you

View original source

https://www.youtube.com/watch?v=W6mj5JzugBY