Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Added new structure with function calling to reduce flakiness and reduce transaction time #81

Merged
merged 11 commits into from
May 3, 2024
Prev Previous commit
Next Next commit
- Added searching along the route polyline
mayurvir committed Apr 16, 2024
commit bdd132372f2097d0640210caa392f9be942a1ffb
2 changes: 1 addition & 1 deletion config/registry.json
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
"rules": [
"item.descriptor should not be used in search intent for this domain",
"search should have fulfillment for this domain. fulfillment should only contain location for this domain.",
"fulfillment should contain only 1 stop for this domain",
"fulfillment must have only 1 stop for this domain",
"If a route polygon has been shared, fulfillment.stops[i].location should have the polygon field."
]
},
4 changes: 2 additions & 2 deletions schemas/jsons/search.js
Original file line number Diff line number Diff line change
@@ -72,10 +72,10 @@ export default {
},
polygon: {
type: "string",
description: "This describes the route on which the item needs to be searched. This should be used if the request is to search along a route"
description: "This describes the route on which the item needs to be searched. This should be used if the instruction is to search along a route and there is a selelected route in user profile."
}
},
required: ['gps']
required: []
},
time: {
type: "object",
44 changes: 35 additions & 9 deletions services/AI.js
Original file line number Diff line number Diff line change
@@ -740,6 +740,14 @@ class AI {
]
}
}

// Add profile context
let profile_context = [];
if(this.session?.profile){
profile_context = [
{ role: 'system', content: `User profile : ${JSON.stringify(this.session.profile)}` }
]
}

const schema = BECKN_ACTIONS[action]['schema'];

@@ -761,21 +769,17 @@ class AI {
messages: [
...domain_context,
...last_action_context,
...profile_context,
...messages],
tools: tools,
tool_choice: "auto", // auto is default, but we'll be explicit
});
const responseMessage = JSON.parse(response.choices[0].message?.tool_calls[0]?.function?.arguments) || null;
let responseMessage = JSON.parse(response.choices[0].message?.tool_calls[0]?.function?.arguments) || null;

responseMessage = await this._cleanup_beckn_message(action, responseMessage);

logger.verbose(`Got beckn message from instruction : ${JSON.stringify(responseMessage)}`);

// update message for init and confirm. Cleanup incorrect `items` for init and incorrect `items` and `billing` details for confirm
if((action=='init' || action=='confirm') && this.session?.beckn_transaction?.responses[this.session?.profile?.last_action]){
responseMessage.order = {
...responseMessage.order,
...this.session?.beckn_transaction?.responses[this.session?.profile?.last_action]?.message?.order
}

}

return responseMessage
}
@@ -785,6 +789,27 @@ class AI {
}
}

async _cleanup_beckn_message(action, message){
// cleanup polygon
if(action=='search' && message.intent.fulfillment.stops){
for(let stop of message.intent.fulfillment.stops){
if(stop.location?.polygon){
delete stop.location?.gps;
}
}
}

// update message for init and confirm. Cleanup incorrect `items` for init and incorrect `items` and `billing` details for confirm
if((action=='init' || action=='confirm') && this.session?.beckn_transaction?.responses[this.session?.profile?.last_action]){
message.order = {
...message.order,
...this.session?.beckn_transaction?.responses[this.session?.profile?.last_action]?.message?.order
}
}

return message;
}

async getResponseFromOpenAI(messages){

const context = [
@@ -812,6 +837,7 @@ class AI {
messages.push(responseMessage);

for (let tool of toolCalls) {
logger.warn(`Executing tool : ${tool.function.name} ...`);
const parameters = JSON.parse(tool.function.arguments);
const functionToCall = this.tools[tool.function.name];
if (functionToCall) {
8 changes: 5 additions & 3 deletions services/MapService.js
Original file line number Diff line number Diff line change
@@ -71,9 +71,11 @@ class MapsService {
}

async selectRoute(index) {
logger.info(`Selecting route ${index}`);
if (this.session.routes && index >= 0 && index < this.session.routes.length) {
this.session.profile.selected_route = this.session.routes[index];
logger.info(`Selecting route ${index.index}`);
if (this.session.routes && index.index >= 0 && index.index < this.session.routes.length) {
this.session.profile.selected_route = {
polyline: this.session.routes[index.index].overview_polyline.points
}
return true;
} else {
return false;