only_ever_generator 0.2.6 → 0.2.7
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/dist/bootstrap/app.js +47 -44
- package/dist/card_gen/generate_cards.js +46 -32
- package/dist/logger.js +41 -0
- package/dist/parse/parse_source_content.js +23 -9
- package/dist/typology_gen/generate_typology.js +21 -12
- package/dist/utils/generate_args.js +1 -11
- package/package.json +1 -1
- package/src/bootstrap/app.ts +70 -57
- package/src/card_gen/generate_cards.ts +13 -1
- package/src/index.ts +1 -1
- package/src/logger.ts +31 -0
- package/src/parse/parse_source_content.ts +23 -10
- package/src/services/open_ai_service.ts +1 -0
- package/src/typology_gen/generate_typology.ts +7 -0
- package/src/utils/generate_args.ts +2 -11
package/dist/bootstrap/app.js
CHANGED
|
@@ -20,13 +20,10 @@ const generate_args_1 = require("../utils/generate_args");
|
|
|
20
20
|
const calculate_gap_fill_1 = require("../gap_fill/calculate_gap_fill");
|
|
21
21
|
/// OnlyEverGenerator
|
|
22
22
|
class OnlyEverGenerator {
|
|
23
|
-
constructor(apiKey, model, generationContent
|
|
24
|
-
// prompt: any,
|
|
25
|
-
// content: any,
|
|
26
|
-
// expected_fields: Array<string>
|
|
27
|
-
) {
|
|
23
|
+
constructor(apiKey, model, generationContent) {
|
|
28
24
|
this.api_key = "";
|
|
29
|
-
|
|
25
|
+
/// these fields will be populated inside the constructor
|
|
26
|
+
this.parsedContent = {};
|
|
30
27
|
this.promptForTypology = "";
|
|
31
28
|
this.promptForCardGen = "";
|
|
32
29
|
this.typologyResponse = {};
|
|
@@ -35,72 +32,78 @@ class OnlyEverGenerator {
|
|
|
35
32
|
this.gapFillResponse = {};
|
|
36
33
|
this.api_key = apiKey;
|
|
37
34
|
this.openAiService = new open_ai_service_1.OpenAiService(apiKey, model !== null && model !== void 0 ? model : "gpt-3.5-turbo-1106");
|
|
38
|
-
|
|
35
|
+
const parsedData = new parse_source_content_1.ParseSourceContent(generationContent.content).parseData();
|
|
36
|
+
this.parsedContent = {
|
|
37
|
+
title: parsedData.title,
|
|
38
|
+
headings: parsedData.headings,
|
|
39
|
+
content: parsedData.content,
|
|
40
|
+
},
|
|
41
|
+
// parsedData.type == 'cards' ? this.typologyResponse = parsedData.taxonomy : this.typologyResponse = null;
|
|
42
|
+
this.typologyResponse = parsedData.taxonomy;
|
|
39
43
|
this.expectedFields = generationContent.content.fields; //returnFields();
|
|
40
44
|
this.promptForTypology = (0, typology_prompt_1.returnTypologyPrompt)(generationContent.prompt.typology);
|
|
41
45
|
this.promptForCardGen = (0, card_gen_prompt_1.returnCardGenPrompt)(generationContent.prompt.card_generation);
|
|
42
46
|
}
|
|
43
47
|
generate() {
|
|
44
48
|
return __awaiter(this, arguments, void 0, function* (generate_typology = false, generate_card = false) {
|
|
45
|
-
|
|
46
|
-
//let typologyPrompt = returnTypologyPrompt();
|
|
47
|
-
let typologyPrompt = this.promptForTypology;
|
|
48
|
-
// let cardPrompt = returnCardGenPrompt();
|
|
49
|
-
let cardPrompt = this.promptForCardGen;
|
|
50
|
-
let args = new generate_args_1.GenerateArgs(generate_card, generate_typology, false, {
|
|
51
|
-
typology_prompt: typologyPrompt,
|
|
52
|
-
card_gen_prompt: cardPrompt,
|
|
53
|
-
summary_prompt: "",
|
|
54
|
-
});
|
|
49
|
+
let args = new generate_args_1.GenerateArgs(generate_card, generate_typology, false);
|
|
55
50
|
const responseToReturn = [];
|
|
56
51
|
const whatNeedsToBeGenerated = args.getWhatNeedsToBeGenerated();
|
|
57
52
|
for (let elem of whatNeedsToBeGenerated)
|
|
58
53
|
if (elem == "generate_tyopology") {
|
|
59
|
-
this.typologyResponse = yield this.generateTypology(
|
|
54
|
+
this.typologyResponse = yield this.generateTypology(this.promptForTypology);
|
|
60
55
|
responseToReturn.push(this.typologyResponse);
|
|
61
56
|
}
|
|
62
57
|
else if (elem == "generate_card") {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
/// for cards gen to occur, there must be presence of source taxonomy
|
|
59
|
+
if (this.shouldTheCardBeGeneratedAfterTypologyResponse()) {
|
|
60
|
+
this.cardgenResponse = yield this.generateCard(this.promptForCardGen, JSON.stringify(this.typologyResponse), false);
|
|
61
|
+
responseToReturn.push(this.cardgenResponse);
|
|
62
|
+
/// check if gap fill is required ie coverage determination
|
|
63
|
+
if (this.cardgenResponse.status_code == 200) {
|
|
64
|
+
this.gapFillResponse = yield this.generationForGapFill(this.typologyResponse, this.cardgenResponse);
|
|
65
|
+
responseToReturn.push(this.gapFillResponse);
|
|
66
66
|
}
|
|
67
|
-
else {
|
|
68
|
-
this.cardgenResponse = yield this.generateCard((_b = args.prompts.card_gen_prompt) !== null && _b !== void 0 ? _b : "", this.parsedContent + JSON.stringify(this.typologyResponse), false);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
this.cardgenResponse = yield this.generateCard((_c = args.prompts.card_gen_prompt) !== null && _c !== void 0 ? _c : "", this.parsedContent, false);
|
|
73
67
|
}
|
|
74
|
-
responseToReturn.push(this.cardgenResponse);
|
|
75
|
-
}
|
|
76
|
-
if (this.cardgenResponse.status_code == 200) {
|
|
77
|
-
let gapFill = (0, calculate_gap_fill_1.gapFilling)(this.typologyResponse, this.cardgenResponse);
|
|
78
|
-
if (gapFill.remainingConcepts.length !== 0 ||
|
|
79
|
-
gapFill.remainingFacts.length !== 0) {
|
|
80
|
-
this.gapFillResponse = yield this.generateCard((_d = args.prompts.card_gen_prompt) !== null && _d !== void 0 ? _d : "", this.parsedContent +
|
|
81
|
-
"Generate cards only suitable for the given remaining concepts and facts" +
|
|
82
|
-
JSON.stringify(gapFill) +
|
|
83
|
-
"Exclude generating these cards" +
|
|
84
|
-
JSON.stringify(this.cardgenResponse.cards_data), true);
|
|
85
68
|
}
|
|
86
|
-
responseToReturn.push(this.gapFillResponse);
|
|
87
|
-
}
|
|
88
69
|
return responseToReturn;
|
|
89
70
|
// return [typologyPrompt, cardPrompt];
|
|
90
71
|
});
|
|
91
72
|
}
|
|
92
|
-
|
|
73
|
+
shouldTheCardBeGeneratedAfterTypologyResponse() {
|
|
74
|
+
if (this.typologyResponse) {
|
|
75
|
+
return this.typologyResponse.generate_cards.state == true;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
generationForGapFill(typologyData, cardGenData) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
let gapFill = (0, calculate_gap_fill_1.gapFilling)(typologyData, cardGenData);
|
|
84
|
+
let response;
|
|
85
|
+
if (gapFill.remainingConcepts.length !== 0 ||
|
|
86
|
+
gapFill.remainingFacts.length !== 0) {
|
|
87
|
+
response = yield this.generateCard(this.promptForCardGen +
|
|
88
|
+
"Generate cards only suitable for the given remaining concepts and facts" +
|
|
89
|
+
JSON.stringify(gapFill) +
|
|
90
|
+
"Exclude generating these cards", JSON.stringify(cardGenData.cards_data), true);
|
|
91
|
+
}
|
|
92
|
+
return response;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
generateCard(prompt, additionalContent, isGapFill) {
|
|
93
96
|
return __awaiter(this, void 0, void 0, function* () {
|
|
94
97
|
let generateCards = new generate_cards_1.GenerateCards(this.openAiService);
|
|
95
|
-
|
|
98
|
+
this.cardgenResponse = yield generateCards.generateCards(prompt !== null && prompt !== void 0 ? prompt : "", this.parsedContent + additionalContent, isGapFill);
|
|
96
99
|
// let response = await this.openAiService?.sendRequest(prompt,this.parsedContent);
|
|
97
100
|
// response['type'] = 'card_gen';
|
|
98
|
-
return cardgenResponse;
|
|
101
|
+
return this.cardgenResponse;
|
|
99
102
|
});
|
|
100
103
|
}
|
|
101
104
|
generateTypology(prompt) {
|
|
102
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
-
let response = yield new generate_typology_1.GenerateTypology(this.openAiService, prompt, this.parsedContent, this.expectedFields).generate();
|
|
106
|
+
let response = yield new generate_typology_1.GenerateTypology(this.openAiService, prompt, JSON.stringify(this.parsedContent), this.expectedFields).generate();
|
|
104
107
|
return response;
|
|
105
108
|
});
|
|
106
109
|
}
|
|
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.GenerateCards = void 0;
|
|
13
|
+
const logger_1 = require("../logger");
|
|
13
14
|
class GenerateCards {
|
|
14
15
|
constructor(openAiService) {
|
|
15
16
|
this.openAiService = openAiService;
|
|
@@ -39,42 +40,55 @@ class GenerateCards {
|
|
|
39
40
|
});
|
|
40
41
|
}
|
|
41
42
|
parse(generatedData, isGapFill) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
try {
|
|
45
|
+
const cardData = [];
|
|
46
|
+
let usage_data = generatedData.metadata;
|
|
47
|
+
const status_code = generatedData.status_code;
|
|
48
|
+
const missing_concepts = generatedData.generated_content.missing_concepts;
|
|
49
|
+
const missing_facts = generatedData.generated_content.missing_facts;
|
|
50
|
+
const unparsedTestCards = generatedData.generated_content.test_cards;
|
|
51
|
+
const type = generatedData.type;
|
|
52
|
+
if (unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
|
|
53
|
+
for (let elem of unparsedTestCards) {
|
|
54
|
+
if (elem.type == "flash") {
|
|
55
|
+
cardData.push(this.parseFlashCard(elem));
|
|
56
|
+
}
|
|
57
|
+
else if (elem.type == "mcq") {
|
|
58
|
+
cardData.push(this.parseMcqCard(elem));
|
|
59
|
+
}
|
|
60
|
+
else if (elem.type == "cloze") {
|
|
61
|
+
cardData.push(this.parseClozeCard(elem));
|
|
62
|
+
}
|
|
63
|
+
else if (elem.type == "match") {
|
|
64
|
+
cardData.push(this.parseMatchCard(elem));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
59
67
|
}
|
|
60
|
-
else
|
|
61
|
-
|
|
68
|
+
else {
|
|
69
|
+
if (!isGapFill) {
|
|
70
|
+
usage_data.status = "failed";
|
|
71
|
+
}
|
|
62
72
|
}
|
|
73
|
+
return {
|
|
74
|
+
status_code: status_code,
|
|
75
|
+
metadata: usage_data,
|
|
76
|
+
type: type,
|
|
77
|
+
missing_concepts: missing_concepts,
|
|
78
|
+
missing_facts: missing_facts,
|
|
79
|
+
cards_data: cardData,
|
|
80
|
+
};
|
|
63
81
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
catch (e) {
|
|
83
|
+
yield new logger_1.ErrorLogger({
|
|
84
|
+
"type": 'card_parsing',
|
|
85
|
+
"data": e.message,
|
|
86
|
+
}).log();
|
|
87
|
+
return {
|
|
88
|
+
status_code: 500,
|
|
89
|
+
};
|
|
68
90
|
}
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
status_code: status_code,
|
|
72
|
-
metadata: usage_data,
|
|
73
|
-
type: type,
|
|
74
|
-
missing_concepts: missing_concepts,
|
|
75
|
-
missing_facts: missing_facts,
|
|
76
|
-
cards_data: cardData,
|
|
77
|
-
};
|
|
91
|
+
});
|
|
78
92
|
}
|
|
79
93
|
parseFlashCard(data) {
|
|
80
94
|
let displayTitle = this.generateFlashCardDisplayTitle(data.card_content.front, data.card_content.back);
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// this method will call out atlas function and will write to a doc, incase of any errors:
|
|
3
|
+
/// this is only for developmemt use
|
|
4
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
6
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
7
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
8
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
9
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
10
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.ErrorLogger = void 0;
|
|
18
|
+
const axios_1 = __importDefault(require("axios"));
|
|
19
|
+
class ErrorLogger {
|
|
20
|
+
constructor(data) {
|
|
21
|
+
this.data = data;
|
|
22
|
+
}
|
|
23
|
+
log() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
try {
|
|
26
|
+
let response = yield axios_1.default.post('https://us-east-1.aws.data.mongodb-api.com/app/oe-phase1-tkmsy/endpoint/oe_gen_logger', {
|
|
27
|
+
data: this.data,
|
|
28
|
+
}, {
|
|
29
|
+
headers: {
|
|
30
|
+
"Content-Type": ['application/json']
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return response;
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.log(e);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.ErrorLogger = ErrorLogger;
|
|
@@ -7,18 +7,32 @@ class ParseSourceContent {
|
|
|
7
7
|
this.content = sourceContent;
|
|
8
8
|
}
|
|
9
9
|
parseData() {
|
|
10
|
-
if
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{ 'content': afterSanitized }
|
|
17
|
-
]);
|
|
10
|
+
// if(this.content.type == 'source') {
|
|
11
|
+
let dataAfterRemovingUnWantedBlocks = this.removeSectionsByTitle(this.content.content);
|
|
12
|
+
let afterSanitized = this.sanitizeBlocks(dataAfterRemovingUnWantedBlocks);
|
|
13
|
+
let taxonomy;
|
|
14
|
+
if (this.content.taxonomy) {
|
|
15
|
+
taxonomy = this.content.taxonomy;
|
|
18
16
|
}
|
|
19
17
|
else {
|
|
20
|
-
|
|
18
|
+
taxonomy = null;
|
|
21
19
|
}
|
|
20
|
+
return {
|
|
21
|
+
type: this.content.type,
|
|
22
|
+
title: this.content.title,
|
|
23
|
+
content: afterSanitized,
|
|
24
|
+
headings: this.content.headings,
|
|
25
|
+
taxonomy: this.content.taxonomy,
|
|
26
|
+
};
|
|
27
|
+
// } else if(this.content.type == 'cards'){
|
|
28
|
+
// return {
|
|
29
|
+
// type :'card',
|
|
30
|
+
// title: this.content.title,
|
|
31
|
+
// content: afterSanitized,
|
|
32
|
+
// headings: this.content.headings
|
|
33
|
+
// taxonomy: this.content.taxonomy,
|
|
34
|
+
// };
|
|
35
|
+
// }
|
|
22
36
|
// return JSON.stringify(afterSanitized);
|
|
23
37
|
}
|
|
24
38
|
removeSectionsByTitle(data) {
|
|
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.GenerateTypology = void 0;
|
|
13
|
+
const logger_1 = require("../logger");
|
|
13
14
|
class GenerateTypology {
|
|
14
15
|
constructor(openAiService, prompt, content, expected_fields) {
|
|
15
16
|
this.prompt = '';
|
|
@@ -22,19 +23,27 @@ class GenerateTypology {
|
|
|
22
23
|
generate() {
|
|
23
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
25
|
var _a, _b, _c;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
try {
|
|
27
|
+
const response = yield ((_a = this.openAiService) === null || _a === void 0 ? void 0 : _a.sendRequest(this.prompt, this.content));
|
|
28
|
+
response['type'] = 'typology';
|
|
29
|
+
response.metadata = {
|
|
30
|
+
"req_time": response.generated_at,
|
|
31
|
+
"req_type": response.type,
|
|
32
|
+
"req_tokens": (_b = response.usage_data) === null || _b === void 0 ? void 0 : _b.prompt_tokens,
|
|
33
|
+
"res_tokens": (_c = response.usage_data) === null || _c === void 0 ? void 0 : _c.completion_tokens,
|
|
34
|
+
};
|
|
35
|
+
if (response.status_code == 200) {
|
|
36
|
+
return this.parseTypologyOnSuccess(response);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return response;
|
|
40
|
+
}
|
|
35
41
|
}
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
catch (e) {
|
|
43
|
+
yield new logger_1.ErrorLogger({
|
|
44
|
+
"type": 'typology_parsing',
|
|
45
|
+
"data": e.message
|
|
46
|
+
}).log();
|
|
38
47
|
}
|
|
39
48
|
});
|
|
40
49
|
}
|
|
@@ -2,23 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GenerateArgs = void 0;
|
|
4
4
|
class GenerateArgs {
|
|
5
|
-
constructor(generate_card, generate_typology, generate_summary
|
|
6
|
-
typology_prompt: '',
|
|
7
|
-
card_gen_prompt: '',
|
|
8
|
-
summary_prompt: ''
|
|
9
|
-
}) {
|
|
5
|
+
constructor(generate_card, generate_typology, generate_summary) {
|
|
10
6
|
this.generate_card = false;
|
|
11
7
|
this.generate_typology = false;
|
|
12
8
|
this.generate_summary = false;
|
|
13
|
-
this.prompts = {
|
|
14
|
-
typology_prompt: '',
|
|
15
|
-
card_gen_prompt: '',
|
|
16
|
-
summary_prompt: ''
|
|
17
|
-
};
|
|
18
9
|
this.generate_card = generate_card;
|
|
19
10
|
this.generate_typology = generate_typology;
|
|
20
11
|
this.generate_summary = generate_summary;
|
|
21
|
-
this.prompts = prompts;
|
|
22
12
|
}
|
|
23
13
|
getWhatNeedsToBeGenerated() {
|
|
24
14
|
let returnData = [];
|
package/package.json
CHANGED
package/src/bootstrap/app.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { GenerateArgs } from "../utils/generate_args";
|
|
|
8
8
|
import { returnFields } from "../constants/source_data";
|
|
9
9
|
import { returnTypologyData } from "../parse/response_format_typology";
|
|
10
10
|
import { gapFilling } from "../gap_fill/calculate_gap_fill";
|
|
11
|
+
import { title } from "process";
|
|
12
|
+
import { ErrorLogger } from "../logger";
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
/// OnlyEverGenerator
|
|
@@ -15,122 +17,133 @@ import { gapFilling } from "../gap_fill/calculate_gap_fill";
|
|
|
15
17
|
export class OnlyEverGenerator {
|
|
16
18
|
public api_key: string = "";
|
|
17
19
|
public openAiService: OpenAiService;
|
|
18
|
-
|
|
20
|
+
|
|
21
|
+
/// these fields will be populated inside the constructor
|
|
22
|
+
parsedContent: any = {};
|
|
19
23
|
promptForTypology: string = "";
|
|
20
24
|
promptForCardGen: string = "";
|
|
21
25
|
expectedFields: Array<string>;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
typologyResponse: any = {};
|
|
29
|
+
cardgenResponse: any = {};
|
|
30
|
+
summarizeResponse = {};
|
|
31
|
+
gapFillResponse: any = {};
|
|
32
|
+
|
|
33
|
+
|
|
22
34
|
constructor(
|
|
23
35
|
apiKey: string,
|
|
24
36
|
model: string,
|
|
25
37
|
generationContent : any
|
|
26
|
-
// prompt: any,
|
|
27
|
-
// content: any,
|
|
28
|
-
// expected_fields: Array<string>
|
|
29
38
|
) {
|
|
30
39
|
this.api_key = apiKey;
|
|
31
40
|
this.openAiService = new OpenAiService(
|
|
32
41
|
apiKey,
|
|
33
42
|
model ?? "gpt-3.5-turbo-1106"
|
|
34
43
|
);
|
|
44
|
+
const parsedData = new ParseSourceContent(generationContent.content).parseData()
|
|
45
|
+
this.parsedContent = {
|
|
46
|
+
title: parsedData.title,
|
|
47
|
+
headings: parsedData.headings,
|
|
48
|
+
content: parsedData.content,
|
|
49
|
+
|
|
50
|
+
},
|
|
51
|
+
// parsedData.type == 'cards' ? this.typologyResponse = parsedData.taxonomy : this.typologyResponse = null;
|
|
52
|
+
this.typologyResponse = parsedData.taxonomy
|
|
35
53
|
|
|
36
|
-
this.parsedContent = new ParseSourceContent(generationContent.content).parseData();
|
|
37
54
|
this.expectedFields = generationContent.content.fields; //returnFields();
|
|
38
55
|
this.promptForTypology = returnTypologyPrompt(generationContent.prompt.typology);
|
|
39
56
|
this.promptForCardGen = returnCardGenPrompt(generationContent.prompt.card_generation);
|
|
40
57
|
}
|
|
41
58
|
|
|
42
|
-
typologyResponse: any = {};
|
|
43
|
-
cardgenResponse: any = {};
|
|
44
|
-
summarizeResponse = {};
|
|
45
|
-
gapFillResponse: any = {};
|
|
46
59
|
|
|
47
60
|
|
|
48
61
|
async generate(
|
|
49
62
|
generate_typology: boolean = false,
|
|
50
63
|
generate_card: boolean = false
|
|
51
64
|
): Promise<Array<any>> {
|
|
52
|
-
|
|
53
|
-
let typologyPrompt = this.promptForTypology;
|
|
54
|
-
|
|
55
|
-
// let cardPrompt = returnCardGenPrompt();
|
|
56
|
-
let cardPrompt = this.promptForCardGen;
|
|
57
|
-
let args = new GenerateArgs(generate_card, generate_typology, false, {
|
|
58
|
-
typology_prompt: typologyPrompt,
|
|
59
|
-
card_gen_prompt: cardPrompt,
|
|
60
|
-
summary_prompt: "",
|
|
61
|
-
});
|
|
65
|
+
let args = new GenerateArgs(generate_card, generate_typology, false, );
|
|
62
66
|
const responseToReturn = [];
|
|
63
67
|
const whatNeedsToBeGenerated = args.getWhatNeedsToBeGenerated();
|
|
64
68
|
for (let elem of whatNeedsToBeGenerated)
|
|
65
69
|
if (elem == "generate_tyopology") {
|
|
66
70
|
this.typologyResponse = await this.generateTypology(
|
|
67
|
-
|
|
71
|
+
this.promptForTypology
|
|
68
72
|
);
|
|
69
73
|
responseToReturn.push(this.typologyResponse);
|
|
70
74
|
} else if (elem == "generate_card") {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
/// for cards gen to occur, there must be presence of source taxonomy
|
|
76
|
+
if(this.shouldTheCardBeGeneratedAfterTypologyResponse()){
|
|
77
|
+
this.cardgenResponse = await this.generateCard(
|
|
78
|
+
this.promptForCardGen,
|
|
79
|
+
JSON.stringify(this.typologyResponse),
|
|
80
|
+
false,
|
|
81
|
+
)
|
|
82
|
+
responseToReturn.push(this.cardgenResponse);
|
|
83
|
+
|
|
84
|
+
/// check if gap fill is required ie coverage determination
|
|
85
|
+
if(this.cardgenResponse.status_code == 200) {
|
|
86
|
+
this.gapFillResponse = await this.generationForGapFill(this.typologyResponse, this.cardgenResponse);
|
|
87
|
+
responseToReturn.push(this.gapFillResponse);
|
|
88
|
+
}
|
|
89
|
+
|
|
80
90
|
}
|
|
81
|
-
|
|
91
|
+
}
|
|
92
|
+
return responseToReturn;
|
|
93
|
+
// return [typologyPrompt, cardPrompt];
|
|
94
|
+
|
|
95
|
+
}
|
|
82
96
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
responseToReturn.push(this.cardgenResponse);
|
|
97
|
+
shouldTheCardBeGeneratedAfterTypologyResponse(){
|
|
98
|
+
if(this.typologyResponse){
|
|
99
|
+
return this.typologyResponse.generate_cards.state == true;
|
|
100
|
+
}else{
|
|
101
|
+
return false;
|
|
92
102
|
}
|
|
93
|
-
|
|
94
|
-
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async generationForGapFill(typologyData: any, cardGenData: any) {
|
|
107
|
+
let gapFill = gapFilling(typologyData, cardGenData);
|
|
108
|
+
let response :any ;
|
|
95
109
|
if (
|
|
96
110
|
gapFill.remainingConcepts.length !== 0 ||
|
|
97
111
|
gapFill.remainingFacts.length !== 0
|
|
98
112
|
) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.parsedContent +
|
|
113
|
+
response = await this.generateCard(
|
|
114
|
+
this.promptForCardGen +
|
|
102
115
|
"Generate cards only suitable for the given remaining concepts and facts" +
|
|
103
116
|
JSON.stringify(gapFill) +
|
|
104
|
-
"Exclude generating these cards"
|
|
105
|
-
JSON.stringify(
|
|
117
|
+
"Exclude generating these cards",
|
|
118
|
+
JSON.stringify(cardGenData.cards_data),
|
|
106
119
|
true
|
|
107
120
|
);
|
|
108
121
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return responseToReturn;
|
|
113
|
-
// return [typologyPrompt, cardPrompt];
|
|
114
|
-
|
|
122
|
+
return response;
|
|
123
|
+
|
|
115
124
|
}
|
|
116
125
|
|
|
117
|
-
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
async generateCard(prompt: string, additionalContent: string, isGapFill: boolean) {
|
|
118
129
|
let generateCards = new GenerateCards(this.openAiService);
|
|
119
|
-
|
|
130
|
+
this.cardgenResponse = await generateCards.generateCards(
|
|
120
131
|
prompt ?? "",
|
|
121
|
-
|
|
132
|
+
this.parsedContent + additionalContent,
|
|
122
133
|
isGapFill
|
|
123
134
|
);
|
|
135
|
+
|
|
124
136
|
// let response = await this.openAiService?.sendRequest(prompt,this.parsedContent);
|
|
125
137
|
// response['type'] = 'card_gen';
|
|
126
|
-
return cardgenResponse;
|
|
138
|
+
return this.cardgenResponse;
|
|
127
139
|
}
|
|
128
140
|
|
|
141
|
+
|
|
129
142
|
async generateTypology(prompt: string) {
|
|
130
143
|
let response = await new GenerateTypology(
|
|
131
144
|
this.openAiService,
|
|
132
145
|
prompt,
|
|
133
|
-
this.parsedContent,
|
|
146
|
+
JSON.stringify(this.parsedContent),
|
|
134
147
|
this.expectedFields
|
|
135
148
|
).generate();
|
|
136
149
|
return response;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ErrorLogger } from "../logger";
|
|
1
2
|
import { OpenAiService } from "../services/open_ai_service";
|
|
2
3
|
|
|
3
4
|
export class GenerateCards {
|
|
@@ -27,7 +28,8 @@ export class GenerateCards {
|
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
parse(generatedData: any, isGapFill: boolean) {
|
|
31
|
+
async parse(generatedData: any, isGapFill: boolean) {
|
|
32
|
+
try{
|
|
31
33
|
const cardData = [];
|
|
32
34
|
let usage_data = generatedData.metadata;
|
|
33
35
|
const status_code = generatedData.status_code;
|
|
@@ -63,6 +65,16 @@ if(unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
|
|
|
63
65
|
missing_facts: missing_facts,
|
|
64
66
|
cards_data: cardData,
|
|
65
67
|
};
|
|
68
|
+
}catch (e:any){
|
|
69
|
+
await new ErrorLogger({
|
|
70
|
+
"type": 'card_parsing',
|
|
71
|
+
"data": e.message,
|
|
72
|
+
}).log();
|
|
73
|
+
return {
|
|
74
|
+
status_code: 500,
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
}
|
|
66
78
|
}
|
|
67
79
|
|
|
68
80
|
parseFlashCard(data: any) {
|
package/src/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { OnlyEverGenerator } from "./bootstrap/app";
|
|
|
11
11
|
|
|
12
12
|
/// While Publishing the package , and using this code as a separate npm module
|
|
13
13
|
/// uncomment the below line and comment all the others, expect the import of OnlyEverGenerator
|
|
14
|
-
|
|
14
|
+
export {OnlyEverGenerator};
|
|
15
15
|
|
|
16
16
|
//. All the Codes Below uses express and are strictly for development purpose, while publishing the package, comment everything
|
|
17
17
|
//below this line
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// this method will call out atlas function and will write to a doc, incase of any errors:
|
|
2
|
+
/// this is only for developmemt use
|
|
3
|
+
|
|
4
|
+
import axios from "axios";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class ErrorLogger{
|
|
8
|
+
data: any;
|
|
9
|
+
constructor(data: any){
|
|
10
|
+
this.data = data;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async log() {
|
|
14
|
+
try{
|
|
15
|
+
let response = await axios.post(
|
|
16
|
+
'https://us-east-1.aws.data.mongodb-api.com/app/oe-phase1-tkmsy/endpoint/oe_gen_logger',
|
|
17
|
+
{
|
|
18
|
+
data: this.data,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
headers : {
|
|
22
|
+
"Content-Type" : ['application/json']
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
);
|
|
27
|
+
return response;}catch(e){
|
|
28
|
+
console.log(e)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -8,18 +8,31 @@ export class ParseSourceContent{
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
parseData() {
|
|
11
|
-
if(this.content.type == 'source') {
|
|
11
|
+
// if(this.content.type == 'source') {
|
|
12
12
|
let dataAfterRemovingUnWantedBlocks = this.removeSectionsByTitle(this.content.content);
|
|
13
13
|
let afterSanitized = this.sanitizeBlocks(dataAfterRemovingUnWantedBlocks);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
let taxonomy;
|
|
15
|
+
if(this.content.taxonomy){
|
|
16
|
+
taxonomy = this.content.taxonomy
|
|
17
|
+
}else{
|
|
18
|
+
taxonomy = null;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
type: this.content.type,
|
|
22
|
+
title: this.content.title,
|
|
23
|
+
content: afterSanitized,
|
|
24
|
+
headings: this.content.headings,
|
|
25
|
+
taxonomy: this.content.taxonomy,
|
|
26
|
+
}
|
|
27
|
+
// } else if(this.content.type == 'cards'){
|
|
28
|
+
// return {
|
|
29
|
+
// type :'card',
|
|
30
|
+
// title: this.content.title,
|
|
31
|
+
// content: afterSanitized,
|
|
32
|
+
// headings: this.content.headings
|
|
33
|
+
// taxonomy: this.content.taxonomy,
|
|
34
|
+
// };
|
|
35
|
+
// }
|
|
23
36
|
|
|
24
37
|
// return JSON.stringify(afterSanitized);
|
|
25
38
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
import { parseOpenAiFailureResponse, parseOpenAiSuccessResponse } from "../utils/parse_openai_response";
|
|
3
3
|
import { openAiEndPoint } from "../constants/api_constants";
|
|
4
|
+
import { ErrorLogger } from "../logger";
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
export class OpenAiService{
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { OpenAiService } from "../services/open_ai_service";
|
|
2
2
|
import { returnTypologyData } from "../parse/response_format_typology";
|
|
3
|
+
import { ErrorLogger } from "../logger";
|
|
3
4
|
|
|
4
5
|
export class GenerateTypology{
|
|
5
6
|
public openAiService: OpenAiService;
|
|
@@ -13,6 +14,7 @@ export class GenerateTypology{
|
|
|
13
14
|
this.expectedFields = expected_fields.map((elem:string)=>elem.toLowerCase());
|
|
14
15
|
}
|
|
15
16
|
async generate(){
|
|
17
|
+
try{
|
|
16
18
|
const response = await this.openAiService?.sendRequest(this.prompt,this.content);
|
|
17
19
|
response['type'] = 'typology';
|
|
18
20
|
response.metadata = {
|
|
@@ -25,6 +27,11 @@ export class GenerateTypology{
|
|
|
25
27
|
return this.parseTypologyOnSuccess(response);
|
|
26
28
|
} else {
|
|
27
29
|
return response;
|
|
30
|
+
}} catch (e: any){
|
|
31
|
+
await new ErrorLogger({
|
|
32
|
+
"type": 'typology_parsing',
|
|
33
|
+
"data": e.message
|
|
34
|
+
}).log();
|
|
28
35
|
}
|
|
29
36
|
}
|
|
30
37
|
|
|
@@ -2,21 +2,12 @@ export class GenerateArgs{
|
|
|
2
2
|
public generate_card : boolean = false;
|
|
3
3
|
public generate_typology: boolean = false;
|
|
4
4
|
public generate_summary: boolean = false;
|
|
5
|
-
public prompts = {
|
|
6
|
-
typology_prompt: '',
|
|
7
|
-
card_gen_prompt: '',
|
|
8
|
-
summary_prompt: ''
|
|
9
|
-
}
|
|
10
5
|
|
|
11
|
-
constructor(generate_card: boolean,generate_typology: boolean, generate_summary: boolean,
|
|
12
|
-
typology_prompt: '',
|
|
13
|
-
card_gen_prompt: '',
|
|
14
|
-
summary_prompt: ''
|
|
15
|
-
}){
|
|
6
|
+
constructor(generate_card: boolean,generate_typology: boolean, generate_summary: boolean, ){
|
|
16
7
|
this.generate_card = generate_card;
|
|
17
8
|
this.generate_typology = generate_typology;
|
|
18
9
|
this.generate_summary = generate_summary;
|
|
19
|
-
|
|
10
|
+
|
|
20
11
|
}
|
|
21
12
|
|
|
22
13
|
getWhatNeedsToBeGenerated(){
|