only_ever_generator 0.4.9 → 0.5.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/dist/card_gen/generate_cards.js +8 -199
- package/dist/constants/source_data.js +1253 -883
- package/dist/index.js +20 -20
- package/dist/parse/parse_card/parse_cloze_card.js +86 -0
- package/dist/parse/parse_card/parse_match_card.js +59 -0
- package/dist/parse/parse_card/parse_mcq_card.js +103 -0
- package/dist/parse/parse_card_response.js +82 -272
- package/dist/parse/response_format_card.js +311 -149
- package/package.json +1 -1
- package/src/card_gen/generate_cards.ts +22 -237
- package/src/constants/source_data.ts +1478 -936
- package/src/index.ts +22 -20
- package/src/parse/parse_card/parse_cloze_card.ts +90 -0
- package/src/parse/parse_card/parse_match_card.ts +59 -0
- package/src/parse/parse_card/parse_mcq_card.ts +105 -0
- package/src/parse/parse_card_response.ts +88 -282
- package/src/parse/response_format_card.ts +371 -194
- package/src/utils/generate_args.ts +1 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ErrorLogger } from "../logger";
|
|
2
|
+
import { ParseCardResponse } from "../parse/parse_card_response";
|
|
2
3
|
import { OpenAiService } from "../services/open_ai_service";
|
|
3
4
|
|
|
4
5
|
export class GenerateCards {
|
|
@@ -7,246 +8,30 @@ export class GenerateCards {
|
|
|
7
8
|
this.openAiService = openAiService;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
async generateCards(
|
|
11
|
+
async generateCards(
|
|
12
|
+
prompt: string,
|
|
13
|
+
parsedContent: string,
|
|
14
|
+
isGapFill: boolean,
|
|
15
|
+
headings: Array<any>
|
|
16
|
+
) {
|
|
11
17
|
let response = await this.openAiService?.sendRequest(prompt, parsedContent);
|
|
12
|
-
|
|
13
|
-
response["type"] = isGapFill ? "gap_fill":"card_gen";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// console.log("response to card generation ", response);
|
|
19
|
+
response["type"] = isGapFill ? "gap_fill" : "card_gen";
|
|
20
|
+
response.metadata = {
|
|
21
|
+
req_time: response.generated_at ?? new Date(),
|
|
22
|
+
req_type: response.type,
|
|
23
|
+
req_tokens: response.usage_data?.prompt_tokens,
|
|
24
|
+
res_tokens: response.usage_data?.completion_tokens,
|
|
25
|
+
model: this.openAiService.model,
|
|
20
26
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
} else {
|
|
26
|
-
response.metadata.status = "failed";
|
|
27
|
-
response.metadata.err_message = response.message;
|
|
28
|
-
return response;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
parse(generatedData: any, isGapFill: boolean, headings:Array<any>) {
|
|
33
|
-
let usage_data = generatedData.metadata;
|
|
34
|
-
const status_code = generatedData.status_code;
|
|
35
|
-
try{
|
|
36
|
-
const cardData = [];
|
|
37
|
-
const unparsedTestCards = generatedData.generated_content.test_cards;
|
|
38
|
-
const type = generatedData.type;
|
|
39
|
-
if(unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
|
|
40
|
-
for (let elem of unparsedTestCards) {
|
|
41
|
-
// if(headings.includes(elem.card_reference)){
|
|
42
|
-
|
|
43
|
-
// }else{
|
|
44
|
-
// elem.card_reference = '';
|
|
45
|
-
// }
|
|
46
|
-
if (elem.type == "flash") {
|
|
47
|
-
cardData.push(this.parseFlashCard(elem));
|
|
48
|
-
} else if (elem.type == "mcq") {
|
|
49
|
-
cardData.push(this.parseMcqCard(elem));
|
|
50
|
-
} else if (elem.type == "cloze") {
|
|
51
|
-
cardData.push(this.parseClozeCard(elem));
|
|
52
|
-
} else if (elem.type == "match") {
|
|
53
|
-
cardData.push(this.parseMatchCard(elem));
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
if(!isGapFill) {
|
|
58
|
-
usage_data.status = "failed";
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return {
|
|
65
|
-
status_code: 200,
|
|
66
|
-
metadata: usage_data,
|
|
67
|
-
type: type,
|
|
68
|
-
missing_concepts: [],
|
|
69
|
-
missing_facts: [],
|
|
70
|
-
cards_data: cardData,
|
|
71
|
-
};
|
|
72
|
-
}catch (e:any){
|
|
73
|
-
new ErrorLogger({
|
|
74
|
-
"type": 'card_parsing',
|
|
75
|
-
"data": e.message,
|
|
76
|
-
"response": generatedData,
|
|
77
|
-
}).log();
|
|
78
|
-
return {
|
|
79
|
-
status_code: 500,
|
|
80
|
-
metadata : usage_data,
|
|
81
|
-
type: generatedData.type,
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// return {
|
|
85
|
-
// status_code: 500,
|
|
86
|
-
// type: 'card_gen',
|
|
87
|
-
// }
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
parseFlashCard(data: any) {
|
|
92
|
-
let displayTitle = this.generateFlashCardDisplayTitle(
|
|
93
|
-
data.card_content.front,
|
|
94
|
-
data.card_content.back
|
|
95
|
-
);
|
|
96
|
-
let flashCardData = {
|
|
97
|
-
type: {
|
|
98
|
-
category: 'learning',
|
|
99
|
-
sub_type: data.type,
|
|
100
|
-
},
|
|
101
|
-
heading: data.card_reference,
|
|
102
|
-
displayTitle: displayTitle,
|
|
103
|
-
content: {
|
|
104
|
-
front_content: data.card_content.front,
|
|
105
|
-
back_content: data.card_content.back,
|
|
106
|
-
},
|
|
107
|
-
concepts: data.concepts,
|
|
108
|
-
facts: data.facts,
|
|
109
|
-
bloomLevel: data.bloom_level,
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
return flashCardData;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
generateFlashCardDisplayTitle(front: string, back: string) {
|
|
116
|
-
return `${front} ---- ${back}`;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
parseMcqCard(data: any) {
|
|
120
|
-
let mcqAnswers = [];
|
|
121
|
-
if(data.card_content.choices !== undefined && data.card_content.choices.length != 0) {
|
|
122
|
-
for (let choice of data.card_content.choices) {
|
|
123
|
-
let answer = {
|
|
124
|
-
answer: choice.choice,
|
|
125
|
-
is_correct: choice.is_correct,
|
|
126
|
-
};
|
|
127
|
-
mcqAnswers.push(answer);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
let displayTitle = this.generateMcqCardDisplayTitle(
|
|
132
|
-
data.card_content.prompt,
|
|
133
|
-
mcqAnswers
|
|
134
|
-
);
|
|
135
|
-
let mcqCard = {
|
|
136
|
-
type: {
|
|
137
|
-
category: 'learning',
|
|
138
|
-
sub_type: data.type,
|
|
139
|
-
},
|
|
140
|
-
heading: data.card_reference,
|
|
141
|
-
displayTitle: displayTitle,
|
|
142
|
-
content: {
|
|
143
|
-
question: data.card_content.prompt,
|
|
144
|
-
answers: mcqAnswers,
|
|
145
|
-
},
|
|
146
|
-
concepts: data.concepts,
|
|
147
|
-
facts: data.facts,
|
|
148
|
-
bloomLevel: data.bloom_level,
|
|
149
|
-
|
|
150
|
-
};
|
|
151
|
-
return mcqCard;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
generateMcqCardDisplayTitle(question: string, answers: any) {
|
|
155
|
-
let answersString = [];
|
|
156
|
-
if(answers.length != 0) {
|
|
157
|
-
for (let option of answers) {
|
|
158
|
-
let currentIndex = answers.indexOf(option) + 1;
|
|
159
|
-
let temp = `${currentIndex} . ${option.answer} `;
|
|
160
|
-
answersString.push(temp);
|
|
161
|
-
}
|
|
162
|
-
let resultString = answersString.join("");
|
|
163
|
-
let finalDisplayTitle = `${question} ---- ${resultString}`;
|
|
164
|
-
return finalDisplayTitle;
|
|
27
|
+
if (response.status_code == 200) {
|
|
28
|
+
response.metadata.status = "completed";
|
|
29
|
+
let parseCard = new ParseCardResponse().parse(response,isGapFill);
|
|
30
|
+
return parseCard;
|
|
165
31
|
} else {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
parseClozeCard(data: any) {
|
|
172
|
-
let displayTitle = this.generateClozeCardDisplayTitle(
|
|
173
|
-
data.card_content.prompt,
|
|
174
|
-
data.card_content.options
|
|
175
|
-
);
|
|
176
|
-
let clozeCardData = {
|
|
177
|
-
type: {
|
|
178
|
-
category: 'learning',
|
|
179
|
-
sub_type: data.type,
|
|
180
|
-
},
|
|
181
|
-
heading: data.card_reference,
|
|
182
|
-
displayTitle: displayTitle,
|
|
183
|
-
content: {
|
|
184
|
-
question: data.card_content.prompt,
|
|
185
|
-
options: data.card_content.options,
|
|
186
|
-
},
|
|
187
|
-
concepts: data.concepts,
|
|
188
|
-
facts: data.facts,
|
|
189
|
-
bloomLevel: data.bloom_level,
|
|
190
|
-
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
return clozeCardData;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
generateClozeCardDisplayTitle(question: string, answers: Array<any>) {
|
|
197
|
-
let optionsString = '';
|
|
198
|
-
if(answers.length !== 0) {
|
|
199
|
-
optionsString = answers
|
|
200
|
-
.map((item: { option: any }) => {
|
|
201
|
-
if(item.option !== undefined) {
|
|
202
|
-
return item.option;
|
|
203
|
-
} else {
|
|
204
|
-
return '';
|
|
205
|
-
}
|
|
206
|
-
})
|
|
207
|
-
.join(", ");
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return `${question} ---- ${optionsString}`;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
parseMatchCard(cardData: any) {
|
|
214
|
-
let content = cardData.card_content;
|
|
215
|
-
|
|
216
|
-
let displayTitle = this.generateMatchCardDisplayTitle(content);
|
|
217
|
-
let matchCard = {
|
|
218
|
-
type: {
|
|
219
|
-
category: 'learning',
|
|
220
|
-
sub_type: cardData.type,
|
|
221
|
-
},
|
|
222
|
-
heading: cardData.card_reference,
|
|
223
|
-
content: content,
|
|
224
|
-
// content: cardData.card_content,
|
|
225
|
-
displayTitle: displayTitle,
|
|
226
|
-
concepts: cardData.concepts,
|
|
227
|
-
facts: cardData.facts,
|
|
228
|
-
bloomLevel: cardData.bloom_level,
|
|
229
|
-
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
return matchCard;
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
generateMatchCardDisplayTitle(answers: any) {
|
|
237
|
-
let titles: string[] = [];
|
|
238
|
-
let counter = 65;
|
|
239
|
-
for (let data of answers) {
|
|
240
|
-
|
|
241
|
-
let value = data.right_item.join(',');
|
|
242
|
-
let leftData = data.left_item;
|
|
243
|
-
let letter = String.fromCharCode(counter);
|
|
244
|
-
titles.push(`${letter}. ${leftData} -- ${value}`);
|
|
245
|
-
counter++;
|
|
32
|
+
response.metadata.status = "failed";
|
|
33
|
+
response.metadata.err_message = response.message;
|
|
34
|
+
return response;
|
|
246
35
|
}
|
|
247
|
-
let displayTitle = titles.join(",");
|
|
248
|
-
return displayTitle;
|
|
249
36
|
}
|
|
250
37
|
}
|
|
251
|
-
|
|
252
|
-
|