From 2a33fe6fa6370730e279e8adf5a9325195993c25 Mon Sep 17 00:00:00 2001 From: Karun Agarwal <113603846+singhalkarun@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:29:16 +0530 Subject: [PATCH 01/28] Update build-and-push.yml --- .github/workflows/build-and-push.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index 07fa9c5..7411fcb 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -3,6 +3,7 @@ name: Build and Push Docker Image on: push: branches: + - dev - main env: From 183462b578f4f087a428229fe6d641a0bbac184e Mon Sep 17 00:00:00 2001 From: Karun Agarwal <113603846+singhalkarun@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:41:47 +0530 Subject: [PATCH 02/28] Update build-and-push.yml --- .github/workflows/build-and-push.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index 7411fcb..1b1ee79 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -45,8 +45,3 @@ jobs: cache-to: type=gha,mode=max tags: ${{ env.DOCKER_REGISTRY }}/${{ steps.lower-repo.outputs.repository }}:${{env.DOCKER_IMAGE_TAG}} labels: org.opencontainers.image.source=https://github.com/${{steps.lower-repo.outputs.repository}} - - - name: Invoke deployment hook - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.WEBHOOK_URL }} From 265922684c5bdacbd3713a9c958c73adb9dd8a5b Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Tue, 5 Dec 2023 09:17:04 +0530 Subject: [PATCH 03/28] Intergrated wadhwani APIs --- src/app.controller.ts | 18 +++++++----- src/modules/aiTools/ai-tools.service.ts | 35 ++++++++++++++++++++++ src/xstate/prompt/prompt.machine.ts | 39 ++++++++++++++++++++++--- src/xstate/prompt/prompt.service.ts | 13 ++++++++- 4 files changed, 92 insertions(+), 13 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 9e9149e..ddae3eb 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -996,15 +996,17 @@ export class AppController { break; } messageType = "final_response" - let resArray = result.text.split("\n") - let compareText = result.textInEnglish.split('\n') - if(compareText[compareText.length-1].slice(0,12)=="Registration"){ - textToaudio = "" - } else { - textToaudio = resArray.pop() + if(botFlowService.state.context.isWadhwaniResponse == 'false'){ + let resArray = result.text.split("\n") + let compareText = result.textInEnglish.split('\n') + if(compareText[compareText.length-1].slice(0,12)=="Registration"){ + textToaudio = "" + } else { + textToaudio = resArray.pop() + } + // verboseLogger("Array to convert",resArray) + result.text = `${formatStringsToTable(resArray)}\n${textToaudio}` } - // verboseLogger("Array to convert",resArray) - result.text = `${formatStringsToTable(resArray)}\n${textToaudio}` } // verboseLogger("textToaudio =",textToaudio) let audioStartTime = Date.now(); diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index a61e081..0f700d8 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -7,6 +7,10 @@ import { MonitoringService } from '../monitoring/monitoring.service'; const fetch = require('../../common/fetch'); const nodefetch = require('node-fetch'); const { Headers } = require('node-fetch'); +const path = require('path'); +const filePath = path.resolve(__dirname, '../../common/en.json'); +const engMessage = require(filePath); + @Injectable() export class AiToolsService { constructor( @@ -240,6 +244,37 @@ export class AiToolsService { } } + async getResponseViaWadhwani(text: string) { + try{ + var myHeaders = new Headers(); + myHeaders.append("accept", "application/json"); + myHeaders.append("X-API-Key", this.configService.get("WADHWANI_API_KEY")); + let response: any = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/detect_query_intent?query=${text}`, { + headers: myHeaders, + "method": "GET", + "mode": "cors", + "credentials": "omit" + }); + response = await response.text() + if(response == '"Invalid"'){ + return engMessage["message.invalid_question"] + } + response = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/get_bot_response?query=${response}`, { + headers: myHeaders, + "method": "GET", + "mode": "cors", + "credentials": "omit" + }); + response = (await response.text()).replace(/^\"|\"$/g, '') + return response + } catch(error){ + console.log(error) + return { + error + } + } + } + async getBhashiniConfig(task,config) { const cacheKey = `getBhashiniConfig:${JSON.stringify({ task, config })}`; diff --git a/src/xstate/prompt/prompt.machine.ts b/src/xstate/prompt/prompt.machine.ts index 331761e..abdde6b 100644 --- a/src/xstate/prompt/prompt.machine.ts +++ b/src/xstate/prompt/prompt.machine.ts @@ -785,7 +785,8 @@ export const botFlowMachine3:any = lastAadhaarDigits:'', state:'onGoing', userId:'', - isOTPVerified: false + isOTPVerified: false, + isWadhwaniResponse: "false" }, states: { checkStateAndJump: { @@ -887,11 +888,10 @@ export const botFlowMachine3:any = }, { cond: "ifInvalidClassifier", - target: "getUserQuestion", + target: "wadhwaniClassifier", actions: [ assign({ - response: () => engMessage["message.invalid_question"], - type:"pause" + isWadhwaniResponse: "true" }) ] }, @@ -915,6 +915,37 @@ export const botFlowMachine3:any = } } }, + wadhwaniClassifier: { + invoke: { + src: "wadhwaniClassifier", + onDone: { + target: 'endFlow', + actions: [ + assign({ + response: (_, event) => event.data, + userAadhaarNumber: "", + lastAadhaarDigits: "", + isOTPVerified: false, + type: '', + isWadhwaniResponse: "false" + }) + ] + }, + onError: { + target: 'error', + actions: [ + assign({ + userAadhaarNumber: "", + lastAadhaarDigits: "", + isOTPVerified: false, + error: (_, event) => event.data.message, + type: '', + isWadhwaniResponse: "false" + }) + ] + } + } + }, checkIfOTPHasBeenVerified: { invoke: { src: "getInput", diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 42b55cf..e1b7601 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -198,6 +198,16 @@ export class PromptServices { return `${userDetails}${userErrors.join("\n")}` } + async wadhwaniClassifier (context) { + try{ + let response: any = await this.aiToolsService.getResponseViaWadhwani(context.query) + if (response.error) throw new Error(`${response.error}, please try again.`) + return response; + } catch (error){ + return Promise.reject(error) + } + } + allFunctions() { return { getInput: this.getInput.bind(this), @@ -205,7 +215,8 @@ export class PromptServices { logError: this.logError.bind(this), validateAadhaarNumber: this.validateAadhaarNumber.bind(this), validateOTP: this.validateOTP.bind(this), - fetchUserData: this.fetchUserData.bind(this) + fetchUserData: this.fetchUserData.bind(this), + wadhwaniClassifier: this.wadhwaniClassifier.bind(this), } } From 0623c3ff9db187267d70422264ae76c25485cdcb Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Tue, 5 Dec 2023 11:37:18 +0530 Subject: [PATCH 04/28] updated wadhwani API logic --- src/modules/aiTools/ai-tools.service.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 0f700d8..aeeef1a 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -249,17 +249,7 @@ export class AiToolsService { var myHeaders = new Headers(); myHeaders.append("accept", "application/json"); myHeaders.append("X-API-Key", this.configService.get("WADHWANI_API_KEY")); - let response: any = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/detect_query_intent?query=${text}`, { - headers: myHeaders, - "method": "GET", - "mode": "cors", - "credentials": "omit" - }); - response = await response.text() - if(response == '"Invalid"'){ - return engMessage["message.invalid_question"] - } - response = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/get_bot_response?query=${response}`, { + let response: any = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/get_bot_response?query=${text}`, { headers: myHeaders, "method": "GET", "mode": "cors", From f1320eefe5966f4b341d1e089dc20211e8443cfe Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Wed, 6 Dec 2023 11:21:17 +0530 Subject: [PATCH 05/28] removing links from responses. --- src/app.controller.ts | 3 ++- src/common/utils.ts | 8 ++++++++ src/xstate/prompt/prompt.machine.ts | 12 ++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index ddae3eb..1b311ac 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -5,7 +5,7 @@ import { interpret } from "xstate"; import { Language } from "./language"; import { ConfigService } from "@nestjs/config"; import { AiToolsService } from "./modules/aiTools/ai-tools.service"; -import { formatStringsToTable, wordToNumber } from "./common/utils"; +import { formatStringsToTable, removeLinks, wordToNumber } from "./common/utils"; import { ConversationService } from "./modules/conversation/conversation.service"; import { PrismaService } from "./global-services/prisma.service"; import { CustomLogger } from "./common/logger"; @@ -1010,6 +1010,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) if(result['audio']['error']){ await this.telemetryService.capture({ diff --git a/src/common/utils.ts b/src/common/utils.ts index 6091dc2..199690a 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -211,4 +211,12 @@ export const wordToNumber = (input,type='benId') => { return number + "th"; } } +} + +export const removeLinks = (inputString) => { + // Define a regular expression pattern for identifying links + var linkPattern = /http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+/g; + // Use the replace() method to replace all links with an empty string + var resultString = inputString.replace(linkPattern, ''); + return resultString; } \ No newline at end of file diff --git a/src/xstate/prompt/prompt.machine.ts b/src/xstate/prompt/prompt.machine.ts index abdde6b..54af5fd 100644 --- a/src/xstate/prompt/prompt.machine.ts +++ b/src/xstate/prompt/prompt.machine.ts @@ -882,7 +882,8 @@ export const botFlowMachine3:any = actions: [ assign({ response: () => engMessage["message.convoStarter"], - type:"pause" + type:"pause", + isWadhwaniResponse: "false" }) ] }, @@ -899,7 +900,8 @@ export const botFlowMachine3:any = target: 'checkIfOTPHasBeenVerified', actions: [ assign({ - queryType: (_,event) => {console.log(`assigning queryType = ${event.data}`); return event.data} + queryType: (_,event) => {console.log(`assigning queryType = ${event.data}`); return event.data}, + isWadhwaniResponse: "false" }) ] } @@ -926,8 +928,7 @@ export const botFlowMachine3:any = userAadhaarNumber: "", lastAadhaarDigits: "", isOTPVerified: false, - type: '', - isWadhwaniResponse: "false" + type: '' }) ] }, @@ -939,8 +940,7 @@ export const botFlowMachine3:any = lastAadhaarDigits: "", isOTPVerified: false, error: (_, event) => event.data.message, - type: '', - isWadhwaniResponse: "false" + type: '' }) ] } From 9703113c9ca046a28ce01b8647a8692b1861056d Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Thu, 7 Dec 2023 11:55:22 +0530 Subject: [PATCH 06/28] updated classifier logic --- src/modules/aiTools/ai-tools.service.ts | 21 ++++++++++++++------- src/xstate/prompt/prompt.service.ts | 9 ++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index aeeef1a..694a9e3 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -223,14 +223,21 @@ export class AiToolsService { async textClassification(text: string) { try{ var myHeaders = new Headers(); - myHeaders.append("Content-Type", "application/json"); - let body = { - text: text - } - let response: any = await fetch(`${this.configService.get("HUGGINGFACE_TEXT_CLASSIFICATION_BASE_URL")}`, { + myHeaders.append("accept", "application/json"); + myHeaders.append("X-API-Key", this.configService.get("WADHWANI_API_KEY")); + // let body = { + // text: text + // } + // let response: any = await fetch(`${this.configService.get("HUGGINGFACE_TEXT_CLASSIFICATION_BASE_URL")}`, { + // headers: myHeaders, + // "body": JSON.stringify(body), + // "method": "POST", + // "mode": "cors", + // "credentials": "omit" + // }); + let response: any = await fetch(`${this.configService.get("WADHWANI_BASE_URL")}/detect_query_intent?query=${text}`, { headers: myHeaders, - "body": JSON.stringify(body), - "method": "POST", + "method": "GET", "mode": "cors", "credentials": "omit" }); diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index e1b7601..7ef4e3e 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -36,15 +36,10 @@ export class PromptServices { try{ let response: any = await this.aiToolsService.textClassification(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) - if (response == "LABEL_2") { - this.monitoringService.incrementUntrainedQueryCount() + if (response == `"Installment not received"`) return "payment" + else { return "invalid" } - if (response == "LABEL_1") return "payment" - if (response == "LABEL_0") return "payment" - if (response == "LABEL_3") return "convo_starter" - if (response == "LABEL_4") return "convo_ender" - return response; } catch (error){ return Promise.reject(error) } From 671fa8e8d50fc00c5f888414bcba631cab45dbc5 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Thu, 7 Dec 2023 13:38:54 +0530 Subject: [PATCH 07/28] Update flow for conversational queries --- src/app.controller.ts | 2 +- src/xstate/prompt/prompt.gaurds.ts | 2 +- src/xstate/prompt/prompt.machine.ts | 9 ++++----- src/xstate/prompt/prompt.service.ts | 3 +++ 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 1b311ac..06e39e2 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -995,7 +995,7 @@ export class AppController { this.monitoringService.incrementTotalSessionsInEnglishCount(); break; } - messageType = "final_response" + messageType = botFlowService.state.context.queryType == "convo" ? "convo_response" : "final_response" if(botFlowService.state.context.isWadhwaniResponse == 'false'){ let resArray = result.text.split("\n") let compareText = result.textInEnglish.split('\n') diff --git a/src/xstate/prompt/prompt.gaurds.ts b/src/xstate/prompt/prompt.gaurds.ts index 1c3bae5..b316c00 100644 --- a/src/xstate/prompt/prompt.gaurds.ts +++ b/src/xstate/prompt/prompt.gaurds.ts @@ -22,6 +22,6 @@ export const promptGuards = { ifInvalidClassifier: (_,event) => event.data == "invalid", - ifConvoStarterOrEnder: (_,event) => event.data == "convo_starter" || event.data == "convo_ender" + ifConvoStarterOrEnder: (_,event) => event.data == "convo" } \ No newline at end of file diff --git a/src/xstate/prompt/prompt.machine.ts b/src/xstate/prompt/prompt.machine.ts index 54af5fd..0477607 100644 --- a/src/xstate/prompt/prompt.machine.ts +++ b/src/xstate/prompt/prompt.machine.ts @@ -878,12 +878,11 @@ export const botFlowMachine3:any = onDone: [ { cond: "ifConvoStarterOrEnder", - target: "getUserQuestion", + target: "wadhwaniClassifier", actions: [ assign({ - response: () => engMessage["message.convoStarter"], - type:"pause", - isWadhwaniResponse: "false" + queryType: (_,event) => event.data, + isWadhwaniResponse: "true" }) ] }, @@ -900,7 +899,7 @@ export const botFlowMachine3:any = target: 'checkIfOTPHasBeenVerified', actions: [ assign({ - queryType: (_,event) => {console.log(`assigning queryType = ${event.data}`); return event.data}, + queryType: (_,event) => event.data, isWadhwaniResponse: "false" }) ] diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 7ef4e3e..92cd865 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -36,6 +36,9 @@ export class PromptServices { try{ let response: any = await this.aiToolsService.textClassification(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) + if (response == `"Invalid"`) return "convo" + if (response == `"convo_starter"`) return "convo" + if (response == `"convo_ender"`) return "convo" if (response == `"Installment not received"`) return "payment" else { return "invalid" From 2679e3a590d8d3ca0e2d68c9f9f3df80ce1db960 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Mon, 11 Dec 2023 16:37:14 +0530 Subject: [PATCH 08/28] added weather classifier --- src/app.controller.ts | 6 ++- src/modules/aiTools/ai-tools.service.ts | 31 +++++++++++++++ src/xstate/prompt/prompt.machine.ts | 53 ++++++++++++++++++++++++- src/xstate/prompt/prompt.service.ts | 51 ++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 3 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 06e39e2..2058936 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -50,6 +50,8 @@ export class PromptDto { conversationId?: string; @IsOptional() identifier?: string; + @IsOptional() + location?: any; } @Controller() @@ -373,7 +375,9 @@ export class AppController { inputLanguage: prompt.inputLanguage, lastAadhaarDigits:'', state:'onGoing', - isOTPVerified: false + isOTPVerified: false, + lat: prompt.input?.location?.lat, + long: prompt.input?.location?.long } let botFlowService = interpret(botFlowMachine.withContext(conversation || defaultContext)).start(); diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 694a9e3..7ea7e7d 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -91,6 +91,8 @@ export class AiToolsService { let textArray = text.split("\n") for(let i=0;i event.data.query, @@ -872,6 +874,53 @@ export const botFlowMachine3:any = } } }, + questionClassifierWeather: { + invoke: { + src: "weatherClassifier", + onDone: [ + { + cond: "ifInvalidClassifier", + target: "questionClassifier" + }, + { + target: 'getWeatherInfo' + } + ], + onError: { + target: 'error', + actions: [ + assign({ + error: (_, event) => event.data.message, + type: '' + }) + ] + } + } + }, + getWeatherInfo:{ + invoke: { + src: "getWeatherInfo", + onDone: [ + { + target: 'endFlow', + actions: [ + assign({ + response: (_, event) => event.data, + }) + ] + } + ], + onError: { + target: 'error', + actions: [ + assign({ + error: (_, event) => event.data.message, + type: '' + }) + ] + } + } + }, questionClassifier: { invoke: { src: "questionClassifier", diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 92cd865..b48f9a9 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -31,6 +31,19 @@ export class PromptServices { async getInput (context) { return context } + + async weatherClassifier(context) { + try{ + let response: any = await this.aiToolsService.textClassificationForWeather(context.query) + if (response.error) throw new Error(`${response.error}, please try again.`) + if (response == `LABEL_6`) return "weather" + else { + return "invalid" + } + } catch (error){ + return Promise.reject(error) + } + } async questionClassifier (context) { try{ @@ -206,6 +219,42 @@ export class PromptServices { } } + async getWeatherInfo (context) { + try{ + if(!context.lat || !context.long){ + return "Please enable location and try again." + } + var requestOptions: RequestInit= { + method: 'GET', + redirect: 'follow' + }; + + let weather: any = await fetch(`https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/${context.lat},${context.long}?unitGroup=metric&key=${this.configService.get('WEATHER_PROVIDER_API_KEY')}&contentType=json`, requestOptions) + .then(response => response.json()) + .then(result => {return result}) + .catch(error => console.log('error', error)); + let weatherString = '🌦️ *Weather Forecast for the Next 10 Days*'; + weather.days.slice(0,10).forEach((data,index)=>{ + weatherString+=` +*Day ${index+1}:* +- Date: ${data.datetime} +- Temperature: ${data.temp}°C +- Conditions: ${data.conditions} +- Precipitation: ${data.precip*10}% + +` + }) + weatherString+=` +Feel free to reach out if you need more details or have any specific concerns. Happy farming! 🚜🌾 + +The data is shared from https://weather.visualcrossing.com + ` + return weatherString + } catch (error){ + return Promise.reject(error) + } + } + allFunctions() { return { getInput: this.getInput.bind(this), @@ -215,6 +264,8 @@ export class PromptServices { validateOTP: this.validateOTP.bind(this), fetchUserData: this.fetchUserData.bind(this), wadhwaniClassifier: this.wadhwaniClassifier.bind(this), + weatherClassifier: this.weatherClassifier.bind(this), + getWeatherInfo: this.getWeatherInfo.bind(this) } } From 72b0f624c26237d53f30d76e5a80025f4b685231 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Mon, 11 Dec 2023 16:47:54 +0530 Subject: [PATCH 09/28] added fetch --- src/xstate/prompt/prompt.service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index b48f9a9..10f22e5 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -15,6 +15,8 @@ const path = require('path'); const filePath = path.resolve(__dirname, '../../common/kisanPortalErrors.json'); const PMKissanProtalErrors = require(filePath); import * as moment from "moment"; +const fetch = require('../../common/fetch'); + @Injectable() export class PromptServices { From dbcb60d7af2fc85e9458d9bf83500ca5b1509330 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Mon, 11 Dec 2023 17:17:38 +0530 Subject: [PATCH 10/28] added logs to debug --- src/xstate/prompt/prompt.service.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 10f22e5..cc17249 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -31,10 +31,12 @@ export class PromptServices { } async getInput (context) { + console.log("getInput") return context } async weatherClassifier(context) { + console.log("weatherClassifier") try{ let response: any = await this.aiToolsService.textClassificationForWeather(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) @@ -48,6 +50,7 @@ export class PromptServices { } async questionClassifier (context) { + console.log("questionClassifier") try{ let response: any = await this.aiToolsService.textClassification(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) @@ -212,6 +215,7 @@ export class PromptServices { } async wadhwaniClassifier (context) { + console.log("wadhwaniClassifier") try{ let response: any = await this.aiToolsService.getResponseViaWadhwani(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) @@ -222,6 +226,7 @@ export class PromptServices { } async getWeatherInfo (context) { + console.log("getWeatherInfo") try{ if(!context.lat || !context.long){ return "Please enable location and try again." From f81cb836e8e16a9f35fc00bd54b7b970f4a1b7a7 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Mon, 11 Dec 2023 17:21:41 +0530 Subject: [PATCH 11/28] added logs --- src/modules/aiTools/ai-tools.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 7ea7e7d..5fbf700 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -240,6 +240,7 @@ export class AiToolsService { "credentials": "omit" }); response = await response.json() + console.log(response) } while(response["error"]!=null) response = response[0].label return response From a401f46e1359385e4a672331e0b1a73bcfdeafbf Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Mon, 11 Dec 2023 17:27:52 +0530 Subject: [PATCH 12/28] Added auth for classifier --- src/modules/aiTools/ai-tools.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 5fbf700..d887201 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -226,7 +226,7 @@ export class AiToolsService { try{ var myHeaders = new Headers(); myHeaders.append("accept", "application/json"); - myHeaders.append("X-API-Key", `Bearer ${this.configService.get("CLASSIFIER_API_KEY")}`); + myHeaders.append("Authorization", `Bearer ${this.configService.get("CLASSIFIER_API_KEY")}`); let body = { text: text } From 5523e0012618121539f3c32d56f457948c602a21 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Tue, 12 Dec 2023 10:50:31 +0530 Subject: [PATCH 13/28] added crop classifier --- src/modules/aiTools/ai-tools.service.ts | 30 +++++++++++++++- src/xstate/prompt/prompt.gaurds.ts | 4 +++ src/xstate/prompt/prompt.machine.ts | 32 +++++++++++++++++ src/xstate/prompt/prompt.service.ts | 48 ++++++++++++++++++++++++- 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index d887201..77dd806 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -240,7 +240,6 @@ export class AiToolsService { "credentials": "omit" }); response = await response.json() - console.log(response) } while(response["error"]!=null) response = response[0].label return response @@ -252,6 +251,35 @@ export class AiToolsService { } } + async classificationForCrop(text: string) { + try{ + var myHeaders = new Headers(); + myHeaders.append("accept", "application/json"); + myHeaders.append("Authorization", `Bearer ${this.configService.get("CLASSIFIER_API_KEY")}`); + let body = { + inputs: text + } + let response: any; + do{ + response = await fetch(`${this.configService.get("CROP_CLASSIFICATION_BASE_URL")}`, { + headers: myHeaders, + "body": JSON.stringify(body), + "method": "POST", + "mode": "cors", + "credentials": "omit" + }); + response = await response.json() + } while(response["error"]!=null) + response = response[0] + return response + } catch(error){ + console.log(error) + return { + error + } + } + } + async textClassification(text: string) { try{ var myHeaders = new Headers(); diff --git a/src/xstate/prompt/prompt.gaurds.ts b/src/xstate/prompt/prompt.gaurds.ts index b316c00..1fd1068 100644 --- a/src/xstate/prompt/prompt.gaurds.ts +++ b/src/xstate/prompt/prompt.gaurds.ts @@ -22,6 +22,10 @@ export const promptGuards = { ifInvalidClassifier: (_,event) => event.data == "invalid", + ifSale: (_,event) => event.data == "sale", + + ifValidCrop: (_,event) => event.data, + ifConvoStarterOrEnder: (_,event) => event.data == "convo" } \ No newline at end of file diff --git a/src/xstate/prompt/prompt.machine.ts b/src/xstate/prompt/prompt.machine.ts index ccafbe2..d3605f3 100644 --- a/src/xstate/prompt/prompt.machine.ts +++ b/src/xstate/prompt/prompt.machine.ts @@ -878,6 +878,10 @@ export const botFlowMachine3:any = invoke: { src: "weatherClassifier", onDone: [ + { + cond: "ifSale", + target: "checkCropNER" + }, { cond: "ifInvalidClassifier", target: "questionClassifier" @@ -897,6 +901,34 @@ export const botFlowMachine3:any = } } }, + checkCropNER: { + invoke: { + src: "checkCropNER", + onDone: [ + { + cond: "ifValidCrop", + target: "endFlow", + actions: [ + assign({ + response: (_, event) => event.data, + }) + ] + }, + { + target: "questionClassifier" + } + ], + onError: { + target: 'error', + actions: [ + assign({ + error: (_, event) => event.data.message, + type: '' + }) + ] + } + } + }, getWeatherInfo:{ invoke: { src: "getWeatherInfo", diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index cc17249..75675bb 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -16,6 +16,7 @@ const filePath = path.resolve(__dirname, '../../common/kisanPortalErrors.json'); const PMKissanProtalErrors = require(filePath); import * as moment from "moment"; const fetch = require('../../common/fetch'); +import * as NodeGeocoder from 'node-geocoder'; @Injectable() @@ -41,6 +42,7 @@ export class PromptServices { let response: any = await this.aiToolsService.textClassificationForWeather(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) if (response == `LABEL_6`) return "weather" + if (response == `LABEL_5`) return "sale" else { return "invalid" } @@ -262,6 +264,49 @@ The data is shared from https://weather.visualcrossing.com } } + async checkCropNER (context) { + console.log("checkCropNER") + try{ + if(!context.lat || !context.long){ + return "Please enable location and try again." + } + let response: any = await this.aiToolsService.classificationForCrop(context.query) + if (response.error) throw new Error(`${response.error}, please try again.`) + if (response.entity_group != `CROP`) return false + const options = { + provider: 'mapbox', + apiKey: process.env['MAP_BOX_API_KEY'], // Replace with your Google Maps API key + formatter: 'json', + }; + const geocoder = NodeGeocoder(options); + + let location_info = await geocoder.reverse({ lat: context.lat, lon: context.long }) + let crop = response.word.charAt(0).toUpperCase() + response.word.slice(1) + let api_key = this.configService.get("MARKET_DATA_API_KEY"); + let url = `https://api.data.gov.in/resource/9ef84268-d588-465a-a308-a864a43d0070?api-key=${api_key}&format=json&limit=1000&filters[state]=${location_info[0].state}&filters[district]=${location_info[0].district}&filters[commodity]=${crop}` + let cropData = await fetch(url) + .then(response => response.json()) + .then(result => {return result}) + .catch(error => console.log('error', error)); + if(!cropData.records.length) return false + let returnString = `*Crop:* ${cropData.records[0].commodity}` + cropData.records.forEach(data=>{ + returnString+=` +*Market:* ${data.market} +*Current Wholesale Rate:* ${data.modal_price} +` + }) + returnString+=` +Please note that these rates are subject to change based on market conditions. For real-time and more detailed information, we recommend checking with your local market or contacting relevant agricultural authorities. + +If you have any further questions or need assistance, feel free to ask. Wishing you a successful harvest! + ` + return returnString + } catch (error){ + return Promise.reject(error) + } + } + allFunctions() { return { getInput: this.getInput.bind(this), @@ -272,7 +317,8 @@ The data is shared from https://weather.visualcrossing.com fetchUserData: this.fetchUserData.bind(this), wadhwaniClassifier: this.wadhwaniClassifier.bind(this), weatherClassifier: this.weatherClassifier.bind(this), - getWeatherInfo: this.getWeatherInfo.bind(this) + getWeatherInfo: this.getWeatherInfo.bind(this), + checkCropNER: this.checkCropNER.bind(this) } } From 099e66635e57caba85b4ef520c75b42d075c89c0 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Tue, 12 Dec 2023 10:51:23 +0530 Subject: [PATCH 14/28] added node-geocoder --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index c5ec923..2412bd4 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "moment-timezone": "^0.5.43", "node-cron": "^3.0.2", "node-fetch": "2", + "node-geocoder": "^4.2.0", "nodemailer": "^6.9.1", "posthog-node": "^2.6.0", "prom-client": "^14.2.0", From b9b483642a400acbd6461233c393ca2ce553d46f Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Tue, 12 Dec 2023 11:36:51 +0530 Subject: [PATCH 15/28] static message if no crop data --- src/xstate/prompt/prompt.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 75675bb..0c259c5 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -288,7 +288,7 @@ The data is shared from https://weather.visualcrossing.com .then(response => response.json()) .then(result => {return result}) .catch(error => console.log('error', error)); - if(!cropData.records.length) return false + if(!cropData.records.length) return "Data is not available at https://agmarknet.gov.in/" let returnString = `*Crop:* ${cropData.records[0].commodity}` cropData.records.forEach(data=>{ returnString+=` From ce5d5bd417fd18dd51bca01747f9898a24570be6 Mon Sep 17 00:00:00 2001 From: Amruth-Vamshi <130033232+Amruth-Vamshi@users.noreply.github.com> Date: Thu, 4 Jan 2024 08:24:09 +0530 Subject: [PATCH 16/28] Update kisanPortalErrors.json --- src/common/kisanPortalErrors.json | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/common/kisanPortalErrors.json b/src/common/kisanPortalErrors.json index 0d906fb..9fe0b12 100644 --- a/src/common/kisanPortalErrors.json +++ b/src/common/kisanPortalErrors.json @@ -155,6 +155,38 @@ "text":"Dear {{farmer_name}}, I checked your record and found that your Land details were not uploaded by the State/UT Government. I suggest you visit the Block/Tehsil/District Office and get it updated on the PM-KISAN portal", "types":["payment"] }, + "Farmer Record has been rejected by PFMS / Bank": { + "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", + "types":["payment"] + }, + "Waiting for approval by state": { + "text": "Dear {{farmer_name}}, I have checked your status and found that your PMKISAN account is being verified by the State. Please visit your nearest district/ block office and get your details updated on the PM KISAN portal.", + "types":["payment"] + }, + "1st installment transaction failed to 15th installment transaction failed": { + "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", + "types":["payment"] + }, + "Farmer belongs to Exclusion category": { + "text": "Dear {{farmer_name}}, I have checked your status and found that you have been marked as ineligible by the State as you belong to Exclusion category as per PMKISAN scheme guidelines. If this status is not correct, please Visit your nearest district/ block office and get your details updated on the PM KISAN portal.", + "types":["payment"] + }, + "Invalid account type. Account is not Saving or Jandhan": { + "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", + "types":["payment"] + }, + "Farmer marked as \"InEligible\"": { + "text": "Dear {{farmer_name}}, I have checked your status and found that you have been marked as ineligible by the State as you belong to Exclusion category as per PMKISAN scheme guidelines. If this status is not correct, please Visit your nearest district/ block office and get your details updated on the PM KISAN portal.", + "types":["payment"] + }, + "Temporary hold due to account revalidation": { + "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", + "types":["payment"] + }, + "Exempted/Not given": { + "text": "Dear {{farmer_name}}, I have checked your status and found that you have been marked as ineligible by the State as you belong to Exclusion category as per PMKISAN scheme guidelines. If this status is not correct, please Visit your nearest district/ block office and get your details updated on the PM KISAN portal.", + "types":["payment"] + }, "No Errors": { "text": "Dear {{farmer_name}}, I have checked your account and found that you received all the instalments till now from your PM KISAN registration date {{Reg_Date (DD-MM-YYYY)}}. You will receive your next instalment payment soon in your Aadhaar seeded bank account." } From 9d90f76e32891728245d9b81004239f248a53efb Mon Sep 17 00:00:00 2001 From: Amruth-Vamshi <130033232+Amruth-Vamshi@users.noreply.github.com> Date: Thu, 4 Jan 2024 08:25:20 +0530 Subject: [PATCH 17/28] Update kisanPortalErrors.json --- src/common/kisanPortalErrors.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/common/kisanPortalErrors.json b/src/common/kisanPortalErrors.json index 9fe0b12..eef4847 100644 --- a/src/common/kisanPortalErrors.json +++ b/src/common/kisanPortalErrors.json @@ -155,15 +155,15 @@ "text":"Dear {{farmer_name}}, I checked your record and found that your Land details were not uploaded by the State/UT Government. I suggest you visit the Block/Tehsil/District Office and get it updated on the PM-KISAN portal", "types":["payment"] }, - "Farmer Record has been rejected by PFMS / Bank": { + "Farmer Record has been rejected by PFMS / Bank": { "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", "types":["payment"] }, - "Waiting for approval by state": { + "Waiting for approval by state": { "text": "Dear {{farmer_name}}, I have checked your status and found that your PMKISAN account is being verified by the State. Please visit your nearest district/ block office and get your details updated on the PM KISAN portal.", "types":["payment"] }, - "1st installment transaction failed to 15th installment transaction failed": { + "1st installment transaction failed to 15th installment transaction failed": { "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", "types":["payment"] }, @@ -171,19 +171,19 @@ "text": "Dear {{farmer_name}}, I have checked your status and found that you have been marked as ineligible by the State as you belong to Exclusion category as per PMKISAN scheme guidelines. If this status is not correct, please Visit your nearest district/ block office and get your details updated on the PM KISAN portal.", "types":["payment"] }, - "Invalid account type. Account is not Saving or Jandhan": { + "Invalid account type. Account is not Saving or Jandhan": { "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", "types":["payment"] }, - "Farmer marked as \"InEligible\"": { + "Farmer marked as \"InEligible\"": { "text": "Dear {{farmer_name}}, I have checked your status and found that you have been marked as ineligible by the State as you belong to Exclusion category as per PMKISAN scheme guidelines. If this status is not correct, please Visit your nearest district/ block office and get your details updated on the PM KISAN portal.", "types":["payment"] }, - "Temporary hold due to account revalidation": { + "Temporary hold due to account revalidation": { "text": "Dear {{farmer_name}}, I have checked your status and found that there is some issue with your bank account. Link your Aadhaar with your active bank account and enable it for DBT payment to continue to avail the benefits.", "types":["payment"] }, - "Exempted/Not given": { + "Exempted/Not given": { "text": "Dear {{farmer_name}}, I have checked your status and found that you have been marked as ineligible by the State as you belong to Exclusion category as per PMKISAN scheme guidelines. If this status is not correct, please Visit your nearest district/ block office and get your details updated on the PM KISAN portal.", "types":["payment"] }, From 937a1401856cb342e99ee7ec41e685af535acd8e Mon Sep 17 00:00:00 2001 From: Amruth-Vamshi <130033232+Amruth-Vamshi@users.noreply.github.com> Date: Tue, 16 Jan 2024 21:32:25 +0530 Subject: [PATCH 18/28] Update main.ts --- src/main.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index f788f12..4dccf93 100644 --- a/src/main.ts +++ b/src/main.ts @@ -63,7 +63,11 @@ async function bootstrap() { process.exit(0); }); - app.enableCors(); + app.enableCors({ + origin: configService.get('CORS_ALLOWED_ORIGINS', '').split(','), + methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'], + credentials: true, + }); await app.register(multipart); await app.register(compression, { encodings: ["gzip", "deflate"] }); app.useStaticAssets({ root: join(__dirname, "../../fileUploads") }); From 1324e9af91caa30b3705eb7dd42d66dafa228f2b Mon Sep 17 00:00:00 2001 From: Amruth-Vamshi <130033232+Amruth-Vamshi@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:23:27 +0530 Subject: [PATCH 19/28] added logs to debug. --- src/app.controller.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 2058936..a043a1f 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -104,7 +104,7 @@ export class AppController { userId: userId, flowId: configid },"error") - // verboseLogger("User input", promptDto) + verboseLogger("User input", promptDto) //create or get user and conversation let user; try{ @@ -113,7 +113,10 @@ export class AppController { id: userId } }) - }catch{ + verboseLogger("got user", user) + }catch(error){ + errorLogger(error) + verboseLogger("got user", user) this.monitoringService.incrementTotalSessionsCount() // verboseLogger("creating new user with id =",userId) await this.telemetryService.capture({ @@ -148,11 +151,13 @@ export class AppController { }) } if(!user) { + console.log("creating user") user = await this.prismaService.user.create({ data:{ id: userId } }) + console.log(user) } let conversation = await this.conversationService.getConversationState( userId, From 562727bb368025a67bb56cae6b48e70e06e7f901 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:29:24 +0530 Subject: [PATCH 20/28] Revert "added logs to debug." This reverts commit 1324e9af91caa30b3705eb7dd42d66dafa228f2b. --- src/app.controller.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index a043a1f..2058936 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -104,7 +104,7 @@ export class AppController { userId: userId, flowId: configid },"error") - verboseLogger("User input", promptDto) + // verboseLogger("User input", promptDto) //create or get user and conversation let user; try{ @@ -113,10 +113,7 @@ export class AppController { id: userId } }) - verboseLogger("got user", user) - }catch(error){ - errorLogger(error) - verboseLogger("got user", user) + }catch{ this.monitoringService.incrementTotalSessionsCount() // verboseLogger("creating new user with id =",userId) await this.telemetryService.capture({ @@ -151,13 +148,11 @@ export class AppController { }) } if(!user) { - console.log("creating user") user = await this.prismaService.user.create({ data:{ id: userId } }) - console.log(user) } let conversation = await this.conversationService.getConversationState( userId, From 3df2e288fdda880a71c5eda9e9cab3b80102cbc1 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:30:00 +0530 Subject: [PATCH 21/28] Revert "static message if no crop data" This reverts commit b9b483642a400acbd6461233c393ca2ce553d46f. --- src/xstate/prompt/prompt.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 0c259c5..75675bb 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -288,7 +288,7 @@ The data is shared from https://weather.visualcrossing.com .then(response => response.json()) .then(result => {return result}) .catch(error => console.log('error', error)); - if(!cropData.records.length) return "Data is not available at https://agmarknet.gov.in/" + if(!cropData.records.length) return false let returnString = `*Crop:* ${cropData.records[0].commodity}` cropData.records.forEach(data=>{ returnString+=` From af4b0096a37ef053c7c2b2b0884fc7ce20fb553f Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:30:27 +0530 Subject: [PATCH 22/28] Revert "added node-geocoder" This reverts commit 099e66635e57caba85b4ef520c75b42d075c89c0. --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 2412bd4..c5ec923 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,6 @@ "moment-timezone": "^0.5.43", "node-cron": "^3.0.2", "node-fetch": "2", - "node-geocoder": "^4.2.0", "nodemailer": "^6.9.1", "posthog-node": "^2.6.0", "prom-client": "^14.2.0", From 45ec42c9689008a6c3ce47e252a1ddd1330344a6 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:33:37 +0530 Subject: [PATCH 23/28] Revert "added crop classifier" This reverts commit 5523e0012618121539f3c32d56f457948c602a21. --- src/modules/aiTools/ai-tools.service.ts | 30 +--------------- src/xstate/prompt/prompt.gaurds.ts | 4 --- src/xstate/prompt/prompt.machine.ts | 32 ----------------- src/xstate/prompt/prompt.service.ts | 48 +------------------------ 4 files changed, 2 insertions(+), 112 deletions(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 77dd806..d887201 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -240,6 +240,7 @@ export class AiToolsService { "credentials": "omit" }); response = await response.json() + console.log(response) } while(response["error"]!=null) response = response[0].label return response @@ -251,35 +252,6 @@ export class AiToolsService { } } - async classificationForCrop(text: string) { - try{ - var myHeaders = new Headers(); - myHeaders.append("accept", "application/json"); - myHeaders.append("Authorization", `Bearer ${this.configService.get("CLASSIFIER_API_KEY")}`); - let body = { - inputs: text - } - let response: any; - do{ - response = await fetch(`${this.configService.get("CROP_CLASSIFICATION_BASE_URL")}`, { - headers: myHeaders, - "body": JSON.stringify(body), - "method": "POST", - "mode": "cors", - "credentials": "omit" - }); - response = await response.json() - } while(response["error"]!=null) - response = response[0] - return response - } catch(error){ - console.log(error) - return { - error - } - } - } - async textClassification(text: string) { try{ var myHeaders = new Headers(); diff --git a/src/xstate/prompt/prompt.gaurds.ts b/src/xstate/prompt/prompt.gaurds.ts index 1fd1068..b316c00 100644 --- a/src/xstate/prompt/prompt.gaurds.ts +++ b/src/xstate/prompt/prompt.gaurds.ts @@ -22,10 +22,6 @@ export const promptGuards = { ifInvalidClassifier: (_,event) => event.data == "invalid", - ifSale: (_,event) => event.data == "sale", - - ifValidCrop: (_,event) => event.data, - ifConvoStarterOrEnder: (_,event) => event.data == "convo" } \ No newline at end of file diff --git a/src/xstate/prompt/prompt.machine.ts b/src/xstate/prompt/prompt.machine.ts index d3605f3..ccafbe2 100644 --- a/src/xstate/prompt/prompt.machine.ts +++ b/src/xstate/prompt/prompt.machine.ts @@ -878,10 +878,6 @@ export const botFlowMachine3:any = invoke: { src: "weatherClassifier", onDone: [ - { - cond: "ifSale", - target: "checkCropNER" - }, { cond: "ifInvalidClassifier", target: "questionClassifier" @@ -901,34 +897,6 @@ export const botFlowMachine3:any = } } }, - checkCropNER: { - invoke: { - src: "checkCropNER", - onDone: [ - { - cond: "ifValidCrop", - target: "endFlow", - actions: [ - assign({ - response: (_, event) => event.data, - }) - ] - }, - { - target: "questionClassifier" - } - ], - onError: { - target: 'error', - actions: [ - assign({ - error: (_, event) => event.data.message, - type: '' - }) - ] - } - } - }, getWeatherInfo:{ invoke: { src: "getWeatherInfo", diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 75675bb..cc17249 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -16,7 +16,6 @@ const filePath = path.resolve(__dirname, '../../common/kisanPortalErrors.json'); const PMKissanProtalErrors = require(filePath); import * as moment from "moment"; const fetch = require('../../common/fetch'); -import * as NodeGeocoder from 'node-geocoder'; @Injectable() @@ -42,7 +41,6 @@ export class PromptServices { let response: any = await this.aiToolsService.textClassificationForWeather(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) if (response == `LABEL_6`) return "weather" - if (response == `LABEL_5`) return "sale" else { return "invalid" } @@ -264,49 +262,6 @@ The data is shared from https://weather.visualcrossing.com } } - async checkCropNER (context) { - console.log("checkCropNER") - try{ - if(!context.lat || !context.long){ - return "Please enable location and try again." - } - let response: any = await this.aiToolsService.classificationForCrop(context.query) - if (response.error) throw new Error(`${response.error}, please try again.`) - if (response.entity_group != `CROP`) return false - const options = { - provider: 'mapbox', - apiKey: process.env['MAP_BOX_API_KEY'], // Replace with your Google Maps API key - formatter: 'json', - }; - const geocoder = NodeGeocoder(options); - - let location_info = await geocoder.reverse({ lat: context.lat, lon: context.long }) - let crop = response.word.charAt(0).toUpperCase() + response.word.slice(1) - let api_key = this.configService.get("MARKET_DATA_API_KEY"); - let url = `https://api.data.gov.in/resource/9ef84268-d588-465a-a308-a864a43d0070?api-key=${api_key}&format=json&limit=1000&filters[state]=${location_info[0].state}&filters[district]=${location_info[0].district}&filters[commodity]=${crop}` - let cropData = await fetch(url) - .then(response => response.json()) - .then(result => {return result}) - .catch(error => console.log('error', error)); - if(!cropData.records.length) return false - let returnString = `*Crop:* ${cropData.records[0].commodity}` - cropData.records.forEach(data=>{ - returnString+=` -*Market:* ${data.market} -*Current Wholesale Rate:* ${data.modal_price} -` - }) - returnString+=` -Please note that these rates are subject to change based on market conditions. For real-time and more detailed information, we recommend checking with your local market or contacting relevant agricultural authorities. - -If you have any further questions or need assistance, feel free to ask. Wishing you a successful harvest! - ` - return returnString - } catch (error){ - return Promise.reject(error) - } - } - allFunctions() { return { getInput: this.getInput.bind(this), @@ -317,8 +272,7 @@ If you have any further questions or need assistance, feel free to ask. Wishing fetchUserData: this.fetchUserData.bind(this), wadhwaniClassifier: this.wadhwaniClassifier.bind(this), weatherClassifier: this.weatherClassifier.bind(this), - getWeatherInfo: this.getWeatherInfo.bind(this), - checkCropNER: this.checkCropNER.bind(this) + getWeatherInfo: this.getWeatherInfo.bind(this) } } From 694cf7026d0e9124c22dd53aa29659a53d6df422 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:34:06 +0530 Subject: [PATCH 24/28] Revert "Added auth for classifier" This reverts commit a401f46e1359385e4a672331e0b1a73bcfdeafbf. --- src/modules/aiTools/ai-tools.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index d887201..5fbf700 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -226,7 +226,7 @@ export class AiToolsService { try{ var myHeaders = new Headers(); myHeaders.append("accept", "application/json"); - myHeaders.append("Authorization", `Bearer ${this.configService.get("CLASSIFIER_API_KEY")}`); + myHeaders.append("X-API-Key", `Bearer ${this.configService.get("CLASSIFIER_API_KEY")}`); let body = { text: text } From c35f0c1010f396602723f59e1ebbb6336032cc46 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:34:15 +0530 Subject: [PATCH 25/28] Revert "added logs" This reverts commit f81cb836e8e16a9f35fc00bd54b7b970f4a1b7a7. --- src/modules/aiTools/ai-tools.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 5fbf700..7ea7e7d 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -240,7 +240,6 @@ export class AiToolsService { "credentials": "omit" }); response = await response.json() - console.log(response) } while(response["error"]!=null) response = response[0].label return response From e3fbe069d80a7ac199f0febec2054cfb525cfdf6 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:34:22 +0530 Subject: [PATCH 26/28] Revert "added logs to debug" This reverts commit dbcb60d7af2fc85e9458d9bf83500ca5b1509330. --- src/xstate/prompt/prompt.service.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index cc17249..10f22e5 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -31,12 +31,10 @@ export class PromptServices { } async getInput (context) { - console.log("getInput") return context } async weatherClassifier(context) { - console.log("weatherClassifier") try{ let response: any = await this.aiToolsService.textClassificationForWeather(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) @@ -50,7 +48,6 @@ export class PromptServices { } async questionClassifier (context) { - console.log("questionClassifier") try{ let response: any = await this.aiToolsService.textClassification(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) @@ -215,7 +212,6 @@ export class PromptServices { } async wadhwaniClassifier (context) { - console.log("wadhwaniClassifier") try{ let response: any = await this.aiToolsService.getResponseViaWadhwani(context.query) if (response.error) throw new Error(`${response.error}, please try again.`) @@ -226,7 +222,6 @@ export class PromptServices { } async getWeatherInfo (context) { - console.log("getWeatherInfo") try{ if(!context.lat || !context.long){ return "Please enable location and try again." From 0211dcad508077f0e8e9d2fffb772f0d8fe1f4e2 Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:34:37 +0530 Subject: [PATCH 27/28] Revert "added fetch" This reverts commit 72b0f624c26237d53f30d76e5a80025f4b685231. --- src/xstate/prompt/prompt.service.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index 10f22e5..b48f9a9 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -15,8 +15,6 @@ const path = require('path'); const filePath = path.resolve(__dirname, '../../common/kisanPortalErrors.json'); const PMKissanProtalErrors = require(filePath); import * as moment from "moment"; -const fetch = require('../../common/fetch'); - @Injectable() export class PromptServices { From 4ad08de5ee359f48ffbd3d2e4c06cc9ffb9aad0e Mon Sep 17 00:00:00 2001 From: Amruth Vamshi Date: Fri, 9 Feb 2024 11:34:46 +0530 Subject: [PATCH 28/28] Revert "added weather classifier" This reverts commit 2679e3a590d8d3ca0e2d68c9f9f3df80ce1db960. --- src/app.controller.ts | 6 +-- src/modules/aiTools/ai-tools.service.ts | 31 --------------- src/xstate/prompt/prompt.machine.ts | 53 +------------------------ src/xstate/prompt/prompt.service.ts | 51 ------------------------ 4 files changed, 3 insertions(+), 138 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 2058936..06e39e2 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -50,8 +50,6 @@ export class PromptDto { conversationId?: string; @IsOptional() identifier?: string; - @IsOptional() - location?: any; } @Controller() @@ -375,9 +373,7 @@ export class AppController { inputLanguage: prompt.inputLanguage, lastAadhaarDigits:'', state:'onGoing', - isOTPVerified: false, - lat: prompt.input?.location?.lat, - long: prompt.input?.location?.long + isOTPVerified: false } let botFlowService = interpret(botFlowMachine.withContext(conversation || defaultContext)).start(); diff --git a/src/modules/aiTools/ai-tools.service.ts b/src/modules/aiTools/ai-tools.service.ts index 7ea7e7d..694a9e3 100644 --- a/src/modules/aiTools/ai-tools.service.ts +++ b/src/modules/aiTools/ai-tools.service.ts @@ -91,8 +91,6 @@ export class AiToolsService { let textArray = text.split("\n") for(let i=0;i event.data.query, @@ -874,53 +872,6 @@ export const botFlowMachine3:any = } } }, - questionClassifierWeather: { - invoke: { - src: "weatherClassifier", - onDone: [ - { - cond: "ifInvalidClassifier", - target: "questionClassifier" - }, - { - target: 'getWeatherInfo' - } - ], - onError: { - target: 'error', - actions: [ - assign({ - error: (_, event) => event.data.message, - type: '' - }) - ] - } - } - }, - getWeatherInfo:{ - invoke: { - src: "getWeatherInfo", - onDone: [ - { - target: 'endFlow', - actions: [ - assign({ - response: (_, event) => event.data, - }) - ] - } - ], - onError: { - target: 'error', - actions: [ - assign({ - error: (_, event) => event.data.message, - type: '' - }) - ] - } - } - }, questionClassifier: { invoke: { src: "questionClassifier", diff --git a/src/xstate/prompt/prompt.service.ts b/src/xstate/prompt/prompt.service.ts index b48f9a9..92cd865 100644 --- a/src/xstate/prompt/prompt.service.ts +++ b/src/xstate/prompt/prompt.service.ts @@ -31,19 +31,6 @@ export class PromptServices { async getInput (context) { return context } - - async weatherClassifier(context) { - try{ - let response: any = await this.aiToolsService.textClassificationForWeather(context.query) - if (response.error) throw new Error(`${response.error}, please try again.`) - if (response == `LABEL_6`) return "weather" - else { - return "invalid" - } - } catch (error){ - return Promise.reject(error) - } - } async questionClassifier (context) { try{ @@ -219,42 +206,6 @@ export class PromptServices { } } - async getWeatherInfo (context) { - try{ - if(!context.lat || !context.long){ - return "Please enable location and try again." - } - var requestOptions: RequestInit= { - method: 'GET', - redirect: 'follow' - }; - - let weather: any = await fetch(`https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/${context.lat},${context.long}?unitGroup=metric&key=${this.configService.get('WEATHER_PROVIDER_API_KEY')}&contentType=json`, requestOptions) - .then(response => response.json()) - .then(result => {return result}) - .catch(error => console.log('error', error)); - let weatherString = '🌦️ *Weather Forecast for the Next 10 Days*'; - weather.days.slice(0,10).forEach((data,index)=>{ - weatherString+=` -*Day ${index+1}:* -- Date: ${data.datetime} -- Temperature: ${data.temp}°C -- Conditions: ${data.conditions} -- Precipitation: ${data.precip*10}% - -` - }) - weatherString+=` -Feel free to reach out if you need more details or have any specific concerns. Happy farming! 🚜🌾 - -The data is shared from https://weather.visualcrossing.com - ` - return weatherString - } catch (error){ - return Promise.reject(error) - } - } - allFunctions() { return { getInput: this.getInput.bind(this), @@ -264,8 +215,6 @@ The data is shared from https://weather.visualcrossing.com validateOTP: this.validateOTP.bind(this), fetchUserData: this.fetchUserData.bind(this), wadhwaniClassifier: this.wadhwaniClassifier.bind(this), - weatherClassifier: this.weatherClassifier.bind(this), - getWeatherInfo: this.getWeatherInfo.bind(this) } }