import { SLMDataExtractor } from "../dataExtractors/SLMDataExtractor.js"; import { Utility } from "../utils/Utility.js"; import { DataInjector } from "../dataInjectors/DataInjector.js"; import { ExtractionReponse } from "../types/ExtractionResponse.js"; import { BasicLLMInferenceEngine, BasicInferenceConfig } from "@optimaxer/web-core"; export class FormEngine { private dataExtractor: SLMDataExtractor; private dataInjector: DataInjector; private llmInstance!: BasicLLMInferenceEngine; // Default configuration object private static defaultConfig: BasicInferenceConfig = { model: 'gemma', inferenceEngine: 'mediapipe', mode: 'local', devMode: true, connectorUrl: '', fallBackModel: 'openai-gpt-4o-mini', apiKey: '' }; constructor(config: Partial = {}) { const finalConfig = Object.assign({}, FormEngine.defaultConfig, config); this.llmInstance = new BasicLLMInferenceEngine(finalConfig); this.dataExtractor = new SLMDataExtractor() this.dataInjector = new DataInjector() } /** * setup * @param config - The configuration for the Form Engine setup. * @returns Response * * This function initializes the Form Engine by creating an instance of the InferenceEngine. */ static async setup(config: Partial = {}): Promise { const instance = new FormEngine(); const finalConfig = Object.assign({}, FormEngine.defaultConfig, config); // Initialize the InferenceEngine instance.llmInstance = await BasicLLMInferenceEngine.init(finalConfig); if(instance.llmInstance) { return instance; } else { return instance; } } /** * extract * @param formSchema * @param text * @returns { [key: string]: any } * * This function extracts data from the given text using the SLM model and injects the extracted data into the form schema. */ async extract(formSchema: { [key: string]: any }, text: string = ''): Promise { try { if(text.length < 1){ // Access the clipboard data text = await navigator.clipboard.readText(); console.log("Clipboard Text", text); // Ensure the clipboardText is not empty if (!text) { console.error("Clipboard is empty or access is denied."); return new ExtractionReponse(400,{}, 'Clipboard is empty or access is denied.'); } } let injectedData: { [key: string]: any } = {}; if(Utility.hasNestedObject(formSchema)){ const extracted_JSON: { [key: string]: any } = await this.dataExtractor.extractDataForComplexObject(text,formSchema, this.llmInstance); return new ExtractionReponse(200, extracted_JSON, 'Complex Extractions'); }else{ const extracted_JSON: { [key: string]: any } = await this.dataExtractor.extractData(text,formSchema, this.llmInstance); const flatten_JSON = Utility.flattenJsonObject(extracted_JSON); injectedData = this.dataInjector.injectData(flatten_JSON, formSchema); } return new ExtractionReponse(200, injectedData, 'Data Extraction Successful'); } catch (error) { console.error("Error in data extraction", error); return new ExtractionReponse(400,{}, 'Error in data extraction'); } } /** * rawExtract * This function extracts data from the given text using the SLM model. And does not require a schema. * @param text The text to extract data from. * @returns { [key: string]: any } */ async rawExtract(text: string = ''): Promise { try { if(text.length < 1){ // Access the clipboard data text = await navigator.clipboard.readText(); console.log("Clipboard Text", text); // Ensure the clipboardText is not empty if (!text) { console.error("Clipboard is empty or access is denied."); return new ExtractionReponse(400,{}, 'Clipboard is empty or access is denied.'); } } const extracted_JSON: { [key: string]: any } = await this.dataExtractor.extractData(text, {}, this.llmInstance); const flatten_JSON = Utility.flattenJsonObject(extracted_JSON); return new ExtractionReponse(200, flatten_JSON, 'Data Extraction Successful'); } catch (error) { console.error("Error in raw data extraction", error); return new ExtractionReponse(400,{}, 'Error in raw data extraction'); } } }