From 7300f1a3c7779e7e2c795fdcd8895428c6f84b78 Mon Sep 17 00:00:00 2001 From: AmruthVamshi Date: Mon, 18 Nov 2024 16:42:59 +0530 Subject: [PATCH] Update language detection API and refactored logs --- src/app.controller.ts | 38 ++++--- src/modules/aiTools/ai-tools.service.ts | 128 ++++++++++++++---------- src/xstate/prompt/prompt.service.ts | 2 +- 3 files changed, 96 insertions(+), 72 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 3db8ffc..dd1b6c0 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -194,7 +194,7 @@ export class AppController { configid ) - console.log("fetched conversation: ", conversation) + // console.log("fetched conversation: ", conversation) //handle text and audio if(promptDto.text){ type = "Text" @@ -202,9 +202,9 @@ export class AppController { if(/^\d+$/.test(userInput)){ prompt.inputLanguage = Language.en } else { - console.log("IN ELSE....") + // console.log("IN ELSE....") try { - let response = await this.aiToolsService.detectLanguage(userInput) + let response = await this.aiToolsService.detectLanguage(userInput, userId, sessionId) prompt.inputLanguage = response["language"] as Language } catch (error) { await this.telemetryService.capture({ @@ -241,14 +241,14 @@ export class AppController { tags: ['bot','detect_language','error'] }) } - console.log("LANGUAGE DETECTED...") + // console.log("LANGUAGE DETECTED...") //@ts-ignore if(prompt.inputLanguage == 'unk'){ prompt.inputLanguage = prompt.input.inputLanguage as Language } // verboseLogger("Detected Language =", prompt.inputLanguage) } - console.log("TELEMETRYYYYY") + // console.log("TELEMETRYYYYY") await this.telemetryService.capture({ eventName: "Detect language", eventType: "DETECT_LANGUAGE", @@ -286,9 +286,9 @@ export class AppController { prompt.inputLanguage = promptDto.inputLanguage as Language let response; if(['askingAadhaarNumber','askingOTP','askLastAaadhaarDigits','confirmInput2','confirmInput3','confirmInput4'].indexOf(conversation?.currentState) != -1) - response = await this.aiToolsService.speechToText(promptDto.media.text,Language.en) + response = await this.aiToolsService.speechToText(promptDto.media.text,Language.en,userId,sessionId) else - response = await this.aiToolsService.speechToText(promptDto.media.text,prompt.inputLanguage) + response = await this.aiToolsService.speechToText(promptDto.media.text,prompt.inputLanguage,userId,sessionId) if(response.error) { await this.telemetryService.capture({ @@ -379,7 +379,7 @@ export class AppController { } conversation.inputType = type; - console.log("CP 1...") + // console.log("CP 1...") //get flow let botFlowMachine; switch(configid){ @@ -415,7 +415,7 @@ export class AppController { } }) }else { - console.log("creating a new message in Message table...") + // console.log("creating a new message in Message table...") await this.prismaService.message.create({ data:{ text: type=="Text"?promptDto.text:null, @@ -437,7 +437,7 @@ export class AppController { textInEnglish: "", error: null } - res['audio'] = await this.aiToolsService.textToSpeech(res.text,prompt.inputLanguage) + res['audio'] = await this.aiToolsService.textToSpeech(res.text,prompt.inputLanguage,userId,sessionId) if(res['audio']['error']){ await this.telemetryService.capture({ eventName: "Text to speech error", @@ -510,7 +510,7 @@ export class AppController { return res } else { //translate to english - console.log("Translating to English...") + // console.log("Translating to English...") let translateStartTime = Date.now(); if(userInput == 'resend OTP'){ this.monitoringService.incrementResentOTPCount() @@ -520,7 +520,9 @@ export class AppController { let response = await this.aiToolsService.translate( prompt.inputLanguage as Language, Language.en, - userInput + userInput, + userId, + sessionId ) if(!response['text']) { await this.telemetryService.capture({ @@ -746,7 +748,9 @@ export class AppController { let response = await this.aiToolsService.translate( Language.en, prompt.inputLanguage as Language, - result.text + result.text, + userId, + sessionId ) if(!response['text']){ await this.telemetryService.capture({ @@ -870,7 +874,9 @@ export class AppController { let response = await this.aiToolsService.translate( Language.en, prompt.inputLanguage as Language, - placeholder + placeholder, + userId, + sessionId ) if(!response['text']){ await this.telemetryService.capture({ @@ -1035,7 +1041,7 @@ export class AppController { // verboseLogger("textToaudio =",textToaudio) let audioStartTime = Date.now(); textToaudio = removeLinks(textToaudio) - result['audio'] = await this.aiToolsService.textToSpeech(textToaudio,isNumber ? Language.en : prompt.inputLanguage) + result['audio'] = await this.aiToolsService.textToSpeech(textToaudio,isNumber ? Language.en : prompt.inputLanguage,userId,sessionId) if(result['audio']['error']){ await this.telemetryService.capture({ eventName: "Text to speech error", @@ -1107,7 +1113,7 @@ export class AppController { result['audio'] = {text: "",error: error.message} } } - console.log("Saving conversation..") + // console.log("Saving conversation..") conversation = await this.conversationService.saveConversation( sessionId, userId, diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 87cf0ca..eb4ceac 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -18,48 +18,46 @@ export class AiToolsService { private readonly monitoringService: MonitoringService, @Inject(CACHE_MANAGER) private readonly cacheManager: Cache, ) {} - async detectLanguage(text: string): Promise { - console.log("DETECTING LANGUAGE....") - var myHeaders = new Headers(); - myHeaders.append("Content-Type", "application/json"); - - var body = JSON.stringify({ - modelId: this.configService.get("TEXT_LANG_DETECTION_MODEL"), - task: "txt-lang-detection", - input:[{ - source: text?.replace("?","")?.trim() - }], - userId: null - }); - - var requestOptions = { - method: "POST", - headers: myHeaders, - body - }; - + async detectLanguage(text: string, userId: string, sessionId: string): Promise { + // console.log("DETECTING LANGUAGE....") try { - this.monitoringService.incrementBhashiniCount() - let response:any = await fetch( - 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/compute', - requestOptions - ) - response = await response.json() - let language: Language; - if(response.output && response.output.length){ - language = response.output[0]?.langPrediction[0]?.langCode as Language - this.monitoringService.incrementBhashiniSuccessCount() - return { - language: language || 'unk', - error: null - } - } else { - this.monitoringService.incrementBhashiniFailureCount() - return { - language: 'unk', - error: null - } + let input = { + input: [ + { + source: text, + }, + ], + }; + + let response: any = await this.computeBhashini( + this.configService.get('BHASHINI_DHRUVA_AUTHORIZATION'), + 'txt-lang-detection', + 'bhashini/iiiith/indic-lang-detection-all', + this.configService.get('BHASHINI_DHRUVA_ENDPOINT'), + {}, + input, + userId, + sessionId + ) + if(response["error"]){ + console.log(response["error"]) + throw new Error(response["error"]) + } + let language: Language; + if(response.output && response.output.length){ + language = response.data?.pipelineResponse[0]?.output[0]?.langPrediction[0]?.langCode as Language + this.monitoringService.incrementBhashiniSuccessCount() + return { + language: language || 'unk', + error: null + } + } else { + this.monitoringService.incrementBhashiniFailureCount() + return { + language: 'unk', + error: null } + } } catch (error) { this.monitoringService.incrementBhashiniFailureCount() if(isMostlyEnglish(text?.replace("?","")?.trim())) { @@ -79,7 +77,9 @@ export class AiToolsService { async translate( source: Language, target: Language, - text: string + text: string, + userId: string, + sessionId: string ) { try { const urlRegex = /(https?:\/\/[^\s]+)|(www\.[^\s]+)/g; @@ -93,7 +93,7 @@ export class AiToolsService { "targetLanguage": target } } - let bhashiniConfig: any = await this.getBhashiniConfig('translation',config) + let bhashiniConfig: any = await this.getBhashiniConfig('translation',config,userId,sessionId) let textArray = textWithoutUrls.split("\n") for(let i=0;i