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,168 @@
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 { buildSummarySchema } from "../helper/schema_helper/build_summary_schema";
10
+
11
+ export class GenerateSummaryCards {
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 promptIdForSummaryCards: 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
+ promptIdForSummaryCards: string,
35
+ generationCurriculum: boolean
36
+ ) {
37
+ this.openAiService = openAiService;
38
+ this.openAIHelper = new OpenAIHelper(this.openAiService.api_key);
39
+ this.content = content;
40
+ this.promptIdForSummaryCards = promptIdForSummaryCards;
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 buildSummarySchema(
53
+ headings.length > 0 ? headings : [""],
54
+ "summary_cards",
55
+ true
56
+ );
57
+ const openAiResponse: any =
58
+ await this.openAIHelper.openAI.responses.create({
59
+ prompt: {
60
+ id: this.promptIdForSummaryCards,
61
+ // version: "35",
62
+ variables: {
63
+ heading_type: this.type == "video" ? "timecode" : "h1 heading",
64
+ source_title: this.content.title,
65
+ source_content: JSON.stringify(this.content.content),
66
+ source_headings: JSON.stringify(this.content.h1_headings || []),
67
+ },
68
+ },
69
+ max_output_tokens: 30000,
70
+ // input: [
71
+ // {
72
+ // role: "user",
73
+ // content: [
74
+ // {
75
+ // type: "input_text",
76
+ // text: JSON.stringify(this.content),
77
+ // },
78
+ // ],
79
+ // },
80
+ // ],
81
+ // input: JSON.stringify(this.content),
82
+ store: true,
83
+ text: {
84
+ format: {
85
+ type: "json_schema",
86
+ name: schema.name,
87
+ strict: schema.strict,
88
+ schema: schema.schema,
89
+ },
90
+ },
91
+ });
92
+ if (!openAiResponse) {
93
+ await log_error({
94
+ flow: "manual_generation",
95
+ data: openAiResponse,
96
+ error: "empty_openai_response",
97
+ timestamp: new Date(),
98
+ source_id: this.sourceId,
99
+ type: {
100
+ request_type: "concept_fact",
101
+ n: 1,
102
+ },
103
+ });
104
+ return;
105
+ }
106
+
107
+ openAiResponse.metadata = {
108
+ req_time: openAiResponse.created ?? new Date(),
109
+ req_type: {
110
+ type: "breadth",
111
+ sub_type: "summary_cards",
112
+ n: 1,
113
+ },
114
+ req_tokens: openAiResponse.usage?.input_tokens ?? 0,
115
+ res_tokens: openAiResponse.usage?.output_tokens ?? 0,
116
+ prompt: {
117
+ id: openAiResponse.prompt.id,
118
+ version: openAiResponse.prompt.version,
119
+ },
120
+ // prompt_tokens_details: openAiResponse.usage?.,
121
+ model: this.openAiService.model,
122
+ usage: openAiResponse.usage,
123
+ status: "completed",
124
+ };
125
+ const summaryCards = this.parseJson(openAiResponse);
126
+
127
+ return {
128
+ summary_cards: summaryCards,
129
+ metadata: openAiResponse.metadata,
130
+ };
131
+ } catch (error) {
132
+ console.log(error);
133
+ await log_error({
134
+ flow: this.generationCurriculum
135
+ ? "curriculum_manual_generation"
136
+ : "manual_generation",
137
+ data: error,
138
+ timestamp: new Date(),
139
+ error: "error_in_concept_facts_generation",
140
+ source_id: this.sourceId,
141
+ type: {
142
+ request_type: "concept_fact",
143
+ n: 1,
144
+ },
145
+ });
146
+ return;
147
+ }
148
+ }
149
+
150
+ parseJson(response: any) {
151
+ try {
152
+ const summaryCards = JSON.parse(response.output_text).summary_cards;
153
+ if (summaryCards) {
154
+ return summaryCards.map((item: any) => {
155
+ return {
156
+ ...item,
157
+ reference: restoreQuotesInString(item.reference),
158
+ };
159
+ });
160
+ }
161
+ } catch (e) {
162
+ throw new ParsingError(
163
+ "Something went wrong in parsing the json",
164
+ response
165
+ );
166
+ }
167
+ }
168
+ }