Noob question re: using a Node.js script in a function node

Hello. I would like to run this Node.js script in a function node (used in this article).

import OpenAI from "openai";

const openai = new OpenAI({apiKey: '...'});

async function main() {
  const response = await openai.chat.completions.create({
    model: "gpt-4-vision-preview",
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "What’s in this image?" },
          {
            type: "image_url",
            image_url:
              "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
          },
        ],
      },
    ],
  });
  console.log(response.choices[0]);
}
main();

I copied the above (and inserted my API key) and put return msg; as the last line of code.

I then wired the output of the above function node to an http request node with these settings

(seems odd to have to pass my API key again as bearer authentication, but am pretty sure that URL requires it).

Doing the above yields nothing in the debug screen. I also wired a separate debug node after the function node and still get nothing, indicating my function node is not doing anything. I have a feeling that I am missing something obvious here. Should the above node.js script work in Node-RED, or do I need to do something else?

Here is the URL that I was trying to use:

https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg

Hi @grant1

The Node Red function Node requires some more involvement, then just a simple copy and paste.

  1. You cant import directly in the script
  2. If any script your using, is based on an async response, you can't simply return msg

To address the above you need to.

  1. Register the 3rd party library in the function node settings
  2. Use node.send instead of return msg to support async

See the import below, to start you off (pay attention to the Setup tab)

[{"id":"b47ddfdc7da359a6","type":"function","z":"197c16697aae09bd","name":"Open AI","func":"const OAI = new openai.OpenAI({ apiKey: '...' });\n\nasync function main() {\n    const response = await OAI.chat.completions.create({\n        model: \"gpt-4-vision-preview\",\n        messages: [\n            {\n                role: \"user\",\n                content: [\n                    { type: \"text\", text: \"What’s in this image?\" },\n                    {\n                        type: \"image_url\",\n                        image_url:\n                            \"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg\",\n                    },\n                ],\n            },\n        ],\n    });\n    node.send({ payload: response.choices[0] })\n}\nmain();","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"openai","module":"openai"}],"x":500,"y":360,"wires":[["0a281ed001c5fb67"]]},{"id":"756d370590baf3ca","type":"inject","z":"197c16697aae09bd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":360,"wires":[["b47ddfdc7da359a6"]]},{"id":"0a281ed001c5fb67","type":"debug","z":"197c16697aae09bd","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":360,"wires":[]}]

Thank you! Perhaps a year ago I was messing with a nodejs script and I had forgotten about registering the 3rd party library in the Setup tab.

Since I made my original post, I came up with another way to accomlish the same thing by injecting the JSON data like this, but using the function node is way better.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.