only_ever_generator 8.1.0 → 8.1.2

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.
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.GenerateSummaryCards = void 0;
13
+ const openai_helper_1 = require("../helper/openai_helper");
14
+ const logger_1 = require("../logger");
15
+ const sanitize_strings_1 = require("../utils/sanitize_strings");
16
+ const build_summary_schema_1 = require("../helper/schema_helper/build_summary_schema");
17
+ class GenerateSummaryCards {
18
+ constructor(openAiService, sourceId, content, type, promptIdForSummaryCards, generationCurriculum) {
19
+ this.type = "";
20
+ this.openAiService = openAiService;
21
+ this.openAIHelper = new openai_helper_1.OpenAIHelper(this.openAiService.api_key);
22
+ this.content = content;
23
+ this.promptIdForSummaryCards = promptIdForSummaryCards;
24
+ this.type = type;
25
+ this.sourceId = sourceId;
26
+ this.generationCurriculum = generationCurriculum;
27
+ }
28
+ generate() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ var _a, _b, _c, _d, _e;
31
+ try {
32
+ const headings = this.type === "text"
33
+ ? this.content.h1_headings || [""]
34
+ : this.content.timecodes || [""];
35
+ const schema = yield (0, build_summary_schema_1.buildSummarySchema)(headings.length > 0 ? headings : [""], "summary_cards", true);
36
+ const openAiResponse = yield this.openAIHelper.openAI.responses.create({
37
+ prompt: {
38
+ id: this.promptIdForSummaryCards,
39
+ // version: "35",
40
+ variables: {
41
+ heading_type: this.type == "video" ? "timecode" : "h1 heading",
42
+ source_title: this.content.title,
43
+ source_content: JSON.stringify(this.content.content),
44
+ source_headings: JSON.stringify(this.content.h1_headings || []),
45
+ },
46
+ },
47
+ max_output_tokens: 30000,
48
+ // input: [
49
+ // {
50
+ // role: "user",
51
+ // content: [
52
+ // {
53
+ // type: "input_text",
54
+ // text: JSON.stringify(this.content),
55
+ // },
56
+ // ],
57
+ // },
58
+ // ],
59
+ // input: JSON.stringify(this.content),
60
+ store: true,
61
+ text: {
62
+ format: {
63
+ type: "json_schema",
64
+ name: schema.name,
65
+ strict: schema.strict,
66
+ schema: schema.schema,
67
+ },
68
+ },
69
+ });
70
+ if (!openAiResponse) {
71
+ yield (0, logger_1.log_error)({
72
+ flow: "manual_generation",
73
+ data: openAiResponse,
74
+ error: "empty_openai_response",
75
+ timestamp: new Date(),
76
+ source_id: this.sourceId,
77
+ type: {
78
+ request_type: "concept_fact",
79
+ n: 1,
80
+ },
81
+ });
82
+ return;
83
+ }
84
+ openAiResponse.metadata = {
85
+ req_time: (_a = openAiResponse.created) !== null && _a !== void 0 ? _a : new Date(),
86
+ req_type: {
87
+ type: "breadth",
88
+ sub_type: "summary_cards",
89
+ n: 1,
90
+ },
91
+ req_tokens: (_c = (_b = openAiResponse.usage) === null || _b === void 0 ? void 0 : _b.input_tokens) !== null && _c !== void 0 ? _c : 0,
92
+ res_tokens: (_e = (_d = openAiResponse.usage) === null || _d === void 0 ? void 0 : _d.output_tokens) !== null && _e !== void 0 ? _e : 0,
93
+ prompt: {
94
+ id: openAiResponse.prompt.id,
95
+ version: openAiResponse.prompt.version,
96
+ },
97
+ // prompt_tokens_details: openAiResponse.usage?.,
98
+ model: this.openAiService.model,
99
+ usage: openAiResponse.usage,
100
+ status: "completed",
101
+ };
102
+ const summaryCards = this.parseJson(openAiResponse);
103
+ return {
104
+ summary_cards: summaryCards,
105
+ metadata: openAiResponse.metadata,
106
+ };
107
+ }
108
+ catch (error) {
109
+ console.log(error);
110
+ yield (0, logger_1.log_error)({
111
+ flow: this.generationCurriculum
112
+ ? "curriculum_manual_generation"
113
+ : "manual_generation",
114
+ data: error,
115
+ timestamp: new Date(),
116
+ error: "error_in_concept_facts_generation",
117
+ source_id: this.sourceId,
118
+ type: {
119
+ request_type: "concept_fact",
120
+ n: 1,
121
+ },
122
+ });
123
+ return;
124
+ }
125
+ });
126
+ }
127
+ parseJson(response) {
128
+ try {
129
+ const summaryCards = JSON.parse(response.output_text).summary_cards;
130
+ if (summaryCards) {
131
+ return summaryCards.map((item) => {
132
+ return Object.assign(Object.assign({}, item), { reference: (0, sanitize_strings_1.restoreQuotesInString)(item.reference) });
133
+ });
134
+ }
135
+ }
136
+ catch (e) {
137
+ throw new logger_1.ParsingError("Something went wrong in parsing the json", response);
138
+ }
139
+ }
140
+ }
141
+ exports.GenerateSummaryCards = GenerateSummaryCards;
142
+ //# sourceMappingURL=summarize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"summarize.js","sourceRoot":"","sources":["../../src/typology_gen/summarize.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,2DAAuD;AAEvD,sCAAiE;AACjE,gEAGmC;AACnC,uFAAkF;AAElF,MAAa,oBAAoB;IAa/B,YACE,aAA4B,EAC5B,QAAgB,EAChB,OAKC,EACD,IAAY,EACZ,uBAA+B,EAC/B,oBAA6B;QAbxB,SAAI,GAAW,EAAE,CAAC;QAevB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,YAAY,GAAG,IAAI,4BAAY,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACjE,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,uBAAuB,GAAG,uBAAuB,CAAC;QACvD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAA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AAE,YAAY;oBAC3B,QAAQ,EAAE,cAAc,CAAC,QAAQ;iBAClC,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBACnB,MAAM,IAAA,kBAAS,EAAC;oBACd,IAAI,EAAE,IAAI,CAAC,oBAAoB;wBAC7B,CAAC,CAAC,8BAA8B;wBAChC,CAAC,CAAC,mBAAmB;oBACvB,IAAI,EAAE,KAAK;oBACX,SAAS,EAAE,IAAI,IAAI,EAAE;oBACrB,KAAK,EAAE,mCAAmC;oBAC1C,SAAS,EAAE,IAAI,CAAC,QAAQ;oBACxB,IAAI,EAAE;wBACJ,YAAY,EAAE,cAAc;wBAC5B,CAAC,EAAE,CAAC;qBACL;iBACF,CAAC,CAAC;gBACH,OAAO;YACT,CAAC;QACH,CAAC;KAAA;IAED,SAAS,CAAC,QAAa;QACrB,IAAI,CAAC;YACH,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,aAAa,CAAC;YACpE,IAAI,YAAY,EAAE,CAAC;gBACjB,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC,IAAS,EAAE,EAAE;oBACpC,uCACK,IAAI,KACP,SAAS,EAAE,IAAA,wCAAqB,EAAC,IAAI,CAAC,SAAS,CAAC,IAChD;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,IAAI,qBAAY,CACpB,0CAA0C,EAC1C,QAAQ,CACT,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AA7JD,oDA6JC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "only_ever_generator",
3
- "version": "8.1.0",
3
+ "version": "8.1.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -13,6 +13,7 @@ import { BaseParamType } from "../types/base_param_type";
13
13
  import { SourceTaxonomy } from "../types/source_taxonomy_type";
14
14
  import { MongoConceptFactSource } from "../types/mongo_concept_fact_type";
15
15
  import { GenerateConceptFacts } from "../typology_gen/generate_concept_facts";
16
+ import { GenerateSummaryCards } from "../typology_gen/summarize";
16
17
 
17
18
  export class OnlyEverGenerator {
18
19
  public api_key: string = "";
@@ -33,6 +34,7 @@ export class OnlyEverGenerator {
33
34
  public promptIdForTypology: string;
34
35
  public promptIdForConceptFacts: string;
35
36
  public promptIdForCardsGeneration: string;
37
+ public promptIdForSummaryCards: string;
36
38
  public n: number = 0;
37
39
  generationVariables: {
38
40
  bloom_instructions: string;
@@ -55,6 +57,7 @@ export class OnlyEverGenerator {
55
57
  promptIdForTypology: string,
56
58
  promptIdForConceptFacts: string,
57
59
  promptIdForCardsGeneration: string,
60
+ promptIdForSummaryCards: string,
58
61
  generationVariables: {
59
62
  bloom_instructions: string;
60
63
  card_instructions: string;
@@ -66,6 +69,7 @@ export class OnlyEverGenerator {
66
69
  this.promptIdForTypology = promptIdForTypology;
67
70
  this.promptIdForConceptFacts = promptIdForConceptFacts;
68
71
  this.promptIdForCardsGeneration = promptIdForCardsGeneration;
72
+ this.promptIdForSummaryCards = promptIdForSummaryCards;
69
73
  this.api_key = apiKey;
70
74
  this.generationVariables = generationVariables;
71
75
  this.generationCurriculum = generationCurriculum;
@@ -104,55 +108,113 @@ export class OnlyEverGenerator {
104
108
  const whatNeedsToBeGenerated = args.getWhatNeedsToBeGenerated();
105
109
  for (let elem of whatNeedsToBeGenerated)
106
110
  if (elem == "generate_tyopology") {
107
- console.log("Generating typology");
108
111
  this.typologyResponse = await this.generateTypology();
109
- console.log("Typology generated");
110
112
  console.log(this.typologyResponse?.generate_cards?.value);
111
- console.log(
112
- JSON.stringify(this.typologyResponse?.generate_cards?.value > 30)
113
- );
114
113
 
115
114
  if (
116
115
  this.typologyResponse?.status_code == 200 &&
117
116
  (this.typologyResponse?.generate_cards?.value ?? 30) > 30
118
117
  ) {
119
- console.log("Concept facts generating");
120
- const response = await this.generateConceptFacts(
121
- this.typologyResponse
118
+ // Run concept facts and summary cards generation in parallel
119
+ console.log(
120
+ "Concept facts and summary cards generating in parallel"
122
121
  );
123
- this.typologyResponse.concepts_facts =
124
- response?.concepts_facts ?? [];
125
- this.typologyResponse.metadata?.push(response?.metadata ?? {});
126
- console.log("Concept facts generated");
127
- console.log(this.typologyResponse);
128
- const embeddings = await this.generateEmbeddings(
129
- this.typologyResponse
130
- );
131
- console.log("Embedding generated");
132
- const localConsolidation = await this.localConsolidation(
133
- embeddings.concepts_facts,
134
- this.parsedContent!.title
135
- );
136
- console.log("Local consolidation done");
137
- this.typologyResponse!.concepts_facts =
138
- localConsolidation.sourceTaxonomyOps;
139
- const globalConsolidation = await this.globalConsolidation(
140
- localConsolidation.sourceTaxonomyOps,
141
- this.parsedContent!.source_id,
142
- 0.8
143
- );
144
- console.log("Global consolidation done");
145
- this.typologyResponse!.concepts_facts =
146
- globalConsolidation.source_taxonomy;
147
- this.typologyResponse!.metadata?.push(embeddings.metadata);
148
- responseToReturn.push({
149
- type: "typology",
150
- data: this.typologyResponse,
151
- qdrantOps: {
152
- update: globalConsolidation.global_updates,
153
- inserts: globalConsolidation.global_inserts,
154
- },
155
- });
122
+ const [conceptFactsResult, summaryCardsResult] =
123
+ await Promise.allSettled([
124
+ this.generateConceptFacts(this.typologyResponse),
125
+ this.generateSummaryCards(),
126
+ ]);
127
+
128
+ // Handle concept facts result
129
+ if (
130
+ conceptFactsResult.status === "fulfilled" &&
131
+ conceptFactsResult.value
132
+ ) {
133
+ const response = conceptFactsResult.value;
134
+ this.typologyResponse.concepts_facts =
135
+ response?.concepts_facts ?? [];
136
+ this.typologyResponse.metadata?.push(response?.metadata ?? {});
137
+ console.log("Concept facts generated");
138
+ } else {
139
+ const errorReason =
140
+ conceptFactsResult.status === "rejected"
141
+ ? conceptFactsResult.reason
142
+ : "Unknown error";
143
+ console.log("Concept facts generation failed:", errorReason);
144
+ this.typologyResponse.concepts_facts = [];
145
+ }
146
+
147
+ // Handle summary cards result
148
+ if (
149
+ summaryCardsResult.status === "fulfilled" &&
150
+ summaryCardsResult.value
151
+ ) {
152
+ const summaryCards = summaryCardsResult.value;
153
+ this.typologyResponse.summary_cards =
154
+ summaryCards?.summary_cards ?? [];
155
+ this.typologyResponse.metadata?.push(
156
+ summaryCards?.metadata ?? {}
157
+ );
158
+ console.log("Summary cards generated");
159
+ } else {
160
+ const errorReason =
161
+ summaryCardsResult.status === "rejected"
162
+ ? summaryCardsResult.reason
163
+ : "Unknown error";
164
+ console.log("Summary cards generation failed:", errorReason);
165
+ this.typologyResponse.summary_cards = [];
166
+ }
167
+
168
+ // Process embeddings and consolidation only if concept facts were generated
169
+ if (this.typologyResponse.concepts_facts.length > 0) {
170
+ console.log(this.typologyResponse);
171
+ const embeddings = await this.generateEmbeddings(
172
+ this.typologyResponse
173
+ );
174
+ console.log("Embedding generated");
175
+ const localConsolidation = await this.localConsolidation(
176
+ embeddings.concepts_facts,
177
+ this.parsedContent!.title
178
+ );
179
+ console.log("Local consolidation done");
180
+ this.typologyResponse!.concepts_facts =
181
+ localConsolidation.sourceTaxonomyOps;
182
+ const globalConsolidation = await this.globalConsolidation(
183
+ localConsolidation.sourceTaxonomyOps,
184
+ this.parsedContent!.source_id,
185
+ 0.8
186
+ );
187
+ console.log("Global consolidation done");
188
+ this.typologyResponse!.concepts_facts =
189
+ globalConsolidation.source_taxonomy;
190
+ this.typologyResponse!.metadata?.push(embeddings.metadata);
191
+ responseToReturn.push({
192
+ type: "typology",
193
+ data: this.typologyResponse,
194
+ qdrantOps: {
195
+ update: globalConsolidation.global_updates,
196
+ inserts: globalConsolidation.global_inserts,
197
+ },
198
+ });
199
+ } else {
200
+ // If concept facts failed, still return typology without embeddings
201
+ responseToReturn.push({
202
+ type: "typology",
203
+ data: this.typologyResponse,
204
+ qdrantOps: {
205
+ update: [],
206
+ inserts: [],
207
+ },
208
+ });
209
+ }
210
+
211
+ // Add summary cards response if they were generated
212
+ if (this.typologyResponse.summary_cards.length > 0) {
213
+ responseToReturn.push({
214
+ type: "summary_cards",
215
+ data: this.typologyResponse,
216
+ });
217
+ }
156
218
  } else {
157
219
  responseToReturn.push({
158
220
  type: "typology",
@@ -273,6 +335,26 @@ export class OnlyEverGenerator {
273
335
  return response;
274
336
  }
275
337
 
338
+ async generateSummaryCards() {
339
+ const type = this.parsedContent?.type ?? "text";
340
+ const content = {
341
+ title: this.parsedContent?.title ?? "",
342
+ ...(type === "text"
343
+ ? { h1_headings: this.parsedContent?.headings ?? [""] }
344
+ : { timecodes: this.parsedContent?.headings ?? [""] }),
345
+ content: this.parsedContent?.content ?? [],
346
+ };
347
+ let response = await new GenerateSummaryCards(
348
+ this.openAiService,
349
+ this.parsedContent?.source_id ?? "",
350
+ content,
351
+ type,
352
+ this.promptIdForSummaryCards,
353
+ this.generationCurriculum
354
+ ).generate();
355
+ return response;
356
+ }
357
+
276
358
  async generateEmbeddings(typologyResponse: any) {
277
359
  const concepts_facts = typologyResponse.concepts_facts;
278
360
  const texts = concepts_facts.map((e: MongoConceptFactSource) => e.text);
@@ -0,0 +1,43 @@
1
+ import { sanitizeStringsForSchema } from "../../utils/sanitize_strings";
2
+ import { database, ObjectId, setUp } from "../mongo_helper";
3
+
4
+ // Function to populate enums in a parsed schema
5
+ export async function buildSummarySchema(
6
+ headings: string[],
7
+ name: string = "summary_cards",
8
+ strict: boolean = true
9
+ ) {
10
+ setUp();
11
+ const objectId = new ObjectId("690ae043116a8c929f3a4cd5");
12
+ const schemaObj = await database.collection("_prompts").findOne({
13
+ _id: objectId,
14
+ });
15
+ if (!schemaObj) {
16
+ throw new Error("Schema not found");
17
+ }
18
+ const schema = schemaObj.schema;
19
+ if (!schema) {
20
+ throw new Error("Schema not found");
21
+ }
22
+ // Deep clone to avoid mutating the original
23
+ const populatedSchema = JSON.parse(schema);
24
+
25
+ // Update name and strict mode
26
+ populatedSchema.name = name;
27
+ populatedSchema.strict = strict;
28
+
29
+ // Sanitize headings to remove quotes for OpenAI structured outputs
30
+ const sanitizedStrings = sanitizeStringsForSchema(headings);
31
+
32
+ // Type-safe enum population for reference field in summary_cards items
33
+ // This populates the enum for cards 2-N (takeaway cards)
34
+ if (
35
+ populatedSchema.schema?.properties?.summary_cards?.items?.properties
36
+ ?.reference?.enum
37
+ ) {
38
+ populatedSchema.schema.properties.summary_cards.items.properties.reference.enum =
39
+ sanitizedStrings;
40
+ }
41
+
42
+ return populatedSchema;
43
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
+ import { ObjectId } from "mongodb";
1
2
  import { OnlyEverGenerator } from "./bootstrap/app";
3
+ import { OpenAiService } from "./services/open_ai_service";
4
+ import { GenerateSummaryCards } from "./typology_gen/summarize";
5
+ import { database, setUp } from "./helper/mongo_helper";
2
6
  // import { LocalConsolidation } from "./embedding_generation/consolidation/local_consolidation";
3
7
  // import { embeddingsResp } from "./typology-parsed-response";
4
8
  // import { GlobalConsolidation } from "./embedding_generation/consolidation/global_consolidation";
@@ -9,9 +13,9 @@ import { OnlyEverGenerator } from "./bootstrap/app";
9
13
  // import { GenerateConceptFacts } from "./typology_gen/generate_concept_facts";
10
14
  // import { BaseParamType } from "./types/base_param_type";
11
15
  // import dotenv from "dotenv";
12
- // import express from "express";
13
- // const app = express();
14
- // const port = 3000;
16
+ import express from "express";
17
+ const app = express();
18
+ const port = 3000;
15
19
 
16
20
  // // import { OnlyEverGenerator } from "./bootstrap/app";
17
21
  // import { database, ObjectId, setUp } from "./helper/mongo_helper";
@@ -23,156 +27,36 @@ import { OnlyEverGenerator } from "./bootstrap/app";
23
27
  export { OnlyEverGenerator };
24
28
  // dotenv.config();
25
29
 
26
- // app.get("/generate", async (req, res) => {
27
- // try {
28
- // const openAIKey =
29
- // "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";
30
- // const promptForSummary = {
31
- // promptId: "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
32
- // // version: "13",
33
- // };
34
- // const promptForConceptFacts = {
35
- // promptId: "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
36
- // // version: "6",
37
- // };
38
-
39
- // setUp();
40
-
41
- // const source = await database.collection("_source").findOne({
42
- // _id: new ObjectId("68b114fdce5ca556db39fd12"),
43
- // });
44
-
45
- // const contentForGen = {
46
- // prompt: {
47
- // typology: "",
48
- // card_generation: "",
49
- // },
50
- // content: {
51
- // source_id: source?._id?.toString() || "",
52
- // title: source?.title || "",
53
- // type: source?.source_type || "",
54
- // headings: source?.headings || [],
55
- // content: source?.content || [],
56
- // fields: source?.fields || [],
57
- // taxonomy: source?.learn_value
58
- // ? {
59
- // generate_cards: source?.generate_cards || {
60
- // state: false,
61
- // reason: "",
62
- // },
63
- // concepts_facts: source?.concepts_facts || [],
64
- // fields: source?.fields || [],
65
- // learn_value: source?.learn_value || {
66
- // value: 0,
67
- // reason: "",
68
- // bloom_levels: [],
69
- // bloom_levels_reason: "",
70
- // },
71
- // }
72
- // : undefined,
73
- // },
74
- // };
75
- // const prompts = await getPrompts("text");
76
- // const bloomInstructions = prompts.bloom_instructions;
77
- // const cardInstructions = prompts.card_instructions;
78
- // const cardExamples = prompts.card_examples;
79
-
80
- // console.log("Initializing OnlyEverGenerator");
81
- // const onlyEverGenerator = new OnlyEverGenerator(
82
- // process.env.OPEN_AI_KEY as string,
83
- // "o4-mini",
84
- // contentForGen,
85
-
86
- // "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
87
- // "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
88
- // "pmpt_688118a923e4819098176a13a2f401920d2ea17d881cc6c6",
89
- // {
90
- // bloom_instructions: bloomInstructions,
91
- // card_instructions: cardInstructions,
92
- // card_examples: cardExamples,
93
- // },
94
- // false,
95
- // 0
96
- // );
97
- // const response = await onlyEverGenerator.generate(false, true);
98
-
99
- // res.send(response);
100
- // } catch (error) {
101
- // res.status(500).send(error);
102
- // }
103
- // });
104
-
105
- // app.listen(port, () => {
106
- // console.log(`Server is running on port ${port}`);
107
- // });
108
-
109
- // (async () => {
110
- // const promptForSummary = {
111
- // promptId: "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
112
- // // version: "13",
113
- // };
114
- // const promptForConceptFacts = {
115
- // promptId: "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
116
- // // version: "6",
117
- // };
118
-
119
- // setUp();
120
-
121
- // const source = await database.collection("_source").findOne({
122
- // _id: new ObjectId("68b114cdf23998242ce03235"),
123
- // });
124
-
125
- // const contentForGen = {
126
- // prompt: {
127
- // typology: "",
128
- // card_generation: "",
129
- // },
130
- // content: {
131
- // source_id: source?._id?.toString() || "",
132
- // title: source?.title || "",
133
- // type: source?.source_type || "",
134
- // headings: source?.headings || [],
135
- // content: source?.content || [],
136
- // fields: source?.fields || [],
137
- // taxonomy: source?.learn_value
138
- // ? {
139
- // generate_cards: source?.generate_cards || {
140
- // state: false,
141
- // reason: "",
142
- // },
143
- // concepts_facts: source?.concepts_facts || [],
144
- // fields: source?.fields || [],
145
- // learn_value: source?.learn_value || {
146
- // value: 0,
147
- // reason: "",
148
- // bloom_levels: [],
149
- // bloom_levels_reason: "",
150
- // },
151
- // }
152
- // : undefined,
153
- // },
154
- // };
155
- // const prompts = await getPrompts("text");
156
- // const bloomInstructions = prompts.bloom_instructions;
157
- // const cardInstructions = prompts.card_instructions;
158
- // const cardExamples = prompts.card_examples;
159
-
160
- // console.log("Initializing OnlyEverGenerator");
161
- // const onlyEverGenerator = new OnlyEverGenerator(
162
- // process.env.OPEN_AI_KEY as string,
163
- // "o4-mini",
164
- // contentForGen,
165
-
166
- // "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
167
- // "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
168
- // "pmpt_688118a923e4819098176a13a2f401920d2ea17d881cc6c6",
169
- // {
170
- // bloom_instructions: bloomInstructions,
171
- // card_instructions: cardInstructions,
172
- // card_examples: cardExamples,
173
- // },
174
- // false
175
- // );
176
- // const response = await onlyEverGenerator.generate(true, false);
177
- // console.log(response);
178
- // })();
30
+ app.listen(port, () => {
31
+ console.log(`Server is running on port ${port}`);
32
+ });
33
+
34
+ app.get("/summarize", async (req, res) => {
35
+ setUp();
36
+ const promptIdForSummaryCards =
37
+ "pmpt_6902c30a973481968395935cc6dfa78605f50d2b01d4afbd";
38
+ const sourceId = "68b114dac849a25097ed1778";
39
+ const source = await database
40
+ .collection("_source")
41
+ .findOne({ _id: new ObjectId(sourceId) });
42
+ const content = {
43
+ title: source?.title,
44
+ h1_headings: source?.headings,
45
+ content: source?.content,
46
+ };
47
+ const openAiService = new OpenAiService(
48
+ process.env.OPEN_AI_KEY || "",
49
+ "gpt-5-mini"
50
+ );
51
+ const summarize = new GenerateSummaryCards(
52
+ openAiService,
53
+ sourceId,
54
+ content,
55
+ "text",
56
+ promptIdForSummaryCards,
57
+ false
58
+ );
59
+ const summaryCards = await summarize.generate();
60
+ console.log(summaryCards);
61
+ res.json(summaryCards);
62
+ });