Skip to content

Commit

Permalink
Update language detection API and refactored logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Amruth-Vamshi committed Nov 18, 2024
1 parent bebca87 commit 7300f1a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 72 deletions.
38 changes: 22 additions & 16 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,17 @@ export class AppController {
configid
)

console.log("fetched conversation: ", conversation)
// console.log("fetched conversation: ", conversation)
//handle text and audio
if(promptDto.text){
type = "Text"
let detectLanguageStartTime = Date.now();
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({
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -379,7 +379,7 @@ export class AppController {
}

conversation.inputType = type;
console.log("CP 1...")
// console.log("CP 1...")
//get flow
let botFlowMachine;
switch(configid){
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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()
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
128 changes: 73 additions & 55 deletions src/modules/aiTools/ai-tools.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,46 @@ export class AiToolsService {
private readonly monitoringService: MonitoringService,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
) {}
async detectLanguage(text: string): Promise<any> {
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<any> {
// 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())) {
Expand All @@ -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;
Expand All @@ -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<textArray.length;i++){
Expand All @@ -110,7 +110,9 @@ export class AiToolsService {
"source": textArray[i]
}
]
}
},
userId,
sessionId
)
if(response["error"]){
console.log(response["error"])
Expand All @@ -134,14 +136,16 @@ export class AiToolsService {

async speechToText(
base64audio: string,
language: Language
language: Language,
userId: string,
sessionId: string
) {
try {
let config: any = await this.getBhashiniConfig('asr',{
"language": {
"sourceLanguage": language
}
})
},userId,sessionId)
let requestConfig = {
"language": {
"sourceLanguage": language
Expand All @@ -165,7 +169,9 @@ export class AiToolsService {
"audioContent": base64audio
}
]
}
},
userId,
sessionId
)
if(response["error"]){
console.log(response["error"])
Expand All @@ -186,14 +192,16 @@ export class AiToolsService {

async textToSpeech(
text: string,
language: Language
language: Language,
userId: string,
sessionId: string
) {
try {
let config: any = await this.getBhashiniConfig('tts',{
"language": {
"sourceLanguage": language
}
})
},userId,sessionId)

let response: any = await this.computeBhashini(
config?.pipelineInferenceAPIEndPoint?.inferenceApiKey?.value,
Expand All @@ -211,7 +219,9 @@ export class AiToolsService {
"source": text
}
]
}
},
userId,
sessionId
)
if(response["error"]){
console.log(response["error"])
Expand Down Expand Up @@ -266,13 +276,17 @@ export class AiToolsService {
var myHeaders = new Headers();
myHeaders.append("accept", "application/json");
myHeaders.append("X-API-Key", this.configService.get("WADHWANI_API_KEY"));
let startDate = new Date();
console.log(`${startDate}: userId: ${userId} sessionId: ${sessionId} Waiting for ${this.configService.get("WADHWANI_BASE_URL")}/get_bot_response?query=${text}&user_id=${userId}&session_id=${sessionId} to respond ...`)
let response: any = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/get_bot_response?query=${text}&user_id=${userId}&session_id=${sessionId}`, {
headers: myHeaders,
"method": "GET",
"mode": "cors",
"credentials": "omit"
});
let endDate = new Date();
response = await response.json()
console.log(`${endDate}: userId: ${userId} sessionId: ${sessionId} Responded succesfully in ${endDate.getTime()-startDate.getTime()} ms.`)
return response
} catch(error){
console.log(error)
Expand All @@ -282,7 +296,7 @@ export class AiToolsService {
}
}

async getBhashiniConfig(task,config) {
async getBhashiniConfig(task,config,userId, sessionId) {
const cacheKey = `getBhashiniConfig:${JSON.stringify({ task, config })}`;

const cachedData = await this.cacheManager.get(cacheKey);
Expand Down Expand Up @@ -320,14 +334,16 @@ export class AiToolsService {
};
try{
this.monitoringService.incrementBhashiniCount()
console.log(`${new Date()}: Waiting for ${this.configService.get("ULCA_CONFIG_URL")} (config API) to respond ...`)
let startDate = new Date();
console.log(`${startDate}: userId: ${userId} sessionId: ${sessionId} Waiting for ${this.configService.get("ULCA_CONFIG_URL")} (config API) to respond ...`)
let response = await fetch(this.configService.get("ULCA_CONFIG_URL"), requestOptions)
if(response.status != 200){
console.log(response)
throw new Error(`${new Date()}: API call to '${this.configService.get("ULCA_CONFIG_URL")}' with config '${JSON.stringify(config,null,3)}' failed with status code ${response.status}`)
}
let endDate = new Date();
response = await response.json()
console.log(`${new Date()}: Responded succesfully`)
console.log(`${endDate}: userId: ${userId} sessionId: ${sessionId} Responded succesfully in ${endDate.getTime()-startDate.getTime()} ms.`)
this.monitoringService.incrementBhashiniSuccessCount()
await this.cacheManager.set(cacheKey, response, 86400);
return response
Expand All @@ -340,7 +356,7 @@ export class AiToolsService {
}
}

async computeBhashini(authorization, task, serviceId, url, config, input) {
async computeBhashini(authorization, task, serviceId, url, config, input, userId, sessionId) {
const cacheKey = `computeBhashini:${JSON.stringify({ task, serviceId, url, config, input })}`;
if(task != 'asr'){
const cachedData = await this.cacheManager.get(cacheKey);
Expand Down Expand Up @@ -382,14 +398,16 @@ export class AiToolsService {

try{
this.monitoringService.incrementBhashiniCount()
console.log(`${new Date()}: Waiting for ${url} for task (${task}) to respond ...`)
let startDate = new Date();
console.log(`${startDate}: userId: ${userId} sessionId: ${sessionId} Waiting for ${url} for task (${task}) to respond ...`)
let response = await fetch(url, requestOptions)
if(response.status != 200){
console.log(response)
throw new Error(`${new Date()}: API call to '${url}' with config '${JSON.stringify(config,null,3)}' failed with status code ${response.status}`)
}
let endDate = new Date();
response = await response.json()
console.log(`${new Date()}: Responded succesfully.`)
console.log(`${endDate}: userId: ${userId} sessionId: ${sessionId} Responded succesfully in ${endDate.getTime()-startDate.getTime()} ms.`)
this.monitoringService.incrementBhashiniSuccessCount()
if(task != 'asr') {
await this.cacheManager.set(cacheKey, response, 7200);
Expand Down
2 changes: 1 addition & 1 deletion src/xstate/prompt/prompt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class PromptServices {
}

async questionClassifier (context) {
console.log("IN questionclassifier")
// console.log("IN questionclassifier")
try{
let response: any = await this.aiToolsService.getResponseViaWadhwani(context.sessionId, context.userId, context.query)
if (response.error) throw new Error(`${response.error}, please try again.`)
Expand Down

0 comments on commit 7300f1a

Please sign in to comment.