File size: 7,466 Bytes
0a84888 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
---
#https://www.notion.so/n8n/Frontmatter-432c2b8dff1f43d4b1c8d20075510fe4
title: AI coding
description: Use GPT to generate code in the Code node.
contentType: explanation
---
# AI coding with GPT
Not available on self-hosted.
Python isn't supported.
///
## Use AI in the Code node
--8<-- "_snippets/code/ai-how-to.md"
## Usage limits
During the trial phase there are no usage limits. If n8n makes the feature permanent, there may be usage limits as part of your pricing tier.
## Feature limits
The ChatGPT implementation in n8n has the following limitations:
* The AI writes code that manipulates data from the n8n workflow. You can't ask it to pull in data from other sources.
* The AI doesn't know your data, just the schema, so you need to tell it things like how to find the data you want to extract, or how to check for null.
* Nodes before the Code node must execute and deliver data to the Code node before you run your AI query.
* Doesn't work with large incoming data schemas.
* May have issues if there are a lot of nodes before the code node.
## Writing good prompts
<!-- vale off -->
Writing good prompts increases the chance of getting useful code back.
Some general tips:
* Provide examples: if possible, give a sample expected output. This helps the AI to better understand the transformation or logic you’re aiming for.
* Describe the processing steps: if there are specific processing steps or logic that should apply to the data, list them in sequence. For example: "First, filter out all users under 18. Then, sort the remaining users by their last name."
* Avoid ambiguities: while the AI understands various instructions, being clear and direct ensures you get the most accurate code. Instead of saying "Get the older users," you might say "Filter users who are 60 years and above."
* Be clear about what you expect as the output. Do you want the data transformed, filtered, aggregated, or sorted? Provide as much detail as possible.
And some n8n-specific guidance:
* Think about the input data: make sure ChatGPT knows which pieces of the data you want to access, and what the incoming data represents. You may need to tell ChatGPT about the availability of n8n's built-in methods and variables.
* Declare interactions between nodes: if your logic involves data from multiple nodes, specify how they should interact. "Merge the output of 'Node A' with 'Node B' based on the 'userID' property". if you prefer data to come from certain nodes or to ignore others, be clear: "Only consider data from the 'Purchases' node and ignore the 'Refunds' node."
* Ensure the output is compatible with n8n. Refer to [Data structure](/data/data-structure.md) for more information on the data structure n8n requires.
### Example prompts
These examples show a range of possible prompts and tasks.
#### Example 1: Find a piece of data inside a second dataset
To try the example yourself, [download the example workflow](/_workflows/ai-code/find-a-piece-of-data.json) and import it into n8n.
In the third Code node, enter this prompt:
> The slack data contains only one item. The input data represents all Notion users. Sometimes the person property that holds the email can be null. I want to find the notionId of the Slack user and return it.
Take a look at the code the AI generates.
This is the JavaScript you need:
```js
const slackUser = $("Mock Slack").all()[0];
const notionUsers = $input.all();
const slackUserEmail = slackUser.json.email;
const notionUser = notionUsers.find(
(user) => user.json.person && user.json.person.email === slackUserEmail
);
return notionUser ? [{ json: { notionId: notionUser.json.id } }] : [];
```
#### Example 2: Data transformation
To try the example yourself, [download the example workflow](/_workflows/ai-code/data-transformation.json) and import it into n8n.
In the **Join items** Code node, enter this prompt:
> Return a single line of text that has all usernames listed with a comma. Each username should be enquoted with a double quotation mark.
Take a look at the code the AI generates.
This is the JavaScript you need:
```js
const items = $input.all();
const usernames = items.map((item) => `"${item.json.username}"`);
const result = usernames.join(", ");
return [{ json: { usernames: result } }];
```
#### Example 3: Summarize data and create a Slack message
To try the example yourself, [download the example workflow](/_workflows/ai-code/summarize-data.json) and import it into n8n.
In the **Summarize** Code node, enter this prompt:
> Create a markdown text for Slack that counts how many ideas, features and bugs have been submitted. The type of submission is saved in the property_type field. A feature has the property "Feature", a bug has the property "Bug" and an idea has the property "Bug". Also, list the five top submissions by vote in that message. Use "<url|text>" as markdown for links.
Take a look at the code the AI generates.
This is the JavaScript you need:
```js
const submissions = $input.all();
// Count the number of ideas, features, and bugs
let ideaCount = 0;
let featureCount = 0;
let bugCount = 0;
submissions.forEach((submission) => {
switch (submission.json.property_type[0]) {
case "Idea":
ideaCount++;
break;
case "Feature":
featureCount++;
break;
case "Bug":
bugCount++;
break;
}
});
// Sort submissions by votes and take the top 5
const topSubmissions = submissions
.sort((a, b) => b.json.property_votes - a.json.property_votes)
.slice(0, 5);
let topSubmissionText = "";
topSubmissions.forEach((submission) => {
topSubmissionText += `<${submission.json.url}|${submission.json.name}> with ${submission.json.property_votes} votes\n`;
});
// Construct the Slack message
const slackMessage = `*Summary of Submissions*\n
Ideas: ${ideaCount}\n
Features: ${featureCount}\n
Bugs: ${bugCount}\n
Top 5 Submissions:\n
${topSubmissionText}`;
return [{ json: { slackMessage } }];
```
<!-- vale on -->
### Reference incoming node data explicitly
If your incoming data contains nested fields, using dot notation to reference them can help the AI understand what data you want.

To try the example yourself, [download the example workflow](/_workflows/ai-code/reference-incoming-data-explicitly.json) and import it into n8n.
In the second Code node, enter this prompt:
> The data in "Mock data" represents a list of people. For each person, return a new item containing personal_info.first_name and work_info.job_title.
This is the JavaScript you need:
```js
const items = $input.all();
const newItems = items.map((item) => {
const firstName = item.json.personal_info.first_name;
const jobTitle = item.json.work_info.job_title;
return {
json: {
firstName,
jobTitle,
},
};
});
return newItems;
```
### Related resources
Pluralsight offer a short guide on [How to use ChatGPT to write code](https://www.pluralsight.com/blog/software-development/how-use-chatgpt-programming-coding){:target=_blank .external-link}, which includes example prompts.
## Fixing the code
The AI-generated code may work without any changes, but you may have to edit it. You need to be aware of n8n's [Data structure](/data/data-structure.md). You may also find n8n's built-in methods and variables useful.
|