ChatGPT does Node-RED

7 Likes

I gave it the prompt:
Create a JSON file named "flows.json" that is a valid Node-RED flow file that can be imported into the Node-RED editor. The flow must contain three nodes. They are as follows, an "Inject" node, a "Function" node, and a "Debug" node. The nodes must be wired in that order. When "Inject" is pressed a msg object is sent containing a payload expected to be a string to the "Function" node. The "Function" node then saves the payload to an array as a flow context variable, and returns a msg object with a payload of the string and an additional property with the key "test" and the value of true if the string was not in the array beforehand otherwise false.

ChatGPT's response:

Here is an example of a valid flows.json file that meets the requirements you specified:

[
    {
        "id": "n1",
        "type": "inject",
        "z": "",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 120,
        "y": 100,
        "wires": [
            [
                "n2"
            ]
        ]
    },
    {
        "id": "n2",
        "type": "function",
        "z": "",
        "name": "",
        "func": "if (typeof context.get('myArray') === 'undefined') {\n    context.set('myArray', []);\n}\n\nconst myArray = context.get('myArray');\nconst payload = typeof msg.payload === 'string' ? msg.payload : '';\nlet test = true;\n\nif (!myArray.includes(payload)) {\n    myArray.push(payload);\n    test = false;\n}\n\nreturn {\n    payload,\n    test\n};",
        "outputs": 1,
        "noerr": 0,
        "x": 300,
        "y": 100,
        "wires": [
            [
                "n3"
            ]
        ]
    },
    {
        "id": "n3",
        "type": "debug",
        "z": "",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 480,
        "y": 100,
        "wires": []
    }
]

The "Inject" node sends a message with a string payload to the "Function" node. The "Function" node then checks if a flow context variable called myArray exists. If it doesn't, it creates the variable and initializes it to an empty array. The node then adds the string from the msg.payload property to the array if it is not already in the array, and returns a new msg object with the original string as the payload property and an additional property called test that is set to true if the string was already in the array, and false otherwise. Finally, the "Debug" node receives the msg object and outputs it to the debug sidebar in the Node-RED editor.

All in all, it did pretty well. I made some slight changes to the resulting function:

// I manually replaced the word "context" with "flow" throughout
if (typeof flow.get('myArray') === 'undefined') {
    flow.set('myArray', []);
};
const myArray = flow.get('myArray');

const payload = typeof msg.payload === 'string' ? msg.payload : '';
let test = true;

if (!myArray.includes(payload)) {
    myArray.push(payload);
    test = false;
};

// This line was manually added to save the array
flow.set('myArray', myArray);

return {
    payload,
    test
};

What a wild tool to play with.

3 Likes

I've been using ChatGPT as well. Pretty cool but often doesn't work.
The best success case I had was when I asked it to create a function node that takes as input a string and to single out specific pieces of that string separated by a space that match a certain pattern.
It produced an array with exactly that info.
I then told it that there would always only be a single result and therefore instead of an array I needed a string. It did that well.
Then I asked that the output should also include the original message, which it did too.

Negatives:
Often it's code doesn't run.
Also when I ask it to create a flows.json it can't always be imported and when it can it often has flaws.

I believe the reason it may struggle is that Node-RED is quite new. When something is very new online it can be hard to find for example, search results about it on Google. I noticed when I had it generating package.json files that it would always have old dependencies.

Here is one it generated for example:

{
"name": "my-node-red-electron-project",
"version": "1.0.3",
"description": "A full-stack Node-RED and Electron project for a senior developer",
"main": "flows.js",
"scripts": {
"start": "node-red",
"dev": "node-red -s settings.js",
"electron": "electron ."
},
"dependencies": {
"node-red": "^0.21.1",
"node-red-node-email": "^0.3.6",
"node-red-node-mongodb": "^0.2.4",
"node-red-node-socketio": "^0.1.9",
"electron": "^8.2.0"
},
"devDependencies": {
"eslint": "^6.1.0"
}
}

I think this is a glimpse into ChatGPT's data-set, in a way revealing why it might struggle with Node-RED for now. I do find that the more direct and specific you are with your prompts, the better the responses will be. I guess it's no surprise with these tools you'll experience "bad data in, bad data out" kind of scenarios.

It does a great job getting you 90% of the way there most of the time if you really think about your inputs. I don't mind filling in the last 10% and troubleshooting.

1 Like

I would agree with you, but the problem is that the flows.json format is very difficult to debug. So if the ChatGPT output can't be imported at all into Node Red it isn't very helpful. If it can be imported but there are some flaws in the flow then I'd agree with you.

Just tested it out on JSONata

2 Likes

It's aware of its own speech pattern!

cough! cough! SKYNET cough!

2 Likes

Just to let people know OpenAIs - ChatGPT (Chat Bot)is now closed for new users max users reached for some days at least

I've not been able to use it for the past week - just says error

I've just tried to login (I'm a registered user) - it took a few goes before it accepted me, but at least I can still access ChatGPT.

I'm pretty sure that I read in their documentation that the dataset has nothing past 2021.

1 Like

It's making me lazy!

When faced with something unfamiliar, I always researched it, found out how it works, then tested a possible solution.

Now I just ask ChatGPT.....

1 Like

You still need to test the solution - it isn't always correct. Nor is it always the best.

3 Likes

Correct!
it reminds you of that quite often during discussions.

However, with some potential large amounts of cash injections, that is reportedly being thrashed out in boardrooms, I would imagine it wont take long for it to re-enter "classrooms" and catch up on the latest gossip.

There is a Visual Code extension for it already!

It won't take long for classrooms to disappear altogether. We are now just at version 0.1 alpha. Give it 5 years. At the current rate of AI development, it won't take very long. It is learning from all the input as well, so it only gets smarter. Next step is for all those AI's to work together (text, video, photo, speech, etc)
Highly recommend to follow two minute papers on youtube.

Did I mention? it was able to diagnose a condition I have?

its a turning point in AI for sure

... grabbing screenshot I took the other day

Found the screenshot I took.

Its pretty dam imnpressisve (I am indeed a Diabetic)

1 Like

Yes it is crazy, great and scary at the same time!

1 Like

Completely agree!

I think some industries could see some affect from this development

Yes indeed, it is a very helpfull tool, but be cautious because the results aren't always correct.

I have been spending an evening last week generating jmespath expressions and Ansible tasks for my daily job. And the result looked VERY plauzible and good. So good that I kept on going.

But the next day I tried it at work, but I couldn't use any of it. It was useless. It had combined statements that look very logical, so logical that you think: why the hell couldn't I find something simple like this via googling. But the statements unfortunately couldn't be combined at the end in the real world...

But if you use it for some other stuff, it is indeed very useful.