only_ever_generator 1.0.3 → 1.0.5
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 +91 -0
- package/dist/bootstrap/app.d.ts.map +1 -0
- package/dist/bootstrap/app.js +120 -84
- package/dist/bootstrap/app.js.map +1 -0
- package/dist/card_gen/generate_cards.d.ts +7 -0
- package/dist/card_gen/generate_cards.d.ts.map +1 -0
- package/dist/card_gen/generate_cards.js +60 -0
- package/dist/card_gen/generate_cards.js.map +1 -0
- package/dist/config.d.ts +8 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +4 -1
- package/dist/config.js.map +1 -0
- package/dist/constants/api_constants.d.ts +3 -0
- package/dist/constants/api_constants.d.ts.map +1 -0
- package/dist/constants/api_constants.js +11 -0
- package/dist/constants/api_constants.js.map +1 -0
- package/dist/constants/prompt_data.d.ts +5 -0
- package/dist/constants/prompt_data.d.ts.map +1 -0
- package/dist/constants/prompt_data.js +303 -0
- package/dist/constants/prompt_data.js.map +1 -0
- package/dist/constants/prompts/card_gen_prompt.d.ts +2 -0
- package/dist/constants/prompts/card_gen_prompt.d.ts.map +1 -0
- package/dist/constants/prompts/card_gen_prompt.js +100 -203
- package/dist/constants/prompts/card_gen_prompt.js.map +1 -0
- package/dist/constants/prompts/typology_prompt.d.ts +2 -0
- package/dist/constants/prompts/typology_prompt.d.ts.map +1 -0
- package/dist/constants/prompts/typology_prompt.js +78 -34
- package/dist/constants/prompts/typology_prompt.js.map +1 -0
- package/dist/constants/source_data.d.ts +173 -0
- package/dist/constants/source_data.d.ts.map +1 -0
- package/dist/constants/source_data.js +962 -10
- package/dist/constants/source_data.js.map +1 -0
- package/dist/embedding_generation/consolidation/global_consolidation.d.ts +33 -0
- package/dist/embedding_generation/consolidation/global_consolidation.d.ts.map +1 -0
- package/dist/embedding_generation/consolidation/global_consolidation.js +76 -0
- package/dist/embedding_generation/consolidation/global_consolidation.js.map +1 -0
- package/dist/embedding_generation/consolidation/local_consolidation.d.ts +39 -0
- package/dist/embedding_generation/consolidation/local_consolidation.d.ts.map +1 -0
- package/dist/embedding_generation/consolidation/local_consolidation.js +105 -0
- package/dist/embedding_generation/consolidation/local_consolidation.js.map +1 -0
- package/dist/embedding_generation/consolidation/write_consolidated_data.d.ts +46 -0
- package/dist/embedding_generation/consolidation/write_consolidated_data.d.ts.map +1 -0
- package/dist/embedding_generation/consolidation/write_consolidated_data.js +69 -0
- package/dist/embedding_generation/consolidation/write_consolidated_data.js.map +1 -0
- package/dist/embedding_generation/generate_embeddings.d.ts +28 -0
- package/dist/embedding_generation/generate_embeddings.d.ts.map +1 -0
- package/dist/embedding_generation/generate_embeddings.js +54 -0
- package/dist/embedding_generation/generate_embeddings.js.map +1 -0
- package/dist/embedding_generation/parse_embedding_response.d.ts +26 -0
- package/dist/embedding_generation/parse_embedding_response.d.ts.map +1 -0
- package/dist/embedding_generation/parse_embedding_response.js +29 -0
- package/dist/embedding_generation/parse_embedding_response.js.map +1 -0
- package/dist/gap_fill/calculate_gap_fill.d.ts +5 -0
- package/dist/gap_fill/calculate_gap_fill.d.ts.map +1 -0
- package/dist/gap_fill/calculate_gap_fill.js +43 -0
- package/dist/gap_fill/calculate_gap_fill.js.map +1 -0
- package/dist/helper/qdrant_db_methods.d.ts +40 -0
- package/dist/helper/qdrant_db_methods.d.ts.map +1 -0
- package/dist/helper/qdrant_db_methods.js +63 -0
- package/dist/helper/qdrant_db_methods.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +86 -149
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +6 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +42 -0
- package/dist/logger.js.map +1 -0
- package/dist/parse/parse_card/parse_cloze_card.d.ts +10 -0
- package/dist/parse/parse_card/parse_cloze_card.d.ts.map +1 -0
- package/dist/parse/parse_card/parse_cloze_card.js +126 -0
- package/dist/parse/parse_card/parse_cloze_card.js.map +1 -0
- package/dist/parse/parse_card/parse_flash_cards.d.ts +19 -0
- package/dist/parse/parse_card/parse_flash_cards.d.ts.map +1 -0
- package/dist/parse/parse_card/parse_flash_cards.js +34 -0
- package/dist/parse/parse_card/parse_flash_cards.js.map +1 -0
- package/dist/parse/parse_card/parse_match_card.d.ts +16 -0
- package/dist/parse/parse_card/parse_match_card.d.ts.map +1 -0
- package/dist/parse/parse_card/parse_match_card.js +82 -0
- package/dist/parse/parse_card/parse_match_card.js.map +1 -0
- package/dist/parse/parse_card/parse_mcq_card.d.ts +24 -0
- package/dist/parse/parse_card/parse_mcq_card.d.ts.map +1 -0
- package/dist/parse/parse_card/parse_mcq_card.js +104 -0
- package/dist/parse/parse_card/parse_mcq_card.js.map +1 -0
- package/dist/parse/parse_card_response.d.ts +19 -0
- package/dist/parse/parse_card_response.d.ts.map +1 -0
- package/dist/parse/parse_card_response.js +100 -0
- package/dist/parse/parse_card_response.js.map +1 -0
- package/dist/parse/parse_source_content.d.ts +25 -0
- package/dist/parse/parse_source_content.d.ts.map +1 -0
- package/dist/parse/parse_source_content.js +186 -0
- package/dist/parse/parse_source_content.js.map +1 -0
- package/dist/parse/response_format_card.d.ts +177 -0
- package/dist/parse/response_format_card.d.ts.map +1 -0
- package/dist/parse/response_format_card.js +372 -0
- package/dist/parse/response_format_card.js.map +1 -0
- package/dist/parse/response_format_typology.d.ts +2 -0
- package/dist/parse/response_format_typology.d.ts.map +1 -0
- package/dist/{parse_response → parse}/response_format_typology.js +18 -18
- package/dist/parse/response_format_typology.js.map +1 -0
- package/dist/services/open_ai_service.d.ts +16 -0
- package/dist/services/open_ai_service.d.ts.map +1 -0
- package/dist/services/open_ai_service.js +92 -0
- package/dist/services/open_ai_service.js.map +1 -0
- package/dist/services/qdrant_service.d.ts +4 -0
- package/dist/services/qdrant_service.d.ts.map +1 -0
- package/dist/services/qdrant_service.js +14 -0
- package/dist/services/qdrant_service.js.map +1 -0
- package/dist/typology-parsed-response.d.ts +62 -0
- package/dist/typology-parsed-response.d.ts.map +1 -0
- package/dist/typology-parsed-response.js +1936 -0
- package/dist/typology-parsed-response.js.map +1 -0
- package/dist/typology_gen/generate_typology.d.ts +29 -0
- package/dist/typology_gen/generate_typology.d.ts.map +1 -0
- package/dist/typology_gen/generate_typology.js +92 -15
- package/dist/typology_gen/generate_typology.js.map +1 -0
- package/dist/utils/generate_args.d.ts +8 -0
- package/dist/utils/generate_args.d.ts.map +1 -0
- package/dist/utils/generate_args.js +28 -0
- package/dist/utils/generate_args.js.map +1 -0
- package/dist/utils/parse_openai_response.d.ts +11 -0
- package/dist/utils/parse_openai_response.d.ts.map +1 -0
- package/dist/utils/parse_openai_response.js +11 -10
- package/dist/utils/parse_openai_response.js.map +1 -0
- package/package.json +19 -7
- package/prompts.json +23 -0
- package/readme.md +16 -13
- package/src/bootstrap/app.ts +167 -107
- package/src/card_gen/generate_cards.ts +58 -0
- package/src/config.ts +5 -3
- package/src/constants/api_constants.ts +7 -0
- package/src/constants/prompt_data.ts +301 -0
- package/src/constants/prompts/card_gen_prompt.ts +101 -208
- package/src/constants/prompts/typology_prompt.ts +81 -33
- package/src/constants/source_data.ts +1029 -23
- package/src/embedding_generation/consolidation/global_consolidation.ts +94 -0
- package/src/embedding_generation/consolidation/local_consolidation.ts +141 -0
- package/src/embedding_generation/consolidation/write_consolidated_data.ts +98 -0
- package/src/embedding_generation/generate_embeddings.ts +41 -0
- package/src/embedding_generation/parse_embedding_response.ts +31 -0
- package/src/gap_fill/calculate_gap_fill.ts +50 -0
- package/src/helper/qdrant_db_methods.ts +77 -0
- package/src/index.ts +98 -164
- package/src/logger.ts +30 -0
- package/src/parse/parse_card/parse_cloze_card.ts +136 -0
- package/src/parse/parse_card/parse_flash_cards.ts +33 -0
- package/src/parse/parse_card/parse_match_card.ts +94 -0
- package/src/parse/parse_card/parse_mcq_card.ts +105 -0
- package/src/parse/parse_card_response.ts +117 -0
- package/src/parse/parse_source_content.ts +202 -0
- package/src/parse/response_format_card.ts +386 -0
- package/src/parse/response_format_typology.ts +44 -0
- package/src/services/open_ai_service.ts +88 -0
- package/src/services/qdrant_service.ts +10 -0
- package/src/typology-parsed-response.ts +1932 -0
- package/src/typology_gen/generate_typology.ts +101 -12
- package/src/utils/generate_args.ts +29 -0
- package/src/utils/parse_openai_response.ts +17 -19
- package/tsconfig.json +8 -3
- package/dist/class/parse/parse_source_content.js +0 -62
- package/dist/class/services/open_ai_service.js +0 -25
- package/dist/parse_response/response_format_card.js +0 -210
- package/dist/service/open_ai_request.js +0 -58
- package/src/class/parse/parse_source_content.ts +0 -71
- package/src/class/services/open_ai_service.ts +0 -18
- package/src/parse_response/response_format_card.ts +0 -211
- package/src/parse_response/response_format_typology.ts +0 -44
- package/src/service/open_ai_request.ts +0 -46
|
@@ -1,14 +1,103 @@
|
|
|
1
|
-
import { OpenAiService } from "../
|
|
2
|
-
import { returnTypologyData } from "../
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { OpenAiService } from "../services/open_ai_service";
|
|
2
|
+
import { returnTypologyData } from "../parse/response_format_typology";
|
|
3
|
+
import { ErrorLogger } from "../logger";
|
|
4
|
+
|
|
5
|
+
export class GenerateTypology {
|
|
6
|
+
public openAiService: OpenAiService;
|
|
7
|
+
public prompt: string = "";
|
|
8
|
+
public content: string = "";
|
|
9
|
+
expectedFields: Array<string>;
|
|
10
|
+
constructor(
|
|
11
|
+
openAiService: OpenAiService,
|
|
12
|
+
prompt: string,
|
|
13
|
+
content: string,
|
|
14
|
+
expected_fields: Array<string>
|
|
15
|
+
) {
|
|
16
|
+
this.openAiService = openAiService;
|
|
17
|
+
this.prompt = prompt;
|
|
18
|
+
this.content = content;
|
|
19
|
+
this.expectedFields = expected_fields.map((elem: string) =>
|
|
20
|
+
elem.toLowerCase()
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
async generate() {
|
|
24
|
+
try {
|
|
25
|
+
const response = await this.openAiService?.sendRequest(
|
|
26
|
+
this.prompt,
|
|
27
|
+
this.content
|
|
28
|
+
);
|
|
29
|
+
response["request_type"] = {
|
|
30
|
+
type: "breadth",
|
|
31
|
+
n: 1,
|
|
32
|
+
};
|
|
33
|
+
response.metadata = {
|
|
34
|
+
req_time: response.generated_at ?? new Date(),
|
|
35
|
+
req_type: {
|
|
36
|
+
type: "breadth",
|
|
37
|
+
n: 1,
|
|
38
|
+
},
|
|
39
|
+
req_tokens: response.usage_data?.prompt_tokens,
|
|
40
|
+
res_tokens: response.usage_data?.completion_tokens,
|
|
41
|
+
prompt_tokens_details: response.usage_data?.prompt_tokens_details,
|
|
42
|
+
model: this.openAiService.model,
|
|
43
|
+
};
|
|
44
|
+
if (response.status_code == 200) {
|
|
45
|
+
return this.parseTypologyOnSuccess(response);
|
|
46
|
+
} else {
|
|
47
|
+
response.metadata.err_message = response.message;
|
|
10
48
|
return response;
|
|
11
|
-
|
|
12
|
-
|
|
49
|
+
}
|
|
50
|
+
} catch (e: any) {
|
|
51
|
+
await new ErrorLogger({
|
|
52
|
+
type: "typology_parsing",
|
|
53
|
+
data: e.message,
|
|
54
|
+
}).log();
|
|
13
55
|
}
|
|
14
|
-
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
parseTypologyOnSuccess(responseData: any) {
|
|
59
|
+
responseData.metadata.status = "completed";
|
|
60
|
+
const generatedContent = responseData.generated_content;
|
|
61
|
+
const concepts = generatedContent.concepts.map((e: any) => {
|
|
62
|
+
return {
|
|
63
|
+
text: e.concept_text,
|
|
64
|
+
type: "concept",
|
|
65
|
+
reference: e.reference,
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
const facts = generatedContent.facts.map((e: any) => {
|
|
69
|
+
return {
|
|
70
|
+
text: e.fact_text,
|
|
71
|
+
type: "fact",
|
|
72
|
+
reference: e.reference,
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
status_code: 200,
|
|
78
|
+
metadata: [responseData.metadata],
|
|
79
|
+
field: this.parseFields(generatedContent.field),
|
|
80
|
+
concepts_facts: [...concepts, ...facts],
|
|
81
|
+
generate_cards: generatedContent.generate_cards,
|
|
82
|
+
summary_cards: generatedContent.summary_cards,
|
|
83
|
+
// type: responseData.type,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
parseFields(fields: Array<string>) {
|
|
88
|
+
const fieldKeys = ["primary_field", "secondary_field", "tertiary_field"];
|
|
89
|
+
return fields.slice(0, 3).map((item, index) => ({
|
|
90
|
+
[fieldKeys[index]]: item,
|
|
91
|
+
reconcile: !this.expectedFields.includes(item.toLowerCase()),
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async parseTypologyOnFailure(responseData: any) {
|
|
96
|
+
responseData.metadata.status = "failed";
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
status_code: responseData.status_code,
|
|
100
|
+
metadata: responseData.metadata,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +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,21 +1,19 @@
|
|
|
1
|
-
export function parseOpenAiSuccessResponse(responseData: any){
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
11
|
}
|
|
12
12
|
|
|
13
|
-
export function parseOpenAiFailureResponse(errorResponse: any){
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
}
|
|
13
|
+
export function parseOpenAiFailureResponse(errorResponse: any) {
|
|
14
|
+
// let statusCode =
|
|
15
|
+
return {
|
|
16
|
+
status_code: errorResponse.status,
|
|
17
|
+
message: errorResponse.data?.error?.code,
|
|
18
|
+
};
|
|
19
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -5,8 +5,13 @@
|
|
|
5
5
|
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
6
6
|
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
7
7
|
"strict": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationMap": true,
|
|
10
|
+
"emitDeclarationOnly": false,
|
|
8
11
|
"rootDir": "./src",
|
|
9
|
-
"outDir": "./dist" /* Enable all strict type-checking options. */
|
|
12
|
+
"outDir": "./dist", /* Enable all strict type-checking options. */
|
|
13
|
+
"sourceMap": true
|
|
10
14
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
15
|
+
"include": ["src/**/*"],
|
|
16
|
+
"exclude": ["node_modules", "**/*.test.ts"]
|
|
17
|
+
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ParseSourceContent = void 0;
|
|
4
|
-
class ParseSourceContent {
|
|
5
|
-
constructor(sourceContent) {
|
|
6
|
-
this.titles_to_remove = ['See also', 'References', 'Further reading', 'External links', 'Notes and references', 'Bibliography', 'Notes', 'Cited sources'];
|
|
7
|
-
this.content = sourceContent;
|
|
8
|
-
}
|
|
9
|
-
parse() {
|
|
10
|
-
let dataAfterRemovingUnWantedBlocks = this.removeSectionsByTitle(this.content);
|
|
11
|
-
let afterSanitized = this.sanitizeBlocks(dataAfterRemovingUnWantedBlocks);
|
|
12
|
-
return JSON.stringify(afterSanitized);
|
|
13
|
-
}
|
|
14
|
-
removeSectionsByTitle(data) {
|
|
15
|
-
let dataAfterRemoving = [];
|
|
16
|
-
for (let elem of data) {
|
|
17
|
-
if (elem.block_type == 'heading' && this.titles_to_remove.includes(elem.content)) {
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
if (elem.children) {
|
|
21
|
-
elem.children = this.removeSectionsByTitle(elem.children);
|
|
22
|
-
}
|
|
23
|
-
dataAfterRemoving.push(elem);
|
|
24
|
-
}
|
|
25
|
-
return dataAfterRemoving;
|
|
26
|
-
}
|
|
27
|
-
sanitizeWikiContent(content) {
|
|
28
|
-
// Remove newline characters
|
|
29
|
-
content = content.replace(/\\n/g, ' ');
|
|
30
|
-
// Remove internal link references, keeping only the link text
|
|
31
|
-
// Pattern explanation: [[link|text|index|wiki]] --> text
|
|
32
|
-
content = content.replace(/\[\[.*?\|(.*?)\|.*?\|wiki\]\]/g, '$1');
|
|
33
|
-
// Remove external links, keeping only the link text
|
|
34
|
-
// Pattern explanation: [url text] --> text
|
|
35
|
-
content = content.replace(/\[http[s]?:\/\/[^\s]+ ([^\]]+)\]/g, '$1');
|
|
36
|
-
// Remove Markdown link references, keeping only the link text
|
|
37
|
-
// Pattern explanation:  --> link text
|
|
38
|
-
content = content.replace(/\!\[([^\]]+)\]\([^\)]+\)/g, '$1');
|
|
39
|
-
return content;
|
|
40
|
-
}
|
|
41
|
-
sanitizeBlocks(blocks) {
|
|
42
|
-
let sanitizedBlocks = [];
|
|
43
|
-
blocks.forEach(block => {
|
|
44
|
-
let sanitizedBlock = {};
|
|
45
|
-
for (let key in block) {
|
|
46
|
-
let value = block[key];
|
|
47
|
-
if (typeof value === 'string') {
|
|
48
|
-
sanitizedBlock[key] = this.sanitizeWikiContent(value);
|
|
49
|
-
}
|
|
50
|
-
else if (Array.isArray(value)) {
|
|
51
|
-
sanitizedBlock[key] = this.sanitizeBlocks(value);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
sanitizedBlock[key] = value;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
sanitizedBlocks.push(sanitizedBlock);
|
|
58
|
-
});
|
|
59
|
-
return sanitizedBlocks;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.ParseSourceContent = ParseSourceContent;
|
|
@@ -1,25 +0,0 @@
|
|
|
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.OpenAiService = void 0;
|
|
13
|
-
const open_ai_request_1 = require("../../service/open_ai_request");
|
|
14
|
-
class OpenAiService {
|
|
15
|
-
constructor(apiKey, model) {
|
|
16
|
-
this.api_key = apiKey;
|
|
17
|
-
this.model = model;
|
|
18
|
-
}
|
|
19
|
-
sendRequest(prompt, content) {
|
|
20
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
return yield (0, open_ai_request_1.openAIRequest)(content, prompt, this.api_key, this.model);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.OpenAiService = OpenAiService;
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseResponse = exports.returnResponse = void 0;
|
|
4
|
-
const responseData = {
|
|
5
|
-
"flash_cards": [
|
|
6
|
-
{
|
|
7
|
-
"question": "What is the primary function of the 'Electrolysis'?",
|
|
8
|
-
"answer": "Electrolysis is the passing of a direct electric current through an electrolyte producing chemical reactions at the electrodes and decomposition of the materials.",
|
|
9
|
-
"heading": "Overview"
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
"question": "What did Michael Faraday discover while studying the process of electrolysis under Humphry Davy?",
|
|
13
|
-
"answer": "Michael Faraday discovered two laws of electrolysis.",
|
|
14
|
-
"heading": "History"
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
"question": "What can electrolysis be used for in the manufacturing sector?",
|
|
18
|
-
"answer": "Electrolysis can be used for electroplating and electrochemical machining.",
|
|
19
|
-
"heading": "Manufacturing processes"
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
"question": "In the context of electrolysis, what does the term 'Decomposition potential' refer to?",
|
|
23
|
-
"answer": "Decomposition potential or decomposition voltage refers to the minimum voltage between anode and cathode of an electrolytic cell needed for electrolysis to occur.",
|
|
24
|
-
"heading": "Decomposition potential"
|
|
25
|
-
}
|
|
26
|
-
],
|
|
27
|
-
"mcqs": [
|
|
28
|
-
{
|
|
29
|
-
"question": "What is the primary purpose of 'Electrometallurgy'?",
|
|
30
|
-
"answers": [
|
|
31
|
-
{
|
|
32
|
-
"answer": "Production of aluminium",
|
|
33
|
-
"is_correct": true
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
"answer": "Chlorine production",
|
|
37
|
-
"is_correct": false
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
"answer": "Purifying copper",
|
|
41
|
-
"is_correct": false
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
"answer": "Rust removal",
|
|
45
|
-
"is_correct": false
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
"answer": "Hydrogen production",
|
|
49
|
-
"is_correct": false
|
|
50
|
-
}
|
|
51
|
-
],
|
|
52
|
-
"heading": "Industrial uses"
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
"question": "What can electrolysis be used for in the context of batteries?",
|
|
56
|
-
"answers": [
|
|
57
|
-
{
|
|
58
|
-
"answer": "Spontaneous redox reactions",
|
|
59
|
-
"is_correct": true
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
"answer": "Energy-releasing reactions",
|
|
63
|
-
"is_correct": false
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
"answer": "Disinfectant production",
|
|
67
|
-
"is_correct": false
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
"answer": "Fuel production",
|
|
71
|
-
"is_correct": false
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
"answer": "Gas diffusion in reactors",
|
|
75
|
-
"is_correct": false
|
|
76
|
-
}
|
|
77
|
-
],
|
|
78
|
-
"heading": "Related processes"
|
|
79
|
-
}
|
|
80
|
-
],
|
|
81
|
-
"cloze_cards": [
|
|
82
|
-
{
|
|
83
|
-
"question": "Electrolysis is the passing of a {{c0:direct electric current}} through an {{c1:electrolyte}} producing {{c2:chemical reactions}} at the {{c3:electrodes}} and {{c4:decomposition}} of the materials.",
|
|
84
|
-
"options": [
|
|
85
|
-
{
|
|
86
|
-
"option": "direct electric current",
|
|
87
|
-
"cloze": "c0"
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
"option": "electrolyte",
|
|
91
|
-
"cloze": "c1"
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
"option": "chemical reactions",
|
|
95
|
-
"cloze": "c2"
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
"option": "electrodes",
|
|
99
|
-
"cloze": "c3"
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
"option": "decomposition",
|
|
103
|
-
"cloze": "c4"
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
"option": "metallic objects",
|
|
107
|
-
"cloze": null
|
|
108
|
-
}
|
|
109
|
-
]
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
"question": "In electrolysis, the decomposition potential or decomposition voltage refers to the minimum voltage between {{c0:anode}} and {{c1:cathode}} of an electrolytic cell needed for electrolysis to occur.",
|
|
113
|
-
"options": [
|
|
114
|
-
{
|
|
115
|
-
"option": "anode",
|
|
116
|
-
"cloze": "c0"
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
"option": "cathode",
|
|
120
|
-
"cloze": "c1"
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
"option": "electrolyte",
|
|
124
|
-
"cloze": null
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
"option": "chemical reactions",
|
|
128
|
-
"cloze": null
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
"option": "oxygen",
|
|
132
|
-
"cloze": null
|
|
133
|
-
},
|
|
134
|
-
{
|
|
135
|
-
"option": "hydrogen",
|
|
136
|
-
"cloze": null
|
|
137
|
-
}
|
|
138
|
-
]
|
|
139
|
-
}
|
|
140
|
-
]
|
|
141
|
-
};
|
|
142
|
-
function returnResponse() {
|
|
143
|
-
return responseData;
|
|
144
|
-
}
|
|
145
|
-
exports.returnResponse = returnResponse;
|
|
146
|
-
function parseResponse() {
|
|
147
|
-
let cardData = [];
|
|
148
|
-
let data = returnResponse();
|
|
149
|
-
let keys = Object.keys(data);
|
|
150
|
-
if (keys) {
|
|
151
|
-
for (let elem of keys) {
|
|
152
|
-
if (elem == 'flash_cards') {
|
|
153
|
-
cardData.push(...parseFlashCard(data.flash_cards));
|
|
154
|
-
}
|
|
155
|
-
else if (elem == 'cloze_cards') {
|
|
156
|
-
cardData.push(...parseClozeCards(data.cloze_cards));
|
|
157
|
-
}
|
|
158
|
-
else if (elem == 'mcqs') {
|
|
159
|
-
cardData.push(...parseMcq(data.mcqs));
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return cardData;
|
|
164
|
-
}
|
|
165
|
-
exports.parseResponse = parseResponse;
|
|
166
|
-
/// takes array of
|
|
167
|
-
function parseFlashCard(cards) {
|
|
168
|
-
let flashCardData = [];
|
|
169
|
-
for (let elem of cards) {
|
|
170
|
-
flashCardData.push({
|
|
171
|
-
"type": "flash",
|
|
172
|
-
"heading": elem.heading,
|
|
173
|
-
"content": JSON.stringify({
|
|
174
|
-
"front_content": elem.question,
|
|
175
|
-
"back_content": elem.answer
|
|
176
|
-
})
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
return flashCardData;
|
|
180
|
-
}
|
|
181
|
-
/// takes array of
|
|
182
|
-
function parseMcq(cardsData) {
|
|
183
|
-
let mcqCards = [];
|
|
184
|
-
for (let elem of cardsData) {
|
|
185
|
-
mcqCards.push({
|
|
186
|
-
"type": "mcq",
|
|
187
|
-
"heading": elem.heading,
|
|
188
|
-
"content": JSON.stringify({
|
|
189
|
-
"question": elem.question,
|
|
190
|
-
"answers": elem.answers
|
|
191
|
-
})
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
return mcqCards;
|
|
195
|
-
}
|
|
196
|
-
/// takes array of
|
|
197
|
-
function parseClozeCards(cardsData) {
|
|
198
|
-
let clozeCards = [];
|
|
199
|
-
for (let elem of cardsData) {
|
|
200
|
-
clozeCards.push({
|
|
201
|
-
"type": "cloze",
|
|
202
|
-
"heading": elem.heading,
|
|
203
|
-
"content": JSON.stringify({
|
|
204
|
-
"question": elem.question,
|
|
205
|
-
"options": elem.options
|
|
206
|
-
})
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
return clozeCards;
|
|
210
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.openAIRequest = void 0;
|
|
16
|
-
const axios_1 = __importDefault(require("axios"));
|
|
17
|
-
const parse_openai_response_js_1 = require("../utils/parse_openai_response.js");
|
|
18
|
-
function openAIRequest(content, prompt, token, model) {
|
|
19
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
try {
|
|
21
|
-
let message = [
|
|
22
|
-
{
|
|
23
|
-
"role": "system", "content": prompt
|
|
24
|
-
}, {
|
|
25
|
-
"role": "user", "content": content
|
|
26
|
-
}
|
|
27
|
-
];
|
|
28
|
-
const url = 'https://api.openai.com/v1/chat/completions';
|
|
29
|
-
let response = yield axios_1.default.post(url,
|
|
30
|
-
//data
|
|
31
|
-
{
|
|
32
|
-
"model": model,
|
|
33
|
-
"messages": message,
|
|
34
|
-
"response_format": { "type": "json_object" },
|
|
35
|
-
},
|
|
36
|
-
///config
|
|
37
|
-
{
|
|
38
|
-
headers: {
|
|
39
|
-
Authorization: "Bearer " + token,
|
|
40
|
-
"Content-Type": ['application/json']
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
if (response.status == 200) {
|
|
44
|
-
console.log('success');
|
|
45
|
-
return (0, parse_openai_response_js_1.parseOpenAiSuccessResponse)(response.data);
|
|
46
|
-
// return JSON.parse(response.data.choices[0].message.content);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
console.log('failed');
|
|
50
|
-
return response.statusText;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
return (0, parse_openai_response_js_1.parseOpenAiFailureResponse)(err.response);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
exports.openAIRequest = openAIRequest;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
export class ParseSourceContent{
|
|
2
|
-
public content: Array<any>;
|
|
3
|
-
|
|
4
|
-
titles_to_remove = ['See also', 'References', 'Further reading', 'External links', 'Notes and references', 'Bibliography', 'Notes', 'Cited sources'];
|
|
5
|
-
|
|
6
|
-
constructor(sourceContent:Array<any>){
|
|
7
|
-
this.content = sourceContent;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
parse() {
|
|
11
|
-
let dataAfterRemovingUnWantedBlocks = this.removeSectionsByTitle(this.content);
|
|
12
|
-
let afterSanitized = this.sanitizeBlocks(dataAfterRemovingUnWantedBlocks);
|
|
13
|
-
return JSON.stringify(afterSanitized);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
removeSectionsByTitle(data: Array<any>){
|
|
17
|
-
let dataAfterRemoving = [];
|
|
18
|
-
for(let elem of data){
|
|
19
|
-
if(elem.block_type == 'heading' && this.titles_to_remove.includes(elem.content)){
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
if(elem.children){
|
|
23
|
-
elem.children = this.removeSectionsByTitle(elem.children)
|
|
24
|
-
}
|
|
25
|
-
dataAfterRemoving.push(elem)
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
return dataAfterRemoving;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
sanitizeWikiContent(content: String) {
|
|
33
|
-
// Remove newline characters
|
|
34
|
-
content = content.replace(/\\n/g, ' ');
|
|
35
|
-
|
|
36
|
-
// Remove internal link references, keeping only the link text
|
|
37
|
-
// Pattern explanation: [[link|text|index|wiki]] --> text
|
|
38
|
-
content = content.replace(/\[\[.*?\|(.*?)\|.*?\|wiki\]\]/g, '$1');
|
|
39
|
-
|
|
40
|
-
// Remove external links, keeping only the link text
|
|
41
|
-
// Pattern explanation: [url text] --> text
|
|
42
|
-
content = content.replace(/\[http[s]?:\/\/[^\s]+ ([^\]]+)\]/g, '$1');
|
|
43
|
-
|
|
44
|
-
// Remove Markdown link references, keeping only the link text
|
|
45
|
-
// Pattern explanation:  --> link text
|
|
46
|
-
content = content.replace(/\!\[([^\]]+)\]\([^\)]+\)/g, '$1');
|
|
47
|
-
|
|
48
|
-
return content;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
sanitizeBlocks(blocks: Array<any>) {
|
|
52
|
-
let sanitizedBlocks = <any>[] ;
|
|
53
|
-
blocks.forEach(block => {
|
|
54
|
-
let sanitizedBlock: any = {};
|
|
55
|
-
for (let key in block) {
|
|
56
|
-
let value = block[key];
|
|
57
|
-
if (typeof value === 'string') {
|
|
58
|
-
sanitizedBlock[key] = this.sanitizeWikiContent(value);
|
|
59
|
-
} else if (Array.isArray(value)) {
|
|
60
|
-
sanitizedBlock[key] = this.sanitizeBlocks(value);
|
|
61
|
-
} else {
|
|
62
|
-
sanitizedBlock[key] = value;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
sanitizedBlocks.push(sanitizedBlock);
|
|
66
|
-
});
|
|
67
|
-
return sanitizedBlocks;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { openAIRequest } from "../../service/open_ai_request";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export class OpenAiService{
|
|
5
|
-
public api_key: String;
|
|
6
|
-
public model: String;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
constructor(apiKey: String, model:String){
|
|
10
|
-
this.api_key = apiKey;
|
|
11
|
-
this.model =model
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async sendRequest(prompt: String,content: String){
|
|
15
|
-
return await openAIRequest(content,prompt,this.api_key,this.model);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|