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,169 +1,180 @@
1
- import { buildConceptFactSchema } from "../helper/schema_helper/build_concept_facts_schema";
2
- import { OpenAIHelper } from "../helper/openai_helper";
3
- import { OpenAiService } from "../services/open_ai_service";
4
- import { ErrorLogger, log_error, ParsingError } from "../logger";
5
- import {
6
- restoreQuotesInObject,
7
- restoreQuotesInString,
8
- } from "../utils/sanitize_strings";
9
- import { GenerationVariablesSchema } from "../types/generation_variables_schema";
10
- import { promptIds } from "../services/get_prompts";
11
-
12
- export class GenerateConceptFacts {
13
- public openAiService: OpenAiService;
14
- public openAIHelper: OpenAIHelper;
15
- public sourceId: string;
16
- public generationCurriculum: boolean;
17
- public generationVariablesSchema: GenerationVariablesSchema;
18
- public content: {
19
- title: string;
20
- h1_headings?: string[];
21
- timecodes?: string[];
22
- content: any[];
23
- };
24
- public type: string = "";
25
- constructor(
26
- openAiService: OpenAiService,
27
- sourceId: string,
28
- content: {
29
- title: string;
30
- h1_headings?: string[];
31
- timecodes?: string[];
32
- content: any[];
33
- },
34
- type: string,
35
- generationCurriculum: boolean,
36
- generationVariablesSchema: GenerationVariablesSchema
37
- ) {
38
- this.openAiService = openAiService;
39
- this.openAIHelper = new OpenAIHelper(this.openAiService.api_key);
40
- this.content = content;
41
- this.type = type;
42
- this.sourceId = sourceId;
43
- this.generationCurriculum = generationCurriculum;
44
- this.generationVariablesSchema = generationVariablesSchema;
45
- }
46
-
47
- async generate() {
48
- try {
49
- const headings =
50
- this.type === "text"
51
- ? this.content.h1_headings || [""]
52
- : this.content.timecodes || [""];
53
- // const schema =
54
- // this.generationVariablesSchema.output_schema.concepts_facts;
55
- const schema = await buildConceptFactSchema(
56
- headings.length > 0 ? headings : [""],
57
- this.generationVariablesSchema.output_schema.concepts_facts
58
- );
59
- const openAiResponse: any =
60
- await this.openAIHelper.openAI.responses.create({
61
- prompt: {
62
- id: promptIds.conceptFacts,
63
- // version: "200",
64
- variables: {
65
- heading_type: this.type == "video" ? "timecode" : "h1 heading",
66
- },
67
- },
68
- max_output_tokens: 30000,
69
- input: [
70
- {
71
- role: "user",
72
- content: [
73
- {
74
- type: "input_text",
75
- text: JSON.stringify(this.content),
76
- },
77
- ],
78
- },
79
- ],
80
- // input: JSON.stringify(this.content),
81
- store: true,
82
- text: {
83
- format: {
84
- type: "json_schema",
85
- name: schema.name,
86
- strict: schema.strict,
87
- schema: schema.schema,
88
- },
89
- },
90
- });
91
- if (!openAiResponse) {
92
- await log_error({
93
- flow: "manual_generation",
94
- data: openAiResponse,
95
- error: "empty_openai_response",
96
- timestamp: new Date(),
97
- source_id: this.sourceId,
98
- type: {
99
- request_type: "concept_fact",
100
- n: 1,
101
- },
102
- });
103
- return;
104
- }
105
-
106
- // // Restore quotes in the response (works without original mapping)
107
- // if (openAiResponse.output) {
108
- // openAiResponse.output = restoreQuotesInObject(openAiResponse.output);
109
- // }
110
-
111
- openAiResponse.metadata = {
112
- req_time: openAiResponse.created ?? new Date(),
113
- req_type: {
114
- type: "breadth",
115
- sub_type: "concept_fact",
116
- n: 1,
117
- },
118
- req_tokens: openAiResponse.usage?.input_tokens ?? 0,
119
- res_tokens: openAiResponse.usage?.output_tokens ?? 0,
120
- prompt: openAiResponse.prompt,
121
- // prompt_tokens_details: openAiResponse.usage?.,
122
- model: this.openAiService.model,
123
- usage: openAiResponse.usage,
124
- status: "completed",
125
- };
126
- const concepts_facts = this.parseJson(openAiResponse);
127
-
128
- return {
129
- concepts_facts: concepts_facts,
130
- metadata: openAiResponse.metadata,
131
- };
132
- } catch (error) {
133
- console.log(error);
134
- await log_error({
135
- flow: this.generationCurriculum
136
- ? "curriculum_manual_generation"
137
- : "manual_generation",
138
- data: error,
139
- timestamp: new Date(),
140
- error: "error_in_concept_facts_generation",
141
- source_id: this.sourceId,
142
- type: {
143
- request_type: "concept_fact",
144
- n: 1,
145
- },
146
- });
147
- return;
148
- }
149
- }
150
-
151
- parseJson(response: any) {
152
- try {
153
- const concepts_facts = JSON.parse(response.output_text).concepts_facts;
154
- if (concepts_facts) {
155
- return concepts_facts.map((item: any) => {
156
- return {
157
- ...item,
158
- reference: restoreQuotesInString(item.reference),
159
- };
160
- });
161
- }
162
- } catch (e) {
163
- throw new ParsingError(
164
- "Something went wrong in parsing the json",
165
- response
166
- );
167
- }
168
- }
169
- }
1
+ import { buildConceptFactSchema } from "../helper/schema_helper/build_concept_facts_schema";
2
+ import { OpenAIHelper } from "../helper/openai_helper";
3
+ import { OpenAiService } from "../services/open_ai_service";
4
+ import { ErrorLogger, log_error, ParsingError } from "../logger";
5
+ import {
6
+ restoreQuotesInObject,
7
+ restoreQuotesInString,
8
+ } from "../utils/sanitize_strings";
9
+ import { jsonrepair } from "jsonrepair";
10
+
11
+ export class GenerateConceptFacts {
12
+ public openAiService: OpenAiService;
13
+ public openAIHelper: OpenAIHelper;
14
+ public sourceId: string;
15
+ public generationCurriculum: boolean;
16
+ public content: {
17
+ title: string;
18
+ h1_headings?: string[];
19
+ timecodes?: string[];
20
+ content: any[];
21
+ };
22
+ public type: string = "";
23
+ public promptIdForConceptFacts: string;
24
+ constructor(
25
+ openAiService: OpenAiService,
26
+ sourceId: string,
27
+ content: {
28
+ title: string;
29
+ h1_headings?: string[];
30
+ timecodes?: string[];
31
+ content: any[];
32
+ },
33
+ type: string,
34
+ promptIdForConceptFacts: string,
35
+ generationCurriculum: boolean
36
+ ) {
37
+ this.openAiService = openAiService;
38
+ this.openAIHelper = new OpenAIHelper(this.openAiService.api_key);
39
+ this.content = content;
40
+ this.promptIdForConceptFacts = promptIdForConceptFacts;
41
+ this.type = type;
42
+ this.sourceId = sourceId;
43
+ this.generationCurriculum = generationCurriculum;
44
+ }
45
+
46
+ async generate() {
47
+ try {
48
+ const headings =
49
+ this.type === "text"
50
+ ? this.content.h1_headings || [""]
51
+ : this.content.timecodes || [""];
52
+ const schema = await buildConceptFactSchema(
53
+ headings.length > 0 ? headings : [""],
54
+ "concept_fact_gen_schema",
55
+ true
56
+ );
57
+ const openAiResponse: any =
58
+ await this.openAIHelper.openAI.responses.create({
59
+ prompt: {
60
+ id: this.promptIdForConceptFacts,
61
+ version: "200",
62
+ variables: {
63
+ heading_type: this.type == "video" ? "timecode" : "h1 heading",
64
+ },
65
+ },
66
+ max_output_tokens: 30000,
67
+ input: [
68
+ {
69
+ role: "user",
70
+ content: [
71
+ {
72
+ type: "input_text",
73
+ text: JSON.stringify(this.content),
74
+ },
75
+ ],
76
+ },
77
+ ],
78
+ // input: JSON.stringify(this.content),
79
+ store: true,
80
+ text: {
81
+ format: {
82
+ type: "json_schema",
83
+ name: schema.name,
84
+ strict: schema.strict,
85
+ schema: schema.schema,
86
+ },
87
+ },
88
+ });
89
+ if (!openAiResponse) {
90
+ await log_error({
91
+ flow: "manual_generation",
92
+ data: openAiResponse,
93
+ error: "empty_openai_response",
94
+ timestamp: new Date(),
95
+ source_id: this.sourceId,
96
+ type: {
97
+ request_type: "concept_fact",
98
+ n: 1,
99
+ },
100
+ });
101
+ return;
102
+ }
103
+
104
+ // // Restore quotes in the response (works without original mapping)
105
+ // if (openAiResponse.output) {
106
+ // openAiResponse.output = restoreQuotesInObject(openAiResponse.output);
107
+ // }
108
+
109
+ openAiResponse.metadata = {
110
+ req_time: openAiResponse.created ?? new Date(),
111
+ req_type: {
112
+ type: "breadth",
113
+ sub_type: "concept_fact",
114
+ n: 1,
115
+ },
116
+ req_tokens: openAiResponse.usage?.input_tokens ?? 0,
117
+ res_tokens: openAiResponse.usage?.output_tokens ?? 0,
118
+ prompt: openAiResponse.prompt,
119
+ // prompt_tokens_details: openAiResponse.usage?.,
120
+ model: this.openAiService.model,
121
+ usage: openAiResponse.usage,
122
+ status: "completed",
123
+ };
124
+ const concepts_facts = this.parseJson(openAiResponse);
125
+
126
+ return {
127
+ concepts_facts: concepts_facts,
128
+ metadata: openAiResponse.metadata,
129
+ };
130
+ } catch (error) {
131
+ console.log(error);
132
+ await log_error({
133
+ flow: this.generationCurriculum
134
+ ? "curriculum_manual_generation"
135
+ : "manual_generation",
136
+ data: error,
137
+ timestamp: new Date(),
138
+ error: "error_in_concept_facts_generation",
139
+ source_id: this.sourceId,
140
+ type: {
141
+ request_type: "concept_fact",
142
+ n: 1,
143
+ },
144
+ });
145
+ return;
146
+ }
147
+ }
148
+
149
+ parseJson(response: any) {
150
+ let parsedJson: any;
151
+
152
+ try {
153
+ parsedJson = JSON.parse(response.output_text);
154
+ } catch (e) {
155
+ // If original JSON parse fails, attempt to repair and parse
156
+ try {
157
+ const repairedJson = jsonrepair(response.output_text);
158
+ parsedJson = JSON.parse(repairedJson);
159
+ } catch (repairError) {
160
+ // If repair also fails, throw exception
161
+ throw new ParsingError(
162
+ "Something went wrong in repairing the json",
163
+ response
164
+ );
165
+ }
166
+ }
167
+
168
+ // Process the parsed content (same flow for both original and repaired JSON)
169
+ const concepts_facts = parsedJson.concepts_facts;
170
+ if (concepts_facts) {
171
+ return concepts_facts.map((item: any) => {
172
+ return {
173
+ ...item,
174
+ reference: restoreQuotesInString(item.reference),
175
+ };
176
+ });
177
+ }
178
+ return [];
179
+ }
180
+ }