@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/README.md +14 -0
- package/examples/assistants.json +60 -82
- package/examples/audio.json +182 -63
- package/examples/files.json +70 -32
- package/examples/images.json +56 -28
- package/lib.js +666 -688
- package/locales/de-DE/node.json +2 -1
- package/locales/en-US/node.json +1 -1
- package/locales/ja/node.json +2 -1
- package/locales/zh-CN/node.json +2 -1
- package/node.html +2562 -572
- package/node.js +61 -98
- package/package.json +2 -3
package/node.js
CHANGED
|
@@ -1,113 +1,76 @@
|
|
|
1
|
-
|
|
2
|
-
const lib = require(
|
|
1
|
+
"use strict";
|
|
2
|
+
const lib = require("./lib.js");
|
|
3
3
|
|
|
4
4
|
module.exports = function (RED) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
11
|
+
let node = this;
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
32
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
57
|
+
RED.nodes.registerType("OpenAI API", OpenaiApiNode);
|
|
58
|
+
class ServiceHostNode {
|
|
59
|
+
constructor(n) {
|
|
60
|
+
RED.nodes.createNode(this, n);
|
|
98
61
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
+
"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
|
-
"
|
|
31
|
-
"form-data": "^4.0.0"
|
|
30
|
+
"openai": "^4.26.1"
|
|
32
31
|
},
|
|
33
32
|
"devDependencies": {
|
|
34
33
|
"eslint": "^8.54.0",
|