Skip to content

Commit

Permalink
Fixes made for general queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurvir committed Mar 30, 2024
1 parent 21b9424 commit 6cdc0ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
15 changes: 9 additions & 6 deletions controllers/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ async function process_wa_webhook(req, res) {
}
}


logger.info(`Received message from ${sender}: ${message}`)

// Process instruction
const process_response = await actionsService.process_instruction(message, session.data)

if(process_response.formatted && process_response.raw && typeof process_response.raw === 'object'){
if(process_response.formatted){
session.data.push({ role: 'user', content: message }); // add user message to session
session.data.push({ role: 'assistant', content: JSON.stringify(process_response.raw) }); // add system response to session
if(process_response.raw && typeof process_response.raw === 'object'){
session.data.push({ role: 'assistant', content: JSON.stringify(process_response.raw) }); // add system response to session
}
else{
session.data.push({ role: 'assistant', content: process_response.formatted }); // add system response to session
}

await db.update_session(sender, session);
}

Expand Down
34 changes: 30 additions & 4 deletions services/AI.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class AI {
* @param {*} text
* @returns
*/
async get_beckn_action_from_text(text){
async get_beckn_action_from_text(text, context=[]){
const openai_messages = [
{ role: 'system', content: `Your job is to analyse the text input given by user and identify if that is an action based on given set of actions. The supported actions with their descriptions are : ${JSON.stringify(openai_config.SUPPORTED_ACTIONS)}.` },
{ role: 'system', content: `You must return a json in the following format {'action':'SOME_ACTION_OR_NULL', 'response': 'Should be reponse based on the query.'}` },
{ role: 'system', content: `If the instruction is an action, the action key should be set under 'action' otehrwise action should be null and response should contain completion for the given text.` },
{ role: 'system', content: `A typical order flow should be search > select > init > confirm.`},
{ role: 'system', content: `If you are asked to prepare an itinery or plan a trip, always ask for user preferences such as accommodation types, journey details, dietary preferences, things of interest, journey dates, journey destination, number of members, special requests.` },
{ role: 'system', content: `Following is the context history for reference.` },
...context,
{ role: 'user', content: text}
]

Expand All @@ -50,6 +51,30 @@ class AI {
}
return response;
}

async get_ai_response_to_query(instruction, context=[]){
const openai_messages = [
{ role: 'system', content: 'If you are asked to prepare an itinery or plan a trip, always ask for user preferences such as accommodation types, journey details, dietary preferences, things of interest, journey dates, journey destination, number of members, special requests.'},
...context,
{ role: 'user', content: instruction}
]

let response = {
action: null,
response: null
}
try{
const completion = await openai.chat.completions.create({
messages: openai_messages,
model: process.env.OPENAI_MODEL_ID
})
response = completion.choices[0].message.content;
}
catch(e){
logger.error(e);
}
return response;
}

async get_beckn_request_from_text(instruction, context=[]){
let action_response = {
Expand Down Expand Up @@ -157,7 +182,7 @@ class AI {
message : null
}

const action = await this.get_beckn_action_from_text(instruction);
const action = await this.get_beckn_action_from_text(instruction, context);
logger.info(`Got action from instruction : ${JSON.stringify(action)}`)
if(action?.action){
response.data.config = await this._get_config_by_action(action.action, instruction, context);
Expand Down Expand Up @@ -199,7 +224,8 @@ class AI {
}
}
else{
response = {...response, message: action.response}
const ai_response = await this.get_ai_response_to_query(instruction, context);
response = {...response, message: ai_response}
}
return response;
}
Expand Down

0 comments on commit 6cdc0ec

Please sign in to comment.