Generate new images within Directus Automate with this custom operation, powered by OpenAI.
Speaker 0: The Directus AI image generation operation will take in a prompt and a couple of other settings and use OpenAI's DALL E 3 model to generate an image, which you can then save into your director's project. So here we have a recipes collection and an item with a recipe that I wrote and there is an empty field here for an image. Because I'm actually not a good chef we're gonna use AI to generate an image which we can then use on things like social media or the recipe website header, things like that. So, we are going to create a new flow, and we'll call this one generate image, and we'll make this a manual flow trigger on the recipes collection. So a button appears on the right.
Now we are gonna manually give it a prompt because we don't have an amount of text suitable to send for an image generation, only a big recipe that's too long. So without a single field here, I'll just call it prompt. You might choose to call it something nicer, and we'll just make it a little text input here. Now that had a key of prompt, and what that means is whenever that button is pressed, we will receive the ID of the item where we press the button and also the prompt itself. And so we are gonna use that prompt and send it off for image generation.
So we will go ahead and create a new operation here, and we will use the AI image generation operation. Now we need to use our open AI API key, we'll pop that in here and our prompt. And once again this is gonna be dynamic so trigger dot body dot prompt. We want high quality, standard quality will do, and we'll make it square so we can share it on our Instagram grid. So we'll hit save.
This will result in having, a URL. So we now need to import that URL into our directors project. To do that, we will use the request URL operation, and we'll make a post request to this Directus project URL slash files slash import. Just a note that I've made image, file creation body, we are going to set the URL to the value that was returned from the last operation, which is just a URL string. So that will import the image to our file library.
The final step is that we want to save that image into into the actual item where we triggered the flow to begin. So we're gonna add one final operation here, which is to update data. We are going to update the recipe specifically trigger dot body dot keys 0 which is the ID. And we are going to update the file value. File, and that is going to be last dot data dot data dot ID because that is a response for an API call.
And that should be it. You should be able to press the button. It will come up with a box for the prompt. It will go and generate the image, import it, and save it back to the image. So let's try that out.
Let's go in here. Let's generate image. I've got this rudimentary prompt written, and we'll run Flow on current item. Now image generation takes a little while so we'll come back when it's done. We're back and it did not work because it is not called file, it is called image.
So we're gonna update that and we'll go back and run it again. Welcome back, and it turns out I'm not a very good engineer because that is not an easy stuffed bell pepper. It is a bowl stuffed with bell peppers. But in any case, you need to write better prompts but that is how you use the Directus AI image generation operation. I'll see you in the next video.