From 6cdc0ecdf6d8d308bffefd228629fa5dcfd6ce41 Mon Sep 17 00:00:00 2001 From: Mayur Virendra Date: Sat, 30 Mar 2024 08:52:07 +0530 Subject: [PATCH] Fixes made for general queries --- controllers/Bot.js | 15 +++++++++------ services/AI.js | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/controllers/Bot.js b/controllers/Bot.js index 57e03fe..4bd24e1 100644 --- a/controllers/Bot.js +++ b/controllers/Bot.js @@ -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); } diff --git a/services/AI.js b/services/AI.js index 9b22036..4defc64 100644 --- a/services/AI.js +++ b/services/AI.js @@ -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} ] @@ -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 = { @@ -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); @@ -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; }