Skip to content

Commit

Permalink
Added re-routing by avoiding certain exxception point
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurvir committed Apr 8, 2024
1 parent c44a05e commit c584cb0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/openai.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"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": "get_routes", "description": "If the user has requested for routes for a travel plan between two places or asked to plan a trip. If the assistant has suggested to re-route in the last message and asked user to share current location, it should be a get_routes." },
{ "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." },
Expand Down
2 changes: 1 addition & 1 deletion controllers/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async function process_text(req, res) {
response.formatted = 'Session & profile cleared! You can start a new session now.';
}
else if(ai.action?.action === 'get_routes'){
const routes = await mapService.generate_routes(message, session.text);
const routes = await mapService.generate_routes(message, session.text, session.avoid_point|| []);
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;
Expand Down
6 changes: 4 additions & 2 deletions controllers/ControlCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,23 @@ export const notify = async (req, res) => {
// send whatsapp and add to context
if(status){
try{
const reply_message = `${message}. Do you want to find alternate routes?`
const reply_message = `${message}. Please share your current location and I'll try to find some alternate routes?`
await action.send_message(session.key, reply_message);

// update session
session.data.avoid_point = point;
if(!session.data.text) session.data.text=[]
session.data.text.push({role: 'assistant', content: reply_message});

await db.update_session(session.key, session);
await db.update_session(session.key, session.data);
}
catch(e){
logger.error(e);
}
}
}
}
res.send("Triggered!")
}
else res.status(400).send('Point and message are required in the body.')
}
35 changes: 26 additions & 9 deletions services/MapService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MapsService {
this.client = new Client({});
}

async getRoutes(source, destination) {
async getRoutes(source, destination, avoidPoint=[]) {
try {
const response = await this.client.directions({
params: {
Expand All @@ -20,7 +20,17 @@ class MapsService {
alternatives: true
}
});
return response.data.routes;
let routes= [];
for(const route of response.data.routes){
const status = await this.checkGpsOnPolygon(avoidPoint, route.overview_polyline.points)
if(!status) routes.push(route)
}

const path = this.get_static_image_path(routes);
logger.info(`Static image path for routes: ${path}`);


return routes;
} catch (error) {
logger.error(error);
return [];
Expand Down Expand Up @@ -105,14 +115,10 @@ class MapsService {
}
})

let polygon_path = '';
routes.forEach((route, index) => {
polygon_path+=`&path=color:${this.get_random_color()}|weight:${5-index}|enc:${route.overview_polyline.points}`;
})
// print path
const path = this.get_static_image_path(routes)
logger.info(`Route image path : ${path}`)

const route_image = `https://maps.googleapis.com/maps/api/staticmap?size=300x300${polygon_path}&key=${process.env.GOOGLE_MAPS_API_KEY}`;
logger.info(`Map url :${route_image}`)

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}`)
Expand Down Expand Up @@ -181,6 +187,17 @@ class MapsService {

return Math.abs(deltaXt) < tolerance;
}

get_static_image_path(routes){
let polygon_path = '';
routes.forEach((route, index) => {
polygon_path+=`&path=color:${this.get_random_color()}|weight:${5-index}|enc:${route.overview_polyline.points}`;
})

const route_image = `https://maps.googleapis.com/maps/api/staticmap?size=300x300${polygon_path}&key=${process.env.GOOGLE_MAPS_API_KEY}`;
return route_image;

}
}

export default MapsService;
8 changes: 7 additions & 1 deletion tests/unit/controllers/controlCenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('API tests for /update-catalog endpoint for an end to end Notify Messag
})

describe('API tests for triggering a roadblock', ()=>{
it.only('Should trigger a roadblock on a selected route', async ()=>{
it('Should trigger a roadblock on a selected route', async ()=>{
const ask1 = "Can you get routes from Denver to Yellowstone national park?";
await request(app).post('/webhook').send({
"From": process.env.TEST_RECEPIENT_NUMBER,
Expand All @@ -112,6 +112,12 @@ describe('API tests for triggering a roadblock', ()=>{
"point":[39.7408351, -104.9874105],
"message": "There is a roadblock on your selected route due to an accident!"
})

const ask3 = "I'm near Glendo";
await request(app).post('/webhook').send({
"From": process.env.TEST_RECEPIENT_NUMBER,
"Body": ask3
})

expect(response.status).equal(200)
})
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/services/maps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Should test the map service', () => {

it('should return GPS coordinates for a valid address', async () => {

const gpsCoordinates = await mapService.lookupGps('1600 Amphitheatre Parkway, Mountain View, CA');
const gpsCoordinates = await mapService.lookupGps('Yellowstone national park');
expect(gpsCoordinates).to.be.an('object');
expect(gpsCoordinates).to.have.property('lat');
expect(gpsCoordinates).to.have.property('lng');
Expand All @@ -34,6 +34,12 @@ describe('Should test the map service', () => {
expect(status).to.be.true;
})

it('Should return path avoiding certail points', async ()=>{
const source ='39.7392358,-104.990251';
const destination = '44.427963, -110.588455';
const pointBeforeCasper = [42.839531, -106.136404];
await mapService.getRoutes(source, destination, pointBeforeCasper);
})

});

0 comments on commit c584cb0

Please sign in to comment.