only_ever_generator 6.0.1 → 6.0.3

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 (43) hide show
  1. package/dist/bootstrap/app.d.ts.map +1 -1
  2. package/dist/bootstrap/app.js +0 -5
  3. package/dist/bootstrap/app.js.map +1 -1
  4. package/dist/card_gen/generate_cards.d.ts.map +1 -1
  5. package/dist/card_gen/generate_cards.js +4 -7
  6. package/dist/card_gen/generate_cards.js.map +1 -1
  7. package/dist/helper/schema_helper/build_card_schema.d.ts +1 -0
  8. package/dist/helper/schema_helper/build_card_schema.d.ts.map +1 -1
  9. package/dist/helper/schema_helper/build_card_schema.js +29 -1
  10. package/dist/helper/schema_helper/build_card_schema.js.map +1 -1
  11. package/dist/helper/schema_helper/build_classify_summarize_schema.d.ts.map +1 -1
  12. package/dist/helper/schema_helper/build_classify_summarize_schema.js +4 -1
  13. package/dist/helper/schema_helper/build_classify_summarize_schema.js.map +1 -1
  14. package/dist/helper/schema_helper/build_concept_facts_schema.d.ts.map +1 -1
  15. package/dist/helper/schema_helper/build_concept_facts_schema.js +4 -1
  16. package/dist/helper/schema_helper/build_concept_facts_schema.js.map +1 -1
  17. package/dist/index.js +63 -60
  18. package/dist/index.js.map +1 -1
  19. package/dist/typology_gen/generate_concept_facts.d.ts.map +1 -1
  20. package/dist/typology_gen/generate_concept_facts.js +10 -1
  21. package/dist/typology_gen/generate_concept_facts.js.map +1 -1
  22. package/dist/typology_gen/generate_typology.d.ts.map +1 -1
  23. package/dist/typology_gen/generate_typology.js +13 -0
  24. package/dist/typology_gen/generate_typology.js.map +1 -1
  25. package/dist/utils/distributed_quote_restoration.d.ts +31 -0
  26. package/dist/utils/distributed_quote_restoration.d.ts.map +1 -0
  27. package/dist/utils/distributed_quote_restoration.js +69 -0
  28. package/dist/utils/distributed_quote_restoration.js.map +1 -0
  29. package/dist/utils/sanitize_strings.d.ts +32 -0
  30. package/dist/utils/sanitize_strings.d.ts.map +1 -0
  31. package/dist/utils/sanitize_strings.js +61 -0
  32. package/dist/utils/sanitize_strings.js.map +1 -0
  33. package/package.json +1 -1
  34. package/src/bootstrap/app.ts +0 -5
  35. package/src/card_gen/generate_cards.ts +8 -8
  36. package/src/helper/schema_helper/build_card_schema.ts +20 -0
  37. package/src/helper/schema_helper/build_classify_summarize_schema.ts +5 -1
  38. package/src/helper/schema_helper/build_concept_facts_schema.ts +5 -1
  39. package/src/index.ts +61 -60
  40. package/src/typology_gen/generate_concept_facts.ts +18 -1
  41. package/src/typology_gen/generate_typology.ts +23 -0
  42. package/src/utils/distributed_quote_restoration.ts +80 -0
  43. package/src/utils/sanitize_strings.ts +65 -0
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ /**
3
+ * Utility for restoring quotes in OpenAI responses across distributed Lambda functions
4
+ * This works without requiring original context from the API call Lambda
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.restoreQuotesInOpenAiResponse = restoreQuotesInOpenAiResponse;
8
+ exports.restoreQuotesInParsedResponse = restoreQuotesInParsedResponse;
9
+ const sanitize_strings_1 = require("./sanitize_strings");
10
+ /**
11
+ * Restores quotes in any OpenAI response structure
12
+ * Can be used in a parsing Lambda that receives the response from an API call Lambda
13
+ * @param openAiResponse - The complete OpenAI response object
14
+ * @returns Response with quotes restored in all string fields
15
+ */
16
+ function restoreQuotesInOpenAiResponse(openAiResponse) {
17
+ var _a, _b;
18
+ if (!openAiResponse) {
19
+ return openAiResponse;
20
+ }
21
+ // Create a deep copy to avoid mutating the original
22
+ const restoredResponse = JSON.parse(JSON.stringify(openAiResponse));
23
+ // Restore quotes in the main output/content
24
+ if (restoredResponse.output) {
25
+ restoredResponse.output = (0, sanitize_strings_1.restoreQuotesInObject)(restoredResponse.output);
26
+ }
27
+ // Handle different response formats
28
+ if (restoredResponse.choices &&
29
+ ((_b = (_a = restoredResponse.choices[0]) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content)) {
30
+ try {
31
+ const content = JSON.parse(restoredResponse.choices[0].message.content);
32
+ restoredResponse.choices[0].message.content = JSON.stringify((0, sanitize_strings_1.restoreQuotesInObject)(content));
33
+ }
34
+ catch (error) {
35
+ // If content is not JSON, restore quotes directly
36
+ restoredResponse.choices[0].message.content = (0, sanitize_strings_1.restoreQuotesInObject)(restoredResponse.choices[0].message.content);
37
+ }
38
+ }
39
+ // Handle any other potential content fields
40
+ if (restoredResponse.content) {
41
+ restoredResponse.content = (0, sanitize_strings_1.restoreQuotesInObject)(restoredResponse.content);
42
+ }
43
+ if (restoredResponse.generated_content) {
44
+ restoredResponse.generated_content = (0, sanitize_strings_1.restoreQuotesInObject)(restoredResponse.generated_content);
45
+ }
46
+ return restoredResponse;
47
+ }
48
+ /**
49
+ * Restores quotes in a parsed response object (after JSON parsing)
50
+ * Use this when you have already parsed the OpenAI response JSON
51
+ * @param parsedResponse - The parsed response object
52
+ * @returns Response with quotes restored
53
+ */
54
+ function restoreQuotesInParsedResponse(parsedResponse) {
55
+ return (0, sanitize_strings_1.restoreQuotesInObject)(parsedResponse);
56
+ }
57
+ /**
58
+ * Example usage in a parsing Lambda function:
59
+ *
60
+ * // Receiving response from API call Lambda
61
+ * const openAiResponse = event.openAiResponse;
62
+ *
63
+ * // Restore quotes without needing original mapping
64
+ * const restoredResponse = restoreQuotesInOpenAiResponse(openAiResponse);
65
+ *
66
+ * // Now process the response with original quotes restored
67
+ * const processedData = processResponse(restoredResponse);
68
+ */
69
+ //# sourceMappingURL=distributed_quote_restoration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"distributed_quote_restoration.js","sourceRoot":"","sources":["../../src/utils/distributed_quote_restoration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAUH,sEA2CC;AAQD,sEAEC;AA7DD,yDAA2D;AAE3D;;;;;GAKG;AACH,SAAgB,6BAA6B,CAAC,cAAmB;;IAC/D,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,oDAAoD;IACpD,MAAM,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC;IAEpE,4CAA4C;IAC5C,IAAI,gBAAgB,CAAC,MAAM,EAAE,CAAC;QAC5B,gBAAgB,CAAC,MAAM,GAAG,IAAA,wCAAqB,EAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC3E,CAAC;IAED,oCAAoC;IACpC,IACE,gBAAgB,CAAC,OAAO;SACxB,MAAA,MAAA,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,0CAAE,OAAO,0CAAE,OAAO,CAAA,EAC7C,CAAC;QACD,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACxE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,SAAS,CAC1D,IAAA,wCAAqB,EAAC,OAAO,CAAC,CAC/B,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,kDAAkD;YAClD,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,GAAG,IAAA,wCAAqB,EACjE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAC5C,CAAC;QACJ,CAAC;IACH,CAAC;IAED,4CAA4C;IAC5C,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC7B,gBAAgB,CAAC,OAAO,GAAG,IAAA,wCAAqB,EAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAC7E,CAAC;IAED,IAAI,gBAAgB,CAAC,iBAAiB,EAAE,CAAC;QACvC,gBAAgB,CAAC,iBAAiB,GAAG,IAAA,wCAAqB,EACxD,gBAAgB,CAAC,iBAAiB,CACnC,CAAC;IACJ,CAAC;IAED,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAgB,6BAA6B,CAAC,cAAmB;IAC/D,OAAO,IAAA,wCAAqB,EAAC,cAAc,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;;;;;;GAWG"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Utility functions for sanitizing strings for OpenAI structured outputs
3
+ * and restoring them back to their original form without requiring original mapping
4
+ */
5
+ /**
6
+ * Sanitizes an array of strings by replacing quotes with a safe placeholder
7
+ * This approach is stateless and doesn't require storing mappings
8
+ * @param strings - Array of strings to sanitize
9
+ * @returns Array of sanitized strings
10
+ */
11
+ export declare function sanitizeStringsForSchema(strings: string[]): string[];
12
+ /**
13
+ * Restores quotes in a string by replacing placeholders with actual quotes
14
+ * This works without needing the original mapping
15
+ * @param sanitizedString - String with quote placeholders
16
+ * @returns Original string with quotes restored
17
+ */
18
+ export declare function restoreQuotesInString(sanitizedString: string): string;
19
+ /**
20
+ * Recursively restores quotes in an object's string values
21
+ * This works across Lambda functions without requiring original context
22
+ * @param obj - Object to process
23
+ * @returns Object with quotes restored
24
+ */
25
+ export declare function restoreQuotesInObject(obj: any): any;
26
+ /**
27
+ * Legacy function signature for backward compatibility
28
+ * Now ignores the originalMapping parameter since it's not needed
29
+ * @deprecated Use restoreQuotesInObject(obj) instead
30
+ */
31
+ export declare function restoreQuotesInObjectLegacy(obj: any, originalMapping?: Map<string, string>): any;
32
+ //# sourceMappingURL=sanitize_strings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize_strings.d.ts","sourceRoot":"","sources":["../../src/utils/sanitize_strings.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAEpE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAErE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAkBnD;AAED;;;;GAIG;AACH,wBAAgB,2BAA2B,CACzC,GAAG,EAAE,GAAG,EACR,eAAe,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACpC,GAAG,CAEL"}
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ /**
3
+ * Utility functions for sanitizing strings for OpenAI structured outputs
4
+ * and restoring them back to their original form without requiring original mapping
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.sanitizeStringsForSchema = sanitizeStringsForSchema;
8
+ exports.restoreQuotesInString = restoreQuotesInString;
9
+ exports.restoreQuotesInObject = restoreQuotesInObject;
10
+ exports.restoreQuotesInObjectLegacy = restoreQuotesInObjectLegacy;
11
+ // Use a unique, reversible encoding for quotes
12
+ const QUOTE_PLACEHOLDER = "___QUOTE___";
13
+ /**
14
+ * Sanitizes an array of strings by replacing quotes with a safe placeholder
15
+ * This approach is stateless and doesn't require storing mappings
16
+ * @param strings - Array of strings to sanitize
17
+ * @returns Array of sanitized strings
18
+ */
19
+ function sanitizeStringsForSchema(strings) {
20
+ return strings.map((str) => str.replace(/"/g, QUOTE_PLACEHOLDER));
21
+ }
22
+ /**
23
+ * Restores quotes in a string by replacing placeholders with actual quotes
24
+ * This works without needing the original mapping
25
+ * @param sanitizedString - String with quote placeholders
26
+ * @returns Original string with quotes restored
27
+ */
28
+ function restoreQuotesInString(sanitizedString) {
29
+ return sanitizedString.replace(new RegExp(QUOTE_PLACEHOLDER, "g"), '"');
30
+ }
31
+ /**
32
+ * Recursively restores quotes in an object's string values
33
+ * This works across Lambda functions without requiring original context
34
+ * @param obj - Object to process
35
+ * @returns Object with quotes restored
36
+ */
37
+ function restoreQuotesInObject(obj) {
38
+ if (typeof obj === "string") {
39
+ return restoreQuotesInString(obj);
40
+ }
41
+ if (Array.isArray(obj)) {
42
+ return obj.map((item) => restoreQuotesInObject(item));
43
+ }
44
+ if (obj && typeof obj === "object") {
45
+ const result = {};
46
+ for (const [key, value] of Object.entries(obj)) {
47
+ result[key] = restoreQuotesInObject(value);
48
+ }
49
+ return result;
50
+ }
51
+ return obj;
52
+ }
53
+ /**
54
+ * Legacy function signature for backward compatibility
55
+ * Now ignores the originalMapping parameter since it's not needed
56
+ * @deprecated Use restoreQuotesInObject(obj) instead
57
+ */
58
+ function restoreQuotesInObjectLegacy(obj, originalMapping) {
59
+ return restoreQuotesInObject(obj);
60
+ }
61
+ //# sourceMappingURL=sanitize_strings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize_strings.js","sourceRoot":"","sources":["../../src/utils/sanitize_strings.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAWH,4DAEC;AAQD,sDAEC;AAQD,sDAkBC;AAOD,kEAKC;AA3DD,+CAA+C;AAC/C,MAAM,iBAAiB,GAAG,aAAa,CAAC;AAExC;;;;;GAKG;AACH,SAAgB,wBAAwB,CAAC,OAAiB;IACxD,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC;AACpE,CAAC;AAED;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,eAAuB;IAC3D,OAAO,eAAe,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,iBAAiB,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAC1E,CAAC;AAED;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,GAAQ;IAC5C,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QACnC,MAAM,MAAM,GAAQ,EAAE,CAAC;QACvB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/C,MAAM,CAAC,GAAG,CAAC,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;GAIG;AACH,SAAgB,2BAA2B,CACzC,GAAQ,EACR,eAAqC;IAErC,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "only_ever_generator",
3
- "version": "6.0.1",
3
+ "version": "6.0.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -94,10 +94,6 @@ export class OnlyEverGenerator {
94
94
  generate_card: boolean = false
95
95
  ): Promise<Array<any>> {
96
96
  try {
97
- console.log(this.promptIdForTypology);
98
- console.log(this.promptIdForConceptFacts);
99
- console.log(this.promptIdForCardsGeneration);
100
- console.log(this.openAiService.api_key);
101
97
  let args = new GenerateArgs(generate_card, generate_typology, false);
102
98
  const responseToReturn = [];
103
99
  const whatNeedsToBeGenerated = args.getWhatNeedsToBeGenerated();
@@ -106,7 +102,6 @@ export class OnlyEverGenerator {
106
102
  console.log("Generating typology");
107
103
  this.typologyResponse = await this.generateTypology();
108
104
  console.log("Typology generated");
109
- console.log(this.typologyResponse);
110
105
 
111
106
  if (
112
107
  this.typologyResponse?.status_code == 200 ||
@@ -1,5 +1,8 @@
1
1
  import { OpenAIHelper } from "../helper/openai_helper";
2
- import { cardSchema } from "../helper/schema_helper/build_card_schema";
2
+ import {
3
+ buildCardSchema,
4
+ cardSchema,
5
+ } from "../helper/schema_helper/build_card_schema";
3
6
  import { ErrorLogger, log_error, ParsingError } from "../logger";
4
7
  import { ParseCardResponse } from "../parse/parse_card_response";
5
8
  import { OpenAiService } from "../services/open_ai_service";
@@ -43,10 +46,7 @@ export class GenerateCards {
43
46
  }
44
47
 
45
48
  async openAIResponse() {
46
- console.log(this.generationVariables);
47
- console.log(this.generationVariables.bloom_instructions);
48
- console.log(this.generationVariables.card_instructions);
49
- console.log(this.generationVariables.card_examples);
49
+ const schema = await buildCardSchema();
50
50
  try {
51
51
  let response: any = await this.openAIHelper.openAI.responses.create({
52
52
  prompt: {
@@ -71,9 +71,9 @@ export class GenerateCards {
71
71
  text: {
72
72
  format: {
73
73
  type: "json_schema",
74
- name: "card_gen_schema",
75
- strict: true,
76
- schema: cardSchema,
74
+ name: schema.name,
75
+ strict: schema.strict,
76
+ schema: schema.schema,
77
77
  },
78
78
  },
79
79
  });
@@ -1,3 +1,23 @@
1
+ import { database, ObjectId, setUp } from "../mongo_helper";
2
+
3
+ export const buildCardSchema = async () => {
4
+ setUp();
5
+ const objectId = new ObjectId("689c5cbd0d9d3bbda64b9584");
6
+ const schemaObj = await database.collection("_prompts").findOne({
7
+ _id: objectId,
8
+ });
9
+ if (!schemaObj) {
10
+ throw new Error("Schema not found");
11
+ }
12
+ const schema = schemaObj.schema;
13
+ if (!schema) {
14
+ throw new Error("Schema not found");
15
+ }
16
+ // Deep clone to avoid mutating the original
17
+ const populatedSchema = JSON.parse(schema);
18
+ return populatedSchema;
19
+ };
20
+
1
21
  export const cardSchema = {
2
22
  type: "object",
3
23
  properties: {
@@ -1,4 +1,5 @@
1
1
  import { database, ObjectId, setUp } from "../mongo_helper";
2
+ import { sanitizeStringsForSchema } from "../../utils/sanitize_strings";
2
3
 
3
4
  // Function to populate enums in a parsed schema
4
5
  export const buildClassifySummarizeSchema = async (headings: string[]) => {
@@ -17,13 +18,16 @@ export const buildClassifySummarizeSchema = async (headings: string[]) => {
17
18
  // Deep clone to avoid mutating the original
18
19
  const populatedSchema = JSON.parse(schema);
19
20
 
21
+ // Sanitize headings to remove quotes for OpenAI structured outputs
22
+ const sanitizedStrings = sanitizeStringsForSchema(headings);
23
+
20
24
  // Type-safe enum population for reference field in summary_cards
21
25
  if (
22
26
  populatedSchema.schema?.properties?.summary_cards?.items?.properties
23
27
  ?.reference?.enum
24
28
  ) {
25
29
  populatedSchema.schema.properties.summary_cards.items.properties.reference.enum =
26
- headings;
30
+ sanitizedStrings;
27
31
  }
28
32
 
29
33
  return populatedSchema;
@@ -1,5 +1,6 @@
1
1
  import { ObjectId } from "mongodb";
2
2
  import { database, setUp } from "../mongo_helper";
3
+ import { sanitizeStringsForSchema } from "../../utils/sanitize_strings";
3
4
 
4
5
  // Function to populate enums in a parsed schema
5
6
  export async function buildConceptFactSchema(
@@ -28,13 +29,16 @@ export async function buildConceptFactSchema(
28
29
 
29
30
  const allowedRefs = [...headings, ""];
30
31
 
32
+ // Sanitize headings to remove quotes for OpenAI structured outputs
33
+ const sanitizedStrings = sanitizeStringsForSchema(allowedRefs);
34
+
31
35
  // Type-safe enum population for reference field
32
36
  if (
33
37
  populatedSchema.schema?.properties?.concepts_facts?.items?.properties
34
38
  ?.reference?.enum
35
39
  ) {
36
40
  populatedSchema.schema.properties.concepts_facts.items.properties.reference.enum =
37
- allowedRefs;
41
+ sanitizedStrings;
38
42
  }
39
43
 
40
44
  return populatedSchema;
package/src/index.ts CHANGED
@@ -16,68 +16,69 @@ import { BaseParamType } from "./types/base_param_type";
16
16
 
17
17
  export { OnlyEverGenerator };
18
18
 
19
- // (async () => {
20
- // const openAIKey =
21
- // "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";
22
- // const promptForSummary = {
23
- // promptId: "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
24
- // version: "13",
25
- // };
26
- // const promptForConceptFacts = {
27
- // promptId: "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
28
- // version: "6",
29
- // };
19
+ (async () => {
20
+ const openAIKey =
21
+ "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";
22
+ const promptForSummary = {
23
+ promptId: "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
24
+ version: "13",
25
+ };
26
+ const promptForConceptFacts = {
27
+ promptId: "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
28
+ version: "6",
29
+ };
30
30
 
31
- // setUp();
31
+ setUp();
32
32
 
33
- // const source = await database.collection("_source").findOne({
34
- // _id: new ObjectId("6753b1ec56e5e922b57ee2df"),
35
- // });
33
+ const source = await database.collection("_source").findOne({
34
+ _id: new ObjectId("6753b1968b647549b03cc3fe"),
35
+ });
36
36
 
37
- // const contentForGen = {
38
- // prompt: {
39
- // typology: "",
40
- // card_generation: "",
41
- // },
42
- // content: {
43
- // source_id: source?._id?.toString() || "",
44
- // title: source?.title || "",
45
- // type: source?.source_type || "",
46
- // headings: source?.headings || [],
47
- // content: source?.content || [],
48
- // fields: source?.fields || [],
49
- // taxonomy: source?.generate_cards
50
- // ? {
51
- // generate_cards: source?.generate_cards || {
52
- // state: false,
53
- // reason: "",
54
- // },
55
- // concepts_facts: source?.concepts_facts || [],
56
- // fields: source?.fields || [],
57
- // }
58
- // : undefined,
59
- // },
60
- // };
61
- // const prompts = await getPrompts("text");
62
- // const bloomInstructions = prompts.bloom_instructions;
63
- // const cardInstructions = prompts.card_instructions;
64
- // const cardExamples = prompts.card_examples;
37
+ const contentForGen = {
38
+ prompt: {
39
+ typology: "",
40
+ card_generation: "",
41
+ },
42
+ content: {
43
+ source_id: source?._id?.toString() || "",
44
+ title: source?.title || "",
45
+ type: source?.source_type || "",
46
+ headings: source?.headings || [],
47
+ content: source?.content || [],
48
+ fields: source?.fields || [],
49
+ taxonomy: source?.generate_cards
50
+ ? {
51
+ generate_cards: source?.generate_cards || {
52
+ state: false,
53
+ reason: "",
54
+ },
55
+ concepts_facts: source?.concepts_facts || [],
56
+ fields: source?.fields || [],
57
+ }
58
+ : undefined,
59
+ },
60
+ };
61
+ const prompts = await getPrompts("text");
62
+ const bloomInstructions = prompts.bloom_instructions;
63
+ const cardInstructions = prompts.card_instructions;
64
+ const cardExamples = prompts.card_examples;
65
65
 
66
- // console.log("Initializing OnlyEverGenerator");
67
- // const onlyEverGenerator = new OnlyEverGenerator(
68
- // process.env.OPEN_AI_KEY as string,
69
- // "o4-mini",
70
- // contentForGen,
66
+ console.log("Initializing OnlyEverGenerator");
67
+ const onlyEverGenerator = new OnlyEverGenerator(
68
+ process.env.OPEN_AI_KEY as string,
69
+ "o4-mini",
70
+ contentForGen,
71
71
 
72
- // "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
73
- // "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
74
- // "pmpt_688118a923e4819098176a13a2f401920d2ea17d881cc6c6",
75
- // {
76
- // bloom_instructions: bloomInstructions,
77
- // card_instructions: cardInstructions,
78
- // card_examples: cardExamples,
79
- // }
80
- // );
81
- // const response = await onlyEverGenerator.generate(true, true);
82
- // console.log(response);
83
- // })();
72
+ "pmpt_687a8872d1c8819088a9cdc97dcb688b0893663621695594",
73
+ "pmpt_687aa547f99c8193b99467ca53630c2607f5a8caf451e7e8",
74
+ "pmpt_688118a923e4819098176a13a2f401920d2ea17d881cc6c6",
75
+ {
76
+ bloom_instructions: bloomInstructions,
77
+ card_instructions: cardInstructions,
78
+ card_examples: cardExamples,
79
+ },
80
+ false
81
+ );
82
+ const response = await onlyEverGenerator.generate(true, true);
83
+ console.log(response);
84
+ })();
@@ -2,6 +2,10 @@ import { buildConceptFactSchema } from "../helper/schema_helper/build_concept_fa
2
2
  import { OpenAIHelper } from "../helper/openai_helper";
3
3
  import { OpenAiService } from "../services/open_ai_service";
4
4
  import { ErrorLogger, log_error, ParsingError } from "../logger";
5
+ import {
6
+ restoreQuotesInObject,
7
+ restoreQuotesInString,
8
+ } from "../utils/sanitize_strings";
5
9
 
6
10
  export class GenerateConceptFacts {
7
11
  public openAiService: OpenAiService;
@@ -94,6 +98,12 @@ export class GenerateConceptFacts {
94
98
  });
95
99
  return;
96
100
  }
101
+
102
+ // // Restore quotes in the response (works without original mapping)
103
+ // if (openAiResponse.output) {
104
+ // openAiResponse.output = restoreQuotesInObject(openAiResponse.output);
105
+ // }
106
+
97
107
  openAiResponse.metadata = {
98
108
  req_time: openAiResponse.created ?? new Date(),
99
109
  req_type: {
@@ -136,7 +146,14 @@ export class GenerateConceptFacts {
136
146
  parseJson(response: any) {
137
147
  try {
138
148
  const concepts_facts = JSON.parse(response.output_text).concepts_facts;
139
- return concepts_facts;
149
+ if (concepts_facts) {
150
+ return concepts_facts.map((item: any) => {
151
+ return {
152
+ ...item,
153
+ reference: restoreQuotesInString(item.reference),
154
+ };
155
+ });
156
+ }
140
157
  } catch (e) {
141
158
  throw new ParsingError(
142
159
  "Something went wrong in parsing the json",
@@ -4,6 +4,10 @@ import { ErrorLogger, log_error, ParsingError } from "../logger";
4
4
  import { OpenAiService } from "../services/open_ai_service";
5
5
  import { text } from "stream/consumers";
6
6
  import { buildClassifySummarizeSchema } from "../helper/schema_helper/build_classify_summarize_schema";
7
+ import {
8
+ restoreQuotesInObject,
9
+ restoreQuotesInString,
10
+ } from "../utils/sanitize_strings";
7
11
 
8
12
  export class GenerateTypology {
9
13
  public openAiService: OpenAiService;
@@ -97,6 +101,12 @@ export class GenerateTypology {
97
101
  });
98
102
  return;
99
103
  }
104
+
105
+ // Restore quotes in the response (works without original mapping)
106
+ // if (openAiResponse.output) {
107
+ // openAiResponse.output = restoreQuotesInObject(openAiResponse.output);
108
+ // }
109
+
100
110
  openAiResponse["request_type"] = {
101
111
  type: "breadth",
102
112
  n: 1,
@@ -158,6 +168,19 @@ export class GenerateTypology {
158
168
  parseJson(response: any) {
159
169
  try {
160
170
  const generatedContent = JSON.parse(response.output_text);
171
+ const summaryCards = generatedContent?.summary_cards ?? [];
172
+ if (summaryCards) {
173
+ const remappedSummaryCards = summaryCards.map((item: any) => {
174
+ return {
175
+ ...item,
176
+ reference: restoreQuotesInString(item.reference),
177
+ };
178
+ });
179
+ return {
180
+ ...generatedContent,
181
+ summary_cards: remappedSummaryCards,
182
+ };
183
+ }
161
184
  return generatedContent;
162
185
  } catch (e) {
163
186
  throw new ParsingError(
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Utility for restoring quotes in OpenAI responses across distributed Lambda functions
3
+ * This works without requiring original context from the API call Lambda
4
+ */
5
+
6
+ import { restoreQuotesInObject } from "./sanitize_strings";
7
+
8
+ /**
9
+ * Restores quotes in any OpenAI response structure
10
+ * Can be used in a parsing Lambda that receives the response from an API call Lambda
11
+ * @param openAiResponse - The complete OpenAI response object
12
+ * @returns Response with quotes restored in all string fields
13
+ */
14
+ export function restoreQuotesInOpenAiResponse(openAiResponse: any): any {
15
+ if (!openAiResponse) {
16
+ return openAiResponse;
17
+ }
18
+
19
+ // Create a deep copy to avoid mutating the original
20
+ const restoredResponse = JSON.parse(JSON.stringify(openAiResponse));
21
+
22
+ // Restore quotes in the main output/content
23
+ if (restoredResponse.output) {
24
+ restoredResponse.output = restoreQuotesInObject(restoredResponse.output);
25
+ }
26
+
27
+ // Handle different response formats
28
+ if (
29
+ restoredResponse.choices &&
30
+ restoredResponse.choices[0]?.message?.content
31
+ ) {
32
+ try {
33
+ const content = JSON.parse(restoredResponse.choices[0].message.content);
34
+ restoredResponse.choices[0].message.content = JSON.stringify(
35
+ restoreQuotesInObject(content)
36
+ );
37
+ } catch (error) {
38
+ // If content is not JSON, restore quotes directly
39
+ restoredResponse.choices[0].message.content = restoreQuotesInObject(
40
+ restoredResponse.choices[0].message.content
41
+ );
42
+ }
43
+ }
44
+
45
+ // Handle any other potential content fields
46
+ if (restoredResponse.content) {
47
+ restoredResponse.content = restoreQuotesInObject(restoredResponse.content);
48
+ }
49
+
50
+ if (restoredResponse.generated_content) {
51
+ restoredResponse.generated_content = restoreQuotesInObject(
52
+ restoredResponse.generated_content
53
+ );
54
+ }
55
+
56
+ return restoredResponse;
57
+ }
58
+
59
+ /**
60
+ * Restores quotes in a parsed response object (after JSON parsing)
61
+ * Use this when you have already parsed the OpenAI response JSON
62
+ * @param parsedResponse - The parsed response object
63
+ * @returns Response with quotes restored
64
+ */
65
+ export function restoreQuotesInParsedResponse(parsedResponse: any): any {
66
+ return restoreQuotesInObject(parsedResponse);
67
+ }
68
+
69
+ /**
70
+ * Example usage in a parsing Lambda function:
71
+ *
72
+ * // Receiving response from API call Lambda
73
+ * const openAiResponse = event.openAiResponse;
74
+ *
75
+ * // Restore quotes without needing original mapping
76
+ * const restoredResponse = restoreQuotesInOpenAiResponse(openAiResponse);
77
+ *
78
+ * // Now process the response with original quotes restored
79
+ * const processedData = processResponse(restoredResponse);
80
+ */