only_ever_generator 8.4.6 → 8.4.8

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.
Files changed (118) hide show
  1. package/dist/bootstrap/app.d.ts +6 -3
  2. package/dist/bootstrap/app.d.ts.map +1 -1
  3. package/dist/bootstrap/app.js +11 -17
  4. package/dist/bootstrap/app.js.map +1 -1
  5. package/dist/card_gen/generate_cards.d.ts +2 -3
  6. package/dist/card_gen/generate_cards.d.ts.map +1 -1
  7. package/dist/card_gen/generate_cards.js +22 -15
  8. package/dist/card_gen/generate_cards.js.map +1 -1
  9. package/dist/constants/prompt_data.d.ts +4 -4
  10. package/dist/constants/prompt_data.js +302 -302
  11. package/dist/constants/prompts/card_gen_prompt.js +160 -160
  12. package/dist/constants/prompts/typology_prompt.js +131 -131
  13. package/dist/constants/source_data.d.ts +171 -171
  14. package/dist/constants/source_data.js +973 -973
  15. package/dist/embedding_generation/local_consolidation.js +104 -104
  16. package/dist/helper/build_concept_facts_schema.d.ts +42 -42
  17. package/dist/helper/build_concept_facts_schema.js +44 -44
  18. package/dist/helper/qdrant_db_methods.d.ts.map +1 -1
  19. package/dist/helper/schema_helper/build_card_schema.d.ts +1 -9
  20. package/dist/helper/schema_helper/build_card_schema.d.ts.map +1 -1
  21. package/dist/helper/schema_helper/build_card_schema.js +47 -50
  22. package/dist/helper/schema_helper/build_card_schema.js.map +1 -1
  23. package/dist/helper/schema_helper/build_concept_facts_schema.d.ts +1 -1
  24. package/dist/helper/schema_helper/build_concept_facts_schema.d.ts.map +1 -1
  25. package/dist/helper/schema_helper/build_concept_facts_schema.js +20 -5
  26. package/dist/helper/schema_helper/build_concept_facts_schema.js.map +1 -1
  27. package/dist/helper/schema_helper/build_summary_schema.d.ts +1 -1
  28. package/dist/helper/schema_helper/build_summary_schema.d.ts.map +1 -1
  29. package/dist/helper/schema_helper/build_summary_schema.js +18 -7
  30. package/dist/helper/schema_helper/build_summary_schema.js.map +1 -1
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +5 -5
  33. package/dist/index.js.map +1 -1
  34. package/dist/parse/response_format_card.d.ts +176 -176
  35. package/dist/parse/response_format_card.js +371 -371
  36. package/dist/parse/response_format_typology.d.ts +1 -1
  37. package/dist/parse/response_format_typology.js +46 -46
  38. package/dist/services/get_prompts.d.ts +8 -7
  39. package/dist/services/get_prompts.d.ts.map +1 -1
  40. package/dist/services/get_prompts.js +69 -21
  41. package/dist/services/get_prompts.js.map +1 -1
  42. package/dist/typology_gen/generate_concept_facts.d.ts +2 -3
  43. package/dist/typology_gen/generate_concept_facts.d.ts.map +1 -1
  44. package/dist/typology_gen/generate_concept_facts.js +25 -15
  45. package/dist/typology_gen/generate_concept_facts.js.map +1 -1
  46. package/dist/typology_gen/generate_typology.d.ts +2 -1
  47. package/dist/typology_gen/generate_typology.d.ts.map +1 -1
  48. package/dist/typology_gen/generate_typology.js +24 -13
  49. package/dist/typology_gen/generate_typology.js.map +1 -1
  50. package/dist/typology_gen/summarize.d.ts +2 -3
  51. package/dist/typology_gen/summarize.d.ts.map +1 -1
  52. package/dist/typology_gen/summarize.js +24 -13
  53. package/dist/typology_gen/summarize.js.map +1 -1
  54. package/package.json +39 -38
  55. package/src/bootstrap/app.ts +418 -416
  56. package/src/card_gen/generate_cards.ts +347 -345
  57. package/src/config.ts +11 -11
  58. package/src/constants/api_constants.ts +7 -7
  59. package/src/constants/prompts/card_gen_prompt.ts +164 -164
  60. package/src/constants/prompts/typology_prompt.ts +139 -139
  61. package/src/embedding_generation/consolidation/global_consolidation.ts +96 -96
  62. package/src/embedding_generation/consolidation/local_consolidation.ts +141 -141
  63. package/src/embedding_generation/consolidation/write_consolidated_data.ts +98 -98
  64. package/src/embedding_generation/generate_embeddings.ts +42 -42
  65. package/src/embedding_generation/parse_embedding_response.ts +31 -31
  66. package/src/enums/card_type_enum.ts +6 -6
  67. package/src/gap_fill/calculate_gap_fill.ts +50 -50
  68. package/src/helper/get_id_from_title.ts +33 -33
  69. package/src/helper/mongo_helper.ts +29 -29
  70. package/src/helper/openai_helper.ts +20 -20
  71. package/src/helper/qdrant_db_methods.ts +77 -77
  72. package/src/helper/schema_helper/build_card_schema.ts +74 -98
  73. package/src/helper/schema_helper/build_classify_summarize_schema.ts +43 -43
  74. package/src/helper/schema_helper/build_concept_facts_schema.ts +45 -31
  75. package/src/helper/schema_helper/build_summary_schema.ts +43 -32
  76. package/src/index.ts +71 -73
  77. package/src/logger.ts +65 -65
  78. package/src/parse/parse_card/parse_cloze_card.ts +146 -146
  79. package/src/parse/parse_card/parse_flash_cards.ts +42 -42
  80. package/src/parse/parse_card/parse_match_card.ts +104 -104
  81. package/src/parse/parse_card/parse_mcq_card.ts +114 -114
  82. package/src/parse/parse_card_response.ts +197 -197
  83. package/src/parse/parse_source_content.ts +212 -212
  84. package/src/services/get_prompts.ts +164 -112
  85. package/src/services/open_ai_service.ts +89 -89
  86. package/src/services/qdrant_service.ts +10 -10
  87. package/src/types/base_param_type.ts +13 -13
  88. package/src/types/mongo_concept_fact_type.ts +12 -12
  89. package/src/types/parsed_card_type.ts +39 -39
  90. package/src/types/raw_card_response_types/generated_card_response_type.ts +59 -59
  91. package/src/types/source_taxonomy_type.ts +24 -24
  92. package/src/typology-parsed-response.ts +1932 -1932
  93. package/src/typology_gen/generate_concept_facts.ts +180 -169
  94. package/src/typology_gen/generate_typology.ts +203 -189
  95. package/src/typology_gen/summarize.ts +176 -164
  96. package/src/utils/distributed_quote_restoration.ts +80 -80
  97. package/src/utils/generate_args.ts +29 -29
  98. package/src/utils/parse_openai_response.ts +19 -19
  99. package/src/utils/sanitize_strings.ts +65 -65
  100. package/tsconfig.json +16 -16
  101. package/dist/constants/default_generation_variables.d.ts +0 -3
  102. package/dist/constants/default_generation_variables.d.ts.map +0 -1
  103. package/dist/constants/default_generation_variables.js +0 -580
  104. package/dist/constants/default_generation_variables.js.map +0 -1
  105. package/dist/services/prompts_test.d.ts +0 -10
  106. package/dist/services/prompts_test.d.ts.map +0 -1
  107. package/dist/services/prompts_test.js +0 -227
  108. package/dist/services/prompts_test.js.map +0 -1
  109. package/dist/types/generation_variables_schema.d.ts +0 -14
  110. package/dist/types/generation_variables_schema.d.ts.map +0 -1
  111. package/dist/types/generation_variables_schema.js +0 -3
  112. package/dist/types/generation_variables_schema.js.map +0 -1
  113. package/dist/utils/test.d.ts +0 -2
  114. package/dist/utils/test.d.ts.map +0 -1
  115. package/dist/utils/test.js +0 -5
  116. package/dist/utils/test.js.map +0 -1
  117. package/src/constants/default_generation_variables.ts +0 -624
  118. package/src/types/generation_variables_schema.ts +0 -16
@@ -1,114 +1,114 @@
1
- import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
2
- import { RawMcqCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
-
4
- export class ParseMcqCard {
5
- parse(data: {
6
- card_content: RawMcqCardResponseType;
7
- type: string;
8
- concepts_facts: MongoConceptFactCards[];
9
- bloom_level: number;
10
- explanation: string;
11
- }) {
12
- try {
13
- let mcqAnswers = [];
14
- if (
15
- data.card_content.choices !== undefined &&
16
- data.card_content.choices.length != 0
17
- ) {
18
- for (let choice of data.card_content.choices) {
19
- let answer = {
20
- answer: choice.choice,
21
- is_correct: choice.is_correct,
22
- };
23
- mcqAnswers.push(answer);
24
- }
25
- }
26
-
27
- let displayTitle = this._generateMcqCardDisplayTitle(
28
- data.card_content.prompt,
29
- mcqAnswers
30
- );
31
- let mcqCard = {
32
- type: {
33
- category: "learning",
34
- sub_type: data.type,
35
- },
36
- heading: "",
37
- displayTitle: displayTitle,
38
- content: {
39
- question: data.card_content.prompt,
40
- answers: mcqAnswers,
41
- },
42
- concepts_facts: data.concepts_facts,
43
- bloom_level: data.bloom_level,
44
- explanation: data.explanation,
45
- };
46
- // return mcqCard;
47
- const isValid = this._validate(mcqCard);
48
- if (isValid == true) {
49
- return mcqCard;
50
- } else {
51
- return null;
52
- }
53
- } catch (e) {
54
- return null;
55
- }
56
- }
57
-
58
- _generateMcqCardDisplayTitle(question: string, answers: any) {
59
- let answersString = [];
60
- if (answers.length != 0) {
61
- for (let option of answers) {
62
- let currentIndex = answers.indexOf(option) + 1;
63
- let temp = `${currentIndex} . ${option.answer} `;
64
- answersString.push(temp);
65
- }
66
- let resultString = answersString.join("");
67
- let finalDisplayTitle = `${question} ---- ${resultString}`;
68
- return finalDisplayTitle;
69
- } else {
70
- return question;
71
- }
72
- }
73
-
74
- /// validate mcq card
75
- // 1. Check if atleast 1 correct answer exists
76
- // 2. Length of answer shouldnt exceed 24 chars
77
- // 3. Length of question shouldnt exceed 90 chars
78
- // 4. If Any option is Empty
79
- _validate(mcqCard: any) {
80
- try {
81
- // let isQuestionValid = mcqCard.content.question.length <= 90;
82
- // if (!isQuestionValid) {
83
- // throw new Error("Question length exceeded");
84
- // }
85
-
86
- /// check if all are wrong answers
87
- let exists = this._checkIfAllAnswersAreWrong(mcqCard.content.answers);
88
- if (exists) {
89
- } else {
90
- throw new Error("Every answers are wrong");
91
- }
92
-
93
- /// check if answers are of length <40 or is 0
94
- // let answerWhoseLengthisGreaterThan40or0 = (
95
- // mcqCard.content.answers ?? []
96
- // ).find((e: any) => e.answer.length == 0 || e.answer.length > 40);
97
- // if (answerWhoseLengthisGreaterThan40or0) {
98
- // throw new Error("Option has length more than 40 or is Empty");
99
- // }
100
- return true;
101
- } catch (e) {
102
- return false;
103
- }
104
- }
105
-
106
- _checkIfAllAnswersAreWrong(answers: any[]) {
107
- let rightAnswer = answers.find((e) => e.is_correct == true);
108
- if (rightAnswer) {
109
- return true;
110
- } else {
111
- return false;
112
- }
113
- }
114
- }
1
+ import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
2
+ import { RawMcqCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
+
4
+ export class ParseMcqCard {
5
+ parse(data: {
6
+ card_content: RawMcqCardResponseType;
7
+ type: string;
8
+ concepts_facts: MongoConceptFactCards[];
9
+ bloom_level: number;
10
+ explanation: string;
11
+ }) {
12
+ try {
13
+ let mcqAnswers = [];
14
+ if (
15
+ data.card_content.choices !== undefined &&
16
+ data.card_content.choices.length != 0
17
+ ) {
18
+ for (let choice of data.card_content.choices) {
19
+ let answer = {
20
+ answer: choice.choice,
21
+ is_correct: choice.is_correct,
22
+ };
23
+ mcqAnswers.push(answer);
24
+ }
25
+ }
26
+
27
+ let displayTitle = this._generateMcqCardDisplayTitle(
28
+ data.card_content.prompt,
29
+ mcqAnswers
30
+ );
31
+ let mcqCard = {
32
+ type: {
33
+ category: "learning",
34
+ sub_type: data.type,
35
+ },
36
+ heading: "",
37
+ displayTitle: displayTitle,
38
+ content: {
39
+ question: data.card_content.prompt,
40
+ answers: mcqAnswers,
41
+ },
42
+ concepts_facts: data.concepts_facts,
43
+ bloom_level: data.bloom_level,
44
+ explanation: data.explanation,
45
+ };
46
+ // return mcqCard;
47
+ const isValid = this._validate(mcqCard);
48
+ if (isValid == true) {
49
+ return mcqCard;
50
+ } else {
51
+ return null;
52
+ }
53
+ } catch (e) {
54
+ return null;
55
+ }
56
+ }
57
+
58
+ _generateMcqCardDisplayTitle(question: string, answers: any) {
59
+ let answersString = [];
60
+ if (answers.length != 0) {
61
+ for (let option of answers) {
62
+ let currentIndex = answers.indexOf(option) + 1;
63
+ let temp = `${currentIndex} . ${option.answer} `;
64
+ answersString.push(temp);
65
+ }
66
+ let resultString = answersString.join("");
67
+ let finalDisplayTitle = `${question} ---- ${resultString}`;
68
+ return finalDisplayTitle;
69
+ } else {
70
+ return question;
71
+ }
72
+ }
73
+
74
+ /// validate mcq card
75
+ // 1. Check if atleast 1 correct answer exists
76
+ // 2. Length of answer shouldnt exceed 24 chars
77
+ // 3. Length of question shouldnt exceed 90 chars
78
+ // 4. If Any option is Empty
79
+ _validate(mcqCard: any) {
80
+ try {
81
+ // let isQuestionValid = mcqCard.content.question.length <= 90;
82
+ // if (!isQuestionValid) {
83
+ // throw new Error("Question length exceeded");
84
+ // }
85
+
86
+ /// check if all are wrong answers
87
+ let exists = this._checkIfAllAnswersAreWrong(mcqCard.content.answers);
88
+ if (exists) {
89
+ } else {
90
+ throw new Error("Every answers are wrong");
91
+ }
92
+
93
+ /// check if answers are of length <40 or is 0
94
+ // let answerWhoseLengthisGreaterThan40or0 = (
95
+ // mcqCard.content.answers ?? []
96
+ // ).find((e: any) => e.answer.length == 0 || e.answer.length > 40);
97
+ // if (answerWhoseLengthisGreaterThan40or0) {
98
+ // throw new Error("Option has length more than 40 or is Empty");
99
+ // }
100
+ return true;
101
+ } catch (e) {
102
+ return false;
103
+ }
104
+ }
105
+
106
+ _checkIfAllAnswersAreWrong(answers: any[]) {
107
+ let rightAnswer = answers.find((e) => e.is_correct == true);
108
+ if (rightAnswer) {
109
+ return true;
110
+ } else {
111
+ return false;
112
+ }
113
+ }
114
+ }
@@ -1,197 +1,197 @@
1
- import { ErrorLogger } from "../logger";
2
- import { ParseClozeCard } from "./parse_card/parse_cloze_card";
3
- import { ParseFlashCard } from "./parse_card/parse_flash_cards";
4
- import { ParseMatchCard } from "./parse_card/parse_match_card";
5
- import { ParseMcqCard } from "./parse_card/parse_mcq_card";
6
- import {
7
- GeneratedCardResponseType,
8
- RawClozeCardResponseType,
9
- RawFlashCardResponseType,
10
- RawMatchCardResponseType,
11
- RawMcqCardResponseType,
12
- RawTestCardResponseType,
13
- } from "../types/raw_card_response_types/generated_card_response_type";
14
- import { SourceTaxonomy } from "../types/source_taxonomy_type";
15
- import {
16
- MongoConceptFactCards,
17
- MongoConceptFactSource,
18
- } from "../types/mongo_concept_fact_type";
19
- import { ParsedCardType } from "../types/parsed_card_type";
20
- import { restoreQuotesInString } from "../utils/sanitize_strings";
21
- export class ParseCardResponse {
22
- async parse(
23
- generatedData: GeneratedCardResponseType,
24
- sourceTaxonomy: SourceTaxonomy,
25
- bloom_level: number
26
- ) {
27
- let usage_data = generatedData.metadata;
28
- try {
29
- const cardData: ParsedCardType[] = [];
30
- const unparsedTestCards = generatedData.generated_content.test_cards;
31
- console.log("Unparsed Test Cards", unparsedTestCards);
32
-
33
- if (unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
34
- for (let elem of unparsedTestCards) {
35
- elem.bloom_level = bloom_level;
36
- const concepts = (elem.concepts ?? []).map((e: any) => {
37
- return {
38
- text: restoreQuotesInString(e),
39
- type: "concept",
40
- };
41
- });
42
- const facts = (elem.facts ?? []).map((e: any) => {
43
- return {
44
- text: restoreQuotesInString(e),
45
- type: "fact",
46
- };
47
- });
48
- const concepts_facts = [...concepts, ...facts];
49
- console.log("Concepts Facts", concepts_facts);
50
- const managedCardConcepts = this._mapIdToConcepts(
51
- concepts_facts,
52
- sourceTaxonomy.concepts_facts
53
- );
54
-
55
- if (elem.type == "flash") {
56
- const flashCard = new ParseFlashCard().parse({
57
- card_content: elem.card_content as RawFlashCardResponseType,
58
- type: elem.type,
59
- concepts_facts: managedCardConcepts,
60
- bloom_level: elem.bloom_level,
61
- explanation: elem.explanation,
62
- });
63
- if (flashCard != null && flashCard) {
64
- flashCard.heading = this._getCardReference(
65
- flashCard,
66
- sourceTaxonomy
67
- );
68
-
69
- cardData.push(flashCard);
70
- }
71
- } else if (elem.type == "mcq") {
72
- const mcqCard = new ParseMcqCard().parse({
73
- card_content: elem.card_content as RawMcqCardResponseType,
74
- type: elem.type,
75
- concepts_facts: managedCardConcepts,
76
- bloom_level: elem.bloom_level,
77
- explanation: elem.explanation,
78
- });
79
-
80
- if (mcqCard != null && mcqCard) {
81
- mcqCard.heading = this._getCardReference(mcqCard, sourceTaxonomy);
82
- cardData.push(mcqCard);
83
- }
84
- } else if (elem.type == "cloze") {
85
- const clozeCard = new ParseClozeCard().parse({
86
- card_content: elem.card_content as RawClozeCardResponseType,
87
- type: elem.type,
88
- concepts_facts: managedCardConcepts,
89
- bloom_level: elem.bloom_level,
90
- explanation: elem.explanation,
91
- });
92
- if (clozeCard && clozeCard != null) {
93
- clozeCard.explanation = elem.explanation;
94
- clozeCard.heading = this._getCardReference(
95
- clozeCard,
96
- sourceTaxonomy
97
- );
98
- cardData.push(clozeCard);
99
- }
100
- } else if (elem.type == "match") {
101
- const matchCard = new ParseMatchCard().parse({
102
- card_content: elem.card_content as RawMatchCardResponseType,
103
- type: elem.type,
104
- concepts_facts: managedCardConcepts,
105
- bloom_level: elem.bloom_level,
106
- explanation: elem.explanation,
107
- });
108
- if (matchCard && matchCard != null) {
109
- matchCard.heading = this._getCardReference(
110
- matchCard,
111
- sourceTaxonomy
112
- );
113
- cardData.push(matchCard);
114
- }
115
- }
116
- }
117
- } else {
118
- usage_data.status = "failed";
119
- }
120
- if (cardData.length == 0) {
121
- usage_data.status = "failed";
122
- }
123
-
124
- return {
125
- status_code: cardData.length == 0 ? 400 : 200,
126
- metadata: usage_data,
127
- type: "card_gen",
128
-
129
- cards_data: cardData,
130
- };
131
- } catch (e: any) {
132
- await new ErrorLogger({
133
- type: "card_parsing",
134
- data: {
135
- error: e.message,
136
- generatedData: generatedData,
137
- sourceTaxonomy: sourceTaxonomy,
138
- },
139
- }).log();
140
- return {
141
- status_code: 500,
142
- metadata: usage_data,
143
- type: "card_gen",
144
- };
145
- }
146
- }
147
-
148
- _parseCard(generatedCardData: any, sourceTaxonomy: any) {
149
- const cardData: ParsedCardType[] = [];
150
- const concepts = (generatedCardData.concepts ?? []).map((e: any) => {
151
- return {
152
- text: e,
153
- type: "concept",
154
- };
155
- });
156
- }
157
- _getCardReference(card: any, sourceTaxonomy: any) {
158
- const cardConcepts = (card.concepts_facts ?? []) as MongoConceptFactCards[];
159
- const sourceConceptsFacts = (sourceTaxonomy.concepts_facts ??
160
- []) as MongoConceptFactSource[];
161
- if (cardConcepts.length == 0 || sourceConceptsFacts.length == 0) {
162
- return "";
163
- }
164
-
165
- const firstMatchedConcept = sourceConceptsFacts.find(
166
- (e: any) => cardConcepts[0].id == e.id
167
- );
168
-
169
- if (firstMatchedConcept) {
170
- return firstMatchedConcept.reference;
171
- } else {
172
- return "";
173
- }
174
- }
175
-
176
- _mapIdToConcepts(
177
- cardConcepts: any[],
178
- sourceConceptsFacts: any[]
179
- ): MongoConceptFactCards[] {
180
- // const cardData = cardC
181
- const managedCardConcepts = [];
182
- for (const cardConcept of cardConcepts) {
183
- const matchedConcept = sourceConceptsFacts.find(
184
- (e: any) =>
185
- e.text.toLowerCase().trim() == cardConcept.text.toLowerCase().trim()
186
- );
187
- if (matchedConcept) {
188
- managedCardConcepts.push({
189
- id: matchedConcept.id,
190
- text: matchedConcept.text,
191
- type: matchedConcept.type,
192
- });
193
- }
194
- }
195
- return managedCardConcepts;
196
- }
197
- }
1
+ import { ErrorLogger } from "../logger";
2
+ import { ParseClozeCard } from "./parse_card/parse_cloze_card";
3
+ import { ParseFlashCard } from "./parse_card/parse_flash_cards";
4
+ import { ParseMatchCard } from "./parse_card/parse_match_card";
5
+ import { ParseMcqCard } from "./parse_card/parse_mcq_card";
6
+ import {
7
+ GeneratedCardResponseType,
8
+ RawClozeCardResponseType,
9
+ RawFlashCardResponseType,
10
+ RawMatchCardResponseType,
11
+ RawMcqCardResponseType,
12
+ RawTestCardResponseType,
13
+ } from "../types/raw_card_response_types/generated_card_response_type";
14
+ import { SourceTaxonomy } from "../types/source_taxonomy_type";
15
+ import {
16
+ MongoConceptFactCards,
17
+ MongoConceptFactSource,
18
+ } from "../types/mongo_concept_fact_type";
19
+ import { ParsedCardType } from "../types/parsed_card_type";
20
+ import { restoreQuotesInString } from "../utils/sanitize_strings";
21
+ export class ParseCardResponse {
22
+ async parse(
23
+ generatedData: GeneratedCardResponseType,
24
+ sourceTaxonomy: SourceTaxonomy,
25
+ bloom_level: number
26
+ ) {
27
+ let usage_data = generatedData.metadata;
28
+ try {
29
+ const cardData: ParsedCardType[] = [];
30
+ const unparsedTestCards = generatedData.generated_content.test_cards;
31
+ console.log("Unparsed Test Cards", unparsedTestCards);
32
+
33
+ if (unparsedTestCards !== undefined && unparsedTestCards.length != 0) {
34
+ for (let elem of unparsedTestCards) {
35
+ elem.bloom_level = bloom_level;
36
+ const concepts = (elem.concepts ?? []).map((e: any) => {
37
+ return {
38
+ text: restoreQuotesInString(e),
39
+ type: "concept",
40
+ };
41
+ });
42
+ const facts = (elem.facts ?? []).map((e: any) => {
43
+ return {
44
+ text: restoreQuotesInString(e),
45
+ type: "fact",
46
+ };
47
+ });
48
+ const concepts_facts = [...concepts, ...facts];
49
+ console.log("Concepts Facts", concepts_facts);
50
+ const managedCardConcepts = this._mapIdToConcepts(
51
+ concepts_facts,
52
+ sourceTaxonomy.concepts_facts
53
+ );
54
+
55
+ if (elem.type == "flash") {
56
+ const flashCard = new ParseFlashCard().parse({
57
+ card_content: elem.card_content as RawFlashCardResponseType,
58
+ type: elem.type,
59
+ concepts_facts: managedCardConcepts,
60
+ bloom_level: elem.bloom_level,
61
+ explanation: elem.explanation,
62
+ });
63
+ if (flashCard != null && flashCard) {
64
+ flashCard.heading = this._getCardReference(
65
+ flashCard,
66
+ sourceTaxonomy
67
+ );
68
+
69
+ cardData.push(flashCard);
70
+ }
71
+ } else if (elem.type == "mcq") {
72
+ const mcqCard = new ParseMcqCard().parse({
73
+ card_content: elem.card_content as RawMcqCardResponseType,
74
+ type: elem.type,
75
+ concepts_facts: managedCardConcepts,
76
+ bloom_level: elem.bloom_level,
77
+ explanation: elem.explanation,
78
+ });
79
+
80
+ if (mcqCard != null && mcqCard) {
81
+ mcqCard.heading = this._getCardReference(mcqCard, sourceTaxonomy);
82
+ cardData.push(mcqCard);
83
+ }
84
+ } else if (elem.type == "cloze") {
85
+ const clozeCard = new ParseClozeCard().parse({
86
+ card_content: elem.card_content as RawClozeCardResponseType,
87
+ type: elem.type,
88
+ concepts_facts: managedCardConcepts,
89
+ bloom_level: elem.bloom_level,
90
+ explanation: elem.explanation,
91
+ });
92
+ if (clozeCard && clozeCard != null) {
93
+ clozeCard.explanation = elem.explanation;
94
+ clozeCard.heading = this._getCardReference(
95
+ clozeCard,
96
+ sourceTaxonomy
97
+ );
98
+ cardData.push(clozeCard);
99
+ }
100
+ } else if (elem.type == "match") {
101
+ const matchCard = new ParseMatchCard().parse({
102
+ card_content: elem.card_content as RawMatchCardResponseType,
103
+ type: elem.type,
104
+ concepts_facts: managedCardConcepts,
105
+ bloom_level: elem.bloom_level,
106
+ explanation: elem.explanation,
107
+ });
108
+ if (matchCard && matchCard != null) {
109
+ matchCard.heading = this._getCardReference(
110
+ matchCard,
111
+ sourceTaxonomy
112
+ );
113
+ cardData.push(matchCard);
114
+ }
115
+ }
116
+ }
117
+ } else {
118
+ usage_data.status = "failed";
119
+ }
120
+ if (cardData.length == 0) {
121
+ usage_data.status = "failed";
122
+ }
123
+
124
+ return {
125
+ status_code: cardData.length == 0 ? 400 : 200,
126
+ metadata: usage_data,
127
+ type: "card_gen",
128
+
129
+ cards_data: cardData,
130
+ };
131
+ } catch (e: any) {
132
+ await new ErrorLogger({
133
+ type: "card_parsing",
134
+ data: {
135
+ error: e.message,
136
+ generatedData: generatedData,
137
+ sourceTaxonomy: sourceTaxonomy,
138
+ },
139
+ }).log();
140
+ return {
141
+ status_code: 500,
142
+ metadata: usage_data,
143
+ type: "card_gen",
144
+ };
145
+ }
146
+ }
147
+
148
+ _parseCard(generatedCardData: any, sourceTaxonomy: any) {
149
+ const cardData: ParsedCardType[] = [];
150
+ const concepts = (generatedCardData.concepts ?? []).map((e: any) => {
151
+ return {
152
+ text: e,
153
+ type: "concept",
154
+ };
155
+ });
156
+ }
157
+ _getCardReference(card: any, sourceTaxonomy: any) {
158
+ const cardConcepts = (card.concepts_facts ?? []) as MongoConceptFactCards[];
159
+ const sourceConceptsFacts = (sourceTaxonomy.concepts_facts ??
160
+ []) as MongoConceptFactSource[];
161
+ if (cardConcepts.length == 0 || sourceConceptsFacts.length == 0) {
162
+ return "";
163
+ }
164
+
165
+ const firstMatchedConcept = sourceConceptsFacts.find(
166
+ (e: any) => cardConcepts[0].id == e.id
167
+ );
168
+
169
+ if (firstMatchedConcept) {
170
+ return firstMatchedConcept.reference;
171
+ } else {
172
+ return "";
173
+ }
174
+ }
175
+
176
+ _mapIdToConcepts(
177
+ cardConcepts: any[],
178
+ sourceConceptsFacts: any[]
179
+ ): MongoConceptFactCards[] {
180
+ // const cardData = cardC
181
+ const managedCardConcepts = [];
182
+ for (const cardConcept of cardConcepts) {
183
+ const matchedConcept = sourceConceptsFacts.find(
184
+ (e: any) =>
185
+ e.text.toLowerCase().trim() == cardConcept.text.toLowerCase().trim()
186
+ );
187
+ if (matchedConcept) {
188
+ managedCardConcepts.push({
189
+ id: matchedConcept.id,
190
+ text: matchedConcept.text,
191
+ type: matchedConcept.type,
192
+ });
193
+ }
194
+ }
195
+ return managedCardConcepts;
196
+ }
197
+ }