From 9c452fa8603afbd690285a4c13f64dd6c74bc16a Mon Sep 17 00:00:00 2001 From: Mayur Virendra Date: Mon, 8 Apr 2024 19:00:41 +0530 Subject: [PATCH] Added issing condition and direction url --- config/openai.json | 2 +- controllers/Bot.js | 19 +++++++++++++++++++ services/MapService.js | 9 ++++++++- tests/apis/agent.test.js | 8 ++++---- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/config/openai.json b/config/openai.json index 422171b..5623c92 100644 --- a/config/openai.json +++ b/config/openai.json @@ -1,7 +1,7 @@ { "SUPPORTED_ACTIONS": [ { "action": "get_routes", "description": "If the user has requested for routes for a travel plan between two places or asked to plan a trip." }, - { "action": "select_route", "description": "If the user selects one of the routes from one of the routes shared earlier by the assistant." }, + { "action": "select_route", "description": "If the user selects one of the routes from the routes shared by the assistant." }, { "action": "search", "description": "If the user clearly indicates to perform a search for a specific product. Sample instructions : 'find a hotel', 'find an ev charger', 'find tickets'" }, { "action": "select", "description": "If the user likes or selects any item, this action should be used. This action can only be called if a search has been called before." }, { "action": "init", "description": "If the user wants to place an order after search and select and has shared the billing details. This action can only be called if a select has been called before." }, diff --git a/controllers/Bot.js b/controllers/Bot.js index 52a7a42..4074c32 100644 --- a/controllers/Bot.js +++ b/controllers/Bot.js @@ -182,6 +182,25 @@ async function process_text(req, res) { session.text.push({ role: 'user', content: message }); session.text.push({ role: 'assistant', content: response.formatted }); } + else if(ai.action?.action === 'select_route'){ + const details_response = await ai.get_details_by_description(`Get the index of selected item based this input from user : ${message}`, [], `{index:1}`); + let route_response = { + message: '' + } + if(details_response && details_response.index){ + session.selected_route = session.routes[details_response.index]; + const url = `https://www.google.com/maps/dir/${session.selected_route.source_gps.lat},${session.selected_route.source_gps.lng}/${session.selected_route.destination_gps.lat},${session.selected_route.destination_gps.lng}/`; + route_response.message = `Your route has been actived. Here is the link to navigate : ${url}. What do you want to do next?`; + + } + const formatting_response = await ai.format_response(route_response, [{ role: 'user', content: message },...session.text]); + response.formatted = formatting_response.message; + + logger.info(`AI response: ${response.formatted}`); + + session.text.push({ role: 'user', content: message }); + session.text.push({ role: 'assistant', content: response.formatted }); + } else if(ai.action?.action == null) { // get ai response diff --git a/services/MapService.js b/services/MapService.js index 3b0a56c..0a15223 100644 --- a/services/MapService.js +++ b/services/MapService.js @@ -94,7 +94,14 @@ class MapsService { } else{ // generate routes - response.data.routes = await this.getRoutes(source_gps, destination_gps); + const routes = await this.getRoutes(source_gps, destination_gps); + response.data.routes = routes.map(route=>{ + return { + ...route, + source_gps: source_gps, + destination_gps: destination_gps + } + }) response.data.routes_formatted = { "description": `these are the various routes that you can take. Which one would you like to select:`, "routes": response.data.routes.map((route, index) => `Route ${index+1}: ${route.summary}`) diff --git a/tests/apis/agent.test.js b/tests/apis/agent.test.js index af7f038..16daca0 100644 --- a/tests/apis/agent.test.js +++ b/tests/apis/agent.test.js @@ -6,7 +6,7 @@ import logger from '../../utils/logger.js' import DBService from '../../services/DBService.js' const expect = chai.expect -beforeEach(async () => { +before(async () => { // Reset all sessions const db = new DBService() await db.clear_all_sessions() @@ -240,7 +240,7 @@ describe('test cases for generating routes', ()=>{ }) }) -describe('test cases for generating routes and selecting a route', ()=>{ +describe.only('test cases for generating routes and selecting a route', ()=>{ it('Should share routes when asked to share routes.', async () => { const ask = "Can you get routes from Denver to Yellowstone national park?"; @@ -253,8 +253,8 @@ describe('test cases for generating routes and selecting a route', ()=>{ }) - it('Should share routes when asked to share routes.', async () => { - const ask = "Lets select the first one."; + it('should select a route and share directions.', async () => { + const ask = "Lets select the first route."; const response = await request(app).post('/webhook').send({ "From": process.env.TEST_RECEPIENT_NUMBER, "Body": ask