only_ever_generator 8.4.6 → 8.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bootstrap/app.d.ts +6 -3
- package/dist/bootstrap/app.d.ts.map +1 -1
- package/dist/bootstrap/app.js +11 -17
- package/dist/bootstrap/app.js.map +1 -1
- package/dist/card_gen/generate_cards.d.ts +2 -3
- package/dist/card_gen/generate_cards.d.ts.map +1 -1
- package/dist/card_gen/generate_cards.js +22 -15
- package/dist/card_gen/generate_cards.js.map +1 -1
- package/dist/constants/prompt_data.d.ts +4 -4
- package/dist/constants/prompt_data.js +302 -302
- package/dist/constants/prompts/card_gen_prompt.js +160 -160
- package/dist/constants/prompts/typology_prompt.js +131 -131
- package/dist/constants/source_data.d.ts +171 -171
- package/dist/constants/source_data.js +973 -973
- package/dist/embedding_generation/local_consolidation.js +104 -104
- package/dist/helper/build_concept_facts_schema.d.ts +42 -42
- package/dist/helper/build_concept_facts_schema.js +44 -44
- package/dist/helper/qdrant_db_methods.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_card_schema.d.ts +1 -9
- package/dist/helper/schema_helper/build_card_schema.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_card_schema.js +47 -50
- package/dist/helper/schema_helper/build_card_schema.js.map +1 -1
- package/dist/helper/schema_helper/build_concept_facts_schema.d.ts +1 -1
- package/dist/helper/schema_helper/build_concept_facts_schema.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_concept_facts_schema.js +20 -5
- package/dist/helper/schema_helper/build_concept_facts_schema.js.map +1 -1
- package/dist/helper/schema_helper/build_summary_schema.d.ts +1 -1
- package/dist/helper/schema_helper/build_summary_schema.d.ts.map +1 -1
- package/dist/helper/schema_helper/build_summary_schema.js +18 -7
- package/dist/helper/schema_helper/build_summary_schema.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/parse/response_format_card.d.ts +176 -176
- package/dist/parse/response_format_card.js +371 -371
- package/dist/parse/response_format_typology.d.ts +1 -1
- package/dist/parse/response_format_typology.js +46 -46
- package/dist/services/get_prompts.d.ts +8 -7
- package/dist/services/get_prompts.d.ts.map +1 -1
- package/dist/services/get_prompts.js +69 -21
- package/dist/services/get_prompts.js.map +1 -1
- package/dist/typology_gen/generate_concept_facts.d.ts +2 -3
- package/dist/typology_gen/generate_concept_facts.d.ts.map +1 -1
- package/dist/typology_gen/generate_concept_facts.js +25 -15
- package/dist/typology_gen/generate_concept_facts.js.map +1 -1
- package/dist/typology_gen/generate_typology.d.ts +2 -1
- package/dist/typology_gen/generate_typology.d.ts.map +1 -1
- package/dist/typology_gen/generate_typology.js +24 -13
- package/dist/typology_gen/generate_typology.js.map +1 -1
- package/dist/typology_gen/summarize.d.ts +2 -3
- package/dist/typology_gen/summarize.d.ts.map +1 -1
- package/dist/typology_gen/summarize.js +24 -13
- package/dist/typology_gen/summarize.js.map +1 -1
- package/package.json +39 -38
- package/src/bootstrap/app.ts +418 -416
- package/src/card_gen/generate_cards.ts +347 -345
- package/src/config.ts +11 -11
- package/src/constants/api_constants.ts +7 -7
- package/src/constants/prompts/card_gen_prompt.ts +164 -164
- package/src/constants/prompts/typology_prompt.ts +139 -139
- package/src/embedding_generation/consolidation/global_consolidation.ts +96 -96
- package/src/embedding_generation/consolidation/local_consolidation.ts +141 -141
- package/src/embedding_generation/consolidation/write_consolidated_data.ts +98 -98
- package/src/embedding_generation/generate_embeddings.ts +42 -42
- package/src/embedding_generation/parse_embedding_response.ts +31 -31
- package/src/enums/card_type_enum.ts +6 -6
- package/src/gap_fill/calculate_gap_fill.ts +50 -50
- package/src/helper/get_id_from_title.ts +33 -33
- package/src/helper/mongo_helper.ts +29 -29
- package/src/helper/openai_helper.ts +20 -20
- package/src/helper/qdrant_db_methods.ts +77 -77
- package/src/helper/schema_helper/build_card_schema.ts +74 -98
- package/src/helper/schema_helper/build_classify_summarize_schema.ts +43 -43
- package/src/helper/schema_helper/build_concept_facts_schema.ts +45 -31
- package/src/helper/schema_helper/build_summary_schema.ts +43 -32
- package/src/index.ts +71 -73
- package/src/logger.ts +65 -65
- package/src/parse/parse_card/parse_cloze_card.ts +146 -146
- package/src/parse/parse_card/parse_flash_cards.ts +42 -42
- package/src/parse/parse_card/parse_match_card.ts +104 -104
- package/src/parse/parse_card/parse_mcq_card.ts +114 -114
- package/src/parse/parse_card_response.ts +197 -197
- package/src/parse/parse_source_content.ts +212 -212
- package/src/services/get_prompts.ts +164 -112
- package/src/services/open_ai_service.ts +89 -89
- package/src/services/qdrant_service.ts +10 -10
- package/src/types/base_param_type.ts +13 -13
- package/src/types/mongo_concept_fact_type.ts +12 -12
- package/src/types/parsed_card_type.ts +39 -39
- package/src/types/raw_card_response_types/generated_card_response_type.ts +59 -59
- package/src/types/source_taxonomy_type.ts +24 -24
- package/src/typology-parsed-response.ts +1932 -1932
- package/src/typology_gen/generate_concept_facts.ts +180 -169
- package/src/typology_gen/generate_typology.ts +203 -189
- package/src/typology_gen/summarize.ts +176 -164
- package/src/utils/distributed_quote_restoration.ts +80 -80
- package/src/utils/generate_args.ts +29 -29
- package/src/utils/parse_openai_response.ts +19 -19
- package/src/utils/sanitize_strings.ts +65 -65
- package/tsconfig.json +16 -16
- package/dist/constants/default_generation_variables.d.ts +0 -3
- package/dist/constants/default_generation_variables.d.ts.map +0 -1
- package/dist/constants/default_generation_variables.js +0 -580
- package/dist/constants/default_generation_variables.js.map +0 -1
- package/dist/services/prompts_test.d.ts +0 -10
- package/dist/services/prompts_test.d.ts.map +0 -1
- package/dist/services/prompts_test.js +0 -227
- package/dist/services/prompts_test.js.map +0 -1
- package/dist/types/generation_variables_schema.d.ts +0 -14
- package/dist/types/generation_variables_schema.d.ts.map +0 -1
- package/dist/types/generation_variables_schema.js +0 -3
- package/dist/types/generation_variables_schema.js.map +0 -1
- package/dist/utils/test.d.ts +0 -2
- package/dist/utils/test.d.ts.map +0 -1
- package/dist/utils/test.js +0 -5
- package/dist/utils/test.js.map +0 -1
- package/src/constants/default_generation_variables.ts +0 -624
- package/src/types/generation_variables_schema.ts +0 -16
|
@@ -1,164 +1,176 @@
|
|
|
1
|
-
import { OpenAIHelper } from "../helper/openai_helper";
|
|
2
|
-
import { OpenAiService } from "../services/open_ai_service";
|
|
3
|
-
import { log_error, ParsingError } from "../logger";
|
|
4
|
-
import { restoreQuotesInString } from "../utils/sanitize_strings";
|
|
5
|
-
import { buildSummarySchema } from "../helper/schema_helper/build_summary_schema";
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
public
|
|
11
|
-
public
|
|
12
|
-
public
|
|
13
|
-
public
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
generationCurriculum: boolean
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
this.type = type;
|
|
39
|
-
this.sourceId = sourceId;
|
|
40
|
-
this.generationCurriculum = generationCurriculum;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
1
|
+
import { OpenAIHelper } from "../helper/openai_helper";
|
|
2
|
+
import { OpenAiService } from "../services/open_ai_service";
|
|
3
|
+
import { log_error, ParsingError } from "../logger";
|
|
4
|
+
import { restoreQuotesInString } from "../utils/sanitize_strings";
|
|
5
|
+
import { buildSummarySchema } from "../helper/schema_helper/build_summary_schema";
|
|
6
|
+
import { jsonrepair } from "jsonrepair";
|
|
7
|
+
|
|
8
|
+
export class GenerateSummaryCards {
|
|
9
|
+
public openAiService: OpenAiService;
|
|
10
|
+
public openAIHelper: OpenAIHelper;
|
|
11
|
+
public sourceId: string;
|
|
12
|
+
public generationCurriculum: boolean;
|
|
13
|
+
public content: {
|
|
14
|
+
title: string;
|
|
15
|
+
h1_headings?: string[];
|
|
16
|
+
timecodes?: string[];
|
|
17
|
+
content: any[];
|
|
18
|
+
};
|
|
19
|
+
public type: string = "";
|
|
20
|
+
public promptIdForSummaryCards: string;
|
|
21
|
+
constructor(
|
|
22
|
+
openAiService: OpenAiService,
|
|
23
|
+
sourceId: string,
|
|
24
|
+
content: {
|
|
25
|
+
title: string;
|
|
26
|
+
h1_headings?: string[];
|
|
27
|
+
timecodes?: string[];
|
|
28
|
+
content: any[];
|
|
29
|
+
},
|
|
30
|
+
type: string,
|
|
31
|
+
promptIdForSummaryCards: string,
|
|
32
|
+
generationCurriculum: boolean
|
|
33
|
+
) {
|
|
34
|
+
this.openAiService = openAiService;
|
|
35
|
+
this.openAIHelper = new OpenAIHelper(this.openAiService.api_key);
|
|
36
|
+
this.content = content;
|
|
37
|
+
this.promptIdForSummaryCards = promptIdForSummaryCards;
|
|
38
|
+
this.type = type;
|
|
39
|
+
this.sourceId = sourceId;
|
|
40
|
+
this.generationCurriculum = generationCurriculum;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async generate() {
|
|
44
|
+
try {
|
|
45
|
+
const headings =
|
|
46
|
+
this.type === "text"
|
|
47
|
+
? this.content.h1_headings || [""]
|
|
48
|
+
: this.content.timecodes || [""];
|
|
49
|
+
const schema = await buildSummarySchema(
|
|
50
|
+
headings.length > 0 ? headings : [""],
|
|
51
|
+
"summary_cards",
|
|
52
|
+
true
|
|
53
|
+
);
|
|
54
|
+
const openAiResponse: any =
|
|
55
|
+
await this.openAIHelper.openAI.responses.create({
|
|
56
|
+
prompt: {
|
|
57
|
+
id: this.promptIdForSummaryCards,
|
|
58
|
+
variables: {
|
|
59
|
+
heading_type: this.type == "video" ? "timecode" : "h1 heading",
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
max_output_tokens: 30000,
|
|
63
|
+
input: [
|
|
64
|
+
{
|
|
65
|
+
role: "user",
|
|
66
|
+
content: [
|
|
67
|
+
{
|
|
68
|
+
type: "input_text",
|
|
69
|
+
text: JSON.stringify(this.content),
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
store: true,
|
|
75
|
+
text: {
|
|
76
|
+
format: {
|
|
77
|
+
type: "json_schema",
|
|
78
|
+
name: schema.name,
|
|
79
|
+
strict: schema.strict,
|
|
80
|
+
schema: schema.schema,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
if (!openAiResponse) {
|
|
85
|
+
await log_error({
|
|
86
|
+
flow: "manual_generation",
|
|
87
|
+
data: openAiResponse,
|
|
88
|
+
error: "empty_openai_response",
|
|
89
|
+
timestamp: new Date(),
|
|
90
|
+
source_id: this.sourceId,
|
|
91
|
+
type: {
|
|
92
|
+
request_type: "summary_cards",
|
|
93
|
+
n: 1,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
openAiResponse.metadata = {
|
|
100
|
+
req_time: openAiResponse.created ?? new Date(),
|
|
101
|
+
req_type: {
|
|
102
|
+
type: "breadth",
|
|
103
|
+
sub_type: "summary_cards",
|
|
104
|
+
n: 1,
|
|
105
|
+
},
|
|
106
|
+
req_tokens: openAiResponse.usage?.input_tokens ?? 0,
|
|
107
|
+
res_tokens: openAiResponse.usage?.output_tokens ?? 0,
|
|
108
|
+
prompt: {
|
|
109
|
+
id: openAiResponse.prompt.id,
|
|
110
|
+
version: openAiResponse.prompt.version,
|
|
111
|
+
},
|
|
112
|
+
// prompt_tokens_details: openAiResponse.usage?.,
|
|
113
|
+
model: this.openAiService.model,
|
|
114
|
+
usage: openAiResponse.usage,
|
|
115
|
+
status: "completed",
|
|
116
|
+
};
|
|
117
|
+
const summaryCards = this.parseJson(openAiResponse);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
summary_cards: summaryCards,
|
|
121
|
+
metadata: openAiResponse.metadata,
|
|
122
|
+
};
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.log(error);
|
|
125
|
+
await log_error({
|
|
126
|
+
flow: this.generationCurriculum
|
|
127
|
+
? "curriculum_manual_generation"
|
|
128
|
+
: "manual_generation",
|
|
129
|
+
data: error,
|
|
130
|
+
timestamp: new Date(),
|
|
131
|
+
error: "error_in_summary_cards_generation",
|
|
132
|
+
source_id: this.sourceId,
|
|
133
|
+
type: {
|
|
134
|
+
request_type: "summary_cards",
|
|
135
|
+
n: 1,
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
return {
|
|
139
|
+
summary_cards: [],
|
|
140
|
+
metadata: {},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
parseJson(response: any) {
|
|
146
|
+
let parsedJson: any;
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
parsedJson = JSON.parse(response.output_text);
|
|
150
|
+
} catch (e) {
|
|
151
|
+
// If original JSON parse fails, attempt to repair and parse
|
|
152
|
+
try {
|
|
153
|
+
const repairedJson = jsonrepair(response.output_text);
|
|
154
|
+
parsedJson = JSON.parse(repairedJson);
|
|
155
|
+
} catch (repairError) {
|
|
156
|
+
// If repair also fails, throw exception
|
|
157
|
+
throw new ParsingError(
|
|
158
|
+
"Something went wrong in repairing the json",
|
|
159
|
+
response
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Process the parsed content (same flow for both original and repaired JSON)
|
|
165
|
+
const summaryCards = parsedJson.summary_cards;
|
|
166
|
+
if (summaryCards) {
|
|
167
|
+
return summaryCards.map((item: any) => {
|
|
168
|
+
return {
|
|
169
|
+
...item,
|
|
170
|
+
reference: restoreQuotesInString(item.reference),
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return [];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -1,80 +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
|
-
*/
|
|
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
|
+
*/
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
export class GenerateArgs {
|
|
2
|
-
public generate_card: boolean = false;
|
|
3
|
-
public generate_typology: boolean = false;
|
|
4
|
-
public generate_summary: boolean = false;
|
|
5
|
-
|
|
6
|
-
constructor(
|
|
7
|
-
generate_card: boolean,
|
|
8
|
-
generate_typology: boolean,
|
|
9
|
-
generate_summary: boolean
|
|
10
|
-
) {
|
|
11
|
-
this.generate_card = generate_card;
|
|
12
|
-
this.generate_typology = generate_typology;
|
|
13
|
-
this.generate_summary = generate_summary;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
getWhatNeedsToBeGenerated() {
|
|
17
|
-
let returnData = [];
|
|
18
|
-
if (this.generate_typology == true) {
|
|
19
|
-
returnData.push("generate_tyopology");
|
|
20
|
-
}
|
|
21
|
-
if (this.generate_summary == true) {
|
|
22
|
-
returnData.push("generate_summary");
|
|
23
|
-
}
|
|
24
|
-
if (this.generate_card == true) {
|
|
25
|
-
returnData.push("generate_card");
|
|
26
|
-
}
|
|
27
|
-
return returnData;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
export class GenerateArgs {
|
|
2
|
+
public generate_card: boolean = false;
|
|
3
|
+
public generate_typology: boolean = false;
|
|
4
|
+
public generate_summary: boolean = false;
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
generate_card: boolean,
|
|
8
|
+
generate_typology: boolean,
|
|
9
|
+
generate_summary: boolean
|
|
10
|
+
) {
|
|
11
|
+
this.generate_card = generate_card;
|
|
12
|
+
this.generate_typology = generate_typology;
|
|
13
|
+
this.generate_summary = generate_summary;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getWhatNeedsToBeGenerated() {
|
|
17
|
+
let returnData = [];
|
|
18
|
+
if (this.generate_typology == true) {
|
|
19
|
+
returnData.push("generate_tyopology");
|
|
20
|
+
}
|
|
21
|
+
if (this.generate_summary == true) {
|
|
22
|
+
returnData.push("generate_summary");
|
|
23
|
+
}
|
|
24
|
+
if (this.generate_card == true) {
|
|
25
|
+
returnData.push("generate_card");
|
|
26
|
+
}
|
|
27
|
+
return returnData;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export function parseOpenAiSuccessResponse(responseData: any) {
|
|
2
|
-
let choices = JSON.parse(responseData.choices[0].message.content);
|
|
3
|
-
let usuage = responseData.usage;
|
|
4
|
-
let createdTime = responseData.created;
|
|
5
|
-
return {
|
|
6
|
-
status_code: 200,
|
|
7
|
-
usage_data: usuage,
|
|
8
|
-
generated_content: choices,
|
|
9
|
-
generated_at: new Date(createdTime * 1000),
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function parseOpenAiFailureResponse(errorResponse: any) {
|
|
14
|
-
// let statusCode =
|
|
15
|
-
return {
|
|
16
|
-
status_code: errorResponse.status,
|
|
17
|
-
message: errorResponse.data?.error?.code,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
1
|
+
export function parseOpenAiSuccessResponse(responseData: any) {
|
|
2
|
+
let choices = JSON.parse(responseData.choices[0].message.content);
|
|
3
|
+
let usuage = responseData.usage;
|
|
4
|
+
let createdTime = responseData.created;
|
|
5
|
+
return {
|
|
6
|
+
status_code: 200,
|
|
7
|
+
usage_data: usuage,
|
|
8
|
+
generated_content: choices,
|
|
9
|
+
generated_at: new Date(createdTime * 1000),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function parseOpenAiFailureResponse(errorResponse: any) {
|
|
14
|
+
// let statusCode =
|
|
15
|
+
return {
|
|
16
|
+
status_code: errorResponse.status,
|
|
17
|
+
message: errorResponse.data?.error?.code,
|
|
18
|
+
};
|
|
19
|
+
}
|