only_ever_generator 0.1.5 → 0.1.6
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 +8 -45
- package/dist/constants/prompts/card_gen_prompt.js +170 -170
- package/dist/constants/prompts/typology_prompt.js +89 -89
- package/dist/gap_fill/calculate_gap_fill.js +42 -0
- package/dist/index.js +54 -49
- package/dist/service/open_ai_request.js +57 -57
- package/package.json +33 -33
- package/readme.md +23 -23
- package/src/bootstrap/app.ts +128 -165
- package/src/card_gen/generate_cards.ts +197 -197
- package/src/config.ts +6 -6
- package/src/constants/api_constants.ts +2 -2
- package/src/constants/prompts/card_gen_prompt.ts +177 -177
- package/src/constants/prompts/typology_prompt.ts +93 -93
- package/src/constants/source_data.ts +35 -35
- package/src/gap_fill/calculate_gap_fill.ts +48 -0
- package/src/index.ts +66 -66
- package/src/parse/parse_card_response.ts +289 -289
- package/src/parse/parse_source_content.ts +70 -70
- package/src/parse/response_format_card.ts +210 -210
- package/src/parse/response_format_typology.ts +43 -43
- package/src/services/open_ai_service.ts +54 -54
- package/src/typology_gen/generate_typology.ts +64 -64
- package/src/utils/generate_args.ts +37 -37
- package/src/utils/parse_openai_response.ts +20 -20
- package/tsconfig.json +12 -12
|
@@ -1,197 +1,197 @@
|
|
|
1
|
-
import { OpenAiService } from "../services/open_ai_service";
|
|
2
|
-
|
|
3
|
-
export class GenerateCards {
|
|
4
|
-
openAiService: OpenAiService;
|
|
5
|
-
constructor(openAiService: OpenAiService) {
|
|
6
|
-
this.openAiService = openAiService;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async generateCards(prompt: string, parsedContent: string, isGapFill: boolean) {
|
|
10
|
-
let response = await this.openAiService?.sendRequest(prompt, parsedContent);
|
|
11
|
-
// console.log("response to card generation ", response);
|
|
12
|
-
response["type"] = isGapFill ? "gap_fill":"card_gen";
|
|
13
|
-
response.metadata = {
|
|
14
|
-
"req_time": response.generated_at,
|
|
15
|
-
"req_type": response.type,
|
|
16
|
-
"req_tokens": response.usage_data?.prompt_tokens,
|
|
17
|
-
"res_tokens": response.usage_data?.completion_tokens,
|
|
18
|
-
};
|
|
19
|
-
if(response.status_code == 200){
|
|
20
|
-
response.metadata.status = "completed";
|
|
21
|
-
return this.parse(response);
|
|
22
|
-
} else {
|
|
23
|
-
response.metadata.status = "failed";
|
|
24
|
-
return response;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
parse(generatedData: any) {
|
|
29
|
-
const cardData = [];
|
|
30
|
-
const usage_data = generatedData.metadata;
|
|
31
|
-
const created_at = generatedData.created_at;
|
|
32
|
-
const status_code = generatedData.status_code;
|
|
33
|
-
const missing_concepts = generatedData.generated_content.missing_concepts;
|
|
34
|
-
const missing_facts = generatedData.generated_content.missing_facts;
|
|
35
|
-
const unparsedTestCards = generatedData.generated_content.test_cards;
|
|
36
|
-
const type = generatedData.type;
|
|
37
|
-
if(unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
|
|
38
|
-
for (let elem of unparsedTestCards) {
|
|
39
|
-
if (elem.type == "flash") {
|
|
40
|
-
cardData.push(this.parseFlashCard(elem));
|
|
41
|
-
} else if (elem.type == "mcq") {
|
|
42
|
-
cardData.push(this.parseMcqCard(elem));
|
|
43
|
-
} else if (elem.type == "cloze") {
|
|
44
|
-
cardData.push(this.parseClozeCard(elem));
|
|
45
|
-
} else if (elem.type == "match") {
|
|
46
|
-
cardData.push(this.parseMatchCard(elem));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
usage_data["created_at"] = created_at;
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
status_code: status_code,
|
|
55
|
-
metadata: usage_data,
|
|
56
|
-
type: type,
|
|
57
|
-
missing_concepts: missing_concepts,
|
|
58
|
-
missing_facts: missing_facts,
|
|
59
|
-
cards_data: cardData,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
parseFlashCard(data: any) {
|
|
64
|
-
let displayTitle = this.generateFlashCardDisplayTitle(
|
|
65
|
-
data.card_content.front,
|
|
66
|
-
data.card_content.back
|
|
67
|
-
);
|
|
68
|
-
let flashCardData = {
|
|
69
|
-
type: data.type,
|
|
70
|
-
heading: data.card_reference,
|
|
71
|
-
displayTitle: displayTitle,
|
|
72
|
-
content: {
|
|
73
|
-
front_content: data.card_content.front,
|
|
74
|
-
back_content: data.card_content.back,
|
|
75
|
-
},
|
|
76
|
-
concepts: data.concepts,
|
|
77
|
-
facts: data.facts,
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
return flashCardData;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
generateFlashCardDisplayTitle(front: string, back: string) {
|
|
84
|
-
return `${front} ---- ${back}`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
parseMcqCard(data: any) {
|
|
88
|
-
let mcqAnswers = [];
|
|
89
|
-
for (let choice of data.card_content.choices) {
|
|
90
|
-
let answer = {
|
|
91
|
-
answer: choice.choice,
|
|
92
|
-
is_correct: choice.is_correct,
|
|
93
|
-
};
|
|
94
|
-
mcqAnswers.push(answer);
|
|
95
|
-
}
|
|
96
|
-
let displayTitle = this.generateMcqCardDisplayTitle(
|
|
97
|
-
data.card_content.prompt,
|
|
98
|
-
mcqAnswers
|
|
99
|
-
);
|
|
100
|
-
let mcqCard = {
|
|
101
|
-
type: data.type,
|
|
102
|
-
heading: data.card_reference,
|
|
103
|
-
displayTitle: displayTitle,
|
|
104
|
-
content: {
|
|
105
|
-
question: data.card_content.prompt,
|
|
106
|
-
answers: mcqAnswers,
|
|
107
|
-
},
|
|
108
|
-
concepts: data.concepts,
|
|
109
|
-
facts: data.facts,
|
|
110
|
-
};
|
|
111
|
-
return mcqCard;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
generateMcqCardDisplayTitle(question: string, answers: any) {
|
|
115
|
-
let answersString = [];
|
|
116
|
-
for (let option of answers) {
|
|
117
|
-
let currentIndex = answers.indexOf(option) + 1;
|
|
118
|
-
let temp = `${currentIndex} . ${option.answer} `;
|
|
119
|
-
answersString.push(temp);
|
|
120
|
-
}
|
|
121
|
-
let resultString = answersString.join("");
|
|
122
|
-
let finalDisplayTitle = `${question} ---- ${resultString}`;
|
|
123
|
-
return finalDisplayTitle;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
parseClozeCard(data: any) {
|
|
127
|
-
let displayTitle = this.generateClozeCardDisplayTitle(
|
|
128
|
-
data.card_content.text,
|
|
129
|
-
data.card_content.options
|
|
130
|
-
);
|
|
131
|
-
let clozeCardData = {
|
|
132
|
-
type: data.type,
|
|
133
|
-
heading: data.card_reference,
|
|
134
|
-
displayTitle: displayTitle,
|
|
135
|
-
content: {
|
|
136
|
-
question: data.card_content.text,
|
|
137
|
-
options: data.card_content.options,
|
|
138
|
-
},
|
|
139
|
-
concepts: data.concepts,
|
|
140
|
-
facts: data.facts,
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
return clozeCardData;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
generateClozeCardDisplayTitle(question: string, answers: any) {
|
|
147
|
-
let optionsString = answers
|
|
148
|
-
.map((item: { option: any }) => item.option)
|
|
149
|
-
.join(", ");
|
|
150
|
-
return `${question} ---- ${optionsString}`;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
parseMatchCard(cardData: any) {
|
|
154
|
-
let data = cardData.card_content;
|
|
155
|
-
const transformedData: { [key: string]: string[] } = {};
|
|
156
|
-
|
|
157
|
-
for (let key in data) {
|
|
158
|
-
if (data.hasOwnProperty(key)) {
|
|
159
|
-
transformedData[key] = [data[key]];
|
|
160
|
-
// let value = data[key].replace(/[\[\]]/g, '');
|
|
161
|
-
// let items = data[key].split(',').map((item: string) => item.trim());
|
|
162
|
-
// map.set(key, items);
|
|
163
|
-
// }
|
|
164
|
-
}
|
|
165
|
-
let displayTitle = this.generateMatchCardDisplayTitle(transformedData);
|
|
166
|
-
let matchCard = {
|
|
167
|
-
type: cardData.type,
|
|
168
|
-
heading: cardData.card_reference,
|
|
169
|
-
content: transformedData,
|
|
170
|
-
// content: cardData.card_content,
|
|
171
|
-
displayTitle: displayTitle,
|
|
172
|
-
concepts: cardData.concepts,
|
|
173
|
-
facts: cardData.facts,
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
return matchCard;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
generateMatchCardDisplayTitle(answers: any) {
|
|
181
|
-
let titles: string[] = [];
|
|
182
|
-
let counter = 65;
|
|
183
|
-
for (let key in answers) {
|
|
184
|
-
if (answers.hasOwnProperty(key)) {
|
|
185
|
-
let value = answers[key];
|
|
186
|
-
// let items = value.split(',').map((item: string) => item.trim());
|
|
187
|
-
// items.forEach((item: any) => {
|
|
188
|
-
let letter = String.fromCharCode(counter);
|
|
189
|
-
titles.push(`${letter}. ${key} -- ${value}`);
|
|
190
|
-
counter++;
|
|
191
|
-
// });
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
let displayTitle = titles.join(",");
|
|
195
|
-
return displayTitle;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
1
|
+
import { OpenAiService } from "../services/open_ai_service";
|
|
2
|
+
|
|
3
|
+
export class GenerateCards {
|
|
4
|
+
openAiService: OpenAiService;
|
|
5
|
+
constructor(openAiService: OpenAiService) {
|
|
6
|
+
this.openAiService = openAiService;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async generateCards(prompt: string, parsedContent: string, isGapFill: boolean) {
|
|
10
|
+
let response = await this.openAiService?.sendRequest(prompt, parsedContent);
|
|
11
|
+
// console.log("response to card generation ", response);
|
|
12
|
+
response["type"] = isGapFill ? "gap_fill":"card_gen";
|
|
13
|
+
response.metadata = {
|
|
14
|
+
"req_time": response.generated_at,
|
|
15
|
+
"req_type": response.type,
|
|
16
|
+
"req_tokens": response.usage_data?.prompt_tokens,
|
|
17
|
+
"res_tokens": response.usage_data?.completion_tokens,
|
|
18
|
+
};
|
|
19
|
+
if(response.status_code == 200){
|
|
20
|
+
response.metadata.status = "completed";
|
|
21
|
+
return this.parse(response);
|
|
22
|
+
} else {
|
|
23
|
+
response.metadata.status = "failed";
|
|
24
|
+
return response;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
parse(generatedData: any) {
|
|
29
|
+
const cardData = [];
|
|
30
|
+
const usage_data = generatedData.metadata;
|
|
31
|
+
const created_at = generatedData.created_at;
|
|
32
|
+
const status_code = generatedData.status_code;
|
|
33
|
+
const missing_concepts = generatedData.generated_content.missing_concepts;
|
|
34
|
+
const missing_facts = generatedData.generated_content.missing_facts;
|
|
35
|
+
const unparsedTestCards = generatedData.generated_content.test_cards;
|
|
36
|
+
const type = generatedData.type;
|
|
37
|
+
if(unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
|
|
38
|
+
for (let elem of unparsedTestCards) {
|
|
39
|
+
if (elem.type == "flash") {
|
|
40
|
+
cardData.push(this.parseFlashCard(elem));
|
|
41
|
+
} else if (elem.type == "mcq") {
|
|
42
|
+
cardData.push(this.parseMcqCard(elem));
|
|
43
|
+
} else if (elem.type == "cloze") {
|
|
44
|
+
cardData.push(this.parseClozeCard(elem));
|
|
45
|
+
} else if (elem.type == "match") {
|
|
46
|
+
cardData.push(this.parseMatchCard(elem));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
usage_data["created_at"] = created_at;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
status_code: status_code,
|
|
55
|
+
metadata: usage_data,
|
|
56
|
+
type: type,
|
|
57
|
+
missing_concepts: missing_concepts,
|
|
58
|
+
missing_facts: missing_facts,
|
|
59
|
+
cards_data: cardData,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
parseFlashCard(data: any) {
|
|
64
|
+
let displayTitle = this.generateFlashCardDisplayTitle(
|
|
65
|
+
data.card_content.front,
|
|
66
|
+
data.card_content.back
|
|
67
|
+
);
|
|
68
|
+
let flashCardData = {
|
|
69
|
+
type: data.type,
|
|
70
|
+
heading: data.card_reference,
|
|
71
|
+
displayTitle: displayTitle,
|
|
72
|
+
content: {
|
|
73
|
+
front_content: data.card_content.front,
|
|
74
|
+
back_content: data.card_content.back,
|
|
75
|
+
},
|
|
76
|
+
concepts: data.concepts,
|
|
77
|
+
facts: data.facts,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return flashCardData;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
generateFlashCardDisplayTitle(front: string, back: string) {
|
|
84
|
+
return `${front} ---- ${back}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
parseMcqCard(data: any) {
|
|
88
|
+
let mcqAnswers = [];
|
|
89
|
+
for (let choice of data.card_content.choices) {
|
|
90
|
+
let answer = {
|
|
91
|
+
answer: choice.choice,
|
|
92
|
+
is_correct: choice.is_correct,
|
|
93
|
+
};
|
|
94
|
+
mcqAnswers.push(answer);
|
|
95
|
+
}
|
|
96
|
+
let displayTitle = this.generateMcqCardDisplayTitle(
|
|
97
|
+
data.card_content.prompt,
|
|
98
|
+
mcqAnswers
|
|
99
|
+
);
|
|
100
|
+
let mcqCard = {
|
|
101
|
+
type: data.type,
|
|
102
|
+
heading: data.card_reference,
|
|
103
|
+
displayTitle: displayTitle,
|
|
104
|
+
content: {
|
|
105
|
+
question: data.card_content.prompt,
|
|
106
|
+
answers: mcqAnswers,
|
|
107
|
+
},
|
|
108
|
+
concepts: data.concepts,
|
|
109
|
+
facts: data.facts,
|
|
110
|
+
};
|
|
111
|
+
return mcqCard;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
generateMcqCardDisplayTitle(question: string, answers: any) {
|
|
115
|
+
let answersString = [];
|
|
116
|
+
for (let option of answers) {
|
|
117
|
+
let currentIndex = answers.indexOf(option) + 1;
|
|
118
|
+
let temp = `${currentIndex} . ${option.answer} `;
|
|
119
|
+
answersString.push(temp);
|
|
120
|
+
}
|
|
121
|
+
let resultString = answersString.join("");
|
|
122
|
+
let finalDisplayTitle = `${question} ---- ${resultString}`;
|
|
123
|
+
return finalDisplayTitle;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
parseClozeCard(data: any) {
|
|
127
|
+
let displayTitle = this.generateClozeCardDisplayTitle(
|
|
128
|
+
data.card_content.text,
|
|
129
|
+
data.card_content.options
|
|
130
|
+
);
|
|
131
|
+
let clozeCardData = {
|
|
132
|
+
type: data.type,
|
|
133
|
+
heading: data.card_reference,
|
|
134
|
+
displayTitle: displayTitle,
|
|
135
|
+
content: {
|
|
136
|
+
question: data.card_content.text,
|
|
137
|
+
options: data.card_content.options,
|
|
138
|
+
},
|
|
139
|
+
concepts: data.concepts,
|
|
140
|
+
facts: data.facts,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return clozeCardData;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
generateClozeCardDisplayTitle(question: string, answers: any) {
|
|
147
|
+
let optionsString = answers
|
|
148
|
+
.map((item: { option: any }) => item.option)
|
|
149
|
+
.join(", ");
|
|
150
|
+
return `${question} ---- ${optionsString}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
parseMatchCard(cardData: any) {
|
|
154
|
+
let data = cardData.card_content;
|
|
155
|
+
const transformedData: { [key: string]: string[] } = {};
|
|
156
|
+
|
|
157
|
+
for (let key in data) {
|
|
158
|
+
if (data.hasOwnProperty(key)) {
|
|
159
|
+
transformedData[key] = [data[key]];
|
|
160
|
+
// let value = data[key].replace(/[\[\]]/g, '');
|
|
161
|
+
// let items = data[key].split(',').map((item: string) => item.trim());
|
|
162
|
+
// map.set(key, items);
|
|
163
|
+
// }
|
|
164
|
+
}
|
|
165
|
+
let displayTitle = this.generateMatchCardDisplayTitle(transformedData);
|
|
166
|
+
let matchCard = {
|
|
167
|
+
type: cardData.type,
|
|
168
|
+
heading: cardData.card_reference,
|
|
169
|
+
content: transformedData,
|
|
170
|
+
// content: cardData.card_content,
|
|
171
|
+
displayTitle: displayTitle,
|
|
172
|
+
concepts: cardData.concepts,
|
|
173
|
+
facts: cardData.facts,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
return matchCard;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
generateMatchCardDisplayTitle(answers: any) {
|
|
181
|
+
let titles: string[] = [];
|
|
182
|
+
let counter = 65;
|
|
183
|
+
for (let key in answers) {
|
|
184
|
+
if (answers.hasOwnProperty(key)) {
|
|
185
|
+
let value = answers[key];
|
|
186
|
+
// let items = value.split(',').map((item: string) => item.trim());
|
|
187
|
+
// items.forEach((item: any) => {
|
|
188
|
+
let letter = String.fromCharCode(counter);
|
|
189
|
+
titles.push(`${letter}. ${key} -- ${value}`);
|
|
190
|
+
counter++;
|
|
191
|
+
// });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
let displayTitle = titles.join(",");
|
|
195
|
+
return displayTitle;
|
|
196
|
+
}
|
|
197
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import "dotenv/config";
|
|
2
|
-
|
|
3
|
-
const config = {
|
|
4
|
-
openAIKey : process.env.OPEN_AI_KEY as string
|
|
5
|
-
}
|
|
6
|
-
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
|
|
3
|
+
const config = {
|
|
4
|
+
openAIKey : process.env.OPEN_AI_KEY as string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
7
|
export default config;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export function openAiEndPoint(){
|
|
2
|
-
return 'https://api.openai.com/v1/chat/completions';
|
|
1
|
+
export function openAiEndPoint(){
|
|
2
|
+
return 'https://api.openai.com/v1/chat/completions';
|
|
3
3
|
}
|