Chat GPT in Node-RED Function Nodes

New Node-RED function with embedded ChatGPT is now open-sourced and available to use!

Joe Pavitt , Steve McLaughlin
Back to Blog Posts
Image representing null

Recently we posted a demo of ChatGPT integration in a Node-RED function node onto our social media accounts. We have now open-sourced this for all to play with, and welcome any and all contributions.

How it Works - Prompt Engineering

OpenAI make a collection of their Generative AI models available via an API. We are wrapping OpenAI's node.js module, and in particular using the openai.createChatCompletion() functionality. For this API, you provide a chat history, and ChatGPT will respond with the next entry in that conversation.

In order to "train" ChatGPT for our use case of populating Node-RED function nodes, we first tried a collection of prompts, defining specific requirements for the contents, e.g. "Always write Javascript", "Never include the wrapping function definition", "Assume the input is always msg".

It turns out though, that we were over-engineering it, we were not getting reliable results and ended up realising that ChatGPT's existing knowledge of Node-RED was sufficient such that we could use that as a prompt:

Here's what we settled on:

messages: [
{role: "system", content: "always respond with content for a Node-RED function node, and don't add any commentary, always use const or let instead of var. Always return msg, unless told otherwise."},
{role: "user", content: prompt}
],

Here we send a system prompt in order to setup ChatGPT, and then follow that immediately with whatever the user has typed. From our (limited) testing, this has given us fairly reliable results.

Breaking this prompt down:

  • "Always respond with content for a Node-RED function node": Ensured no surrounding function () {} definition and set expectations that the function would deal with a msg and likely msg.payload object.
  • "Don't add any commentary": ChatGPT likes to, well, chat. It would always return raw text justifying decisions, etc. Here, we just wanted the code.
  • "Always use const or let instead of var": This was Steve being picky.
  • "Always return msg, unless told otherwise": We found this wasn't mostly required, but occasionally it would try to return a different variable, and we'd lose context of msg.payload, or other data stored in msg. So this just made sure we had the consistency.

The response from this API call is then populated into the contents of the active tab in the function node:

Screenshot 2023-04-21 at 16 08 47

In order to use it yourself, you will need a valid API Key from OpenAI.

Additional Features

This was built in about a day by Steve and Joe, and we had plenty of ideas on what we'd like to add to it. We've open-sourced it, and will add these as issues to the repo, but if anyone want so take a stab at contributing - that'd be most welcome!

  • Insert at Cursor (issue): Currently, the Ask GPT call will replace all of the content of that tab. Would be great to have the code insert wherever the cursor last was in order to add to existing code.

  • Retain Conversation History (issue): Each time a new prompt is provided by the Node-RED user, we send a fresh conversation to OpenAI, meaning that knowledge of previously asked questions are not retained.

  • Client side ChatGPT Config (issue): Currently, when you add a new "function-gpt" node you need to select the ChatGTP Config node and click "Deploy" before you can ask it a question. Our ChatGPT interaction operates server-side (to protect your API key), so Node-RED needs that in the runtime first, before a call to ChatGPT can be made. Ideally, we'd be smarter here and pass client-side creds along with the call such that we can use any changes made by the user at the time of the call.

FlowFuse Assistant - No API Keys Required!

Great news! You no longer need to manage OpenAI API keys or configure ChatGPT nodes. The FlowFuse Assistant is now built directly into Node-RED on FlowFuse Cloud, making AI-powered development even easier.

Available on FlowFuse Cloud, the Assistant offers:

  • Quick Function Node Creation: Add function nodes to your flow without dragging from the palette
  • In-line Code Generation: Generate JavaScript code for function nodes, JSON for JSON editors, and Vue.js for FlowFuse Dashboard ui-template widgets
  • Flow Explainer: Select nodes and click "Explain Flows" to understand what they do

FlowFuse Assistant helps developers work faster and smarter with Node-RED. Start your free trial to experience AI-powered Node-RED development on FlowFuse Cloud.

About the Author

Joe Pavitt

Head of UX & Design

Joe has been working with Node-RED for nearly 10 years, having worked with Nick O'Leary at IBM as a Master Inventor, and been one of the early developers working on the original Node-RED Dashboard. Since joining FlowFuse in 2022, Joe has become the lead for the FlowFuse Dashboard (Dashboard 2.0), created the Node-RED Academy and leads design and front-end development for FlowFuse.

About the Author

Steve McLaughlin

Senior Software Developer

Steve is a Senior Software Engineer at FlowFuse. Prior to joining FlowFuse, he worked as an Engineer in the Automotive Industry and has extensive production environment experience. Steve is also a Node-RED core contributor and has written several popular contribution nodes for Node-RED including drivers for communicating with PLC Hardware

Related Articles:

Sign up for updates