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.
- package/dist/bootstrap/app.d.ts +6 -3
- package/dist/bootstrap/app.d.ts.map +1 -1
- package/dist/bootstrap/app.js +11 -17
- package/dist/bootstrap/app.js.map +1 -1
- package/dist/card_gen/generate_cards.d.ts +2 -3
- package/dist/card_gen/generate_cards.d.ts.map +1 -1
- package/dist/card_gen/generate_cards.js +22 -15
- package/dist/card_gen/generate_cards.js.map +1 -1
- package/dist/constants/prompt_data.d.ts +4 -4
- package/dist/constants/prompt_data.js +302 -302
- package/dist/constants/prompts/card_gen_prompt.js +160 -160
- package/dist/constants/prompts/typology_prompt.js +131 -131
- package/dist/constants/source_data.d.ts +171 -171
- package/dist/constants/source_data.js +973 -973
- package/dist/embedding_generation/local_consolidation.js +104 -104
- package/dist/helper/build_concept_facts_schema.d.ts +42 -42
- package/dist/helper/build_concept_facts_schema.js +44 -44
- package/dist/helper/qdrant_db_methods.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_card_schema.d.ts +1 -9
- package/dist/helper/schema_helper/build_card_schema.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_card_schema.js +47 -50
- package/dist/helper/schema_helper/build_card_schema.js.map +1 -1
- package/dist/helper/schema_helper/build_concept_facts_schema.d.ts +1 -1
- package/dist/helper/schema_helper/build_concept_facts_schema.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_concept_facts_schema.js +20 -5
- package/dist/helper/schema_helper/build_concept_facts_schema.js.map +1 -1
- package/dist/helper/schema_helper/build_summary_schema.d.ts +1 -1
- package/dist/helper/schema_helper/build_summary_schema.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_summary_schema.js +18 -7
- package/dist/helper/schema_helper/build_summary_schema.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/parse/response_format_card.d.ts +176 -176
- package/dist/parse/response_format_card.js +371 -371
- package/dist/parse/response_format_typology.d.ts +1 -1
- package/dist/parse/response_format_typology.js +46 -46
- package/dist/services/get_prompts.d.ts +8 -7
- package/dist/services/get_prompts.d.ts.map +1 -1
- package/dist/services/get_prompts.js +69 -21
- package/dist/services/get_prompts.js.map +1 -1
- package/dist/typology_gen/generate_concept_facts.d.ts +2 -3
- package/dist/typology_gen/generate_concept_facts.d.ts.map +1 -1
- package/dist/typology_gen/generate_concept_facts.js +25 -15
- package/dist/typology_gen/generate_concept_facts.js.map +1 -1
- package/dist/typology_gen/generate_typology.d.ts +2 -1
- package/dist/typology_gen/generate_typology.d.ts.map +1 -1
- package/dist/typology_gen/generate_typology.js +24 -13
- package/dist/typology_gen/generate_typology.js.map +1 -1
- package/dist/typology_gen/summarize.d.ts +2 -3
- package/dist/typology_gen/summarize.d.ts.map +1 -1
- package/dist/typology_gen/summarize.js +24 -13
- package/dist/typology_gen/summarize.js.map +1 -1
- package/package.json +39 -38
- package/src/bootstrap/app.ts +418 -416
- package/src/card_gen/generate_cards.ts +347 -345
- package/src/config.ts +11 -11
- package/src/constants/api_constants.ts +7 -7
- package/src/constants/prompts/card_gen_prompt.ts +164 -164
- package/src/constants/prompts/typology_prompt.ts +139 -139
- package/src/embedding_generation/consolidation/global_consolidation.ts +96 -96
- package/src/embedding_generation/consolidation/local_consolidation.ts +141 -141
- package/src/embedding_generation/consolidation/write_consolidated_data.ts +98 -98
- package/src/embedding_generation/generate_embeddings.ts +42 -42
- package/src/embedding_generation/parse_embedding_response.ts +31 -31
- package/src/enums/card_type_enum.ts +6 -6
- package/src/gap_fill/calculate_gap_fill.ts +50 -50
- package/src/helper/get_id_from_title.ts +33 -33
- package/src/helper/mongo_helper.ts +29 -29
- package/src/helper/openai_helper.ts +20 -20
- package/src/helper/qdrant_db_methods.ts +77 -77
- package/src/helper/schema_helper/build_card_schema.ts +74 -98
- package/src/helper/schema_helper/build_classify_summarize_schema.ts +43 -43
- package/src/helper/schema_helper/build_concept_facts_schema.ts +45 -31
- package/src/helper/schema_helper/build_summary_schema.ts +43 -32
- package/src/index.ts +71 -73
- package/src/logger.ts +65 -65
- package/src/parse/parse_card/parse_cloze_card.ts +146 -146
- package/src/parse/parse_card/parse_flash_cards.ts +42 -42
- package/src/parse/parse_card/parse_match_card.ts +104 -104
- package/src/parse/parse_card/parse_mcq_card.ts +114 -114
- package/src/parse/parse_card_response.ts +197 -197
- package/src/parse/parse_source_content.ts +212 -212
- package/src/services/get_prompts.ts +164 -112
- package/src/services/open_ai_service.ts +89 -89
- package/src/services/qdrant_service.ts +10 -10
- package/src/types/base_param_type.ts +13 -13
- package/src/types/mongo_concept_fact_type.ts +12 -12
- package/src/types/parsed_card_type.ts +39 -39
- package/src/types/raw_card_response_types/generated_card_response_type.ts +59 -59
- package/src/types/source_taxonomy_type.ts +24 -24
- package/src/typology-parsed-response.ts +1932 -1932
- package/src/typology_gen/generate_concept_facts.ts +180 -169
- package/src/typology_gen/generate_typology.ts +203 -189
- package/src/typology_gen/summarize.ts +176 -164
- package/src/utils/distributed_quote_restoration.ts +80 -80
- package/src/utils/generate_args.ts +29 -29
- package/src/utils/parse_openai_response.ts +19 -19
- package/src/utils/sanitize_strings.ts +65 -65
- package/tsconfig.json +16 -16
- package/dist/constants/default_generation_variables.d.ts +0 -3
- package/dist/constants/default_generation_variables.d.ts.map +0 -1
- package/dist/constants/default_generation_variables.js +0 -580
- package/dist/constants/default_generation_variables.js.map +0 -1
- package/dist/services/prompts_test.d.ts +0 -10
- package/dist/services/prompts_test.d.ts.map +0 -1
- package/dist/services/prompts_test.js +0 -227
- package/dist/services/prompts_test.js.map +0 -1
- package/dist/types/generation_variables_schema.d.ts +0 -14
- package/dist/types/generation_variables_schema.d.ts.map +0 -1
- package/dist/types/generation_variables_schema.js +0 -3
- package/dist/types/generation_variables_schema.js.map +0 -1
- package/dist/utils/test.d.ts +0 -2
- package/dist/utils/test.d.ts.map +0 -1
- package/dist/utils/test.js +0 -5
- package/dist/utils/test.js.map +0 -1
- package/src/constants/default_generation_variables.ts +0 -624
- package/src/types/generation_variables_schema.ts +0 -16
|
@@ -1,345 +1,347 @@
|
|
|
1
|
-
import { OpenAIHelper } from "../helper/openai_helper";
|
|
2
|
-
import { buildCardSchema } from "../helper/schema_helper/build_card_schema";
|
|
3
|
-
import { ErrorLogger, log_error, ParsingError } from "../logger";
|
|
4
|
-
import { ParseCardResponse } from "../parse/parse_card_response";
|
|
5
|
-
import { OpenAiService } from "../services/open_ai_service";
|
|
6
|
-
import { GeneratedCardResponseType } from "../types/raw_card_response_types/generated_card_response_type";
|
|
7
|
-
import { SourceTaxonomy } from "../types/source_taxonomy_type";
|
|
8
|
-
import { ParsedCardType } from "../types/parsed_card_type";
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
// Type for the response structure returned by generateCardsForType
|
|
14
|
-
type GenerateCardsResponse = {
|
|
15
|
-
status_code: number;
|
|
16
|
-
metadata: any;
|
|
17
|
-
type: string;
|
|
18
|
-
cards_data?: ParsedCardType[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// Type for list/array of GenerateCardsResponse (including undefined)
|
|
22
|
-
export type GenerateCardsResponseList = (GenerateCardsResponse | undefined)[];
|
|
23
|
-
|
|
24
|
-
export class GenerateCards {
|
|
25
|
-
openAiService: OpenAiService;
|
|
26
|
-
public content: string;
|
|
27
|
-
public sourceId: string;
|
|
28
|
-
public taxonomy: SourceTaxonomy;
|
|
29
|
-
public source_type: string;
|
|
30
|
-
openAIHelper: OpenAIHelper;
|
|
31
|
-
public
|
|
32
|
-
public
|
|
33
|
-
constructor(
|
|
34
|
-
openAiService: OpenAiService,
|
|
35
|
-
sourceId: string,
|
|
36
|
-
content: string,
|
|
37
|
-
taxonomy: SourceTaxonomy,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
45
|
-
this.
|
|
46
|
-
this.
|
|
47
|
-
this.
|
|
48
|
-
this.
|
|
49
|
-
this.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
1
|
+
import { OpenAIHelper } from "../helper/openai_helper";
|
|
2
|
+
import { buildCardSchema } from "../helper/schema_helper/build_card_schema";
|
|
3
|
+
import { ErrorLogger, log_error, ParsingError } from "../logger";
|
|
4
|
+
import { ParseCardResponse } from "../parse/parse_card_response";
|
|
5
|
+
import { OpenAiService } from "../services/open_ai_service";
|
|
6
|
+
import { GeneratedCardResponseType } from "../types/raw_card_response_types/generated_card_response_type";
|
|
7
|
+
import { SourceTaxonomy } from "../types/source_taxonomy_type";
|
|
8
|
+
import { ParsedCardType } from "../types/parsed_card_type";
|
|
9
|
+
import { getPrompts } from "../services/get_prompts";
|
|
10
|
+
import axios from "axios";
|
|
11
|
+
import { jsonrepair } from "jsonrepair";
|
|
12
|
+
|
|
13
|
+
// Type for the response structure returned by generateCardsForType
|
|
14
|
+
type GenerateCardsResponse = {
|
|
15
|
+
status_code: number;
|
|
16
|
+
metadata: any;
|
|
17
|
+
type: string;
|
|
18
|
+
cards_data?: ParsedCardType[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Type for list/array of GenerateCardsResponse (including undefined)
|
|
22
|
+
export type GenerateCardsResponseList = (GenerateCardsResponse | undefined)[];
|
|
23
|
+
|
|
24
|
+
export class GenerateCards {
|
|
25
|
+
openAiService: OpenAiService;
|
|
26
|
+
public content: string;
|
|
27
|
+
public sourceId: string;
|
|
28
|
+
public taxonomy: SourceTaxonomy;
|
|
29
|
+
public source_type: string;
|
|
30
|
+
openAIHelper: OpenAIHelper;
|
|
31
|
+
public promptIdForCardsGeneration: string;
|
|
32
|
+
public generationCurriculum: boolean;
|
|
33
|
+
constructor(
|
|
34
|
+
openAiService: OpenAiService,
|
|
35
|
+
sourceId: string,
|
|
36
|
+
content: string,
|
|
37
|
+
taxonomy: SourceTaxonomy,
|
|
38
|
+
|
|
39
|
+
promptIdForCardsGeneration: string,
|
|
40
|
+
generationCurriculum: boolean,
|
|
41
|
+
source_type: string
|
|
42
|
+
) {
|
|
43
|
+
this.openAiService = openAiService;
|
|
44
|
+
this.openAIHelper = new OpenAIHelper(this.openAiService.api_key);
|
|
45
|
+
this.sourceId = sourceId;
|
|
46
|
+
this.content = content;
|
|
47
|
+
this.taxonomy = taxonomy;
|
|
48
|
+
this.promptIdForCardsGeneration = promptIdForCardsGeneration;
|
|
49
|
+
this.generationCurriculum = generationCurriculum;
|
|
50
|
+
this.source_type = source_type;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async openAIResponse(card_generation_types: string[]) {
|
|
54
|
+
const variables = await getPrompts(this.source_type, card_generation_types);
|
|
55
|
+
const concepts = this.taxonomy.concepts_facts
|
|
56
|
+
.filter((concept) => concept.type === "concept")
|
|
57
|
+
.map((concept) => concept.text);
|
|
58
|
+
const facts = this.taxonomy.concepts_facts
|
|
59
|
+
.filter((fact) => fact.type === "fact")
|
|
60
|
+
.map((fact) => fact.text);
|
|
61
|
+
const schema = await buildCardSchema(
|
|
62
|
+
concepts,
|
|
63
|
+
facts,
|
|
64
|
+
card_generation_types
|
|
65
|
+
);
|
|
66
|
+
try {
|
|
67
|
+
let response: any = await this.openAIHelper.openAI.responses.create({
|
|
68
|
+
prompt: {
|
|
69
|
+
id: this.promptIdForCardsGeneration,
|
|
70
|
+
variables: {
|
|
71
|
+
bloom_level: "1",
|
|
72
|
+
card_types: card_generation_types.join(","),
|
|
73
|
+
bloom_instructions: variables.bloom_instructions,
|
|
74
|
+
card_instructions: variables.card_instructions,
|
|
75
|
+
card_examples: variables.card_examples,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
max_output_tokens: 30000,
|
|
79
|
+
|
|
80
|
+
input: [
|
|
81
|
+
{
|
|
82
|
+
role: "user",
|
|
83
|
+
content: [{ type: "input_text", text: this.content }],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
store: true,
|
|
87
|
+
text: {
|
|
88
|
+
format: {
|
|
89
|
+
type: "json_schema",
|
|
90
|
+
name: schema.name,
|
|
91
|
+
strict: schema.strict,
|
|
92
|
+
schema: schema.schema,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (!response) {
|
|
98
|
+
await log_error({
|
|
99
|
+
flow: this.generationCurriculum
|
|
100
|
+
? "curriculum_manual_generation"
|
|
101
|
+
: "manual_generation",
|
|
102
|
+
data: JSON.stringify(response),
|
|
103
|
+
timestamp: new Date(),
|
|
104
|
+
error: "empty_openai_response",
|
|
105
|
+
source_id: this.sourceId,
|
|
106
|
+
type: {
|
|
107
|
+
request_type: "depth",
|
|
108
|
+
n: 1,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return response;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (error instanceof ParsingError) {
|
|
117
|
+
await log_error({
|
|
118
|
+
flow: this.generationCurriculum
|
|
119
|
+
? "curriculum_manual_generation"
|
|
120
|
+
: "manual_generation",
|
|
121
|
+
data: error,
|
|
122
|
+
timestamp: new Date(),
|
|
123
|
+
error: "parsing_error",
|
|
124
|
+
source_id: this.sourceId,
|
|
125
|
+
type: {
|
|
126
|
+
request_type: "depth",
|
|
127
|
+
n: 1,
|
|
128
|
+
card_types: card_generation_types.join(","),
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
console.log(error);
|
|
133
|
+
await log_error({
|
|
134
|
+
flow: this.generationCurriculum
|
|
135
|
+
? "curriculum_manual_generation"
|
|
136
|
+
: "manual_generation",
|
|
137
|
+
data: error,
|
|
138
|
+
timestamp: new Date(),
|
|
139
|
+
error: "error_in_card_generation",
|
|
140
|
+
source_id: this.sourceId,
|
|
141
|
+
type: {
|
|
142
|
+
request_type: "depth",
|
|
143
|
+
n: 1,
|
|
144
|
+
card_types: card_generation_types.join(","),
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async generateCards(
|
|
152
|
+
n: number,
|
|
153
|
+
card_generation_types: string[]
|
|
154
|
+
): Promise<GenerateCardsResponseList> {
|
|
155
|
+
try {
|
|
156
|
+
// Use Promise.allSettled to allow individual types to fail without affecting others
|
|
157
|
+
const settledResponses = await Promise.allSettled(
|
|
158
|
+
card_generation_types.map(async (card_generation_type) => {
|
|
159
|
+
return await this.generateCardsForType(n, card_generation_type);
|
|
160
|
+
})
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
// Extract fulfilled responses and log rejected ones
|
|
164
|
+
let responses: GenerateCardsResponseList = settledResponses
|
|
165
|
+
.map((result, index) => {
|
|
166
|
+
if (result.status === "fulfilled") {
|
|
167
|
+
if (result.value) {
|
|
168
|
+
return result.value;
|
|
169
|
+
} else {
|
|
170
|
+
// Log when fulfilled but value is undefined
|
|
171
|
+
console.error(
|
|
172
|
+
`Card generation for type ${card_generation_types[index]} returned undefined`
|
|
173
|
+
);
|
|
174
|
+
log_error({
|
|
175
|
+
flow: this.generationCurriculum
|
|
176
|
+
? "curriculum_manual_generation"
|
|
177
|
+
: "manual_generation",
|
|
178
|
+
data: "Fulfilled promise returned undefined value",
|
|
179
|
+
timestamp: new Date(),
|
|
180
|
+
error: "undefined_value_in_card_generation",
|
|
181
|
+
source_id: this.sourceId,
|
|
182
|
+
type: {
|
|
183
|
+
request_type: "depth",
|
|
184
|
+
n: n,
|
|
185
|
+
card_types: card_generation_types[index],
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
// Log the rejection but continue with other types
|
|
192
|
+
console.error(
|
|
193
|
+
`Failed to generate cards for type ${card_generation_types[index]}:`,
|
|
194
|
+
result.reason
|
|
195
|
+
);
|
|
196
|
+
log_error({
|
|
197
|
+
flow: this.generationCurriculum
|
|
198
|
+
? "curriculum_manual_generation"
|
|
199
|
+
: "manual_generation",
|
|
200
|
+
data: result.reason,
|
|
201
|
+
timestamp: new Date(),
|
|
202
|
+
error: "error_in_card_generation_for_type",
|
|
203
|
+
source_id: this.sourceId,
|
|
204
|
+
type: {
|
|
205
|
+
request_type: "depth",
|
|
206
|
+
n: n,
|
|
207
|
+
card_types: card_generation_types[index],
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
.filter(
|
|
214
|
+
(response): response is GenerateCardsResponse =>
|
|
215
|
+
response !== undefined
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
// Flatten all parsed cards from all responses
|
|
219
|
+
// const allParsedCards: AllParsedCards = [];
|
|
220
|
+
// for (const response of responses) {
|
|
221
|
+
// if (response && response.cards_data) {
|
|
222
|
+
// allParsedCards.push(...response.cards_data);
|
|
223
|
+
// }
|
|
224
|
+
// }
|
|
225
|
+
|
|
226
|
+
return responses;
|
|
227
|
+
} catch (e: any) {
|
|
228
|
+
await log_error({
|
|
229
|
+
flow: this.generationCurriculum
|
|
230
|
+
? "curriculum_manual_generation"
|
|
231
|
+
: "manual_generation",
|
|
232
|
+
data: e,
|
|
233
|
+
timestamp: new Date(),
|
|
234
|
+
error: "error_in_card_generation",
|
|
235
|
+
source_id: this.sourceId,
|
|
236
|
+
type: {
|
|
237
|
+
request_type: "depth",
|
|
238
|
+
n: n,
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
return [];
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async generateCardsForType(
|
|
246
|
+
n: number,
|
|
247
|
+
card_generation_type: string
|
|
248
|
+
): Promise<GenerateCardsResponse | undefined> {
|
|
249
|
+
try {
|
|
250
|
+
let response = await this.openAIResponse([card_generation_type]);
|
|
251
|
+
response.metadata = {
|
|
252
|
+
req_time: response.generated_at ?? new Date(),
|
|
253
|
+
req_type: {
|
|
254
|
+
type: "depth",
|
|
255
|
+
n: n,
|
|
256
|
+
card_types: card_generation_type,
|
|
257
|
+
bloom_level: 1,
|
|
258
|
+
},
|
|
259
|
+
req_tokens: response.usage_data?.prompt_tokens,
|
|
260
|
+
res_tokens: response.usage_data?.completion_tokens,
|
|
261
|
+
prompt: {
|
|
262
|
+
id: response.prompt.id,
|
|
263
|
+
version: response.prompt.version,
|
|
264
|
+
},
|
|
265
|
+
prompt_tokens_details: response.usage_data?.prompt_tokens_details,
|
|
266
|
+
model: response.model,
|
|
267
|
+
};
|
|
268
|
+
if (response.status == "completed") {
|
|
269
|
+
response.metadata.status = "completed";
|
|
270
|
+
/// map response to generated_content
|
|
271
|
+
|
|
272
|
+
const testCardsArray = this.parseJson(response);
|
|
273
|
+
const generated_data: GeneratedCardResponseType = {
|
|
274
|
+
metadata: response.metadata,
|
|
275
|
+
generated_content: {
|
|
276
|
+
test_cards: testCardsArray.map((card: any) => {
|
|
277
|
+
return {
|
|
278
|
+
type: card.type,
|
|
279
|
+
card_content: card.card_content,
|
|
280
|
+
concepts: card.concepts ?? [],
|
|
281
|
+
facts: card.facts ?? [],
|
|
282
|
+
bloom_level: card.bloom_level ?? 1,
|
|
283
|
+
explanation: card.explanation ?? "",
|
|
284
|
+
};
|
|
285
|
+
}),
|
|
286
|
+
},
|
|
287
|
+
usage_data: response.usage,
|
|
288
|
+
generated_at: response.created_at,
|
|
289
|
+
status_code: response.status == "completed" ? 200 : 400,
|
|
290
|
+
};
|
|
291
|
+
let parseCard = new ParseCardResponse().parse(
|
|
292
|
+
generated_data,
|
|
293
|
+
this.taxonomy,
|
|
294
|
+
1
|
|
295
|
+
);
|
|
296
|
+
return parseCard;
|
|
297
|
+
} else {
|
|
298
|
+
response.metadata.status = "failed";
|
|
299
|
+
response.metadata.err_message = response.message;
|
|
300
|
+
return response;
|
|
301
|
+
}
|
|
302
|
+
} catch (e: any) {
|
|
303
|
+
await log_error({
|
|
304
|
+
flow: this.generationCurriculum
|
|
305
|
+
? "curriculum_manual_generation"
|
|
306
|
+
: "manual_generation",
|
|
307
|
+
data: e,
|
|
308
|
+
timestamp: new Date(),
|
|
309
|
+
error: "error_in_card_generation",
|
|
310
|
+
source_id: this.sourceId,
|
|
311
|
+
type: {
|
|
312
|
+
request_type: "depth",
|
|
313
|
+
n: n,
|
|
314
|
+
card_types: card_generation_type,
|
|
315
|
+
bloom_level: 1,
|
|
316
|
+
},
|
|
317
|
+
});
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
parseJson(response: any) {
|
|
323
|
+
try {
|
|
324
|
+
const testCardsArray = JSON.parse(response.output_text).test_cards;
|
|
325
|
+
return testCardsArray;
|
|
326
|
+
} catch (e) {
|
|
327
|
+
// If original JSON parse fails, attempt to repair and parse
|
|
328
|
+
try {
|
|
329
|
+
const repairedJson = jsonrepair(response.output_text);
|
|
330
|
+
const parsedJson = JSON.parse(repairedJson);
|
|
331
|
+
if (parsedJson.test_cards) {
|
|
332
|
+
return parsedJson.test_cards;
|
|
333
|
+
}
|
|
334
|
+
throw new ParsingError(
|
|
335
|
+
"Something went wrong in parsing the json",
|
|
336
|
+
response
|
|
337
|
+
);
|
|
338
|
+
} catch (repairError) {
|
|
339
|
+
// If repair also fails, throw exception
|
|
340
|
+
throw new ParsingError(
|
|
341
|
+
"Something went wrong in repairing the json",
|
|
342
|
+
response
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|