@inductiv/node-red-openai-api 0.3.8 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/node.js CHANGED
@@ -1,113 +1,76 @@
1
- 'use strict';
2
- const lib = require('./lib.js');
1
+ "use strict";
2
+ const lib = require("./lib.js");
3
3
 
4
4
  module.exports = function (RED) {
5
- class OpenaiApiNode {
6
- constructor(config) {
7
- RED.nodes.createNode(this, config);
8
- this.service = RED.nodes.getNode(config.service);
9
- this.method = config.method;
5
+ class OpenaiApiNode {
6
+ constructor(config) {
7
+ RED.nodes.createNode(this, config);
8
+ this.service = RED.nodes.getNode(config.service);
9
+ this.method = config.method;
10
10
 
11
- let node = this;
11
+ let node = this;
12
12
 
13
- node.on('input', function (msg) {
14
- let errorFlag = false;
15
- let client = new lib.OpenaiApi();
16
- if (!errorFlag && this.service) {
17
- client.setApiBase(this.service.apiBase);
18
- client.setOrganizationIdHeader(this.service.organizationId);
19
- }
13
+ node.on("input", function (msg) {
14
+ let client = new lib.OpenaiApi();
20
15
 
21
- if (!errorFlag && this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
22
- if (this.service.secureApiKeyIsQuery) {
23
- client.setApiKey(this.service.credentials.secureApiKeyValue,
24
- this.service.secureApiKeyHeaderOrQueryName, true);
25
- } else {
26
- client.setApiKey(this.service.credentials.secureApiKeyValue,
27
- this.service.secureApiKeyHeaderOrQueryName, false);
28
- }
29
- }
16
+ const serviceName = node.method; // Set the service name to call.
17
+ let serviceParametersObject = {
18
+ organization: node.service.organizationId,
19
+ apiBase: node.service.apiBase,
20
+ apiKey: node.service.credentials.secureApiKeyValue || "",
21
+ _node: node,
22
+ payload: { ...msg.payload },
23
+ };
30
24
 
31
- if (!errorFlag){
32
- client.setNodeRef(node);
33
- }
25
+ // Dynamically call the function based on the service name
26
+ const functionName = `${serviceName}`;
27
+ if (typeof client[functionName] === "function") {
28
+ node.status({
29
+ fill: "blue",
30
+ shape: "dot",
31
+ text: "OpenaiApi.status.requesting",
32
+ });
34
33
 
35
- if (!errorFlag) {
36
- client.body = msg.payload;
37
- }
38
-
39
- let result;
40
-
41
- if (!errorFlag) {
42
- const serviceName = node.method; // Specify the service you want to process
43
- let serviceParametersObject = {};
44
-
45
- // Dynamically call the function based on the service name
46
- const functionName = `${serviceName}`;
47
- if (typeof client[functionName] === 'function') {
48
- serviceParametersObject.body = msg.payload || {};
49
- result = client[functionName](serviceParametersObject);
50
- } else {
51
- console.error(`Function ${functionName} does not exist on client.`);
52
- }
53
- }
54
-
55
- if (!errorFlag && result === undefined) {
56
- node.error('Method is not specified.', msg);
57
- errorFlag = true;
58
- }
59
-
60
- let setData = function (msg, response) {
61
- if (response) {
62
- if (response.status) {
63
- msg.statusCode = response.status;
64
- }
65
- if (response.headers) {
66
- msg.headers = response.headers;
67
- }
68
- if (response.config && response.config.url) {
69
- msg.responseUrl = response.config.url;
70
- }
71
- if (response.data) {
72
- msg.payload = response.data;
73
- }
74
- }
75
- return msg;
76
- };
77
-
78
- if (!errorFlag) {
79
- node.status({ fill: 'blue', shape: 'dot', text: 'OpenaiApi.status.requesting' });
80
- result.then(function (response) {
81
- node.send(setData(msg, response));
82
- node.status({});
83
- }).catch(function (error) {
84
- let message = error.message;
85
- let errorData = error.response || {}; // Fallback to an empty object if response is not available
86
- node.error(message, setData(msg, errorData));
87
- node.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.error' });
88
- });
89
- }
34
+ client[functionName](serviceParametersObject)
35
+ .then((payload) => {
36
+ // Update `msg.payload` with the payload from the API response.
37
+ msg.payload = payload;
38
+ node.send(msg);
39
+ node.status({});
40
+ })
41
+ .catch(function (error) {
42
+ node.status({
43
+ fill: "red",
44
+ shape: "ring",
45
+ text: "node-red:common.status.error",
46
+ });
47
+ let errorMessage = error.message;
48
+ node.error(errorMessage, { payload: {} });
90
49
  });
50
+ } else {
51
+ console.error(`Function ${functionName} does not exist on client.`);
91
52
  }
53
+ });
92
54
  }
55
+ }
93
56
 
94
- RED.nodes.registerType('OpenAI API', OpenaiApiNode);
95
- class ServiceHostNode {
96
- constructor(n) {
97
- RED.nodes.createNode(this, n);
57
+ RED.nodes.registerType("OpenAI API", OpenaiApiNode);
58
+ class ServiceHostNode {
59
+ constructor(n) {
60
+ RED.nodes.createNode(this, n);
98
61
 
99
- this.secureApiKeyValue = n.secureApiKeyValue;
100
- this.apiBase = n.apiBase;
101
- this.secureApiKeyHeaderOrQueryName = n.secureApiKeyHeaderOrQueryName;
102
- this.secureApiKeyIsQuery = n.secureApiKeyIsQuery;
103
- this.organizationId = n.organizationId;
104
- }
62
+ this.secureApiKeyValue = n.secureApiKeyValue;
63
+ this.apiBase = n.apiBase;
64
+ this.secureApiKeyHeaderOrQueryName = n.secureApiKeyHeaderOrQueryName;
65
+ this.secureApiKeyIsQuery = n.secureApiKeyIsQuery;
66
+ this.organizationId = n.organizationId;
105
67
  }
68
+ }
106
69
 
107
- RED.nodes.registerType('Service Host', ServiceHostNode, {
108
- credentials: {
109
- secureApiKeyValue: { type: 'password' },
110
- temp: { type: 'text' }
111
- }
112
- });
70
+ RED.nodes.registerType("Service Host", ServiceHostNode, {
71
+ credentials: {
72
+ secureApiKeyValue: { type: "password" },
73
+ temp: { type: "text" },
74
+ },
75
+ });
113
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inductiv/node-red-openai-api",
3
- "version": "0.3.8",
3
+ "version": "0.4.0",
4
4
  "description": "Go beyond ChatGPT and DALL·E 3: this Node-RED node seamlessly integrates a range of OpenAI services, including Assistants, Threads, Vision, and Audio, enabling feature-rich enhancement of your AI workflows with any OpenAI REST API-compatible solution.",
5
5
  "main": "node.js",
6
6
  "engines": {
@@ -27,8 +27,7 @@
27
27
  "ai"
28
28
  ],
29
29
  "dependencies": {
30
- "axios": "^1.6.0",
31
- "form-data": "^4.0.0"
30
+ "openai": "^4.26.1"
32
31
  },
33
32
  "devDependencies": {
34
33
  "eslint": "^8.54.0",