Skip to content

Commit

Permalink
Updated test cases, added collection detection to action detection fu…
Browse files Browse the repository at this point in the history
…nction, fixes
  • Loading branch information
mayurvir committed Apr 6, 2024
1 parent bfb9269 commit 310503e
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 121 deletions.
7 changes: 4 additions & 3 deletions config/openai.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"SUPPORTED_ACTIONS": [
{ "action": "search", "description": "Perform a search for a service or product. If a service or product is not specified, its not a search. Listing all bookings is not a search." },
{ "action": "search", "description": "If the user intends to perform a search for a product." },
{ "action": "select", "description": "If the user likes or selects any item, this action should be used." },
{ "action": "init", "description": "If the user wants to place an order after search and select and has shared the billing details." },
{ "action": "confirm", "description": "Confirm an order. This action gets called when users confirms an order." },
{ "action": "clear_chat", "description": "If the user wants to clear the session or restart session or chat." },
{ "action": "clear_all", "description": "If the user wants to clear the complete session or the profile." }
{ "action": "clear_chat", "description": "If the user wants to clear the chat." },
{ "action": "clear_all", "description": "If the user wants to clear the session." },
{ "action": "booking_collection", "description": "If the assistant had shared a plan or initinerary and user has indicated to make the bookings for that plan." }
],
"SCHEMA_TRANSLATION_CONTEXT": [
{ "role": "system", "content": "Your job is to identify the endpoint, method and request body from the given schema, based on the last user input and return the extracted details in the following JSON structure : \n\n {'url':'', 'method':'', 'body':''}'"},
Expand Down
15 changes: 14 additions & 1 deletion config/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@
],
"rules": [
"search must have two stops for this domain.",
"Supported stop.type : check-in, check-out"
"Supported stop.type : check-in, check-out",
"fulfillment stops should not have location for this domain."
]
},
"tourism": {
"tags": [],
"rules":[
"Tags and fulfillment should not be used for this domain"
]
},
"retail:1.1.0": {
"tags": [],
"rules":[
"Tags and fulfillment should not be used for this domain"
]
}
}
Expand Down
68 changes: 36 additions & 32 deletions controllers/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,36 +147,31 @@ async function process_text(req, res) {
session = EMPTY_SESSION;
response.formatted = 'Session & profile cleared! You can start a new session now.';
}
else if(ai.action?.action == null) {
let booking_collection_yn = await ai.check_if_booking_collection(message, session.text);
if(booking_collection_yn){
logger.info(`Booking collection detected: ${booking_collection_yn}`);

response.formatted = await ai.get_ai_response_to_query('Share the list of bookings to be made? Please include only hotels and tickets to be booked. It should be a short list with just names of bookings to be made. For e.g. Here is a list of bookings you need to make: \n1. hotel at xyz \n2. Tickets for abc \nWhich one do you want to search first?', session.text);
logger.info(`AI response: ${response.formatted}`);

ai.bookings = await ai.get_bookings_array_from_text(response.formatted);
ai.bookings = ai.bookings.bookings || ai.bookings;
ai.bookings && ai.bookings.map(booking =>{
booking.transaction_id = uuidv4();
})
else if(ai.action?.action === 'booking_collection'){
response.formatted = await ai.get_ai_response_to_query('Share the list of bookings to be made? Please include only hotels and tickets to be booked. It should be a short list with just names of bookings to be made. For e.g. Here is a list of bookings you need to make: \n1. hotel at xyz \n2. Tickets for abc \nWhich one do you want to search first?', session.text);
logger.info(`AI response: ${response.formatted}`);

ai.bookings = await ai.get_bookings_array_from_text(response.formatted);
ai.bookings = ai.bookings.bookings || ai.bookings;
ai.bookings && ai.bookings.map(booking =>{
booking.transaction_id = uuidv4();
})

session.text.push({ role: 'user', content: message });
session.text.push({ role: 'assistant', content: response.formatted });
}
else{
// get ai response
response.formatted = await ai.get_ai_response_to_query(message, session.text);
logger.info(`AI response: ${response.formatted}`);

session.text.push({ role: 'user', content: message });
session.text.push({ role: 'assistant', content: 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
response.formatted = await ai.get_ai_response_to_query(message, session.text);
logger.info(`AI response: ${response.formatted}`);

session.text.push({ role: 'user', content: message });
session.text.push({ role: 'assistant', content: response.formatted });
}
else{

session.bookings = ai.bookings;
response = await process_action(ai.action, message, session, sender);
response = await process_action(ai.action, message, session, sender, format);
ai.bookings = response.bookings;

// update actions
Expand Down Expand Up @@ -225,7 +220,7 @@ async function process_text(req, res) {
* @param {*} session
* @returns
*/
async function process_action(action, text, session, sender=null){
async function process_action(action, text, session, sender=null, format='application/json'){
let ai = new AI();
let response = {
raw: null,
Expand All @@ -236,7 +231,7 @@ async function process_action(action, text, session, sender=null){
ai.action = action;
ai.bookings = session.bookings;

actionsService.send_message(sender, `_Please wait while we process your request through open networks..._`)
format!='application/json' && actionsService.send_message(sender, `_Please wait while we process your request through open networks..._`)

// Get schema
const schema = await ai.get_schema_by_action(action.action);
Expand All @@ -249,7 +244,16 @@ async function process_action(action, text, session, sender=null){
if(schema && beckn_context){
let request=null;
if(ai.action.action==='search'){
const message = await ai.get_beckn_message_from_text(text, session.text, beckn_context.domain);
let search_context = [
...session.text.slice(-1)
];
if(session.profile){
search_context=[
{ role: 'system', content: `User pforile: ${JSON.stringify(session.profile)}`},
...search_context
]
}
const message = await ai.get_beckn_message_from_text(text, search_context, beckn_context.domain);
request = {
status: true,
data:{
Expand All @@ -263,13 +267,13 @@ async function process_action(action, text, session, sender=null){
}
}
else{
request = await ai.get_beckn_request_from_text(text, session.actions.raw, beckn_context, schema);
request = await ai.get_beckn_request_from_text(text, [...session.actions.raw.slice(-1)], beckn_context, schema);
}

if(request.status){
// call api
const api_response = await actionsService.call_api(request.data.url, request.data.method, request.data.body, request.data.headers)
actionsService.send_message(sender, `_Your request is processed, generating a response..._`)
format!='application/json' && actionsService.send_message(sender, `_Your request is processed, generating a response..._`)
if(!api_response.status){
response.formatted = `Failed to call the API: ${api_response.error}`
}
Expand All @@ -289,7 +293,7 @@ async function process_action(action, text, session, sender=null){
}
ai.bookings = response.bookings;

const formatted_response = await ai.get_text_from_json(
const formatted_response = await ai.format_response(
api_response.data,
[...session.actions.formatted, { role: 'user', content: text }],
session.profile
Expand All @@ -301,7 +305,7 @@ async function process_action(action, text, session, sender=null){

}
else{
response.formatted = "Could not prepare this request. Can you please try something else?"
response.formatted = "Could not process this request. Can you please try something else?"
}
}

Expand Down
13 changes: 13 additions & 0 deletions schemas/jsons/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
type: "object",
properties: {
action:{
type:"string",
description: "action that the user wants to perform. This should be one of th actions defined by supported actions. If its not one of teh actions, its value should be null."
},
transaction_id:{
type:"string",
description: "Transaction id of the booking to be performed from the given list of bookings. It should not be set if th action is not from one of the bookings. It shold only be used when the action is 'search'"
}
}
}
Loading

0 comments on commit 310503e

Please sign in to comment.