@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.1
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/CHANGELOG.md +387 -1
- package/ExtApi.js +6 -7
- package/Logger.js +74 -0
- package/TdCache.js +81 -176
- package/TdCache_v3.js +261 -0
- package/TiledeskExpression.js +7 -3
- package/TiledeskServices/AIService.js +43 -0
- package/TiledeskServices/utils.js +99 -0
- package/index.js +305 -50
- package/logs/app.log +279 -0
- package/models/IntentsMachineFactory.js +5 -2
- package/models/MockBotsDataSource.js +19 -11
- package/models/TiledeskChatbot.js +97 -79
- package/models/TiledeskChatbotConst.js +12 -17
- package/models/TiledeskChatbotUtil.js +359 -109
- package/models/TiledeskIntentsMachine.js +1 -1
- package/models/faqKbService.js +1 -1
- package/package.json +8 -6
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
- package/tiledeskChatbotPlugs/Filler.js +13 -2
- package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
- package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
- package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
- package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
- package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +182 -0
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
- package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
- package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
- package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -37
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
- package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +135 -0
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
- package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
- package/tiledeskChatbotPlugs/directives/DirReply.js +53 -9
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
- package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
- package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
- /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
|
@@ -3,6 +3,9 @@ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
|
|
|
3
3
|
const { Filler } = require('../Filler');
|
|
4
4
|
let https = require("https");
|
|
5
5
|
const { DirIntent } = require("./DirIntent");
|
|
6
|
+
const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
|
|
7
|
+
const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
|
|
8
|
+
const assert = require("assert");
|
|
6
9
|
require('dotenv').config();
|
|
7
10
|
|
|
8
11
|
class DirAskGPTV2 {
|
|
@@ -12,9 +15,11 @@ class DirAskGPTV2 {
|
|
|
12
15
|
throw new Error('context object is mandatory');
|
|
13
16
|
}
|
|
14
17
|
this.context = context;
|
|
18
|
+
this.chatbot = context.chatbot;
|
|
15
19
|
this.tdcache = this.context.tdcache;
|
|
16
20
|
this.requestId = this.context.requestId;
|
|
17
21
|
this.intentDir = new DirIntent(context);
|
|
22
|
+
this.API_ENDPOINT = this.context.API_ENDPOINT;
|
|
18
23
|
this.log = context.log;
|
|
19
24
|
}
|
|
20
25
|
|
|
@@ -57,11 +62,25 @@ class DirAskGPTV2 {
|
|
|
57
62
|
|
|
58
63
|
// default values
|
|
59
64
|
let answer = "No answers";
|
|
60
|
-
let
|
|
65
|
+
let namespace = this.context.projectId;
|
|
61
66
|
let model = "gpt-3.5-turbo";
|
|
62
67
|
let temperature;
|
|
63
68
|
let max_tokens;
|
|
64
69
|
let top_k;
|
|
70
|
+
let transcript;
|
|
71
|
+
let citations = false;
|
|
72
|
+
let engine;
|
|
73
|
+
//let default_context = "You are an helpful assistant for question-answering tasks.\nUse ONLY the following pieces of retrieved context to answer the question.\nIf you don't know the answer, just say that you don't know.\nIf none of the retrieved context answer the question, add this word to the end <NOANS>\n\n{context}";
|
|
74
|
+
|
|
75
|
+
let contexts = {
|
|
76
|
+
"gpt-3.5-turbo": "You are an helpful assistant for question-answering tasks.\nUse ONLY the pieces of retrieved context delimited by #### to answer the question.\nIf you don't know the answer, just say: \"I don't know<NOANS>\"\n\n####{context}####",
|
|
77
|
+
"gpt-4": "You are an helpful assistant for question-answering tasks.\nUse ONLY the pieces of retrieved context delimited by #### to answer the question.\nIf you don't know the answer, just say that you don't know.\nIf and only if none of the retrieved context is useful for your task, add this word to the end <NOANS>\n\n####{context}####",
|
|
78
|
+
"gpt-4-turbo-preview": "You are an helpful assistant for question-answering tasks.\nUse ONLY the pieces of retrieved context delimited by #### to answer the question.\nIf you don't know the answer, just say that you don't know.\nIf and only if none of the retrieved context is useful for your task, add this word to the end <NOANS>\n\n####{context}####",
|
|
79
|
+
"gpt-4o": "You are an helpful assistant for question-answering tasks. Follow these steps carefully:\n1. Answer in the same language of the user question, regardless of the retrieved context language\n2. Use ONLY the pieces of the retrieved context to answer the question.\n3. If the retrieved context does not contain sufficient information to generate an accurate and informative answer, return <NOANS>\n\n==Retrieved context start==\n{context}\n==Retrieved context end==",
|
|
80
|
+
"gpt-4o-mini": "You are an helpful assistant for question-answering tasks. Follow these steps carefully:\n1. Answer in the same language of the user question, regardless of the retrieved context language\n2. Use ONLY the pieces of the retrieved context to answer the question.\n3. If the retrieved context does not contain sufficient information to generate an accurate and informative answer, return <NOANS>\n\n==Retrieved context start==\n{context}\n==Retrieved context end=="
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let source = null;
|
|
65
84
|
|
|
66
85
|
if (!action.question || action.question === '') {
|
|
67
86
|
console.error("Error: DirAskGPT question attribute is mandatory. Executing condition false...");
|
|
@@ -73,6 +92,9 @@ class DirAskGPTV2 {
|
|
|
73
92
|
return;
|
|
74
93
|
}
|
|
75
94
|
|
|
95
|
+
if (action.namespace) {
|
|
96
|
+
namespace = action.namespace;
|
|
97
|
+
}
|
|
76
98
|
if (action.model) {
|
|
77
99
|
model = action.model;
|
|
78
100
|
}
|
|
@@ -89,6 +111,10 @@ class DirAskGPTV2 {
|
|
|
89
111
|
max_tokens = action.max_tokens;
|
|
90
112
|
}
|
|
91
113
|
|
|
114
|
+
if (action.citations) {
|
|
115
|
+
citations = action.citations;
|
|
116
|
+
}
|
|
117
|
+
|
|
92
118
|
let requestVariables = null;
|
|
93
119
|
requestVariables =
|
|
94
120
|
await TiledeskChatbot.allParametersStatic(
|
|
@@ -99,18 +125,25 @@ class DirAskGPTV2 {
|
|
|
99
125
|
const filled_question = filler.fill(action.question, requestVariables);
|
|
100
126
|
const filled_context = filler.fill(action.context, requestVariables)
|
|
101
127
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
128
|
+
if (action.history) {
|
|
129
|
+
let transcript_string = await TiledeskChatbot.getParameterStatic(
|
|
130
|
+
this.context.tdcache,
|
|
131
|
+
this.context.requestId,
|
|
132
|
+
TiledeskChatbotConst.REQ_TRANSCRIPT_KEY
|
|
133
|
+
)
|
|
134
|
+
if (this.log) { console.log("DirAskGPT transcript string: ", transcript_string) }
|
|
135
|
+
|
|
136
|
+
transcript = await TiledeskChatbotUtil.transcriptJSON(transcript_string);
|
|
137
|
+
if (this.log) { console.log("DirAskGPT transcript ", transcript) }
|
|
108
138
|
}
|
|
109
139
|
|
|
110
|
-
|
|
140
|
+
const kb_endpoint = process.env.KB_ENDPOINT_QA
|
|
141
|
+
if (this.log) { console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint); }
|
|
142
|
+
|
|
143
|
+
let key = await this.getKeyFromIntegrations();
|
|
111
144
|
if (!key) {
|
|
112
145
|
if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
|
|
113
|
-
key = await this.getKeyFromKbSettings(
|
|
146
|
+
key = await this.getKeyFromKbSettings();
|
|
114
147
|
}
|
|
115
148
|
|
|
116
149
|
if (!key) {
|
|
@@ -132,10 +165,52 @@ class DirAskGPTV2 {
|
|
|
132
165
|
}
|
|
133
166
|
|
|
134
167
|
if (publicKey === true) {
|
|
135
|
-
let keep_going = await this.checkQuoteAvailability(
|
|
168
|
+
let keep_going = await this.checkQuoteAvailability();
|
|
136
169
|
if (keep_going === false) {
|
|
137
170
|
if (this.log) { console.log("DirAskGPT - Quota exceeded for tokens. Skip the action")}
|
|
138
|
-
|
|
171
|
+
await this.chatbot.addParameter("flowError", "AskGPT Error: tokens quota exceeded");
|
|
172
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
173
|
+
callback(true);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let ns;
|
|
179
|
+
|
|
180
|
+
if (action.namespaceAsName) {
|
|
181
|
+
// Namespace could be an attribute
|
|
182
|
+
const filled_namespace = filler.fill(action.namespace, requestVariables)
|
|
183
|
+
ns = await this.getNamespace(filled_namespace, null);
|
|
184
|
+
namespace = ns?.id;
|
|
185
|
+
if (this.log) { console.log("DirAskGPT - Retrieved namespace id from name ", namespace); }
|
|
186
|
+
} else {
|
|
187
|
+
ns = await this.getNamespace(null, namespace);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!ns) {
|
|
191
|
+
await this.#assignAttributes(action, answer);
|
|
192
|
+
await this.chatbot.addParameter("flowError", "AskGPT Error: namespace not found");
|
|
193
|
+
if (falseIntent) {
|
|
194
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
195
|
+
callback(true);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
callback();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (ns.engine) {
|
|
203
|
+
engine = ns.engine;
|
|
204
|
+
} else {
|
|
205
|
+
engine = await this.setDefaultEngine()
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (!namespace) {
|
|
209
|
+
console.log("DirAskGPT - Error: namespace is undefined")
|
|
210
|
+
if (falseIntent) {
|
|
211
|
+
await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
|
|
212
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
213
|
+
callback(true);
|
|
139
214
|
return;
|
|
140
215
|
}
|
|
141
216
|
}
|
|
@@ -143,8 +218,10 @@ class DirAskGPTV2 {
|
|
|
143
218
|
let json = {
|
|
144
219
|
question: filled_question,
|
|
145
220
|
gptkey: key,
|
|
146
|
-
namespace:
|
|
147
|
-
model: model
|
|
221
|
+
namespace: namespace,
|
|
222
|
+
model: model,
|
|
223
|
+
citations: citations,
|
|
224
|
+
engine: engine
|
|
148
225
|
};
|
|
149
226
|
if (top_k) {
|
|
150
227
|
json.top_k = top_k;
|
|
@@ -155,13 +232,24 @@ class DirAskGPTV2 {
|
|
|
155
232
|
if (max_tokens) {
|
|
156
233
|
json.max_tokens = max_tokens;
|
|
157
234
|
}
|
|
158
|
-
|
|
159
|
-
|
|
235
|
+
|
|
236
|
+
if (!action.advancedPrompt) {
|
|
237
|
+
if (filled_context) {
|
|
238
|
+
json.system_context = filled_context + "\n" + contexts[model];
|
|
239
|
+
} else {
|
|
240
|
+
json.system_context = contexts[model];
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
json.system_context = filled_context;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (transcript) {
|
|
247
|
+
json.chat_history_dict = await this.transcriptToLLM(transcript);
|
|
160
248
|
}
|
|
249
|
+
|
|
161
250
|
if (this.log) { console.log("DirAskGPT json:", json); }
|
|
162
251
|
|
|
163
252
|
const HTTPREQUEST = {
|
|
164
|
-
// url: server_base_url + "/" + this.context.projectId + "/kb/qa",
|
|
165
253
|
url: kb_endpoint + "/qa",
|
|
166
254
|
headers: {
|
|
167
255
|
'Content-Type': 'application/json',
|
|
@@ -178,11 +266,13 @@ class DirAskGPTV2 {
|
|
|
178
266
|
console.log("DirAskGPT error: ", err);
|
|
179
267
|
}
|
|
180
268
|
if (this.log) { console.log("DirAskGPT resbody:", resbody); }
|
|
181
|
-
|
|
182
|
-
let
|
|
183
|
-
|
|
269
|
+
|
|
270
|
+
// let answer = resbody.answer;
|
|
271
|
+
// let source = resbody.source;
|
|
272
|
+
// await this.#assignAttributes(action, answer, source);
|
|
184
273
|
|
|
185
274
|
if (err) {
|
|
275
|
+
await this.#assignAttributes(action, answer, source);
|
|
186
276
|
if (callback) {
|
|
187
277
|
if (falseIntent) {
|
|
188
278
|
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
@@ -195,9 +285,13 @@ class DirAskGPTV2 {
|
|
|
195
285
|
}
|
|
196
286
|
else if (resbody.success === true) {
|
|
197
287
|
|
|
288
|
+
await this.#assignAttributes(action, resbody.answer, resbody.source);
|
|
198
289
|
if (publicKey === true) {
|
|
199
|
-
let
|
|
200
|
-
|
|
290
|
+
let tokens_usage = {
|
|
291
|
+
tokens: resbody.prompt_token_size,
|
|
292
|
+
model: json.model
|
|
293
|
+
}
|
|
294
|
+
this.updateQuote(tokens_usage);
|
|
201
295
|
}
|
|
202
296
|
|
|
203
297
|
if (trueIntent) {
|
|
@@ -208,6 +302,7 @@ class DirAskGPTV2 {
|
|
|
208
302
|
callback();
|
|
209
303
|
return;
|
|
210
304
|
} else {
|
|
305
|
+
await this.#assignAttributes(action, answer, source);
|
|
211
306
|
if (falseIntent) {
|
|
212
307
|
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
213
308
|
callback(true);
|
|
@@ -325,18 +420,18 @@ class DirAskGPTV2 {
|
|
|
325
420
|
}
|
|
326
421
|
})
|
|
327
422
|
.catch((error) => {
|
|
328
|
-
|
|
423
|
+
console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
|
|
329
424
|
if (callback) {
|
|
330
425
|
callback(error, null);
|
|
331
426
|
}
|
|
332
427
|
});
|
|
333
428
|
}
|
|
334
429
|
|
|
335
|
-
async getKeyFromIntegrations(
|
|
430
|
+
async getKeyFromIntegrations() {
|
|
336
431
|
return new Promise((resolve) => {
|
|
337
432
|
|
|
338
433
|
const INTEGRATIONS_HTTPREQUEST = {
|
|
339
|
-
url:
|
|
434
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
|
|
340
435
|
headers: {
|
|
341
436
|
'Content-Type': 'application/json',
|
|
342
437
|
'Authorization': 'JWT ' + this.context.token
|
|
@@ -364,11 +459,11 @@ class DirAskGPTV2 {
|
|
|
364
459
|
})
|
|
365
460
|
}
|
|
366
461
|
|
|
367
|
-
async getKeyFromKbSettings(
|
|
462
|
+
async getKeyFromKbSettings() {
|
|
368
463
|
return new Promise((resolve) => {
|
|
369
464
|
|
|
370
465
|
const KB_HTTPREQUEST = {
|
|
371
|
-
url:
|
|
466
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
|
|
372
467
|
headers: {
|
|
373
468
|
'Content-Type': 'application/json',
|
|
374
469
|
'Authorization': 'JWT ' + this.context.token
|
|
@@ -394,11 +489,11 @@ class DirAskGPTV2 {
|
|
|
394
489
|
})
|
|
395
490
|
}
|
|
396
491
|
|
|
397
|
-
async checkQuoteAvailability(
|
|
492
|
+
async checkQuoteAvailability() {
|
|
398
493
|
return new Promise((resolve) => {
|
|
399
494
|
|
|
400
495
|
const HTTPREQUEST = {
|
|
401
|
-
url:
|
|
496
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/tokens",
|
|
402
497
|
headers: {
|
|
403
498
|
'Content-Type': 'application/json',
|
|
404
499
|
'Authorization': 'JWT ' + this.context.token
|
|
@@ -424,16 +519,16 @@ class DirAskGPTV2 {
|
|
|
424
519
|
})
|
|
425
520
|
}
|
|
426
521
|
|
|
427
|
-
async updateQuote(
|
|
428
|
-
return new Promise((resolve) => {
|
|
522
|
+
async updateQuote(tokens_usage) {
|
|
523
|
+
return new Promise((resolve, reject) => {
|
|
429
524
|
|
|
430
525
|
const HTTPREQUEST = {
|
|
431
|
-
url:
|
|
526
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
|
|
432
527
|
headers: {
|
|
433
528
|
'Content-Type': 'application/json',
|
|
434
529
|
'Authorization': 'JWT ' + this.context.token
|
|
435
530
|
},
|
|
436
|
-
json:
|
|
531
|
+
json: tokens_usage,
|
|
437
532
|
method: "POST"
|
|
438
533
|
}
|
|
439
534
|
if (this.log) { console.log("DirAskGPT check quote availability HTTPREQUEST", HTTPREQUEST); }
|
|
@@ -442,9 +537,9 @@ class DirAskGPTV2 {
|
|
|
442
537
|
HTTPREQUEST, async (err, resbody) => {
|
|
443
538
|
if (err) {
|
|
444
539
|
console.error("(httprequest) DirAskGPT Increment tokens quote err: ", err);
|
|
445
|
-
|
|
540
|
+
reject(false)
|
|
446
541
|
} else {
|
|
447
|
-
console.log("(httprequest) DirAskGPT Increment token quote resbody: ", resbody);
|
|
542
|
+
// console.log("(httprequest) DirAskGPT Increment token quote resbody: ", resbody);
|
|
448
543
|
resolve(true);
|
|
449
544
|
}
|
|
450
545
|
}
|
|
@@ -452,6 +547,98 @@ class DirAskGPTV2 {
|
|
|
452
547
|
})
|
|
453
548
|
}
|
|
454
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Transforms the transcirpt array in a dictionary like '0': { "question": "xxx", "answer":"xxx"}
|
|
552
|
+
* merging consecutive messages with the same role in a single question or answer.
|
|
553
|
+
* If the first message was sent from assistant, this will be deleted.
|
|
554
|
+
*/
|
|
555
|
+
async transcriptToLLM(transcript) {
|
|
556
|
+
|
|
557
|
+
let objectTranscript = {};
|
|
558
|
+
|
|
559
|
+
if (transcript.length === 0) {
|
|
560
|
+
return objectTranscript;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
let mergedTranscript = [];
|
|
564
|
+
let current = transcript[0];
|
|
565
|
+
|
|
566
|
+
for (let i = 1; i < transcript.length; i++) {
|
|
567
|
+
if (transcript[i].role === current.role) {
|
|
568
|
+
current.content += '\n' + transcript[i].content;
|
|
569
|
+
} else {
|
|
570
|
+
mergedTranscript.push(current);
|
|
571
|
+
current = transcript[i]
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
mergedTranscript.push(current);
|
|
575
|
+
|
|
576
|
+
if (mergedTranscript[0].role === 'assistant') {
|
|
577
|
+
mergedTranscript.splice(0, 1)
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
let counter = 0;
|
|
581
|
+
for (let i = 0; i < mergedTranscript.length - 1; i += 2) {
|
|
582
|
+
// Check if [i] is role user and [i+1] is role assistant??
|
|
583
|
+
assert(mergedTranscript[i].role === 'user');
|
|
584
|
+
assert(mergedTranscript[i+1].role === 'assistant');
|
|
585
|
+
|
|
586
|
+
if (!mergedTranscript[i].content.startsWith('/')) {
|
|
587
|
+
objectTranscript[counter] = {
|
|
588
|
+
question: mergedTranscript[i].content,
|
|
589
|
+
answer: mergedTranscript[i+1].content
|
|
590
|
+
}
|
|
591
|
+
counter++;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
return objectTranscript;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
async getNamespace(name, id) {
|
|
599
|
+
return new Promise((resolve) => {
|
|
600
|
+
const HTTPREQUEST = {
|
|
601
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/kb/namespace/all",
|
|
602
|
+
headers: {
|
|
603
|
+
'Content-Type': 'application/json',
|
|
604
|
+
'Authorization': 'JWT ' + this.context.token
|
|
605
|
+
},
|
|
606
|
+
method: "GET"
|
|
607
|
+
}
|
|
608
|
+
if (this.log) { console.log("DirAskGPT get all namespaces HTTPREQUEST", HTTPREQUEST); }
|
|
609
|
+
this.#myrequest(
|
|
610
|
+
HTTPREQUEST, async (err, namespaces) => {
|
|
611
|
+
if (err) {
|
|
612
|
+
console.error("(httprequest) DirAskGPT get all namespaces err: ", err);
|
|
613
|
+
resolve(null)
|
|
614
|
+
} else {
|
|
615
|
+
if (this.log) { console.log("(httprequest) DirAskGPT get all namespaces resbody: ", namespaces); }
|
|
616
|
+
if (name) {
|
|
617
|
+
let namespace = namespaces.find(n => n.name === name);
|
|
618
|
+
resolve(namespace);
|
|
619
|
+
} else {
|
|
620
|
+
let namespace = namespaces.find(n => n.id === id);
|
|
621
|
+
resolve(namespace);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
)
|
|
627
|
+
})
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async setDefaultEngine() {
|
|
631
|
+
return new Promise((resolve) => {
|
|
632
|
+
let engine = {
|
|
633
|
+
name: "pinecone",
|
|
634
|
+
type: process.env.PINECONE_TYPE,
|
|
635
|
+
apikey: "",
|
|
636
|
+
vector_size: 1536,
|
|
637
|
+
index_name: process.env.PINECONE_INDEX
|
|
638
|
+
}
|
|
639
|
+
resolve(engine);
|
|
640
|
+
})
|
|
641
|
+
}
|
|
455
642
|
|
|
456
643
|
}
|
|
457
644
|
|
|
@@ -10,17 +10,6 @@ class DirAssign {
|
|
|
10
10
|
throw new Error('context object is mandatory.');
|
|
11
11
|
}
|
|
12
12
|
this.context = context;
|
|
13
|
-
// let context = {
|
|
14
|
-
// projectId: projectId,
|
|
15
|
-
// token: token,
|
|
16
|
-
// supportRequest: supportRequest,
|
|
17
|
-
// requestId: supportRequest.request_id,
|
|
18
|
-
// TILEDESK_APIURL: API_URL,
|
|
19
|
-
// TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
|
|
20
|
-
// departmentId: depId,
|
|
21
|
-
// tdcache: tdcache,
|
|
22
|
-
// log: false
|
|
23
|
-
// }
|
|
24
13
|
this.log = context.log;
|
|
25
14
|
}
|
|
26
15
|
|
|
@@ -9,27 +9,17 @@ class DirAssignFromFunction {
|
|
|
9
9
|
throw new Error('context object is mandatory.');
|
|
10
10
|
}
|
|
11
11
|
this.context = context;
|
|
12
|
-
// let context = {
|
|
13
|
-
// projectId: projectId,
|
|
14
|
-
// token: token,
|
|
15
|
-
// supportRequest: supportRequest,
|
|
16
|
-
// requestId: supportRequest.request_id,
|
|
17
|
-
// TILEDESK_APIURL: API_URL,
|
|
18
|
-
// TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
|
|
19
|
-
// departmentId: depId,
|
|
20
|
-
// tdcache: tdcache,
|
|
21
|
-
// log: false
|
|
22
|
-
// }
|
|
23
|
-
this.tdclient = context.tdclient;
|
|
24
|
-
// new TiledeskClient({
|
|
25
|
-
// projectId: context.projectId,
|
|
26
|
-
// token: context.token,
|
|
27
|
-
// APIURL: context.TILEDESK_APIURL,
|
|
28
|
-
// APIKEY: "___",
|
|
29
|
-
// log: context.log
|
|
30
|
-
// });
|
|
31
12
|
this.log = context.log;
|
|
32
13
|
this.tdcache = context.tdcache;
|
|
14
|
+
this.API_ENDPOINT = context.API_ENDPOINT;
|
|
15
|
+
|
|
16
|
+
this.tdClient = new TiledeskClient({
|
|
17
|
+
projectId: this.context.projectId,
|
|
18
|
+
token: this.context.token,
|
|
19
|
+
APIURL: this.API_ENDPOINT,
|
|
20
|
+
APIKEY: "___",
|
|
21
|
+
log: this.log
|
|
22
|
+
});
|
|
33
23
|
}
|
|
34
24
|
|
|
35
25
|
async execute(directive, callback) {
|
|
@@ -77,7 +67,7 @@ class DirAssignFromFunction {
|
|
|
77
67
|
async invoke(functionName, callback) {
|
|
78
68
|
switch (functionName) {
|
|
79
69
|
case "openNow":
|
|
80
|
-
this.
|
|
70
|
+
this.tdClient.openNow((err, result) => {
|
|
81
71
|
if (this.log) {console.log("openNow():", result);}
|
|
82
72
|
if (err) {
|
|
83
73
|
callback(err);
|
|
@@ -91,7 +81,7 @@ class DirAssignFromFunction {
|
|
|
91
81
|
});
|
|
92
82
|
break;
|
|
93
83
|
case "availableAgents":
|
|
94
|
-
this.
|
|
84
|
+
this.tdClient.getProjectAvailableAgents((err, agents) => {
|
|
95
85
|
if (this.log) {console.log("Agents on 'open'", agents);}
|
|
96
86
|
if (err || !agents) {
|
|
97
87
|
console.error("Error getting available agents in DirWhenAvailableAgents", err);
|