@tiledesk/tiledesk-server 2.19.3 → 2.19.5

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.
@@ -21,6 +21,7 @@ const default_engine_hybrid = require('../config/kb/engine.hybrid');
21
21
  const default_embedding = require('../config/kb/embedding');
22
22
  const integrationService = require('./integrationService');
23
23
  const situatedContext = require('../config/kb/situatedContext');
24
+ const { MODELS_MULTIPLIER } = require('../utils/aiUtils');
24
25
 
25
26
  // Job managers
26
27
  let jobManager = new JobManager(AMQP_MANAGER_URL, {
@@ -94,7 +95,7 @@ class AiManager {
94
95
  this.saveBulk(operations, kbs, namespace.id_project, namespace.id).then( async (result) => {
95
96
  let hybrid = namespace.hybrid;
96
97
  let engine = namespace.engine || default_engine;
97
- let embedding = namespace.embedding || default_embedding;
98
+ let embedding = namespace.embedding || Object.assign({}, default_embedding);
98
99
  embedding.api_key = process.env.EMBEDDING_API_KEY || process.env.GPTKEY;
99
100
 
100
101
  let situated_context;
@@ -514,25 +515,6 @@ class AiManager {
514
515
  return true;
515
516
  }
516
517
 
517
- // from webhook
518
- // async scheduleScrape(resources) {
519
-
520
- // let scheduler = new Scheduler({ jobManager: jobManager });
521
-
522
- // resources.forEach(r => {
523
- // winston.debug("(Webhook) Schedule job with following data: ", r);
524
- // scheduler.trainSchedule(r, async (err, result) => {
525
- // if (err) {
526
- // winston.error("Scheduling error: ", err);
527
- // } else {
528
- // winston.info("Scheduling result: ", result);
529
- // }
530
- // });
531
- // })
532
-
533
- // return true;
534
- // }
535
-
536
518
  async startScrape(data) {
537
519
 
538
520
  if (!data.gptkey) {
@@ -625,6 +607,33 @@ class AiManager {
625
607
  : undefined;
626
608
  }
627
609
 
610
+ async getTrackingTokens({ qaResult, publicKey, model, obj, quoteManager, project }) {
611
+ if (publicKey !== true) {
612
+ return qaResult.prompt_token_size;
613
+ }
614
+ let modelKey;
615
+ if (typeof model === 'string') {
616
+ modelKey = model;
617
+ } else if (model && typeof model.name === 'string') {
618
+ modelKey = model.name;
619
+ }
620
+ let multiplier = MODELS_MULTIPLIER[modelKey];
621
+ if (!multiplier) {
622
+ multiplier = 1;
623
+ winston.info("No multiplier found for AI model (qa) " + modelKey);
624
+ }
625
+ obj.multiplier = multiplier;
626
+ obj.tokens = qaResult.prompt_token_size;
627
+ const incremented_key = await quoteManager.incrementTokenCount(project, obj);
628
+ winston.verbose("incremented_key: ", incremented_key);
629
+ const trackingTokens = obj.tokens * obj.multiplier;
630
+ if (qaResult != null && typeof qaResult.prompt_token_size === 'number') {
631
+ qaResult.legacy_prompt_token_size = qaResult.prompt_token_size;
632
+ qaResult.prompt_token_size = trackingTokens;
633
+ }
634
+ return trackingTokens;
635
+ }
636
+
628
637
  }
629
638
 
630
639
  const aiManager = new AiManager();
@@ -0,0 +1,98 @@
1
+ const { Namespace, AnsweredQuestion, UnansweredQuestion } = require('../models/kb_setting');
2
+ const winston = require('../config/winston');
3
+
4
+ class KbQuestionService {
5
+
6
+ constructor() {}
7
+
8
+ async createAnsweredQuestion(id_project, data) {
9
+ try {
10
+ const { namespace, question, answer, tokens, request_id } = data;
11
+
12
+ if (!namespace || !question || !answer) {
13
+ throw new Error({ status: 422, error: 'Missing required parameters: namespace, question and answer' });
14
+ }
15
+
16
+ const isValidNamespace = await this.validateNamespace(id_project, namespace);
17
+ if (!isValidNamespace) {
18
+ throw new Error({ status: 403, error: 'Not allowed. The namespace does not belong to the current project.' });
19
+ }
20
+
21
+ const answeredQuestion = new AnsweredQuestion({
22
+ id_project,
23
+ namespace,
24
+ question,
25
+ answer,
26
+ tokens,
27
+ request_id,
28
+ });
29
+
30
+ return await answeredQuestion.save();
31
+
32
+ } catch (error) {
33
+ throw error;
34
+ }
35
+ }
36
+
37
+ async createUnansweredQuestion(id_project, data) {
38
+ try {
39
+ const { namespace, question, request_id, sender } = data;
40
+
41
+ if (!namespace || !question) {
42
+ throw new Error({ status: 422, error: 'Missing required parameters: namespace and question' });
43
+ }
44
+
45
+ const isValidNamespace = await this.validateNamespace(id_project, namespace);
46
+ if (!isValidNamespace) {
47
+ throw new Error({ status: 403, error: 'Not allowed. The namespace does not belong to the current project.' });
48
+ }
49
+
50
+ const unansweredQuestion = new UnansweredQuestion({
51
+ id_project,
52
+ namespace,
53
+ question,
54
+ request_id,
55
+ sender,
56
+ });
57
+ return await unansweredQuestion.save();
58
+
59
+ } catch (error) {
60
+ throw error;
61
+ }
62
+ }
63
+
64
+ async validateNamespace(id_project, namespace_id) {
65
+ try {
66
+ const namespace = await Namespace.findOne({
67
+ id_project: id_project,
68
+ id: namespace_id
69
+ });
70
+ return !!namespace;
71
+ } catch (error) {
72
+ throw error;
73
+ }
74
+ }
75
+
76
+ async trackKbQaResult(id_project, { namespace, question }, answer, tokens) {
77
+ try {
78
+ if (answer.success === true) {
79
+ await this.createAnsweredQuestion(id_project, {
80
+ namespace,
81
+ question,
82
+ answer: answer.answer,
83
+ tokens,
84
+ });
85
+ } else if (answer.success === false) {
86
+ await this.createUnansweredQuestion(id_project, { namespace, question });
87
+ } else {
88
+ winston.warn('qa answer.success is not true or false: ', answer.success);
89
+ }
90
+ } catch (error) {
91
+ winston.error('qa err: ', error);
92
+ }
93
+ }
94
+
95
+ }
96
+
97
+ const kbQuestionService = new KbQuestionService();
98
+ module.exports = kbQuestionService;