only_ever_generator 0.2.6 → 0.2.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.js +47 -44
- package/dist/card_gen/generate_cards.js +47 -32
- package/dist/constants/prompt_data.js +273 -273
- package/dist/constants/prompts/card_gen_prompt.js +180 -180
- package/dist/constants/prompts/typology_prompt.js +93 -93
- package/dist/logger.js +41 -0
- package/dist/parse/parse_source_content.js +23 -9
- package/dist/typology_gen/generate_typology.js +21 -12
- package/dist/utils/generate_args.js +1 -11
- package/package.json +33 -33
- package/readme.md +23 -23
- package/src/bootstrap/app.ts +150 -141
- package/src/card_gen/generate_cards.ts +251 -239
- package/src/config.ts +6 -6
- package/src/constants/api_constants.ts +2 -2
- package/src/constants/prompt_data.ts +296 -296
- package/src/constants/prompts/card_gen_prompt.ts +372 -372
- package/src/constants/prompts/typology_prompt.ts +202 -202
- package/src/constants/source_data.ts +47 -47
- package/src/gap_fill/calculate_gap_fill.ts +52 -52
- package/src/index.ts +61 -61
- package/src/logger.ts +31 -0
- package/src/parse/parse_card_response.ts +289 -289
- package/src/parse/parse_source_content.ts +94 -81
- package/src/parse/response_format_card.ts +210 -210
- package/src/parse/response_format_typology.ts +43 -43
- package/src/services/open_ai_service.ts +55 -54
- package/src/typology_gen/generate_typology.ts +71 -64
- package/src/utils/generate_args.ts +28 -37
- package/src/utils/parse_openai_response.ts +20 -20
- package/tsconfig.json +12 -12
- package/dist/class/parse/parse_source_content.js +0 -62
- package/dist/class/services/open_ai_service.js +0 -25
- package/dist/parse_response/parse_card_response.js +0 -288
- package/dist/parse_response/response_format_card.js +0 -210
- package/dist/parse_response/response_format_typology.js +0 -47
- package/dist/service/open_ai_request.js +0 -57
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
const typologyResponse = {
|
|
2
|
-
"usage_data": {
|
|
3
|
-
"prompt_tokens": 11611,
|
|
4
|
-
"completion_tokens": 441,
|
|
5
|
-
"total_tokens": 12052
|
|
6
|
-
},
|
|
7
|
-
"generated_content": {
|
|
8
|
-
"field": [
|
|
9
|
-
"Sciences",
|
|
10
|
-
"Technology & Engineering",
|
|
11
|
-
"Education, Learning & Personal Development"
|
|
12
|
-
],
|
|
13
|
-
"concepts": [
|
|
14
|
-
"Electrolysis",
|
|
15
|
-
"Faraday's Laws of Electrolysis",
|
|
16
|
-
"Electrolytic Cell",
|
|
17
|
-
"Decomposition Potential",
|
|
18
|
-
"Oxidation and Reduction at the Electrodes",
|
|
19
|
-
"Electrolysis of Water",
|
|
20
|
-
"Electrolysis of Carbon Dioxide",
|
|
21
|
-
"Electrocrystallization"
|
|
22
|
-
],
|
|
23
|
-
"facts": [
|
|
24
|
-
"Electrolysis is the passing of a direct electric current through an electrolyte producing chemical reactions at the electrodes and decomposition of the materials.",
|
|
25
|
-
"In electrolysis, the quantity of the products is proportional to the current, and when two or more electrolytic cells are connected in series to the same power source, the products produced in the cells are proportional to their equivalent weight.",
|
|
26
|
-
"The main components required to achieve electrolysis are an electrolyte, electrodes, and an external power source.",
|
|
27
|
-
"Faraday's laws of electrolysis detail the amount of the products of electrolysis is related to the number of electrons in the reaction at the electrodes.",
|
|
28
|
-
"Decomposition potential or decomposition voltage refers to the minimum voltage between anode and cathode of an electrolytic cell that is needed for electrolysis to occur.",
|
|
29
|
-
"The electrochemical reduction of carbon dioxide can produce value-added chemicals such as methane, ethylene, and ethanol."
|
|
30
|
-
],
|
|
31
|
-
"generate_cards": true,
|
|
32
|
-
"summary_cards": [
|
|
33
|
-
"Electrolysis is the process of passing direct electric current through an electrolyte, resulting in chemical reactions and the decomposition of materials.",
|
|
34
|
-
"Faraday's laws of electrolysis determine the relationship between the amounts of products generated and the electrons involved in the reaction at the electrodes.",
|
|
35
|
-
"Decomposition potential is the minimum voltage required between anode and cathode for electrolysis to occur.",
|
|
36
|
-
"The electrochemical reduction of carbon dioxide is a potential method for producing valuable chemicals such as methane, ethylene, and ethanol."
|
|
37
|
-
]
|
|
38
|
-
},
|
|
39
|
-
"generated_at": "Tue, 20 Jan 1970 21:17:46 GMT"
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export function returnTypologyData(){
|
|
43
|
-
return typologyResponse as any;
|
|
1
|
+
const typologyResponse = {
|
|
2
|
+
"usage_data": {
|
|
3
|
+
"prompt_tokens": 11611,
|
|
4
|
+
"completion_tokens": 441,
|
|
5
|
+
"total_tokens": 12052
|
|
6
|
+
},
|
|
7
|
+
"generated_content": {
|
|
8
|
+
"field": [
|
|
9
|
+
"Sciences",
|
|
10
|
+
"Technology & Engineering",
|
|
11
|
+
"Education, Learning & Personal Development"
|
|
12
|
+
],
|
|
13
|
+
"concepts": [
|
|
14
|
+
"Electrolysis",
|
|
15
|
+
"Faraday's Laws of Electrolysis",
|
|
16
|
+
"Electrolytic Cell",
|
|
17
|
+
"Decomposition Potential",
|
|
18
|
+
"Oxidation and Reduction at the Electrodes",
|
|
19
|
+
"Electrolysis of Water",
|
|
20
|
+
"Electrolysis of Carbon Dioxide",
|
|
21
|
+
"Electrocrystallization"
|
|
22
|
+
],
|
|
23
|
+
"facts": [
|
|
24
|
+
"Electrolysis is the passing of a direct electric current through an electrolyte producing chemical reactions at the electrodes and decomposition of the materials.",
|
|
25
|
+
"In electrolysis, the quantity of the products is proportional to the current, and when two or more electrolytic cells are connected in series to the same power source, the products produced in the cells are proportional to their equivalent weight.",
|
|
26
|
+
"The main components required to achieve electrolysis are an electrolyte, electrodes, and an external power source.",
|
|
27
|
+
"Faraday's laws of electrolysis detail the amount of the products of electrolysis is related to the number of electrons in the reaction at the electrodes.",
|
|
28
|
+
"Decomposition potential or decomposition voltage refers to the minimum voltage between anode and cathode of an electrolytic cell that is needed for electrolysis to occur.",
|
|
29
|
+
"The electrochemical reduction of carbon dioxide can produce value-added chemicals such as methane, ethylene, and ethanol."
|
|
30
|
+
],
|
|
31
|
+
"generate_cards": true,
|
|
32
|
+
"summary_cards": [
|
|
33
|
+
"Electrolysis is the process of passing direct electric current through an electrolyte, resulting in chemical reactions and the decomposition of materials.",
|
|
34
|
+
"Faraday's laws of electrolysis determine the relationship between the amounts of products generated and the electrons involved in the reaction at the electrodes.",
|
|
35
|
+
"Decomposition potential is the minimum voltage required between anode and cathode for electrolysis to occur.",
|
|
36
|
+
"The electrochemical reduction of carbon dioxide is a potential method for producing valuable chemicals such as methane, ethylene, and ethanol."
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"generated_at": "Tue, 20 Jan 1970 21:17:46 GMT"
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function returnTypologyData(){
|
|
43
|
+
return typologyResponse as any;
|
|
44
44
|
}
|
|
@@ -1,54 +1,55 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import { parseOpenAiFailureResponse, parseOpenAiSuccessResponse } from "../utils/parse_openai_response";
|
|
3
|
-
import { openAiEndPoint } from "../constants/api_constants";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
public
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { parseOpenAiFailureResponse, parseOpenAiSuccessResponse } from "../utils/parse_openai_response";
|
|
3
|
+
import { openAiEndPoint } from "../constants/api_constants";
|
|
4
|
+
import { ErrorLogger } from "../logger";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class OpenAiService{
|
|
8
|
+
public api_key: string;
|
|
9
|
+
public model: string;
|
|
10
|
+
|
|
11
|
+
constructor(apiKey: string, model:string){
|
|
12
|
+
this.api_key = apiKey;
|
|
13
|
+
this.model =model
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async sendRequest(prompt: string,content: string){
|
|
17
|
+
try{
|
|
18
|
+
let message = [
|
|
19
|
+
{
|
|
20
|
+
"role": "system", "content": prompt
|
|
21
|
+
},{
|
|
22
|
+
"role": "user", "content": content
|
|
23
|
+
}
|
|
24
|
+
];
|
|
25
|
+
const url = openAiEndPoint();
|
|
26
|
+
let response = await axios.post(
|
|
27
|
+
url,
|
|
28
|
+
{
|
|
29
|
+
"model": this.model,
|
|
30
|
+
"messages": message,
|
|
31
|
+
"response_format": {"type": "json_object"},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
headers : {
|
|
35
|
+
Authorization : "Bearer "+ this.api_key,
|
|
36
|
+
"Content-Type" : ['application/json']
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if(response.status == 200){
|
|
44
|
+
console.log('success');
|
|
45
|
+
return parseOpenAiSuccessResponse(response.data) as any
|
|
46
|
+
|
|
47
|
+
}else{
|
|
48
|
+
console.log('failed');
|
|
49
|
+
return response.statusText as any;
|
|
50
|
+
}}catch (err:any) {
|
|
51
|
+
return parseOpenAiFailureResponse(err.response);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
@@ -1,65 +1,72 @@
|
|
|
1
|
-
import { OpenAiService } from "../services/open_ai_service";
|
|
2
|
-
import { returnTypologyData } from "../parse/response_format_typology";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
public
|
|
7
|
-
public
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
response
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return response;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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(openAiService: OpenAiService, prompt:string,content:string,expected_fields: Array<string>){
|
|
11
|
+
this.openAiService = openAiService;
|
|
12
|
+
this.prompt = prompt;
|
|
13
|
+
this.content = content;
|
|
14
|
+
this.expectedFields = expected_fields.map((elem:string)=>elem.toLowerCase());
|
|
15
|
+
}
|
|
16
|
+
async generate(){
|
|
17
|
+
try{
|
|
18
|
+
const response = await this.openAiService?.sendRequest(this.prompt,this.content);
|
|
19
|
+
response['type'] = 'typology';
|
|
20
|
+
response.metadata = {
|
|
21
|
+
"req_time": response.generated_at,
|
|
22
|
+
"req_type": response.type,
|
|
23
|
+
"req_tokens": response.usage_data?.prompt_tokens,
|
|
24
|
+
"res_tokens": response.usage_data?.completion_tokens,
|
|
25
|
+
};
|
|
26
|
+
if(response.status_code == 200){
|
|
27
|
+
return this.parseTypologyOnSuccess(response);
|
|
28
|
+
} else {
|
|
29
|
+
return response;
|
|
30
|
+
}} catch (e: any){
|
|
31
|
+
await new ErrorLogger({
|
|
32
|
+
"type": 'typology_parsing',
|
|
33
|
+
"data": e.message
|
|
34
|
+
}).log();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
parseTypologyOnSuccess(responseData : any){
|
|
40
|
+
responseData.metadata.status = "completed";
|
|
41
|
+
const generatedContent = responseData.generated_content;
|
|
42
|
+
return {
|
|
43
|
+
status_code: 200,
|
|
44
|
+
metadata: responseData.metadata,
|
|
45
|
+
field: this.parseFields(generatedContent.field),
|
|
46
|
+
concepts: generatedContent.concepts,
|
|
47
|
+
facts: generatedContent.facts,
|
|
48
|
+
generate_cards : generatedContent.generate_cards,
|
|
49
|
+
summary_cards: generatedContent.summary_cards,
|
|
50
|
+
type: responseData.type
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
parseFields(fields: Array<string>){
|
|
55
|
+
const fieldKeys = ['primary_field', 'secondary_field', 'tertiary_field'];
|
|
56
|
+
return fields.slice(0,3).map((item,index)=> (
|
|
57
|
+
{
|
|
58
|
+
[fieldKeys[index]]: item,
|
|
59
|
+
"reconcile" : !(this.expectedFields.includes(item.toLowerCase()))
|
|
60
|
+
}
|
|
61
|
+
));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async parseTypologyOnFailure(responseData:any){
|
|
65
|
+
responseData.metadata.status = 'failed';
|
|
66
|
+
return {
|
|
67
|
+
status_code: responseData.status_code,
|
|
68
|
+
metadata: responseData.metadata,
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
65
72
|
}
|
|
@@ -1,38 +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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.generate_summary
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
if(this.generate_card == true){
|
|
31
|
-
returnData.push('generate_card');
|
|
32
|
-
}
|
|
33
|
-
return returnData;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
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(generate_card: boolean,generate_typology: boolean, generate_summary: boolean, ){
|
|
7
|
+
this.generate_card = generate_card;
|
|
8
|
+
this.generate_typology = generate_typology;
|
|
9
|
+
this.generate_summary = generate_summary;
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getWhatNeedsToBeGenerated(){
|
|
14
|
+
let returnData = [];
|
|
15
|
+
if(this.generate_typology == true){
|
|
16
|
+
returnData.push('generate_tyopology')
|
|
17
|
+
}
|
|
18
|
+
if(this.generate_summary == true){
|
|
19
|
+
returnData.push('generate_summary')
|
|
20
|
+
}
|
|
21
|
+
if(this.generate_card == true){
|
|
22
|
+
returnData.push('generate_card');
|
|
23
|
+
}
|
|
24
|
+
return returnData;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
38
29
|
}
|
|
@@ -1,21 +1,21 @@
|
|
|
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 usuage = errorResponse.usuage;
|
|
15
|
-
// let statusCode =
|
|
16
|
-
return {
|
|
17
|
-
'status_code': errorResponse.status,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
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 usuage = errorResponse.usuage;
|
|
15
|
+
// let statusCode =
|
|
16
|
+
return {
|
|
17
|
+
'status_code': errorResponse.status,
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
}
|
|
21
21
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
4
|
-
"module": "commonjs" /* Specify what module code is generated. */,
|
|
5
|
-
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
6
|
-
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
7
|
-
"strict": true,
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"outDir": "./dist" /* Enable all strict type-checking options. */
|
|
10
|
-
},
|
|
11
|
-
"exclude": ["./src/*/*.d.ts"]
|
|
12
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
4
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
|
5
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
6
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"outDir": "./dist" /* Enable all strict type-checking options. */
|
|
10
|
+
},
|
|
11
|
+
"exclude": ["./src/*/*.d.ts"]
|
|
12
|
+
}
|
|
@@ -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;
|