No-Code Automation Utilizing n8n Cloud and OpenAI Imaginative and prescient API – Automation with n8n
Step into the way forward for no-code automation! Discover our straightforward, beginner-friendly information on utilizing n8n Cloud for integrating OpenAI Imaginative and prescient API in your workflow automation course of.
The Preparation
Setting Up your Open AI Account
In step one, we’d like a charged OpenAI account with a corresponding API key to ship requests to OpenAI. To open an account, merely click here. If you have already got an account, you’ll be able to skip on to the following level.
Creating your Enter
To feed the API of Open AI with a picture we’d like it in a sure format as a result of we can not merely add it through the API. We will both use photos with the API through Base64 encoded format or a picture URL.
In this tutorial, I’ve already proven how one can robotically create screenshots with n8n and the Urlbox API. For the reason that output of Urlbox is a URL with the hosted picture, we are going to deal with the choice through URL as an alternative of base64 encoded photos.
Should you don’t wish to use the Imaginative and prescient API with urlbox outputs, you’ll be able to select a picture from the Web for testing and choose the precise area for the useful resource for the picture as enter. In the long run, it makes essentially the most sense to retailer your photos through S3 file storage at a hoster similar to AWS after which request them through URL.
You’ll find extra data and extra examples on the way to use the Visions API here.
Setting Up Your n8n workflow
To get began with our automation, we’d like a brand new workflow at n8n. If you have already got an account, you need to create a brand new workflow. You possibly can later copy and paste the automation into each current workflow you prefer it to be part of.
Should you don’t have an account but, you can now start a free trial.
After you’ve registered, you can be taken on to your first, empty, workflow and may get began instantly.
The API Request
To work together with exterior companies at n8n there are principally 2 potentialities, both there’s a native integration from the n8n aspect, or you’ll be able to work together with REST APIs through the HTTP Request node customized. (In case you wish to work together with a GraphQL Api, there may be an additional GraphQL Node, however we won’t cowl this immediately).
Though there’s a native integration for Open AI at n8n, this isn’t (but) tailored to the particular wants of the Imaginative and prescient Endpoint. We are going to subsequently clarify the choice through the HTTP request node for this tutorial. If you want to make use of a mannequin aside from the Imaginative and prescient mannequin, you could find the tutorial for the native integration here.
To proceed, create a brand new HTTP request node in your n8n workflow.
Enter
Should you don’t wish to recreate the urlbox screenshot automation or just wish to take a look at the imaginative and prescient API with a static URL, you’ll be able to skip on to the following level „Technique & URL“.
Since we wish to have a variable enter in our immediate in our instance with the screenshot that we created with urlbox, we now have to attach our HTTP request node that we created on this tutorial with our new one.
We are going to then see the output of the primary node because the enter of our second node and may merely use this knowledge level as a variable later within the course of.
That is what you now ought to see on the left aspect of your new HTTP Reuest node with which we are going to within the following steps create our OpenAI Imaginative and prescient API Request.
Technique & URL
First we have now to outline which permitted operation we wish to carry out for which URL. In our case, we wish to carry out a publish operation beneath the URL „https://api.openai.com/v1/chat/completions“.
Principally, you could find out which operation and URL you must use by wanting on the API documentation. Here you can find the API documentation of OpenAI.
It’s best to test whether or not, for instance, the URL continues to be the identical on the time you’re studying this because it was on the time I wrote it.
That is how the highest of your HTTP Request node ought to appear like if you happen to adopted the steps.
On this case, authentication just isn’t carried out through the built-in subject, however through the header, as you will note within the subsequent step.
Header / Authentication
So as to add a header to our request, it’s essential to first activate „Ship Header“.
As we solely use the header for authentication on this case, we additionally use this because the Title for our parameter.
We now enter „Bearer“ adopted by our OpenAI API key as the worth.
Should you would use this workflow in manufacturing or share the workspace with different folks, you need to deal with your developer secrets and techniques in another way. On this case, nevertheless, we maintain it so simple as doable for quick prototyping causes.
Physique
We now use the physique of our request to outline the mannequin and specify our immediate. To do that, we should first activate „Ship Physique“ and choose JSON because the content material sort.
After we have now accomplished the operation & URL after which the authentication within the final steps, we now must outline what precisely is to be achieved.
To do that, we now outline the request in JSON format primarily based on the specs within the OpenAI API documentation.
Under you’ll be able to see what the completed request appears to be like like:
The request consists of the next elements:
mannequin: Concretization of the mannequin we wish to use, in our case the imaginative and prescient mannequin
messages: We will outline in a number of components what the enter for our mannequin needs to be. In our case, we offer the mannequin with a normal activity within the type of a query through a system immediate. As a person immediate, we now insert our picture within the type of the URL, on this case a variable that’s then resolved as a URL by the enter knowledge. This picture then takes the mannequin and solutions the query posed as a system immediate on its foundation.
max_tokens: This determines the utmost variety of tokens that the mannequin could use to reply our question. We have to outline the max_tokens param as a result of the Visions API has a fairly low default response token depend.
When you have constructed the workflow precisely as I did, you’ll be able to copy the next JSON and paste it beneath Expression. You possibly can then customise it in response to your needs. For instance, if you wish to use a static URL for testing, change {{ $json[„renderUrl“] }} together with your URL.
{"mannequin":"gpt-4-vision-preview","messages":[{"role":"system","content":"What do you see on the screenshot?"},{"role":"user","content":[{"type":"image_url","image_url":{"url":"{{ $json["renderUrl"] }}"}}]}],"max_tokens":200}
The Check Execution
If we now run our workflow, we should always get an output that appears like this (excerpt).
In our case, the AI ought to describe the screenshot of the n8n web site. When you have recreated the workflow, you need to get an identical however not the identical output as a result of non-deterministic nature of the AI.
I hope you loved this tutorial and had been in a position to be taught one thing about n8n, OpenAI and workflow automation.
Should you nonetheless haven’t created an n8n account, now you can begin a free trial.