@inductiv/node-red-openai-api 0.1.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/LICENSE +7 -0
- package/README.md +66 -0
- package/examples/assistants.json +442 -0
- package/examples/audio.json +122 -0
- package/examples/chat.json +87 -0
- package/examples/embeddings.json +72 -0
- package/examples/files.json +72 -0
- package/examples/fine-tuning.json +72 -0
- package/examples/images.json +82 -0
- package/examples/messages.json +77 -0
- package/examples/models.json +92 -0
- package/examples/moderations.json +77 -0
- package/examples/runs.json +77 -0
- package/examples/threads.json +67 -0
- package/icons/icon.png +0 -0
- package/lib.js +617 -0
- package/locales/de-DE/node.json +112 -0
- package/locales/en-US/node.json +112 -0
- package/locales/ja/node.json +112 -0
- package/locales/zh-CN/node.json +112 -0
- package/node.html +771 -0
- package/node.js +256 -0
- package/package.json +45 -0
package/node.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { str } = require('ajv');
|
|
3
|
+
var lib = require('./lib.js');
|
|
4
|
+
|
|
5
|
+
module.exports = function (RED) {
|
|
6
|
+
function OpenaiApiNode(config) {
|
|
7
|
+
RED.nodes.createNode(this, config);
|
|
8
|
+
this.service = RED.nodes.getNode(config.service);
|
|
9
|
+
this.method = config.method;
|
|
10
|
+
|
|
11
|
+
const services = {
|
|
12
|
+
createChatCompletion: {
|
|
13
|
+
body: 'msg',
|
|
14
|
+
},
|
|
15
|
+
createImage: {
|
|
16
|
+
body: 'msg'
|
|
17
|
+
},
|
|
18
|
+
createImageEdit: {
|
|
19
|
+
body: 'msg'
|
|
20
|
+
},
|
|
21
|
+
createImageVariation: {
|
|
22
|
+
body: 'msg'
|
|
23
|
+
},
|
|
24
|
+
createEmbedding: {
|
|
25
|
+
body: 'msg'
|
|
26
|
+
},
|
|
27
|
+
createSpeech: {
|
|
28
|
+
body: 'msg'
|
|
29
|
+
},
|
|
30
|
+
createTranscription: {
|
|
31
|
+
body: 'msg'
|
|
32
|
+
},
|
|
33
|
+
createTranslation: {
|
|
34
|
+
body: 'msg'
|
|
35
|
+
},
|
|
36
|
+
listFiles: {
|
|
37
|
+
body: 'msg'
|
|
38
|
+
},
|
|
39
|
+
createFile: {
|
|
40
|
+
body: 'msg'
|
|
41
|
+
},
|
|
42
|
+
deleteFile: {
|
|
43
|
+
body: 'msg'
|
|
44
|
+
},
|
|
45
|
+
retrieveFile: {
|
|
46
|
+
body: 'msg'
|
|
47
|
+
},
|
|
48
|
+
downloadFile: {
|
|
49
|
+
body: 'msg'
|
|
50
|
+
},
|
|
51
|
+
createFineTuningJob: {
|
|
52
|
+
body: 'msg'
|
|
53
|
+
},
|
|
54
|
+
listPaginatedFineTuningJobs: {
|
|
55
|
+
body: 'msg'
|
|
56
|
+
},
|
|
57
|
+
retrieveFineTuningJob: {
|
|
58
|
+
body: 'msg'
|
|
59
|
+
},
|
|
60
|
+
listFineTuningEvents: {
|
|
61
|
+
body: 'msg'
|
|
62
|
+
},
|
|
63
|
+
cancelFineTuningJob: {
|
|
64
|
+
body: 'msg'
|
|
65
|
+
},
|
|
66
|
+
retrieveModel: {
|
|
67
|
+
body: 'msg'
|
|
68
|
+
},
|
|
69
|
+
deleteModel: {
|
|
70
|
+
body: 'msg'
|
|
71
|
+
},
|
|
72
|
+
createModeration: {
|
|
73
|
+
body: 'msg'
|
|
74
|
+
},
|
|
75
|
+
listAssistants: {
|
|
76
|
+
body: 'msg'
|
|
77
|
+
},
|
|
78
|
+
createAssistant: {
|
|
79
|
+
body: 'msg'
|
|
80
|
+
},
|
|
81
|
+
getAssistant: {
|
|
82
|
+
body: 'msg'
|
|
83
|
+
},
|
|
84
|
+
modifyAssistant: {
|
|
85
|
+
body: 'msg'
|
|
86
|
+
},
|
|
87
|
+
deleteAssistant: {
|
|
88
|
+
body: 'msg'
|
|
89
|
+
},
|
|
90
|
+
createThread: {
|
|
91
|
+
body: 'msg'
|
|
92
|
+
},
|
|
93
|
+
getThread: {
|
|
94
|
+
body: 'msg'
|
|
95
|
+
},
|
|
96
|
+
modifyThread: {
|
|
97
|
+
body: 'msg'
|
|
98
|
+
},
|
|
99
|
+
deleteThread: {
|
|
100
|
+
body: 'msg'
|
|
101
|
+
},
|
|
102
|
+
listMessages: {
|
|
103
|
+
body: 'msg'
|
|
104
|
+
},
|
|
105
|
+
createMessage: {
|
|
106
|
+
body: 'msg'
|
|
107
|
+
},
|
|
108
|
+
getMessage: {
|
|
109
|
+
body: 'msg'
|
|
110
|
+
},
|
|
111
|
+
modifyMessage: {
|
|
112
|
+
body: 'msg'
|
|
113
|
+
},
|
|
114
|
+
createThreadAndRun: {
|
|
115
|
+
body: 'msg'
|
|
116
|
+
},
|
|
117
|
+
listRuns: {
|
|
118
|
+
body: 'msg'
|
|
119
|
+
},
|
|
120
|
+
createRun: {
|
|
121
|
+
body: 'msg'
|
|
122
|
+
},
|
|
123
|
+
getRun: {
|
|
124
|
+
body: 'msg'
|
|
125
|
+
},
|
|
126
|
+
modifyRun: {
|
|
127
|
+
body: 'msg'
|
|
128
|
+
},
|
|
129
|
+
submitToolOuputsToRun: {
|
|
130
|
+
body: 'msg'
|
|
131
|
+
},
|
|
132
|
+
cancelRun: {
|
|
133
|
+
body: 'msg'
|
|
134
|
+
},
|
|
135
|
+
listRunSteps: {
|
|
136
|
+
body: 'msg'
|
|
137
|
+
},
|
|
138
|
+
getRunStep: {
|
|
139
|
+
body: 'msg'
|
|
140
|
+
},
|
|
141
|
+
listAssistantFiles: {
|
|
142
|
+
body: 'msg'
|
|
143
|
+
},
|
|
144
|
+
createAssistantFile: {
|
|
145
|
+
body: 'msg'
|
|
146
|
+
},
|
|
147
|
+
getAssistantFile: {
|
|
148
|
+
body: 'msg'
|
|
149
|
+
},
|
|
150
|
+
deleteAssistantFile: {
|
|
151
|
+
body: 'msg'
|
|
152
|
+
},
|
|
153
|
+
listMessageFiles: {
|
|
154
|
+
body: 'msg'
|
|
155
|
+
},
|
|
156
|
+
getMessageFile: {
|
|
157
|
+
body: 'msg'
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
Object.keys(services).forEach(service => {
|
|
162
|
+
Object.keys(services[service]).forEach(prop => {
|
|
163
|
+
this[`${service}_${prop}`] = config[`${service}_${prop}`];
|
|
164
|
+
this[`${service}_${prop}Type`] = config[`${service}_${prop}Type`] || services[service][prop];
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
var node = this;
|
|
169
|
+
|
|
170
|
+
node.on('input', function (msg) {
|
|
171
|
+
var errorFlag = false;
|
|
172
|
+
var client = new lib.OpenaiApi();
|
|
173
|
+
if (!errorFlag && this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
|
|
174
|
+
if (this.service.secureApiKeyIsQuery) {
|
|
175
|
+
client.setApiKey(this.service.credentials.secureApiKeyValue,
|
|
176
|
+
this.service.secureApiKeyHeaderOrQueryName, true);
|
|
177
|
+
} else {
|
|
178
|
+
client.setApiKey(this.service.credentials.secureApiKeyValue,
|
|
179
|
+
this.service.secureApiKeyHeaderOrQueryName, false);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!errorFlag) {
|
|
184
|
+
client.body = msg.payload;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
var result;
|
|
188
|
+
|
|
189
|
+
if (!errorFlag) {
|
|
190
|
+
const serviceName = node.method; // Specify the service you want to process
|
|
191
|
+
let serviceParametersObject = {};
|
|
192
|
+
|
|
193
|
+
// Dynamically call the function based on the service name
|
|
194
|
+
const functionName = `${serviceName}`;
|
|
195
|
+
if (typeof client[functionName] === 'function') {
|
|
196
|
+
serviceParametersObject.body = msg.payload || {};
|
|
197
|
+
result = client[functionName](serviceParametersObject);
|
|
198
|
+
} else {
|
|
199
|
+
console.error(`Function ${functionName} does not exist on client.`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (!errorFlag && result === undefined) {
|
|
204
|
+
node.error('Method is not specified.', msg);
|
|
205
|
+
errorFlag = true;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
var setData = function (msg, response) {
|
|
209
|
+
if (response) {
|
|
210
|
+
if (response.status) {
|
|
211
|
+
msg.statusCode = response.status;
|
|
212
|
+
}
|
|
213
|
+
if (response.headers) {
|
|
214
|
+
msg.headers = response.headers;
|
|
215
|
+
}
|
|
216
|
+
if (response.config && response.config.url) {
|
|
217
|
+
msg.responseUrl = response.config.url;
|
|
218
|
+
}
|
|
219
|
+
if (response.data) {
|
|
220
|
+
msg.payload = response.data;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return msg;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
if (!errorFlag) {
|
|
227
|
+
node.status({ fill: 'blue', shape: 'dot', text: 'OpenaiApi.status.requesting' });
|
|
228
|
+
result.then(function (response) {
|
|
229
|
+
node.send(setData(msg, response));
|
|
230
|
+
node.status({});
|
|
231
|
+
}).catch(function (error) {
|
|
232
|
+
var message = error.message;
|
|
233
|
+
var errorData = error.response || {}; // Fallback to an empty object if response is not available
|
|
234
|
+
node.error(message, setData(msg, errorData));
|
|
235
|
+
node.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.error' });
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
RED.nodes.registerType('openai-api', OpenaiApiNode);
|
|
242
|
+
function OpenaiApiServiceNode(n) {
|
|
243
|
+
RED.nodes.createNode(this, n);
|
|
244
|
+
|
|
245
|
+
this.secureApiKeyValue = n.secureApiKeyValue;
|
|
246
|
+
this.secureApiKeyHeaderOrQueryName = n.secureApiKeyHeaderOrQueryName;
|
|
247
|
+
this.secureApiKeyIsQuery = n.secureApiKeyIsQuery;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
RED.nodes.registerType('openai-api-service', OpenaiApiServiceNode, {
|
|
251
|
+
credentials: {
|
|
252
|
+
secureApiKeyValue: { type: 'password' },
|
|
253
|
+
temp: { type: 'text' }
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inductiv/node-red-openai-api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node-RED node for interfacing with OpenAI platform services",
|
|
5
|
+
"main": "node.js",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=12.0.0"
|
|
8
|
+
},
|
|
9
|
+
"node-red": {
|
|
10
|
+
"version": ">=1.3.7",
|
|
11
|
+
"nodes": {
|
|
12
|
+
"openai": "node.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"openai",
|
|
17
|
+
"api",
|
|
18
|
+
"automation",
|
|
19
|
+
"chatgpt",
|
|
20
|
+
"llm",
|
|
21
|
+
"agent",
|
|
22
|
+
"automation",
|
|
23
|
+
"node-red",
|
|
24
|
+
"nodered",
|
|
25
|
+
"low-code",
|
|
26
|
+
"ai"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"axios": "^1.6.0",
|
|
30
|
+
"file-type": "^16.5.4",
|
|
31
|
+
"form-data": "^4.0.0",
|
|
32
|
+
"uuid": "^9.0.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"mocha": "9.2.1",
|
|
36
|
+
"node-red": "2.2.2",
|
|
37
|
+
"node-red-node-test-helper": "0.2.7"
|
|
38
|
+
},
|
|
39
|
+
"author": "Allan Bunch",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/allanbunch/node-red-openai-api.git"
|
|
44
|
+
}
|
|
45
|
+
}
|