only_ever_generator 1.0.2 → 1.0.4
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 +92 -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 +34 -0
- package/dist/embedding_generation/consolidation/global_consolidation.d.ts.map +1 -0
- package/dist/embedding_generation/consolidation/global_consolidation.js +77 -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 -140
- 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 +96 -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
package/src/index.ts
CHANGED
|
@@ -1,179 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// import express from "express";
|
|
2
|
+
// import {
|
|
3
|
+
// returnCardResponse,
|
|
4
|
+
// returnHeadings,
|
|
5
|
+
// returnSourceData,
|
|
6
|
+
// } from "./constants/source_data";
|
|
7
|
+
// import config from "./config";
|
|
6
8
|
// const app = express();
|
|
7
9
|
// const port = 3000;
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
// import { returnPromptData } from "./constants/prompt_data";
|
|
11
|
+
// import { GenerateCards } from "./card_gen/generate_cards";
|
|
12
|
+
// import { OpenAiService } from "./services/open_ai_service";
|
|
13
|
+
// import { ParseCardResponse } from "./parse/parse_card_response";
|
|
14
|
+
// import { returnTypologyPrompt } from "./constants/prompts/typology_prompt";
|
|
15
|
+
// import { returnCardGenPrompt } from "./constants/prompts/card_gen_prompt";
|
|
16
|
+
// import { GenerateArgs } from "./utils/generate_args";
|
|
17
|
+
|
|
18
|
+
import { OnlyEverGenerator } from "./bootstrap/app";
|
|
19
|
+
// import { LocalConsolidation } from "./embedding_generation/consolidation/local_consolidation";
|
|
20
|
+
// import { embeddingsResp } from "./typology-parsed-response";
|
|
21
|
+
// import { GlobalConsolidation } from "./embedding_generation/consolidation/global_consolidation";
|
|
22
|
+
// import { ParseEmbeddingResponse } from "./embedding_generation/parse_embedding_response";
|
|
23
|
+
|
|
24
|
+
/// While Publishing the package , and using this code as a separate npm module
|
|
25
|
+
/// uncomment the below line and comment all the others, expect the import of OnlyEverGenerator
|
|
26
|
+
export { OnlyEverGenerator };
|
|
27
|
+
|
|
28
|
+
// . All the Codes Below uses express and are strictly for development purpose, while publishing the package, comment everything
|
|
29
|
+
// below this line
|
|
30
|
+
// let oeGen = new OnlyEverGenerator(config.openAIKey, "gpt-4o-mini", {
|
|
31
|
+
// prompt: returnPromptData(),
|
|
32
|
+
// content: returnSourceData(),
|
|
33
|
+
// });
|
|
34
|
+
// app.get("/", async (req, res) => {
|
|
35
|
+
// // let data = oeGen.returnParsedContent();
|
|
36
|
+
// // let parsedData = parseResponse()
|
|
37
|
+
// let cardPrompt = "";
|
|
11
38
|
|
|
12
|
-
//
|
|
13
|
-
// app.get('/', async (req, res) => {
|
|
14
|
-
// let data = oeGen.returnParsedContent();
|
|
15
|
-
// // let parsedData = parseResponse()
|
|
16
|
-
// res.send(data);
|
|
39
|
+
// res.send(cardPrompt);
|
|
17
40
|
// });
|
|
18
41
|
|
|
19
|
-
// app.get(
|
|
42
|
+
// app.get("/openAI", async (req, res) => {
|
|
20
43
|
// // let prompt = returnPromt();
|
|
21
|
-
|
|
22
|
-
// let
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
// res.send(aiRequest);
|
|
28
|
-
// });
|
|
29
|
-
|
|
30
|
-
// app.get('/typology', async(req,res)=>{
|
|
31
|
-
// {
|
|
32
|
-
// let typologyPrompt = returnTypologyPrompt();
|
|
33
|
-
// let cardPrompt = returnCardGenPrompt();
|
|
34
|
-
// let args = new GenerateArgs(
|
|
35
|
-
// true,
|
|
36
|
-
// true,
|
|
37
|
-
// false,
|
|
38
|
-
// {
|
|
39
|
-
// typology_prompt: typologyPrompt,
|
|
40
|
-
// card_gen_prompt: cardPrompt,
|
|
41
|
-
// summary_prompt: "",
|
|
42
|
-
// }
|
|
43
|
-
// )
|
|
44
|
-
// let typologyRequest = await oeGen.generate(
|
|
45
|
-
// args,
|
|
46
|
-
// );
|
|
47
|
-
// res.send(typologyRequest);
|
|
48
|
-
|
|
49
|
-
// }
|
|
44
|
+
// // let prompt = returnCardGenPrompt();
|
|
45
|
+
// // let content = returnSourceData().toString()
|
|
46
|
+
// // let headings = returnHeadings();
|
|
47
|
+
// // // let aiRequest = await openAIRequest(prompt,content);
|
|
48
|
+
// let aiRequest = await oeGen.generate(true, true);
|
|
49
|
+
// res.send(aiRequest);
|
|
50
50
|
// });
|
|
51
51
|
|
|
52
|
-
// app.
|
|
53
|
-
//
|
|
52
|
+
// app.get("/parseCardData", async (req, res) => {
|
|
53
|
+
// let cardResp = returnCardResponse();
|
|
54
|
+
// let headings = returnHeadings();
|
|
55
|
+
|
|
56
|
+
// cardResp.metadata = {
|
|
57
|
+
// req_time: cardResp.generated_at ?? new Date(),
|
|
58
|
+
// req_type: "card",
|
|
59
|
+
// req_tokens: cardResp.usage_data?.prompt_tokens,
|
|
60
|
+
// res_tokens: cardResp.usage_data?.completion_tokens,
|
|
61
|
+
// model: "40-mini",
|
|
62
|
+
// };
|
|
63
|
+
// let parsedData = new ParseCardResponse().parse(cardResp, false, {});
|
|
64
|
+
// res.send(parsedData);
|
|
54
65
|
// });
|
|
55
66
|
|
|
67
|
+
// app.get("/generate-cards", async (req, res) => {
|
|
68
|
+
// let cardResp = embeddingsResp;
|
|
69
|
+
// oeGen.parsedContent.taxonomy = cardResp;
|
|
70
|
+
// let parsedData = await oeGen.generateCard(
|
|
71
|
+
// returnCardGenPrompt(),
|
|
72
|
+
// JSON.stringify(cardResp),
|
|
73
|
+
// false
|
|
74
|
+
// );
|
|
75
|
+
// res.send(parsedData);
|
|
76
|
+
// });
|
|
56
77
|
|
|
78
|
+
// app.get("/typology", async (req, res) => {
|
|
79
|
+
// {
|
|
80
|
+
// let typologyRequest = await oeGen.generate(true, false);
|
|
81
|
+
// res.send(typologyRequest);
|
|
82
|
+
// }
|
|
83
|
+
// });
|
|
57
84
|
|
|
85
|
+
// app.get("/local-consolidation", async (req, res) => {
|
|
86
|
+
// const concepts_facts = embeddingsResp.concepts_facts;
|
|
58
87
|
|
|
88
|
+
// let locallyConsolidated = new LocalConsolidation().consolidate(
|
|
89
|
+
// concepts_facts,
|
|
90
|
+
// "sourceID"
|
|
91
|
+
// );
|
|
92
|
+
// res.send(locallyConsolidated);
|
|
93
|
+
// });
|
|
59
94
|
|
|
95
|
+
// app.get("/global-consolidation", async (req, res) => {
|
|
96
|
+
// const concepts_facts = embeddingsResp.concepts_facts;
|
|
97
|
+
|
|
98
|
+
// // let globallyConsolidated = await new GlobalConsolidation().consolidate(
|
|
99
|
+
// // concepts_facts,
|
|
100
|
+
// // "sourceID",
|
|
101
|
+
// // 0.2
|
|
102
|
+
// // );
|
|
103
|
+
// // let globallyConsolidated = await oeGen.globalConsolidation(
|
|
104
|
+
// // concepts_facts,
|
|
105
|
+
// // "sourceID",
|
|
106
|
+
// // 0.2
|
|
107
|
+
// // );
|
|
108
|
+
// // res.send(globallyConsolidated);
|
|
109
|
+
// });
|
|
60
110
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export class OnlyEverGenerator{
|
|
65
|
-
public api_key: string = '';
|
|
66
|
-
public openAiService: OpenAiService | undefined;
|
|
67
|
-
parsedContent: String = '';
|
|
68
|
-
constructor(apiKey:string, model: String, content: Array<any>){
|
|
69
|
-
this.api_key = apiKey;
|
|
70
|
-
this.openAiService = new OpenAiService(apiKey,model ?? 'gpt-3.5-turbo-1106');
|
|
71
|
-
this.parsedContent = new ParseSourceContent(content).parse();
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
typologyResponse = {};
|
|
75
|
-
cardgenResponse = {};
|
|
76
|
-
summarizeResponse = {};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
async generate(
|
|
82
|
-
generate_card : boolean = false,
|
|
83
|
-
generate_typology: boolean =false,
|
|
84
|
-
): Promise<Array<any>> {
|
|
85
|
-
let typologyPrompt = returnTypologyPrompt();
|
|
86
|
-
let cardPrompt = returnCardGenPrompt();
|
|
87
|
-
let args = new GenerateArgs(
|
|
88
|
-
generate_card,
|
|
89
|
-
generate_typology,
|
|
90
|
-
false,
|
|
91
|
-
{
|
|
92
|
-
typology_prompt: typologyPrompt,
|
|
93
|
-
card_gen_prompt: cardPrompt,
|
|
94
|
-
summary_prompt: ''
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
);
|
|
98
|
-
const responseToReturn = [];
|
|
99
|
-
const whatNeedsToBeGenerated = args.getWhatNeedsToBeGenerated();
|
|
100
|
-
for(let elem of whatNeedsToBeGenerated)
|
|
101
|
-
if(elem == 'generate_tyopology'){
|
|
102
|
-
this.typologyResponse = await this.generateTypology(args.prompts.typology_prompt ?? '', this.parsedContent);
|
|
103
|
-
responseToReturn.push(this.typologyResponse);
|
|
104
|
-
}else if(elem == 'generate_card'){
|
|
105
|
-
|
|
106
|
-
this.cardgenResponse = await this.generateCard(args.prompts.card_gen_prompt ?? '', this.parsedContent + JSON.stringify(this.typologyResponse));
|
|
107
|
-
responseToReturn.push(this.cardgenResponse);
|
|
108
|
-
}else if(elem == 'generate_summary'){
|
|
109
|
-
this.summarizeResponse = await this.generateSummary(args.prompts.summary_prompt ?? '', this.parsedContent);
|
|
110
|
-
responseToReturn.push(this.summarizeResponse);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return responseToReturn;
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
_returnParsedContent(){
|
|
118
|
-
return this.parsedContent;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
async generateCard(prompt: String, content: String){
|
|
123
|
-
let response = await this.openAiService?.sendRequest(prompt,this.parsedContent);
|
|
124
|
-
response['type'] = 'card_gen';
|
|
125
|
-
return response;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async generateTypology(prompt:String,content:String){
|
|
129
|
-
let response = await this.openAiService?.sendRequest(prompt,this.parsedContent);
|
|
130
|
-
response['type'] = 'typology';
|
|
131
|
-
return response;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async generateSummary(prompt:String,content:String){
|
|
135
|
-
let response = await this.openAiService?.sendRequest(prompt,this.parsedContent);
|
|
136
|
-
response['type']= 'summary';
|
|
137
|
-
return response;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export class GenerateArgs{
|
|
143
|
-
public generate_card : boolean = false;
|
|
144
|
-
public generate_typology: boolean = false;
|
|
145
|
-
public generate_summary: boolean = false;
|
|
146
|
-
public prompts = {
|
|
147
|
-
typology_prompt: '',
|
|
148
|
-
card_gen_prompt: '',
|
|
149
|
-
summary_prompt: ''
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
constructor(generate_card: boolean,generate_typology: boolean, generate_summary: boolean, prompts = {
|
|
153
|
-
typology_prompt: '',
|
|
154
|
-
card_gen_prompt: '',
|
|
155
|
-
summary_prompt: ''
|
|
156
|
-
}){
|
|
157
|
-
this.generate_card = generate_card;
|
|
158
|
-
this.generate_typology = generate_typology;
|
|
159
|
-
this.generate_summary = generate_summary;
|
|
160
|
-
this.prompts = prompts
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
getWhatNeedsToBeGenerated(){
|
|
164
|
-
let returnData = [];
|
|
165
|
-
if(this.generate_typology == true){
|
|
166
|
-
returnData.push('generate_tyopology')
|
|
167
|
-
}
|
|
168
|
-
if(this.generate_summary == true){
|
|
169
|
-
returnData.push('generate_summary')
|
|
170
|
-
}
|
|
171
|
-
if(this.generate_card == true){
|
|
172
|
-
returnData.push('generate_card');
|
|
173
|
-
}
|
|
174
|
-
return returnData;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
}
|
|
111
|
+
// app.listen(port, () => {
|
|
112
|
+
// console.log(`Example app listening at http://localhost:${port}`);
|
|
113
|
+
// });
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// this method will call out atlas function and will write to a doc, incase of any errors:
|
|
2
|
+
/// this is only for developmemt use
|
|
3
|
+
|
|
4
|
+
import axios from "axios";
|
|
5
|
+
|
|
6
|
+
export class ErrorLogger {
|
|
7
|
+
data: any;
|
|
8
|
+
constructor(data: any) {
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async log() {
|
|
13
|
+
try {
|
|
14
|
+
let response = await axios.post(
|
|
15
|
+
"https://us-east-1.aws.data.mongodb-api.com/app/oe-phase1-tkmsy/endpoint/oe_gen_logger",
|
|
16
|
+
{
|
|
17
|
+
data: this.data,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
headers: {
|
|
21
|
+
"Content-Type": ["application/json"],
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
return response;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.log(e);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
export class ParseClozeCard {
|
|
2
|
+
parse(data: any) {
|
|
3
|
+
try {
|
|
4
|
+
const content = data.card_content;
|
|
5
|
+
let correctOptions = content.correct_options;
|
|
6
|
+
let incorrectOptions = content.incorrect_options;
|
|
7
|
+
let allOptions = [...correctOptions, ...incorrectOptions];
|
|
8
|
+
let displayTitle = this._generateClozeCardDisplayTitle(
|
|
9
|
+
data.card_content.prompt,
|
|
10
|
+
allOptions
|
|
11
|
+
);
|
|
12
|
+
let preparedData = this._prepareQuestionAndCorrectAnswers(
|
|
13
|
+
content.prompt,
|
|
14
|
+
correctOptions
|
|
15
|
+
);
|
|
16
|
+
let finalQuestion = preparedData.prompt;
|
|
17
|
+
let parsedCorrectOptions = preparedData.options;
|
|
18
|
+
let parsedIncorrectoptions = incorrectOptions.map((e: any) => {
|
|
19
|
+
return {
|
|
20
|
+
option: e,
|
|
21
|
+
cloze: "null",
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
const finalParsedOptions = [
|
|
25
|
+
...parsedCorrectOptions,
|
|
26
|
+
...parsedIncorrectoptions,
|
|
27
|
+
];
|
|
28
|
+
let clozeCardData = {
|
|
29
|
+
type: {
|
|
30
|
+
category: "learning",
|
|
31
|
+
sub_type: data.type,
|
|
32
|
+
},
|
|
33
|
+
heading: "",
|
|
34
|
+
displayTitle: displayTitle,
|
|
35
|
+
content: {
|
|
36
|
+
question: finalQuestion,
|
|
37
|
+
options: finalParsedOptions,
|
|
38
|
+
},
|
|
39
|
+
concepts: data.concepts,
|
|
40
|
+
facts: data.facts,
|
|
41
|
+
explanation: data.card_content.explanation,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return this._validateCloze(clozeCardData);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_generateClozeCardDisplayTitle(question: string, answers: Array<any>) {
|
|
51
|
+
try {
|
|
52
|
+
let optionsString = "";
|
|
53
|
+
if (answers.length !== 0) {
|
|
54
|
+
optionsString = answers.join(", ");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return `${question} ---- ${optionsString}`;
|
|
58
|
+
} catch (e) {
|
|
59
|
+
throw Error("Error in generating display title");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// validate the cloze card
|
|
64
|
+
// 1. Has Empty cloze
|
|
65
|
+
// 2. has Duplicate Clozes
|
|
66
|
+
// 3. doesnt have any valid option,
|
|
67
|
+
// 4. Question length 320
|
|
68
|
+
// 5. More than 6 options
|
|
69
|
+
// 6. Less than 2 options
|
|
70
|
+
// 7. Max character for individual cloze: 90
|
|
71
|
+
|
|
72
|
+
_prepareQuestionAndCorrectAnswers(
|
|
73
|
+
rawPrompt: String,
|
|
74
|
+
correctOptions: Array<any>
|
|
75
|
+
) {
|
|
76
|
+
try {
|
|
77
|
+
var finalCorrectOptions = <any>[];
|
|
78
|
+
const regex = /{{(.*?)}}/g;
|
|
79
|
+
|
|
80
|
+
const transformed = rawPrompt.replace(regex, (match, p1) => {
|
|
81
|
+
// p1 is the captured group inside {{ }} (e.g., "fruit", "green")
|
|
82
|
+
const idx = correctOptions.indexOf(p1);
|
|
83
|
+
if (idx !== -1) {
|
|
84
|
+
let cloze = `c${idx}`;
|
|
85
|
+
finalCorrectOptions.push({
|
|
86
|
+
option: p1,
|
|
87
|
+
cloze: cloze,
|
|
88
|
+
});
|
|
89
|
+
return `{{c${idx}:${p1}}}`;
|
|
90
|
+
}
|
|
91
|
+
return match; // If not found in correct_options, leave as is or handle accordingly
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
prompt: transformed,
|
|
95
|
+
options: finalCorrectOptions,
|
|
96
|
+
};
|
|
97
|
+
} catch (e) {
|
|
98
|
+
throw Error("Error in preparing question and correct answers");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_validateCloze(clozeCard: any) {
|
|
103
|
+
let clozeRegex = /\{\{c(\d+):([^}]+)\}\}/g;
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
/// validate emptu cloze
|
|
107
|
+
let options = clozeCard.content.options ?? [];
|
|
108
|
+
let question = clozeCard.content.question;
|
|
109
|
+
if (options.length < 2 || options.length > 6) {
|
|
110
|
+
throw Error("Number of cloze options are invalid");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// There are no correct clozes// or null cloze or empty cloze
|
|
114
|
+
let correctOptions = options.find(
|
|
115
|
+
(e: any) => e.cloze != "null" && e.cloze != null && e.cloze.trim() != ""
|
|
116
|
+
);
|
|
117
|
+
if (correctOptions) {
|
|
118
|
+
} else {
|
|
119
|
+
throw Error(" No valid clozes exists");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let rightClozes = options.filter((e: any) => e.cloze.startsWith("c"));
|
|
123
|
+
|
|
124
|
+
/// matches our cloze syntax
|
|
125
|
+
let clozeMatches = [...question.matchAll(clozeRegex)];
|
|
126
|
+
if (clozeMatches.length == 0) {
|
|
127
|
+
throw Error("Question Invalid");
|
|
128
|
+
} else if (clozeMatches.length != rightClozes.length) {
|
|
129
|
+
throw Error(" Clozes in question doesnt match to clozes in options");
|
|
130
|
+
}
|
|
131
|
+
return clozeCard;
|
|
132
|
+
} catch (e: any) {
|
|
133
|
+
throw Error(`Error in validating cloze card ${e.message}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class ParseFlashCard {
|
|
2
|
+
parse(data: any) {
|
|
3
|
+
try {
|
|
4
|
+
let displayTitle = this.generateFlashCardDisplayTitle(
|
|
5
|
+
data.card_content.front,
|
|
6
|
+
data.card_content.back
|
|
7
|
+
);
|
|
8
|
+
let flashCardData = {
|
|
9
|
+
type: {
|
|
10
|
+
category: "learning",
|
|
11
|
+
sub_type: data.type,
|
|
12
|
+
},
|
|
13
|
+
heading: "",
|
|
14
|
+
displayTitle: displayTitle,
|
|
15
|
+
content: {
|
|
16
|
+
front_content: data.card_content.front,
|
|
17
|
+
back_content: data.card_content.back,
|
|
18
|
+
},
|
|
19
|
+
concepts: data.concepts,
|
|
20
|
+
explanation: data.card_content.explanation,
|
|
21
|
+
facts: data.facts,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return flashCardData;
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
generateFlashCardDisplayTitle(front: string, back: string) {
|
|
31
|
+
return `${front} ---- ${back}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { match } from "assert";
|
|
2
|
+
|
|
3
|
+
type InputItem = {
|
|
4
|
+
left_item: string;
|
|
5
|
+
right_item: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type OutputItem = {
|
|
9
|
+
left_item: string;
|
|
10
|
+
right_item: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class ParseMatchCard {
|
|
14
|
+
parse(cardData: any) {
|
|
15
|
+
try {
|
|
16
|
+
let content = cardData.card_content;
|
|
17
|
+
const finalContent = this._parseMatchContent(content);
|
|
18
|
+
|
|
19
|
+
let displayTitle = this._generateMatchCardDisplayTitle(content);
|
|
20
|
+
let matchCard = {
|
|
21
|
+
type: {
|
|
22
|
+
category: "learning",
|
|
23
|
+
sub_type: cardData.type,
|
|
24
|
+
},
|
|
25
|
+
heading: "",
|
|
26
|
+
content: finalContent,
|
|
27
|
+
// content: cardData.card_content,
|
|
28
|
+
displayTitle: displayTitle,
|
|
29
|
+
concepts: cardData.concepts,
|
|
30
|
+
facts: cardData.facts,
|
|
31
|
+
explanation: cardData.card_content.explanation,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return this._validateMatch(matchCard);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_generateMatchCardDisplayTitle(answers: any) {
|
|
41
|
+
let titles: string[] = [];
|
|
42
|
+
let counter = 65;
|
|
43
|
+
for (let data of answers) {
|
|
44
|
+
let value = data.right_item;
|
|
45
|
+
let leftData = data.left_item;
|
|
46
|
+
let letter = String.fromCharCode(counter);
|
|
47
|
+
titles.push(`${letter}. ${leftData} -- ${value}`);
|
|
48
|
+
counter++;
|
|
49
|
+
}
|
|
50
|
+
let displayTitle = titles.join(",");
|
|
51
|
+
return displayTitle;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_parseMatchContent = (input: InputItem[]): OutputItem[] => {
|
|
55
|
+
const grouped = input.reduce<Record<string, OutputItem>>(
|
|
56
|
+
(acc, { left_item, right_item }) => {
|
|
57
|
+
if (!acc[left_item]) {
|
|
58
|
+
acc[left_item] = { left_item, right_item: [] };
|
|
59
|
+
}
|
|
60
|
+
acc[left_item].right_item.push(right_item);
|
|
61
|
+
return acc;
|
|
62
|
+
},
|
|
63
|
+
{}
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return Object.values(grouped);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
_validateMatch(matchCard: any) {
|
|
70
|
+
let matches = matchCard.content;
|
|
71
|
+
let content = [];
|
|
72
|
+
try {
|
|
73
|
+
if (matches.length < 1 || matches.length > 8) {
|
|
74
|
+
throw Error("Invalid number of matches");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (let elem of matches) {
|
|
78
|
+
if (elem.left_item.length <= 30 && elem.left_item.length != 0) {
|
|
79
|
+
if (elem.right_item.length <= 40 && elem.right_item.length != 0) {
|
|
80
|
+
content.push(elem);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (content.length >= 2) {
|
|
85
|
+
matchCard.content = content;
|
|
86
|
+
} else {
|
|
87
|
+
throw Error("Invalid content");
|
|
88
|
+
}
|
|
89
|
+
return matchCard;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export class ParseMcqCard {
|
|
2
|
+
parse(data: any) {
|
|
3
|
+
try {
|
|
4
|
+
let mcqAnswers = [];
|
|
5
|
+
if (
|
|
6
|
+
data.card_content.choices !== undefined &&
|
|
7
|
+
data.card_content.choices.length != 0
|
|
8
|
+
) {
|
|
9
|
+
for (let choice of data.card_content.choices) {
|
|
10
|
+
let answer = {
|
|
11
|
+
answer: choice.choice,
|
|
12
|
+
is_correct: choice.is_correct,
|
|
13
|
+
};
|
|
14
|
+
mcqAnswers.push(answer);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let displayTitle = this._generateMcqCardDisplayTitle(
|
|
19
|
+
data.card_content.prompt,
|
|
20
|
+
mcqAnswers
|
|
21
|
+
);
|
|
22
|
+
let mcqCard = {
|
|
23
|
+
type: {
|
|
24
|
+
category: "learning",
|
|
25
|
+
sub_type: data.type,
|
|
26
|
+
},
|
|
27
|
+
heading: "",
|
|
28
|
+
displayTitle: displayTitle,
|
|
29
|
+
content: {
|
|
30
|
+
question: data.card_content.prompt,
|
|
31
|
+
answers: mcqAnswers,
|
|
32
|
+
},
|
|
33
|
+
concepts: data.concepts,
|
|
34
|
+
facts: data.facts,
|
|
35
|
+
explanation: data.card_content.explanation,
|
|
36
|
+
};
|
|
37
|
+
// return mcqCard;
|
|
38
|
+
const isValid = this._validate(mcqCard);
|
|
39
|
+
if (isValid == true) {
|
|
40
|
+
return mcqCard;
|
|
41
|
+
} else {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
} catch (e) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_generateMcqCardDisplayTitle(question: string, answers: any) {
|
|
50
|
+
let answersString = [];
|
|
51
|
+
if (answers.length != 0) {
|
|
52
|
+
for (let option of answers) {
|
|
53
|
+
let currentIndex = answers.indexOf(option) + 1;
|
|
54
|
+
let temp = `${currentIndex} . ${option.answer} `;
|
|
55
|
+
answersString.push(temp);
|
|
56
|
+
}
|
|
57
|
+
let resultString = answersString.join("");
|
|
58
|
+
let finalDisplayTitle = `${question} ---- ${resultString}`;
|
|
59
|
+
return finalDisplayTitle;
|
|
60
|
+
} else {
|
|
61
|
+
return question;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// validate mcq card
|
|
66
|
+
// 1. Check if atleast 1 correct answer exists
|
|
67
|
+
// 2. Length of answer shouldnt exceed 24 chars
|
|
68
|
+
// 3. Length of question shouldnt exceed 90 chars
|
|
69
|
+
// 4. If Any option is Empty
|
|
70
|
+
_validate(mcqCard: any) {
|
|
71
|
+
try {
|
|
72
|
+
let isQuestionValid = mcqCard.content.question.length <= 90;
|
|
73
|
+
if (!isQuestionValid) {
|
|
74
|
+
throw new Error("Question length exceeded");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// check if all are wrong answers
|
|
78
|
+
let exists = this._checkIfAllAnswersAreWrong(mcqCard.content.answers);
|
|
79
|
+
if (exists) {
|
|
80
|
+
} else {
|
|
81
|
+
throw new Error("Every answers are wrong");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// check if answers are of length <40 or is 0
|
|
85
|
+
let answerWhoseLengthisGreaterThan40or0 = (
|
|
86
|
+
mcqCard.content.answers ?? []
|
|
87
|
+
).find((e: any) => e.answer.length == 0 || e.answer.length > 40);
|
|
88
|
+
if (answerWhoseLengthisGreaterThan40or0) {
|
|
89
|
+
throw new Error("Option has length more than 40 or is Empty");
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_checkIfAllAnswersAreWrong(answers: any[]) {
|
|
98
|
+
let rightAnswer = answers.find((e) => e.is_correct == true);
|
|
99
|
+
if (rightAnswer) {
|
|
100
|
+
return true;
|
|
101
|
+
} else {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|