Chat GPT in Node-RED Function Nodes

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

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.

Written By:

Head of UX & Design
Senior Software Developer

Published on:

Related Articles:

Sign up for updates