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
package/src/bootstrap/app.ts
CHANGED
|
@@ -1,416 +1,418 @@
|
|
|
1
|
-
import {
|
|
2
|
-
GenerateCards,
|
|
3
|
-
GenerateCardsResponseList,
|
|
4
|
-
} from "../card_gen/generate_cards";
|
|
5
|
-
import { ParseSourceContent } from "../parse/parse_source_content";
|
|
6
|
-
import { OpenAiService } from "../services/open_ai_service";
|
|
7
|
-
import { GenerateTypology } from "../typology_gen/generate_typology";
|
|
8
|
-
import { GenerateArgs } from "../utils/generate_args";
|
|
9
|
-
import { GenerateEmbeddings } from "../embedding_generation/generate_embeddings";
|
|
10
|
-
import { LocalConsolidation } from "../embedding_generation/consolidation/local_consolidation";
|
|
11
|
-
import { GlobalConsolidation } from "../embedding_generation/consolidation/global_consolidation";
|
|
12
|
-
import { BaseParamType } from "../types/base_param_type";
|
|
13
|
-
import { SourceTaxonomy } from "../types/source_taxonomy_type";
|
|
14
|
-
import { MongoConceptFactSource } from "../types/mongo_concept_fact_type";
|
|
15
|
-
import { GenerateConceptFacts } from "../typology_gen/generate_concept_facts";
|
|
16
|
-
import { GenerateSummaryCards } from "../typology_gen/summarize";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
public
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
public
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
61
|
-
|
|
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
|
-
const
|
|
90
|
-
const
|
|
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
|
-
const
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if
|
|
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
|
-
summaryCardsResponse
|
|
173
|
-
summaryCardsResponse.summary_cards
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
this.typologyResponse.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
conceptFactsResponse
|
|
190
|
-
conceptFactsResponse.concepts_facts
|
|
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
|
-
this.typologyResponse!.
|
|
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
|
-
this.typologyResponse.concepts_facts
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
this.
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
this.
|
|
296
|
-
this.
|
|
297
|
-
this.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
this.
|
|
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
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
1
|
+
import {
|
|
2
|
+
GenerateCards,
|
|
3
|
+
GenerateCardsResponseList,
|
|
4
|
+
} from "../card_gen/generate_cards";
|
|
5
|
+
import { ParseSourceContent } from "../parse/parse_source_content";
|
|
6
|
+
import { OpenAiService } from "../services/open_ai_service";
|
|
7
|
+
import { GenerateTypology } from "../typology_gen/generate_typology";
|
|
8
|
+
import { GenerateArgs } from "../utils/generate_args";
|
|
9
|
+
import { GenerateEmbeddings } from "../embedding_generation/generate_embeddings";
|
|
10
|
+
import { LocalConsolidation } from "../embedding_generation/consolidation/local_consolidation";
|
|
11
|
+
import { GlobalConsolidation } from "../embedding_generation/consolidation/global_consolidation";
|
|
12
|
+
import { BaseParamType } from "../types/base_param_type";
|
|
13
|
+
import { SourceTaxonomy } from "../types/source_taxonomy_type";
|
|
14
|
+
import { MongoConceptFactSource } from "../types/mongo_concept_fact_type";
|
|
15
|
+
import { GenerateConceptFacts } from "../typology_gen/generate_concept_facts";
|
|
16
|
+
import { GenerateSummaryCards } from "../typology_gen/summarize";
|
|
17
|
+
|
|
18
|
+
export class OnlyEverGenerator {
|
|
19
|
+
public api_key: string = "";
|
|
20
|
+
public openAiService: OpenAiService;
|
|
21
|
+
|
|
22
|
+
/// these fields will be populated inside the constructor
|
|
23
|
+
parsedContent?: {
|
|
24
|
+
title: string;
|
|
25
|
+
headings: string[];
|
|
26
|
+
content: any[];
|
|
27
|
+
taxonomy: SourceTaxonomy;
|
|
28
|
+
source_id: string;
|
|
29
|
+
type: string;
|
|
30
|
+
} = undefined;
|
|
31
|
+
public generationCurriculum: boolean = false;
|
|
32
|
+
public promptIdForTypology: string;
|
|
33
|
+
public promptIdForConceptFacts: string;
|
|
34
|
+
public promptIdForCardsGeneration: string;
|
|
35
|
+
public promptIdForSummaryCards: string;
|
|
36
|
+
public n: number = 0;
|
|
37
|
+
|
|
38
|
+
expectedFields: Array<string> = [];
|
|
39
|
+
typologyResponse?: any = undefined;
|
|
40
|
+
cardgenResponse: GenerateCardsResponseList = [];
|
|
41
|
+
summarizeResponse = {};
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
apiKey: string,
|
|
45
|
+
model: string,
|
|
46
|
+
generationContent: BaseParamType,
|
|
47
|
+
promptIdForTypology: string,
|
|
48
|
+
promptIdForConceptFacts: string,
|
|
49
|
+
promptIdForCardsGeneration: string,
|
|
50
|
+
promptIdForSummaryCards: string,
|
|
51
|
+
|
|
52
|
+
generationCurriculum: boolean,
|
|
53
|
+
n: number | undefined
|
|
54
|
+
) {
|
|
55
|
+
this.promptIdForTypology = promptIdForTypology;
|
|
56
|
+
this.promptIdForConceptFacts = promptIdForConceptFacts;
|
|
57
|
+
this.promptIdForCardsGeneration = promptIdForCardsGeneration;
|
|
58
|
+
this.promptIdForSummaryCards = promptIdForSummaryCards;
|
|
59
|
+
this.api_key = apiKey;
|
|
60
|
+
this.generationCurriculum = generationCurriculum;
|
|
61
|
+
this.n = n ?? 1;
|
|
62
|
+
this.openAiService = new OpenAiService(
|
|
63
|
+
apiKey,
|
|
64
|
+
model ?? "gpt-3.5-turbo-1106"
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const parsedData = new ParseSourceContent(
|
|
68
|
+
generationContent.content
|
|
69
|
+
).parseData();
|
|
70
|
+
(this.parsedContent = {
|
|
71
|
+
title: parsedData.title,
|
|
72
|
+
headings: parsedData.headings,
|
|
73
|
+
content: parsedData.content,
|
|
74
|
+
taxonomy: parsedData.taxonomy,
|
|
75
|
+
source_id: parsedData.source_id,
|
|
76
|
+
type: parsedData.type,
|
|
77
|
+
}),
|
|
78
|
+
(this.typologyResponse = generationContent.content.taxonomy);
|
|
79
|
+
|
|
80
|
+
this.expectedFields = generationContent.content.fields; //returnFields();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async generate(
|
|
84
|
+
generate_typology: boolean = false,
|
|
85
|
+
generate_card: boolean = false,
|
|
86
|
+
card_generation_types: string[] = []
|
|
87
|
+
): Promise<Array<any>> {
|
|
88
|
+
try {
|
|
89
|
+
const args = new GenerateArgs(generate_card, generate_typology, false);
|
|
90
|
+
const responseToReturn: Array<any> = [];
|
|
91
|
+
const whatNeedsToBeGenerated = args.getWhatNeedsToBeGenerated();
|
|
92
|
+
|
|
93
|
+
for (const elem of whatNeedsToBeGenerated) {
|
|
94
|
+
if (elem === "generate_tyopology") {
|
|
95
|
+
const typologyResults = await this.processTypologyGeneration();
|
|
96
|
+
responseToReturn.push(...typologyResults);
|
|
97
|
+
} else if (elem === "generate_card") {
|
|
98
|
+
const cardResult = await this.processCardGeneration(
|
|
99
|
+
card_generation_types
|
|
100
|
+
);
|
|
101
|
+
if (cardResult) {
|
|
102
|
+
responseToReturn.push(cardResult);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return responseToReturn;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.log(error);
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private async processTypologyGeneration(): Promise<Array<any>> {
|
|
115
|
+
this.typologyResponse = await this.generateTypology();
|
|
116
|
+
console.log(this.typologyResponse?.generate_cards?.value);
|
|
117
|
+
|
|
118
|
+
const results: Array<any> = [];
|
|
119
|
+
const isSuccessful = this.typologyResponse?.status_code === 200;
|
|
120
|
+
const learnValueThreshold =
|
|
121
|
+
(this.typologyResponse?.generate_cards?.value ?? 30) > 30;
|
|
122
|
+
|
|
123
|
+
if (!isSuccessful || !learnValueThreshold) {
|
|
124
|
+
results.push({
|
|
125
|
+
type: "typology",
|
|
126
|
+
data: this.typologyResponse,
|
|
127
|
+
});
|
|
128
|
+
return results;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Process summary cards and concept facts
|
|
132
|
+
await this.processSummaryCardsAndConceptFacts();
|
|
133
|
+
|
|
134
|
+
// Only continue if summary cards were generated
|
|
135
|
+
if (this.typologyResponse.summary_cards.length === 0) {
|
|
136
|
+
return results;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Process embeddings and consolidation if concept facts exist
|
|
140
|
+
const hasConceptFacts = this.typologyResponse.concepts_facts.length > 0;
|
|
141
|
+
if (hasConceptFacts) {
|
|
142
|
+
const qdrantOps = await this.processEmbeddingsAndConsolidation();
|
|
143
|
+
results.push({
|
|
144
|
+
type: "typology",
|
|
145
|
+
data: this.typologyResponse,
|
|
146
|
+
qdrantOps,
|
|
147
|
+
});
|
|
148
|
+
} else {
|
|
149
|
+
results.push({
|
|
150
|
+
type: "typology",
|
|
151
|
+
data: this.typologyResponse,
|
|
152
|
+
qdrantOps: { update: [], inserts: [] },
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Add summary cards response if generated
|
|
157
|
+
if (this.typologyResponse.summary_cards.length > 0) {
|
|
158
|
+
results.push({
|
|
159
|
+
type: "summary_cards",
|
|
160
|
+
data: this.typologyResponse,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return results;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private async processSummaryCardsAndConceptFacts(): Promise<void> {
|
|
168
|
+
console.log("generating summary cards");
|
|
169
|
+
const summaryCardsResponse = await this.generateSummaryCards();
|
|
170
|
+
|
|
171
|
+
if (
|
|
172
|
+
summaryCardsResponse &&
|
|
173
|
+
summaryCardsResponse.summary_cards &&
|
|
174
|
+
summaryCardsResponse.summary_cards.length > 0
|
|
175
|
+
) {
|
|
176
|
+
this.typologyResponse.summary_cards = summaryCardsResponse.summary_cards;
|
|
177
|
+
this.typologyResponse.metadata.push(summaryCardsResponse.metadata);
|
|
178
|
+
} else {
|
|
179
|
+
this.typologyResponse.summary_cards = [];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (this.typologyResponse.summary_cards.length === 0) return;
|
|
183
|
+
|
|
184
|
+
const conceptFactsResponse = await this.generateConceptFacts(
|
|
185
|
+
this.typologyResponse
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
if (
|
|
189
|
+
conceptFactsResponse &&
|
|
190
|
+
conceptFactsResponse.concepts_facts &&
|
|
191
|
+
conceptFactsResponse.concepts_facts.length > 0
|
|
192
|
+
) {
|
|
193
|
+
this.typologyResponse.concepts_facts =
|
|
194
|
+
conceptFactsResponse.concepts_facts;
|
|
195
|
+
this.typologyResponse.metadata.push(conceptFactsResponse.metadata);
|
|
196
|
+
} else {
|
|
197
|
+
this.typologyResponse.concepts_facts = [];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private async processEmbeddingsAndConsolidation(): Promise<{
|
|
202
|
+
update: any[];
|
|
203
|
+
inserts: any[];
|
|
204
|
+
}> {
|
|
205
|
+
console.log(this.typologyResponse);
|
|
206
|
+
const embeddings = await this.generateEmbeddings(this.typologyResponse);
|
|
207
|
+
console.log("Embedding generated");
|
|
208
|
+
|
|
209
|
+
const localConsolidation = await this.localConsolidation(
|
|
210
|
+
embeddings.concepts_facts,
|
|
211
|
+
this.parsedContent!.title
|
|
212
|
+
);
|
|
213
|
+
console.log("Local consolidation done");
|
|
214
|
+
this.typologyResponse!.concepts_facts =
|
|
215
|
+
localConsolidation.sourceTaxonomyOps;
|
|
216
|
+
|
|
217
|
+
const globalConsolidation = await this.globalConsolidation(
|
|
218
|
+
localConsolidation.sourceTaxonomyOps,
|
|
219
|
+
this.parsedContent!.source_id,
|
|
220
|
+
0.8
|
|
221
|
+
);
|
|
222
|
+
console.log("Global consolidation done");
|
|
223
|
+
|
|
224
|
+
this.typologyResponse!.concepts_facts = globalConsolidation.source_taxonomy;
|
|
225
|
+
this.typologyResponse!.metadata?.push(embeddings.metadata);
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
update: globalConsolidation.global_updates,
|
|
229
|
+
inserts: globalConsolidation.global_inserts,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private async processCardGeneration(
|
|
234
|
+
card_generation_types: string[]
|
|
235
|
+
): Promise<any | null> {
|
|
236
|
+
if (!this.shouldTheCardBeGeneratedAfterTypologyResponse()) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
console.log("Generating card");
|
|
241
|
+
this.parsedContent!.taxonomy = {
|
|
242
|
+
concepts_facts: this.typologyResponse?.concepts_facts ?? [],
|
|
243
|
+
fields: this.expectedFields,
|
|
244
|
+
summary_cards: this.typologyResponse?.summary_cards ?? [],
|
|
245
|
+
learn_value: this.typologyResponse?.learn_value ?? {
|
|
246
|
+
value: 0,
|
|
247
|
+
reason: "",
|
|
248
|
+
bloom_levels: [],
|
|
249
|
+
bloom_levels_reason: "",
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
this.cardgenResponse = await this.generateCard(
|
|
254
|
+
this.n,
|
|
255
|
+
card_generation_types
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
type: "card_gen",
|
|
260
|
+
data: this.cardgenResponse,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
shouldTheCardBeGeneratedAfterTypologyResponse() {
|
|
265
|
+
if (this.typologyResponse) {
|
|
266
|
+
const hasConceptsFacts =
|
|
267
|
+
this.typologyResponse?.concepts_facts &&
|
|
268
|
+
Array.isArray(this.typologyResponse.concepts_facts) &&
|
|
269
|
+
this.typologyResponse.concepts_facts.length > 0;
|
|
270
|
+
|
|
271
|
+
const learnValueAboveThreshold =
|
|
272
|
+
(this.typologyResponse?.generate_cards?.value ??
|
|
273
|
+
this.typologyResponse?.learn_value?.value ??
|
|
274
|
+
30) > 30;
|
|
275
|
+
|
|
276
|
+
return hasConceptsFacts && learnValueAboveThreshold;
|
|
277
|
+
} else {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async generateCard(
|
|
283
|
+
n: number,
|
|
284
|
+
card_generation_types: string[]
|
|
285
|
+
): Promise<GenerateCardsResponseList> {
|
|
286
|
+
const content = {
|
|
287
|
+
title: this.parsedContent?.title,
|
|
288
|
+
concepts_facts: this.typologyResponse?.concepts_facts,
|
|
289
|
+
content: this.parsedContent?.content,
|
|
290
|
+
};
|
|
291
|
+
let generateCardsResp = await new GenerateCards(
|
|
292
|
+
this.openAiService,
|
|
293
|
+
this.parsedContent?.source_id ?? "",
|
|
294
|
+
JSON.stringify(content),
|
|
295
|
+
this.parsedContent!.taxonomy,
|
|
296
|
+
this.promptIdForCardsGeneration,
|
|
297
|
+
this.generationCurriculum,
|
|
298
|
+
this.parsedContent?.type ?? "text"
|
|
299
|
+
).generateCards(n, card_generation_types);
|
|
300
|
+
return generateCardsResp;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async generateTypology() {
|
|
304
|
+
const type = this.parsedContent?.type ?? "text";
|
|
305
|
+
const content = {
|
|
306
|
+
title: this.parsedContent?.title ?? "",
|
|
307
|
+
...(type === "text"
|
|
308
|
+
? { h1_headings: this.parsedContent?.headings ?? [""] }
|
|
309
|
+
: { timecodes: this.parsedContent?.headings ?? [""] }),
|
|
310
|
+
content: this.parsedContent?.content ?? [],
|
|
311
|
+
};
|
|
312
|
+
try {
|
|
313
|
+
let response = await new GenerateTypology(
|
|
314
|
+
this.openAiService,
|
|
315
|
+
type,
|
|
316
|
+
this.parsedContent?.source_id ?? "",
|
|
317
|
+
content,
|
|
318
|
+
this.expectedFields,
|
|
319
|
+
this.promptIdForTypology,
|
|
320
|
+
this.generationCurriculum
|
|
321
|
+
).generate();
|
|
322
|
+
return response;
|
|
323
|
+
} catch (error) {
|
|
324
|
+
console.log(error);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async generateConceptFacts(typologyResponse: any) {
|
|
329
|
+
const type = this.parsedContent?.type ?? "text";
|
|
330
|
+
const content = {
|
|
331
|
+
title: this.parsedContent?.title ?? "",
|
|
332
|
+
...(type === "text"
|
|
333
|
+
? { h1_headings: this.parsedContent?.headings ?? [""] }
|
|
334
|
+
: { timecodes: this.parsedContent?.headings ?? [""] }),
|
|
335
|
+
key_takeaways: typologyResponse?.summary_cards ?? [],
|
|
336
|
+
content: this.parsedContent?.content ?? [],
|
|
337
|
+
...(typologyResponse && typeof typologyResponse === "object"
|
|
338
|
+
? {
|
|
339
|
+
fields: typologyResponse.field ?? [],
|
|
340
|
+
}
|
|
341
|
+
: {}),
|
|
342
|
+
};
|
|
343
|
+
let response = await new GenerateConceptFacts(
|
|
344
|
+
this.openAiService,
|
|
345
|
+
this.parsedContent?.source_id ?? "",
|
|
346
|
+
content,
|
|
347
|
+
type,
|
|
348
|
+
this.promptIdForConceptFacts,
|
|
349
|
+
this.generationCurriculum
|
|
350
|
+
).generate();
|
|
351
|
+
return response;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async generateSummaryCards() {
|
|
355
|
+
const type = this.parsedContent?.type ?? "text";
|
|
356
|
+
const content = {
|
|
357
|
+
title: this.parsedContent?.title ?? "",
|
|
358
|
+
...(type === "text"
|
|
359
|
+
? { h1_headings: this.parsedContent?.headings ?? [""] }
|
|
360
|
+
: { timecodes: this.parsedContent?.headings ?? [""] }),
|
|
361
|
+
content: this.parsedContent?.content ?? [],
|
|
362
|
+
};
|
|
363
|
+
let response = await new GenerateSummaryCards(
|
|
364
|
+
this.openAiService,
|
|
365
|
+
this.parsedContent?.source_id ?? "",
|
|
366
|
+
content,
|
|
367
|
+
type,
|
|
368
|
+
this.promptIdForSummaryCards,
|
|
369
|
+
this.generationCurriculum
|
|
370
|
+
).generate();
|
|
371
|
+
return response;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async generateEmbeddings(typologyResponse: any) {
|
|
375
|
+
const concepts_facts = typologyResponse.concepts_facts;
|
|
376
|
+
const texts = concepts_facts.map((e: MongoConceptFactSource) => e.text);
|
|
377
|
+
const embeddings = await new GenerateEmbeddings(
|
|
378
|
+
this.openAiService
|
|
379
|
+
).generateEmbeddings(concepts_facts);
|
|
380
|
+
return embeddings;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async localConsolidation(
|
|
384
|
+
concepts_facts: {
|
|
385
|
+
text: string;
|
|
386
|
+
type: string;
|
|
387
|
+
reference: string;
|
|
388
|
+
embedding: number[];
|
|
389
|
+
}[],
|
|
390
|
+
sourceId: string
|
|
391
|
+
) {
|
|
392
|
+
const embeddings = new LocalConsolidation().consolidate(
|
|
393
|
+
concepts_facts,
|
|
394
|
+
sourceId
|
|
395
|
+
);
|
|
396
|
+
return embeddings;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async globalConsolidation(
|
|
400
|
+
concepts_facts: {
|
|
401
|
+
id: string;
|
|
402
|
+
text: string;
|
|
403
|
+
type: string;
|
|
404
|
+
reference: string;
|
|
405
|
+
embedding: number[];
|
|
406
|
+
}[],
|
|
407
|
+
sourceId: string,
|
|
408
|
+
threshold: number
|
|
409
|
+
) {
|
|
410
|
+
const embeddings = await new GlobalConsolidation().consolidate(
|
|
411
|
+
concepts_facts,
|
|
412
|
+
sourceId,
|
|
413
|
+
threshold ?? 0.8
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
return embeddings;
|
|
417
|
+
}
|
|
418
|
+
}
|