N8N workflow - get Magento 2 data, change with Open AI ChatGPT - save to CSV file

After the release of ChatGPT, AI became an even hotter topic than it used to be. Everyone and their dog are trying to become a ChatGPT expert, so I decided to take the plunge too.

Since my business is related to eCommerce, I started to think about what you can do with ChatGPT as an online store owner. A simple example that came to my mind is improving product descriptions. Better product descriptions are good both for humans (better texts sell better) and for SEO (better text content ranks higher).

So, let’s give it a try. The tools we will use in this exercise are:

  • Your online store with API access. Since I mainly work with Magento, that is the store I will use. However, instead of Magento, it can be any platform with an API like BigCommerce or Shopify.
  • N8N. N8N is a business automation tool. You may think about it as a more open version of Zapier. It helps us to automate the process. N8N has pre-built integrations with both Magento and OpenAI, making it simpler than writing API requests from scratch.
  • OpenAI API credentials. OpenAI is the company behind ChatGPT. To use its AI in automated mode, you must have access to their API. It is paid, but relatively cheap.

The plan

What we are going to build is a N8N workflow (automated process) that will:

  • Access Magento online store via REST API and retrieve from there product data
  • Take the existing product descriptions and improve it using OpenAI
  • Save new product data to CSV file for further use

You may ask why save to CSV rather than uploading directly back to online store, but we’ll come to that later.

Preparation

N8N

First we need to have N8N either installed on your server/desktop or purchased on their cloud.

If you go the first route you should follow instructions on N8N Github page. For those who prefer SaaS solutions, N8N Cloud cost starts from 20 euro per month and in my opinion provides better value for money than say Zapier.

Magento API

You need Magento rest API access to work with its data programmatically. How to do it is described in N8N documentation. There is a small, but important catch here. To make it works the way that N8N integration uses, you need to enable “Allow OAuth Access Tokens to be used as standalone Bearer tokens” setting in Store/Configuration/Services/OAuth/Consumer section of your store Magento admin.

Setting Magento to work with N8N

Before you do it, Magento will reject your authorization requests.

After it is done you may follow N8N documentation to set up Magento credentials.

OpenAI API

You need to visit OpenAI sign-up page and create an account there. Then you need to subscribe to paid API usage plan, create API keys.

Save them and your organization ID (can get it under Settings tab) for use with N8N.

OpenAPI settings

That is it with preparation.

Building a workflow

We start from getting Magento product data. In order to do that we create a Magento 2 (2 refers to the major Magento version we use) node in N8N. The node looks like

N8N Magento 2 node

Most thing here are pretty obvious. When we start, to save the time and resources, it is better to limit number of products we are taking from Magento, I would set 3 or 5 in the Limit field initially. Later, when the workflow works fine you may change the limits and process all products. Also we don’t want to get products that for some reasons have no description at all. Once we run the node, if all is fine, we get product data from Magento. It should look as on screenshot below (in JSON format), right hand we have output data.

Magento 2 N8N node with product data

Process Magento product data

Before we start to engage OpenAI API we need to process the data we got from Magento. OpenAI charges us in tokens (as a rule of thumb 1 word passed to API is equal 1.5 tokens), so we only want to pass there the data it needs. So on the next step we will filter Magento data only leaving unique product identifier called SKU and text description that we will feed to OpenAI.

Filter Magento 2 data in n8n

I have to admit here that I have very basic skill in Javascript coding, so actively used ChatGPT to help me with this code. Hence maybe it is not the most elegant solution. But it works OK, we have correct sku and description in the output.

Improving the description with OpenAI

Now the most exciting part – use the power of AI to improve product descriptions.

Here are the settings of the node I have:

N8N OpenAI settings

We use here Chat resource (there are others available), Complete operation and gpt 3.5 turbo model.

The real tricky part here was a prompt. I will put the whole prompt text below:

This is the initial data 
sku:  {{ $json.sku }}, 
description: {{ $json.description }}


In a JSON format:

- Improve the description, making it more engaging, better selling the underlying product. In less than 200 words. In English. Casual Tone. 

Sanitize quotes in description, so they don't break valid JSON

format should be:
{"sku": sku,
"description": new_improved_description}


JSON: 

What is the most tricky here is that for proper further processing we need the data provided in OpenAI response in a valid JSON format. Seems like an easy task, but appeared that ChatGPT isn’t very good in that. The prompt below took me many attempt to build and in around 1 out of 10 cases the JSON is still invalid. Mainly it is about not properly excepted internal quotes (very common in my data set because the product information contained product dimensions in imperial units like inches).

I have tried to explicitly instruct ChatGPT to except the internal quotes, provide more detailed examples, but it hasn’t improved the results significantly, often made them worse.

Processing the results

The results we got from OpenAI API isn’t exactly in the format we need for further use: both SKU and description are within bigger JSON element content that in turns within another element message. So we need to un-nest this JSON and only take from there an sku and a description.

Processing OpenAI output N8N node

We again use custom code node and do a bit of JS coding. As before I engaged ChatGPT to help me with the code. You see that as an output, right hand we only have SKU and new, improved description now.

Saving the data to CSV file

As a final step we’ll save the file to the CSV file. Why CSV file rather than pushing data back to Magento?

N8N-save-data-CSV-file

There are two reasons:

  1. N8N Magento 2 node is a bit limited in capabilities, I haven’t found description among the product attributes it can update. If I really want to do it, I had to use a generic API call node, which requires more work.
  2. Anyway we may probably want to review the data that AI created before pushing it back to the website and CSV is not bad for that. Then in real life importing CSV to Magento is quite easy and trivial operation.

Overall workflow and source code

Overall workflow is presented on the screenshot below

N8N workflow - get Magento 2 data, change with Open AI ChatGPT - save to CSV file

The workflow as a code that may be imported to n8n is available on Github.

That is just a basic example of what can be done. Other uses may be classifying products by tags and categories, augmenting the product data with extra attributes, do customer segmentation. The possibilities are endless.

Leave a Reply

Your email address will not be published. Required fields are marked *