only_ever_generator 0.5.5 → 0.5.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 +5 -5
- package/dist/card_gen/generate_cards.js +8 -3
- package/dist/config.js +1 -1
- package/dist/constants/api_constants.js +1 -1
- package/dist/constants/prompt_data.js +4 -4
- package/dist/constants/source_data.js +382 -382
- package/dist/logger.js +4 -4
- package/dist/parse/parse_card/parse_cloze_card.js +11 -9
- package/dist/parse/parse_card/parse_flash_cards.js +33 -0
- package/dist/parse/parse_card_response.js +8 -31
- package/dist/parse/parse_source_content.js +30 -20
- package/dist/parse/response_format_typology.js +16 -16
- package/dist/services/open_ai_service.js +13 -10
- package/dist/typology_gen/generate_typology.js +19 -15
- package/dist/utils/generate_args.js +3 -3
- package/dist/utils/parse_openai_response.js +7 -7
- package/package.json +2 -2
- package/src/bootstrap/app.ts +69 -79
- package/src/card_gen/generate_cards.ts +12 -3
- package/src/config.ts +3 -3
- package/src/constants/api_constants.ts +3 -3
- package/src/constants/prompt_data.ts +24 -26
- package/src/constants/prompts/card_gen_prompt.ts +2 -4
- package/src/constants/source_data.ts +440 -390
- package/src/index.ts +1 -2
- package/src/logger.ts +24 -25
- package/src/parse/parse_card/parse_cloze_card.ts +54 -42
- package/src/parse/parse_card/parse_flash_cards.ts +33 -0
- package/src/parse/parse_card/parse_match_card.ts +32 -32
- package/src/parse/parse_card_response.ts +28 -47
- package/src/parse/parse_source_content.ts +173 -168
- package/src/parse/response_format_card.ts +0 -2
- package/src/parse/response_format_typology.ts +42 -42
- package/src/services/open_ai_service.ts +50 -48
- package/src/typology_gen/generate_typology.ts +71 -60
- package/src/utils/generate_args.ts +25 -23
- package/src/utils/parse_openai_response.ts +17 -19
- package/dist/bootstrap/app.js.map +0 -1
- package/dist/card_gen/generate_cards.js.map +0 -1
- package/dist/constants/api_constants.js.map +0 -1
- package/dist/constants/prompt_data.js.map +0 -1
- package/dist/constants/prompts/card_gen_prompt.js.map +0 -1
- package/dist/constants/prompts/typology_prompt.js.map +0 -1
- package/dist/constants/source_data.js.map +0 -1
- package/dist/gap_fill/calculate_gap_fill.js.map +0 -1
- package/dist/parse/parse_card/parse_cloze_card.js.map +0 -1
- package/dist/parse/parse_card/parse_match_card.js.map +0 -1
- package/dist/parse/parse_card/parse_mcq_card.js.map +0 -1
- package/dist/parse/parse_card_response.js.map +0 -1
- package/dist/parse/parse_source_content.js.map +0 -1
- package/dist/parse/response_format_card.js.map +0 -1
- package/dist/parse/response_format_typology.js.map +0 -1
- package/dist/services/open_ai_service.js.map +0 -1
- package/dist/typology_gen/generate_typology.js.map +0 -1
- package/dist/utils/generate_args.js.map +0 -1
- package/dist/utils/parse_openai_response.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -17,10 +17,9 @@
|
|
|
17
17
|
|
|
18
18
|
import { OnlyEverGenerator } from "./bootstrap/app";
|
|
19
19
|
|
|
20
|
-
|
|
21
20
|
/// While Publishing the package , and using this code as a separate npm module
|
|
22
21
|
/// uncomment the below line and comment all the others, expect the import of OnlyEverGenerator
|
|
23
|
-
export {OnlyEverGenerator};
|
|
22
|
+
export { OnlyEverGenerator };
|
|
24
23
|
|
|
25
24
|
// . All the Codes Below uses express and are strictly for development purpose, while publishing the package, comment everything
|
|
26
25
|
// below this line
|
package/src/logger.ts
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
|
-
/// this method will call out atlas function and will write to a doc, incase of any errors:
|
|
1
|
+
/// this method will call out atlas function and will write to a doc, incase of any errors:
|
|
2
2
|
/// this is only for developmemt use
|
|
3
3
|
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
|
|
6
|
+
export class ErrorLogger {
|
|
7
|
+
data: any;
|
|
8
|
+
constructor(data: any) {
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
6
11
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
async log() {
|
|
13
|
+
try {
|
|
14
|
+
let response = await axios.post(
|
|
15
|
+
"https://us-east-1.aws.data.mongodb-api.com/app/oe-phase1-tkmsy/endpoint/oe_gen_logger",
|
|
16
|
+
{
|
|
17
|
+
data: this.data,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
headers: {
|
|
21
|
+
"Content-Type": ["application/json"],
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
return response;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.log(e);
|
|
11
28
|
}
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -5,23 +5,32 @@ export class ParseClozeCard {
|
|
|
5
5
|
let correctOptions = content.correct_options;
|
|
6
6
|
let incorrectOptions = content.incorrect_options;
|
|
7
7
|
let allOptions = [...correctOptions, ...incorrectOptions];
|
|
8
|
-
let displayTitle = this._generateClozeCardDisplayTitle(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
let displayTitle = this._generateClozeCardDisplayTitle(
|
|
9
|
+
data.card_content.prompt,
|
|
10
|
+
allOptions
|
|
11
|
+
);
|
|
12
|
+
let preparedData = this._prepareQuestionAndCorrectAnswers(
|
|
13
|
+
content.prompt,
|
|
14
|
+
correctOptions
|
|
15
|
+
);
|
|
16
|
+
let finalQuestion = preparedData.prompt;
|
|
17
|
+
let parsedCorrectOptions = preparedData.options;
|
|
18
|
+
let parsedIncorrectoptions = incorrectOptions.map((e: any) => {
|
|
19
|
+
return {
|
|
20
|
+
option: e,
|
|
21
|
+
cloze: "null",
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
const finalParsedOptions = [
|
|
25
|
+
...parsedCorrectOptions,
|
|
26
|
+
...parsedIncorrectoptions,
|
|
27
|
+
];
|
|
19
28
|
let clozeCardData = {
|
|
20
29
|
type: {
|
|
21
30
|
category: "learning",
|
|
22
31
|
sub_type: data.type,
|
|
23
32
|
},
|
|
24
|
-
heading:"",
|
|
33
|
+
heading: "",
|
|
25
34
|
displayTitle: displayTitle,
|
|
26
35
|
content: {
|
|
27
36
|
question: finalQuestion,
|
|
@@ -39,14 +48,14 @@ export class ParseClozeCard {
|
|
|
39
48
|
}
|
|
40
49
|
|
|
41
50
|
_generateClozeCardDisplayTitle(question: string, answers: Array<any>) {
|
|
42
|
-
try{
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
51
|
+
try {
|
|
52
|
+
let optionsString = "";
|
|
53
|
+
if (answers.length !== 0) {
|
|
54
|
+
optionsString = answers.join(", ");
|
|
55
|
+
}
|
|
48
56
|
|
|
49
|
-
|
|
57
|
+
return `${question} ---- ${optionsString}`;
|
|
58
|
+
} catch (e) {
|
|
50
59
|
throw Error("Error in generating display title");
|
|
51
60
|
}
|
|
52
61
|
}
|
|
@@ -60,31 +69,34 @@ export class ParseClozeCard {
|
|
|
60
69
|
// 6. Less than 2 options
|
|
61
70
|
// 7. Max character for individual cloze: 90
|
|
62
71
|
|
|
63
|
-
_prepareQuestionAndCorrectAnswers(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
_prepareQuestionAndCorrectAnswers(
|
|
73
|
+
rawPrompt: String,
|
|
74
|
+
correctOptions: Array<any>
|
|
75
|
+
) {
|
|
76
|
+
try {
|
|
77
|
+
var finalCorrectOptions = <any>[];
|
|
78
|
+
const regex = /{{(.*?)}}/g;
|
|
67
79
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
const transformed = rawPrompt.replace(regex, (match, p1) => {
|
|
81
|
+
// p1 is the captured group inside {{ }} (e.g., "fruit", "green")
|
|
82
|
+
const idx = correctOptions.indexOf(p1);
|
|
83
|
+
if (idx !== -1) {
|
|
84
|
+
let cloze = `c${idx}`;
|
|
85
|
+
finalCorrectOptions.push({
|
|
86
|
+
option: p1,
|
|
87
|
+
cloze: cloze,
|
|
88
|
+
});
|
|
89
|
+
return `{{c${idx}:${p1}}}`;
|
|
90
|
+
}
|
|
91
|
+
return match; // If not found in correct_options, leave as is or handle accordingly
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
prompt: transformed,
|
|
95
|
+
options: finalCorrectOptions,
|
|
96
|
+
};
|
|
97
|
+
} catch (e) {
|
|
85
98
|
throw Error("Error in preparing question and correct answers");
|
|
86
99
|
}
|
|
87
|
-
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
_validateCloze(clozeCard: any) {
|
|
@@ -118,7 +130,7 @@ export class ParseClozeCard {
|
|
|
118
130
|
}
|
|
119
131
|
return clozeCard;
|
|
120
132
|
} catch (e: any) {
|
|
121
|
-
|
|
133
|
+
throw Error(`Error in validating cloze card ${e.message}`);
|
|
122
134
|
}
|
|
123
135
|
}
|
|
124
136
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class ParseFlashCard {
|
|
2
|
+
parse(data: any) {
|
|
3
|
+
try {
|
|
4
|
+
let displayTitle = this.generateFlashCardDisplayTitle(
|
|
5
|
+
data.card_content.front,
|
|
6
|
+
data.card_content.back
|
|
7
|
+
);
|
|
8
|
+
let flashCardData = {
|
|
9
|
+
type: {
|
|
10
|
+
category: "learning",
|
|
11
|
+
sub_type: data.type,
|
|
12
|
+
},
|
|
13
|
+
heading: "",
|
|
14
|
+
displayTitle: displayTitle,
|
|
15
|
+
content: {
|
|
16
|
+
front_content: data.card_content.front,
|
|
17
|
+
back_content: data.card_content.back,
|
|
18
|
+
},
|
|
19
|
+
concepts: data.concepts,
|
|
20
|
+
explanation: data.card_content.explanation,
|
|
21
|
+
facts: data.facts,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return flashCardData;
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
generateFlashCardDisplayTitle(front: string, back: string) {
|
|
31
|
+
return `${front} ---- ${back}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -14,7 +14,7 @@ export class ParseMatchCard {
|
|
|
14
14
|
parse(cardData: any) {
|
|
15
15
|
try {
|
|
16
16
|
let content = cardData.card_content;
|
|
17
|
-
const finalContent =this._parseMatchContent(content);
|
|
17
|
+
const finalContent = this._parseMatchContent(content);
|
|
18
18
|
|
|
19
19
|
let displayTitle = this._generateMatchCardDisplayTitle(content);
|
|
20
20
|
let matchCard = {
|
|
@@ -22,7 +22,7 @@ export class ParseMatchCard {
|
|
|
22
22
|
category: "learning",
|
|
23
23
|
sub_type: cardData.type,
|
|
24
24
|
},
|
|
25
|
-
heading:"",
|
|
25
|
+
heading: "",
|
|
26
26
|
content: finalContent,
|
|
27
27
|
// content: cardData.card_content,
|
|
28
28
|
displayTitle: displayTitle,
|
|
@@ -37,8 +37,6 @@ export class ParseMatchCard {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
40
|
_generateMatchCardDisplayTitle(answers: any) {
|
|
43
41
|
let titles: string[] = [];
|
|
44
42
|
let counter = 65;
|
|
@@ -53,42 +51,44 @@ export class ParseMatchCard {
|
|
|
53
51
|
return displayTitle;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
const grouped = input.reduce<Record<string, OutputItem>>(
|
|
58
|
-
|
|
59
|
-
acc[left_item]
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
54
|
+
_parseMatchContent = (input: InputItem[]): OutputItem[] => {
|
|
55
|
+
const grouped = input.reduce<Record<string, OutputItem>>(
|
|
56
|
+
(acc, { left_item, right_item }) => {
|
|
57
|
+
if (!acc[left_item]) {
|
|
58
|
+
acc[left_item] = { left_item, right_item: [] };
|
|
59
|
+
}
|
|
60
|
+
acc[left_item].right_item.push(right_item);
|
|
61
|
+
return acc;
|
|
62
|
+
},
|
|
63
|
+
{}
|
|
64
|
+
);
|
|
65
|
+
|
|
65
66
|
return Object.values(grouped);
|
|
66
67
|
};
|
|
67
68
|
|
|
68
|
-
_validateMatch(matchCard: any){
|
|
69
|
+
_validateMatch(matchCard: any) {
|
|
69
70
|
let matches = matchCard.content;
|
|
70
71
|
let content = [];
|
|
71
|
-
try{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
try {
|
|
73
|
+
if (matches.length < 1 || matches.length > 8) {
|
|
74
|
+
throw Error("Invalid number of matches");
|
|
75
|
+
}
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
77
|
+
for (let elem of matches) {
|
|
78
|
+
if (elem.left_item.length <= 30 && elem.left_item.length != 0) {
|
|
79
|
+
if (elem.right_item.length <= 40 && elem.right_item.length != 0) {
|
|
80
|
+
content.push(elem);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}catch(e){
|
|
91
|
-
|
|
83
|
+
}
|
|
84
|
+
if (content.length >= 2) {
|
|
85
|
+
matchCard.content = content;
|
|
86
|
+
} else {
|
|
87
|
+
throw Error("Invalid content");
|
|
88
|
+
}
|
|
89
|
+
return matchCard;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return null;
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ErrorLogger } from "../logger";
|
|
2
2
|
import { ParseClozeCard } from "./parse_card/parse_cloze_card";
|
|
3
|
+
import { ParseFlashCard } from "./parse_card/parse_flash_cards";
|
|
3
4
|
import { ParseMatchCard } from "./parse_card/parse_match_card";
|
|
4
5
|
import { ParseMcqCard } from "./parse_card/parse_mcq_card";
|
|
5
6
|
|
|
@@ -13,9 +14,12 @@ export class ParseCardResponse {
|
|
|
13
14
|
if (unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
|
|
14
15
|
for (let elem of unparsedTestCards) {
|
|
15
16
|
if (elem.type == "flash") {
|
|
16
|
-
const flashCard =
|
|
17
|
+
const flashCard = new ParseFlashCard().parse(elem);
|
|
17
18
|
if (flashCard != null && flashCard) {
|
|
18
|
-
flashCard.heading = this._getCardReference(
|
|
19
|
+
flashCard.heading = this._getCardReference(
|
|
20
|
+
flashCard,
|
|
21
|
+
sourceTaxonomy
|
|
22
|
+
);
|
|
19
23
|
cardData.push(flashCard);
|
|
20
24
|
}
|
|
21
25
|
} else if (elem.type == "mcq") {
|
|
@@ -27,13 +31,19 @@ export class ParseCardResponse {
|
|
|
27
31
|
} else if (elem.type == "cloze") {
|
|
28
32
|
const clozeCard = new ParseClozeCard().parse(elem);
|
|
29
33
|
if (clozeCard && clozeCard != null) {
|
|
30
|
-
clozeCard.heading = this._getCardReference(
|
|
34
|
+
clozeCard.heading = this._getCardReference(
|
|
35
|
+
clozeCard,
|
|
36
|
+
sourceTaxonomy
|
|
37
|
+
);
|
|
31
38
|
cardData.push(clozeCard);
|
|
32
39
|
}
|
|
33
40
|
} else if (elem.type == "match") {
|
|
34
41
|
const matchCard = new ParseMatchCard().parse(elem);
|
|
35
42
|
if (matchCard && matchCard != null) {
|
|
36
|
-
matchCard.heading = this._getCardReference(
|
|
43
|
+
matchCard.heading = this._getCardReference(
|
|
44
|
+
matchCard,
|
|
45
|
+
sourceTaxonomy
|
|
46
|
+
);
|
|
37
47
|
cardData.push(matchCard);
|
|
38
48
|
}
|
|
39
49
|
}
|
|
@@ -43,13 +53,12 @@ export class ParseCardResponse {
|
|
|
43
53
|
usage_data.status = "failed";
|
|
44
54
|
}
|
|
45
55
|
}
|
|
46
|
-
if(cardData.length == 0){
|
|
47
|
-
usage_data.status="failed";
|
|
48
|
-
|
|
56
|
+
if (cardData.length == 0) {
|
|
57
|
+
usage_data.status = "failed";
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
return {
|
|
52
|
-
status_code: cardData.length == 0 ? 400:200,
|
|
61
|
+
status_code: cardData.length == 0 ? 400 : 200,
|
|
53
62
|
metadata: usage_data,
|
|
54
63
|
type: type,
|
|
55
64
|
missing_concepts: [],
|
|
@@ -70,38 +79,6 @@ export class ParseCardResponse {
|
|
|
70
79
|
}
|
|
71
80
|
}
|
|
72
81
|
|
|
73
|
-
parseFlashCard(data: any) {
|
|
74
|
-
try {
|
|
75
|
-
let displayTitle = this.generateFlashCardDisplayTitle(
|
|
76
|
-
data.card_content.front,
|
|
77
|
-
data.card_content.back
|
|
78
|
-
);
|
|
79
|
-
let flashCardData = {
|
|
80
|
-
type: {
|
|
81
|
-
category: "learning",
|
|
82
|
-
sub_type: data.type,
|
|
83
|
-
},
|
|
84
|
-
heading : "",
|
|
85
|
-
displayTitle: displayTitle,
|
|
86
|
-
content: {
|
|
87
|
-
front_content: data.card_content.front,
|
|
88
|
-
back_content: data.card_content.back,
|
|
89
|
-
},
|
|
90
|
-
concepts: data.concepts,
|
|
91
|
-
explanation: data.card_content.explanation,
|
|
92
|
-
facts: data.facts,
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
return flashCardData;
|
|
96
|
-
} catch (e) {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
generateFlashCardDisplayTitle(front: string, back: string) {
|
|
102
|
-
return `${front} ---- ${back}`;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
82
|
_getCardReference(generatedCardData: any, sourceTaxonomy: any) {
|
|
106
83
|
const cardConcepts = generatedCardData.concepts ?? [];
|
|
107
84
|
const cardFacts = generatedCardData.facts ?? [];
|
|
@@ -112,24 +89,28 @@ export class ParseCardResponse {
|
|
|
112
89
|
|
|
113
90
|
const mappedSourceConcepts = sourceConcepts.map((elem: any) => {
|
|
114
91
|
return {
|
|
115
|
-
|
|
92
|
+
text: elem.concept_text,
|
|
116
93
|
reference: elem.reference,
|
|
117
94
|
};
|
|
118
95
|
});
|
|
119
96
|
const mappedSourceFacts = sourceFacts.map((elem: any) => {
|
|
120
97
|
return {
|
|
121
|
-
|
|
98
|
+
text: elem.fact_text,
|
|
122
99
|
reference: elem.reference,
|
|
123
100
|
};
|
|
124
101
|
});
|
|
125
102
|
|
|
126
|
-
const compinedConceptsAndFacts = [
|
|
127
|
-
|
|
128
|
-
|
|
103
|
+
const compinedConceptsAndFacts = [
|
|
104
|
+
...mappedSourceConcepts,
|
|
105
|
+
...mappedSourceFacts,
|
|
106
|
+
];
|
|
107
|
+
const firstMatchedConcept = compinedConceptsAndFacts.find((elem: any) =>
|
|
108
|
+
combinedCardFactsAndConcepts.includes(elem.text)
|
|
109
|
+
);
|
|
129
110
|
|
|
130
|
-
if(firstMatchedConcept){
|
|
111
|
+
if (firstMatchedConcept) {
|
|
131
112
|
return firstMatchedConcept.reference;
|
|
132
|
-
}else{
|
|
113
|
+
} else {
|
|
133
114
|
return "";
|
|
134
115
|
}
|
|
135
116
|
}
|