From fcae920067a41642de6f2da3be66aafb9c3f6d3b Mon Sep 17 00:00:00 2001 From: Mayur Virendra Date: Mon, 8 Apr 2024 18:12:27 +0530 Subject: [PATCH] Added route selection capability --- config/openai.json | 1 + controllers/Bot.js | 2 +- tests/apis/agent.test.js | 57 ++++++++++++++++++++++++++++++ tests/unit/controllers/bot.test.js | 48 +------------------------ 4 files changed, 60 insertions(+), 48 deletions(-) diff --git a/config/openai.json b/config/openai.json index ec28386..422171b 100644 --- a/config/openai.json +++ b/config/openai.json @@ -1,6 +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": "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 c19d10f..52a7a42 100644 --- a/controllers/Bot.js +++ b/controllers/Bot.js @@ -174,7 +174,7 @@ async function process_text(req, res) { } else if(ai.action?.action === 'get_routes'){ const routes = await mapService.generate_routes(message, session.text); - const formatting_response = await ai.format_response(routes.data?.routes_formatted || routes.errors, [...session.actions.formatted, { role: 'user', content: message },...session.text]); + const formatting_response = await ai.format_response(routes.data?.routes_formatted || routes.errors, [{ role: 'user', content: message },...session.text]); response.formatted = formatting_response.message; session.routes = routes.data?.routes || session.routes; logger.info(`AI response: ${response.formatted}`); diff --git a/tests/apis/agent.test.js b/tests/apis/agent.test.js index 904003a..af7f038 100644 --- a/tests/apis/agent.test.js +++ b/tests/apis/agent.test.js @@ -3,8 +3,14 @@ import app from '../../server.js' import request from 'supertest' import * as chai from 'chai' import logger from '../../utils/logger.js' +import DBService from '../../services/DBService.js' const expect = chai.expect +beforeEach(async () => { + // Reset all sessions + const db = new DBService() + await db.clear_all_sessions() +}) describe.skip('API tests for /webhook endpoint for an end to end search > select > init > confirm use case', () => { it('Should test succesful search response using /webhook endpoint', async () => { @@ -208,4 +214,55 @@ describe.skip('Test cases for booking collection', ()=>{ expect(response.status).equal(200); } }) +}) + +describe('test cases for generating routes', ()=>{ + it('Should share routes when asked to share routes.', async () => { + const ask = "Can you get routes from Denver to Yellowstone national park?"; + const response = await request(app).post('/webhook').send({ + "From": process.env.TEST_RECEPIENT_NUMBER, + "Body": ask + }) + logger.info(JSON.stringify(response.text, null, 2)); + expect(response.status).to.be.eq(200) + + }) + + it('Should come back asking for more details.', async () => { + const ask = "Can you get routes to Yellowstone national park?"; + const response = await request(app).post('/webhook').send({ + "From": process.env.TEST_RECEPIENT_NUMBER, + "Body": ask + }) + logger.info(response.text); + expect(response.status).to.be.eq(200) + + }) +}) + +describe('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?"; + const response = await request(app).post('/webhook').send({ + "From": process.env.TEST_RECEPIENT_NUMBER, + "Body": ask + }) + logger.info(JSON.stringify(response.text, null, 2)); + expect(response.status).to.be.eq(200) + + }) + + it('Should share routes when asked to share routes.', async () => { + const ask = "Lets select the first one."; + const response = await request(app).post('/webhook').send({ + "From": process.env.TEST_RECEPIENT_NUMBER, + "Body": ask + }) + logger.info(JSON.stringify(response.text, null, 2)); + expect(response.status).to.be.eq(200) + + }) + + }) \ No newline at end of file diff --git a/tests/unit/controllers/bot.test.js b/tests/unit/controllers/bot.test.js index 3b3247e..7058b4e 100644 --- a/tests/unit/controllers/bot.test.js +++ b/tests/unit/controllers/bot.test.js @@ -8,8 +8,6 @@ const expect = chai.expect const mapService = new MapsService() const ai = new AI(); const actionsService = new ActionService() -import request from 'supertest' -import app from '../../../server.js' describe.only('Test cases for AI', () => { it('Should return message with location polygon', async () => { @@ -151,50 +149,6 @@ describe.only('Test cases for Google maps', () => { const route_image = `https://maps.googleapis.com/maps/api/staticmap?size=300x300${polygon_path}${markers_path}&key=${process.env.GOOGLE_MAPS_API_KEY}`; logger.info(`route_image: ${route_image}`); expect(routes).to.be.an('array'); - }) - - it('It should share a set of routes when given an instruction.', async () => { - const ask = "Can you plan a trip from Denver to Yellowstone national park?"; - - // generate routes - const routesResponse = await mapService.generate_routes(ask); - expect(routesResponse).to.have.property('status'); - expect(routesResponse).to.have.property('data'); - expect(routesResponse.status).to.be.true; - expect(routesResponse.data.routes).to.be.an('array').that.is.not.empty; - }) - - it('It should ask for details when given an incomplete instruction.', async () => { - const ask = "Can you plan a trip to Yellowstone national park?"; - - // generate routes - const routesResponse = await mapService.generate_routes(ask); - expect(routesResponse).to.have.property('status'); - expect(routesResponse).to.have.property('errors'); - expect(routesResponse.status).to.be.false; - expect(routesResponse.errors).to.be.an('array').that.is.not.empty; - }) - - it('Should share routes when asked to share routes.', async () => { - const ask = "Can you get routes from Denver to Yellowstone national park?"; - const response = await request(app).post('/webhook').send({ - "From": process.env.TEST_RECEPIENT_NUMBER, - "Body": ask - }) - logger.info(JSON.stringify(response.text, null, 2)); - expect(response.status).to.be.eq(200) - - }) - - it('Should come back asking for more details.', async () => { - const ask = "Can you get routes to Yellowstone national park?"; - const response = await request(app).post('/webhook').send({ - "From": process.env.TEST_RECEPIENT_NUMBER, - "Body": ask - }) - logger.info(response.text); - expect(response.status).to.be.eq(200) - - }) + }) })