dbm-graph-api 1.1.14 → 1.1.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbm-graph-api",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -13,7 +13,7 @@
13
13
  "dependencies": {
14
14
  "@aws-sdk/client-s3": "^3.741.0",
15
15
  "@aws-sdk/s3-request-presigner": "^3.741.0",
16
- "dbm": "^1.1.10",
16
+ "dbm": "^1.1.11",
17
17
  "mime": "^4.0.6",
18
18
  "sharp": "^0.33.5",
19
19
  "ws": "^8.18.0"
@@ -0,0 +1,69 @@
1
+ import Dbm from "dbm";
2
+
3
+ export default class HelpSectionSuggestions extends Dbm.core.BaseObject {
4
+ _construct() {
5
+ super._construct();
6
+ }
7
+
8
+ async getData(aData, aEncodeSession) {
9
+
10
+ await aEncodeSession.outputController.requireRole("admin");
11
+
12
+ let returnObject = {};
13
+
14
+ let content = aData["value"];
15
+ returnObject["content"] = content;
16
+
17
+ let database = Dbm.getInstance().repository.getItem("graphDatabase").controller;
18
+ let item = await database.createObject("private", ["aiGeneratedHelpSectionSuggestions"]);
19
+ await item.updateField("content", content);
20
+ returnObject["id"] = item.id;
21
+
22
+ let alreadyAdded = [
23
+ "All about this section"
24
+ ];
25
+
26
+ let alreadyAddedString = "The page already have these titles: " + JSON.stringify(alreadyAdded);
27
+
28
+ let contentString = "Content of the page: " + JSON.stringify(content);
29
+
30
+ let instructions = "Analyze the content and generate a list of questions and link titles that this page answers. Do the link titles as a call to action. {additionalInstructions} Respond with only a json object {helpSectionTitles: array of {question: string, linkTitle: string}}, no markdown.";
31
+ instructions = instructions.split("{data}").join(contentString);
32
+
33
+ let additionalInstructions = "";
34
+ instructions = instructions.split("{additionalInstructions}").join(additionalInstructions);
35
+
36
+ let body = {
37
+ "model": "gpt-4o-mini",
38
+ "response_format": { "type": "json_object" },
39
+ "messages": [
40
+ {"role":"system","content": instructions},
41
+ {"role": "user", "content": alreadyAddedString},
42
+ {"role": "user", "content": contentString}
43
+ ],
44
+ "temperature": 0.1
45
+ }
46
+
47
+ let headers = {
48
+ "Content-Type": "application/json",
49
+ 'Authorization': 'Bearer ' + Dbm.getInstance().repository.getItem("openAi").token
50
+ }
51
+
52
+ let response = await fetch('https://api.openai.com/v1/chat/completions', {
53
+ method: "POST",
54
+ headers: headers,
55
+ body: JSON.stringify(body),
56
+ });
57
+
58
+ let data = await response.json();
59
+ await item.updateField("response", data);
60
+
61
+ let message = data.choices[0].message;
62
+ let responseContent = JSON.parse(message.content);
63
+ console.log(responseContent);
64
+
65
+ returnObject["titles"] = responseContent["helpSectionTitles"];
66
+
67
+ return returnObject;
68
+ }
69
+ }
@@ -0,0 +1,94 @@
1
+ import Dbm from "dbm";
2
+ import DbmGraphApi from "../../../index.js";
3
+
4
+ export default class Question extends Dbm.core.BaseObject {
5
+ _construct() {
6
+ super._construct();
7
+ }
8
+
9
+ async getData(aData, aEncodeSession) {
10
+ let returnObject = {};
11
+
12
+ let question = aData["value"];
13
+ returnObject["question"] = question;
14
+
15
+ let database = Dbm.getInstance().repository.getItem("graphDatabase").controller;
16
+ let questionItem = await database.createObject("private", ["customerQuestion"]);
17
+ await questionItem.updateField("question", question);
18
+ returnObject["id"] = questionItem.id;
19
+
20
+ let selectQuery = new DbmGraphApi.range.Query();
21
+ await selectQuery.setObjectType("helpSection");
22
+ selectQuery.includePrivate();
23
+ let items = await selectQuery.getObjects();
24
+
25
+ let currentArray = items;
26
+ let currentArrayLength = currentArray.length;
27
+ let helpPages = new Array(currentArrayLength);
28
+
29
+ for(let i = 0; i < currentArrayLength; i++) {
30
+ let currentItem = currentArray[i];
31
+ let fields = await currentItem.getFields();
32
+
33
+ let codedObject = {id: currentItem.id, content: fields.title};
34
+ helpPages[i] = codedObject;
35
+ }
36
+
37
+ let helpPagesString = JSON.stringify(helpPages);
38
+
39
+ console.log(helpPages);
40
+
41
+ let instructions = "Find out which the best help pages for the question and return the id from this json: {data}. {additionalInstructions} Respond with only a json object {hasMatch: boolean, pages: array with ids of the pages (in order of relevance)}, no markdown.";
42
+ instructions = instructions.split("{data}").join(helpPagesString);
43
+
44
+ let additionalInstructions = "Units are stored in square feet, so convert square meters to feet.";
45
+ instructions = instructions.split("{additionalInstructions}").join(additionalInstructions);
46
+
47
+ let body = {
48
+ "model": "gpt-4o-mini",
49
+ "response_format": { "type": "json_object" },
50
+ "messages": [
51
+ {"role":"system","content": instructions},
52
+ {"role": "user", "content": question}
53
+ ],
54
+ "temperature": 0.7
55
+ }
56
+
57
+ let headers = {
58
+ "Content-Type": "application/json",
59
+ 'Authorization': 'Bearer ' + Dbm.getInstance().repository.getItem("openAi").token
60
+ }
61
+
62
+ let response = await fetch('https://api.openai.com/v1/chat/completions', {
63
+ method: "POST",
64
+ headers: headers,
65
+ body: JSON.stringify(body),
66
+ });
67
+
68
+ let data = await response.json();
69
+ await questionItem.updateField("response", data);
70
+
71
+ let message = data.choices[0].message;
72
+ let content = JSON.parse(message.content);
73
+ console.log(content);
74
+
75
+ let pages = [];
76
+ {
77
+ let currentArray = content.pages;
78
+ let currentArrayLength = currentArray.length;
79
+ for(let i = 0; i < currentArrayLength; i++) {
80
+ let currentId = 1*currentArray[i];
81
+
82
+ if(!isNaN(currentId)) {
83
+ pages.push(currentId);
84
+ await aEncodeSession.encodeSingle(currentId, "helpSection");
85
+ await questionItem.addIncomingRelation(currentId, "suggestedFor");
86
+ }
87
+ }
88
+ }
89
+
90
+ returnObject["answers"] = pages;
91
+
92
+ return returnObject;
93
+ }
94
+ }
@@ -2,8 +2,10 @@ export {default as Example} from "./Example.js";
2
2
  export {default as UploadS3} from "./UploadS3.js";
3
3
  export {default as FreeUrl} from "./FreeUrl.js";
4
4
  export {default as SeoSummary} from "./SeoSummary.js";
5
+ export {default as HelpSectionSuggestions} from "./HelpSectionSuggestions.js";
5
6
  export {default as AltText} from "./AltText.js";
6
7
  export {default as Breadcrumb} from "./Breadcrumb.js";
8
+ export {default as Question} from "./Question.js";
7
9
 
8
10
  export * as server from "./server/index.js";
9
11
 
@@ -181,6 +181,13 @@ let fullEncodeSetup = function() {
181
181
  currentEncode.item.register(encodePrefix + name);
182
182
  currentEncode.item.setValue("encodingType", name);
183
183
  }
184
+
185
+ {
186
+ let name = "helpSection";
187
+ let currentEncode = new DbmGraphApi.range.encode.HelpSection();
188
+ currentEncode.item.register(encodePrefix + name);
189
+ currentEncode.item.setValue("encodingType", name);
190
+ }
184
191
  }
185
192
 
186
193
  export {fullEncodeSetup};
@@ -197,9 +204,11 @@ let fullDataSetup = function() {
197
204
  registerDataFunction("example", new DbmGraphApi.data.Example());
198
205
 
199
206
  registerDataFunction("breadcrumb", new DbmGraphApi.data.Breadcrumb());
207
+ registerDataFunction("question", new DbmGraphApi.data.Question());
200
208
 
201
209
  registerDataFunction("admin/freeUrl", new DbmGraphApi.data.FreeUrl());
202
210
  registerDataFunction("admin/seoSummary", new DbmGraphApi.data.SeoSummary());
211
+ registerDataFunction("admin/helpSectionSuggestions", new DbmGraphApi.data.HelpSectionSuggestions());
203
212
  registerDataFunction("admin/altText", new DbmGraphApi.data.AltText());
204
213
 
205
214
  registerDataFunction("server/status", new DbmGraphApi.data.server.Status());
@@ -0,0 +1,21 @@
1
+ import Dbm from "dbm";
2
+ import EncodeBaseObject from "./EncodeBaseObject.js";
3
+
4
+ export default class HelpSection extends EncodeBaseObject {
5
+ _construct() {
6
+ super._construct();
7
+ }
8
+
9
+ async getEncodedData(aId, aEncodingSession) {
10
+
11
+ let returnObject = {};
12
+
13
+ let object = Dbm.getInstance().repository.getItem("graphDatabase").controller.getObject(aId);
14
+
15
+ let fields = await object.getFields();
16
+ returnObject["title"] = fields["title"] !== null ? fields["title"] : null;
17
+ returnObject["link"] = fields["link"] !== null ? fields["link"] : null;
18
+
19
+ return returnObject;
20
+ }
21
+ }
@@ -16,5 +16,6 @@ export {default as Relations} from "./Relations.js";
16
16
  export {default as ObjectTypes} from "./ObjectTypes.js";
17
17
  export {default as RepresentingPage} from "./RepresentingPage.js";
18
18
  export {default as PageRepresentation} from "./PageRepresentation.js";
19
+ export {default as HelpSection} from "./HelpSection.js";
19
20
 
20
21
  export * as admin from "./admin/index.js";