forked from node-red-jp/node-red-contrib-plugin-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.js
32 lines (31 loc) · 1.35 KB
/
chatgpt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module.exports = async function (RED) {
const { got } = await import('got');
const { Configuration, OpenAIApi } = require('openai');
RED.httpAdmin.post('/chatgpt', RED.auth.needsPermission('notify.write'), async function (req, res) {
try {
const { body } = await got('http://127.0.0.1:1880/flows');
if (!body) {
throw new Error('cannot get flow data');
}
const openai = new OpenAIApi(new Configuration({
organization: process.env.OPENAI_ORG,
apiKey: process.env.OPENAI_API_KEY,
}));
const request = {
model: "gpt-3.5-turbo",
messages: [{
role: "user",
content: "Write a Markdown document with a mermaid sequence diagram without JSON data to describe the following Node-RED flow.\n\n" + body
}]
};
console.log(JSON.stringify(request));
const completion = await openai.createChatCompletion(request);
res.json({ body: completion.data.choices[0].message.content });
console.log(JSON.stringify(completion.data));
} catch (e) {
RED.log.error(e);
res.json({ body: e.toString() });
}
});
RED.plugins.registerPlugin('chatgpt', { type: 'chatgpt' });
};