-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-OpenAI.js
More file actions
368 lines (332 loc) · 11.2 KB
/
MMM-OpenAI.js
File metadata and controls
368 lines (332 loc) · 11.2 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
class PostProcess {
constructor (callbacks) {
this._callbacks = callbacks
}
sendNotification(noti, payload) {
this._callbacks.sendNotification(noti, payload)
}
shellExec(scr) {
this._callbacks.shellExec(scr)
}
getModule(mName = null) {
var modules = this.getModules()
if (mName == null) mName = "MMM-OpenAI"
for (var i = 0; i < modules.length; i++) {
if (modules[i] && modules[i].name == mName) return modules[i]
}
}
getModules() {
return this._callbacks.getModules()
}
}
Module.register('MMM-OpenAI', {
defaultNotification: {
"OPENAI_REQUEST": (payload, sender) => {
const defaultObj = {
method: 'TEXT',
callback: null,
request: {},
prompt: null,
stealth: false,
}
let requested = {}
if (typeof payload === 'string') {
requested = {...defaultObj, ...{ prompt: payload }}
} else {
requested = {...defaultObj, ...payload}
}
if (requested.method !== 'CHAT' && requested.prompt && typeof requested.prompt === 'string') requested.request.prompt = requested.prompt
return requested
},
},
defaults: {
stealth: false,
defaultBehaviour: true,
imageAreaHeight: '400px',
resultLife: 10 * 60 * 1000,
method: 'TEXT', // 'IMAGE'
defaultRequest: {
text: {
model: 'text-davinci-003',
prompt: '',
n: 1, // Warning; consumeing tokens
max_tokens: 512, // Warning; consuming tokens
/* // You should know what you are doing.
temperature: 1, // 0~2
top_p: 0,
suffix: null,
stream: false, // Not prepared for this result, RESERVED. (Don't use it)
logprobs: null,
echo: false,
stop: "\n",
presence_penalty: 0,
best_of: 1, //Warning; consuming tokens
*/
},
image: {
prompt: '',
n: 1,
size: '512x512', // '256x256', '512x512'
response_format:'url', // 'b64_json'
},
chat: {
model: 'gpt-3.5-turbo',
messages: [],
max_tokens: 512,
/* // You should know what you are doing.
temperature: 1, // 0~2
top_p: 0,
suffix: null,
stream: false, // Not prepared for this result, RESERVED. (Don't use it)
logprobs: null,
echo: false,
stop: "\n",
presence_penalty: 0,
best_of: 1, //Warning; consuming tokens,
frequency_penalty: 0,
*/
}
},
notificationHook: {
'AI_EXAMPLE': {
method: 'TEXT',
request: {
prompt: 'Hello, there. Introduce yourself',
}
}
},
postProcessing: (processor, payload) => {
console.log('[OPENAI] Nothing on postprocessing. (default)')
},
telegramCommandText: 'txtai',
telegramCommandImage: 'imgai',
telegramCommandChat: 'chatai',
telegramChatReset: 'reset',
defaultChatInstruction: "Your name is Marvin and you are a paranoid android that reluctantly answers questions with sarcastic responses.",
},
getStyles: function () {
return ['MMM-OpenAI.css']
},
start: function() {
this.requestPool = new Map()
this.notificationSet = { ...this.config.notificationHook, ...this.defaultNotification }
this.resultTimer = null
this.postProcessor = new PostProcess({
sendNotification: (noti, payload) => {
this.sendNotification(noti, payload)
},
shellExec: (scr) => {
if (!scr) return false
this.sendSocketNotification("SHELL_EXEC", scr)
},
getModules: () => {
return MM.getModules()
},
})
},
getDom: function () {
let dom = document.createElement('div')
dom.classList.add('bodice', 'OpenAI')
if (!this.lastResponse) return dom
if (this.lastResponse.stealth) return dom
let {error, response, request, options, responseTime} = this.lastResponse
//if (error) return dom
let req = document.createElement('div')
req.classList.add('prompt')
req.innerHTML = options?.prompt
let res = document.createElement('div')
res.classList.add('response')
if (error) {
res.innerHTML = error?.error?.message ?? error
res.classList.add('responseError')
} else {
if (options.method === 'TEXT') {
res.innerHTML = response.choices[0].text.replace(/^\n\n/, '').replaceAll('\n', '<br/>')
res.classList.add('responseText')
} else if (options.method === 'IMAGE') {
let url = (response.data[0]?.url) ? response.data[0]?.url : `data:image/png;base64,${response.data[0].b64_json}`
res.style.backgroundImage = `url("${url}")`
res.style.setProperty('--imageAreaHeight', this.config.imageAreaHeight)
res.classList.add('responseImage')
} else if (options.method === 'CHAT') {
res.innerHTML = response.choices[0].message.content
res.classList.add('responseChat')
}
}
dom.appendChild(req)
dom.appendChild(res)
if (response.usage) {
const {prompt_tokens, completion_tokens, total_tokens} = response.usage
let usg = document.createElement('div')
usg.classList.add('response', 'responseUsage')
usg.innerHTML = `TOKENS : Prompt ${prompt_tokens}, Completion ${completion_tokens}, Total ${total_tokens}`
dom.appendChild(usg)
}
return dom
},
getCommands: function(commander) {
commander.add({
command: this.config.telegramCommandText,
description: 'Ask to OpenAI with text',
callback: 'command_text',
})
commander.add({
command: this.config.telegramCommandImage,
description: 'Request image to OpenAI',
callback: 'command_image'
})
let reset = new RegExp(`(^${this.config.telegramChatReset})?\\s?(.*)$`)
commander.add({
command: this.config.telegramCommandChat,
description: `ChatGPT implement. To start new dialog, type "\\ ${this.config.telegramCommandChat} new"`,
callback: 'command_chat',
args_pattern: [reset],
})
},
command_chat: function(command, handler) {
const newDialog = (handler, initPrompt = this.config.defaultChatInstruction) => {
handler.say('TEXT', 'New dialog session starts.')
this.tgDialog = [{
role: 'system',
content: initPrompt
}]
handler.say('TEXT', '<b>New Iinitializing</b> ' + initPrompt, {parse_mode: 'HTML'})
}
if (!handler?.args?.[0]) {
newDialog(handler)
return
}
let [ignore, reset, prompt] = handler.args[0]
if (reset) {
newDialog(handler, prompt || this.config.defaultChatInstruction)
return
}
if (!this.tgDialog) newDialog(handler)
this.tgDialog = [...this.tgDialog, {
role: 'user',
content: prompt
}]
let request = { messages: [...this.tgDialog] }
this.request({
method: 'CHAT',
requestable: request,
handler,
})
},
command_text: function(command, handler) {
if (handler.args) {
let request = { prompt: handler.args }
this.request({
method: 'TEXT',
requestable: request,
handler,
})
} else {
handler.reply('There is no prompt to ask to OpenAI.')
}
},
command_image: function(command, handler) {
if (handler.args) {
let request = { prompt: handler.args }
this.request({
method: 'IMAGE',
requestable: request,
handler,
})
} else {
handler.reply('There is no prompt to ask to OpenAI.')
}
},
request: function({method, requestable, handler = null, stealth = this.config.stealth}) {
const methods = ['TEXT', 'IMAGE', 'CHAT']
let t = (methods.includes(method)) ? method.toLowerCase() : false
if (!t) return false
let id = Date.now()
if (handler) this.requestPool.set(id, handler)
let prompt = requestable?.prompt
if (method === 'CHAT') {
prompt = requestable.messages?.findLast((m) => {
return (m.role === 'user')
})?.content
if (!prompt) {
console.log('[OPENAI] Invalid requestable payload', requestable)
if (typeof requestable?.callback === 'function') requestable.callback(false)
return
}
}
let payload = {
request: { ...this.config.defaultRequest[t], ...requestable },
options: {id, method, stealth, prompt}
}
this.sendSocketNotification('REQUEST', payload)
return true
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'RESPONSE') {
if (payload.options?.id && this.requestPool.has(payload.options.id)) {
let handler = this.requestPool.get(payload.options.id)
if (payload.error) {
//payload.response = payload.error?.error?.message ?? payload.error
console.log('[OPENAI] Error:', payload.error)
}
if (typeof handler === 'function') {
handler(payload)
} else if (typeof handler?.reply === 'function') {
if (payload.error) {
let em = payload.error?.error?.message ?? 'Something wrong, see the log.'
console.log('[OPENAI] ERROR : ' + em )
handler.reply('TEXT', em)
} else if (payload.options.method === 'IMAGE') {
handler.reply('PHOTO_URL', payload.response.data[0].url, { caption: payload.request.prompt })
} else if (payload.options.method === 'TEXT'){
handler.reply('TEXT', payload.response.choices[0].text)
} else {
let message = payload.response.choices[0].message
handler.reply('TEXT', message.content)
this.tgDialog.push(message)
}
} else {
console.log('[OPENAI] Invalid Handler)', payload.response)
}
this.requestPool.delete(payload.options.id)
} else {
console.log('[OPENAI] Response without handler (Usually timedout or missing callback)', payload.response)
}
if (typeof this.config.postProcessing === 'function') {
this.config.postProcessing(this.postProcessor, payload)
}
this.lastResponse = payload
if (!payload.stealth) this.updateDom(500)
if (this.config.resultLife > 0) {
clearTimeout(this.resultTimer)
this.resultTimer = setTimeout(() => {
this.lastResponse = null
this.updateDom(500)
}, this.config.resultLife)
}
} // RESPONSE
},
notificationReceived: function (notification, payload, sender) {
for(const [key, factory] of Object.entries(this.notificationSet)) {
if (notification === key) {
let requestable = (typeof factory === 'function') ? factory(payload, sender) : ((typeof factory === 'object') ? factory : {})
if (!(requestable?.request)) {
console.log('[OPENAI] Invalid payload', payload)
if (typeof requestable?.callback === 'function') requestable.callback(false)
return
}
let r = this.request({
method: requestable?.method ?? null,
requestable: requestable.request,
handler: requestable?.callback ?? null,
stealth: requestable?.stealth || this.config.stealth,
})
if (!r) {
if (typeof payload.callback === 'function') payload.callback(false)
this.sendNotification('OPENAI_REQUEST_FAILED_BY_NOTIFICATION', { payload, sender: sender.identifier })
}
return
}
}
}
})