diff --git a/api/app/langchain/ChatAgent.js b/api/app/langchain/ChatAgent.js index 1f255425d2..9322000223 100644 --- a/api/app/langchain/ChatAgent.js +++ b/api/app/langchain/ChatAgent.js @@ -50,7 +50,11 @@ class ChatAgent { let output = 'Internal thoughts & actions taken:\n"'; let actions = input || this.actions; - if (actions[0]?.action) { + if (actions[0]?.action && this.functionsAgent) { + actions = actions.map((step) => ({ + log: `Action: ${step.action?.tool || ''}\nInput: ${step.action?.toolInput?.input || ''}\nObservation: ${step.observation}` + })); + } else if (actions[0]?.action) { actions = actions.map((step) => ({ log: `${step.action.log}\nObservation: ${step.observation}` })); @@ -469,7 +473,7 @@ Only respond with your conversational reply to the following User Message: console.debug(this.tools.map((tool) => tool.name)); } - if (this.tools.length > 0) { + if (this.tools.length > 0 && !this.functionsAgent) { this.tools.push(new SelfReflectionTool({ message, isGpt3: false })); } else if (this.tools.length === 0) { return; diff --git a/api/server/routes/ask/handlers.js b/api/server/routes/ask/handlers.js index 481169d29e..925807e665 100644 --- a/api/server/routes/ask/handlers.js +++ b/api/server/routes/ask/handlers.js @@ -91,6 +91,9 @@ const handleText = async (response, bing = false) => { return text; }; +const isObject = (item) => item && typeof item === 'object' && !Array.isArray(item); +const getString = (input) => isObject(input) ? JSON.stringify(input) : input ; + function formatSteps(steps) { let output = ''; @@ -103,7 +106,7 @@ function formatSteps(steps) { continue; } - output += `Input: ${actionInput}\nOutput: ${observation}`; + output += `Input: ${actionInput?.input || actionInput}\nOutput: ${getString(observation)}`; if (steps.length > 1 && i !== steps.length - 1) { output += '\n---\n'; @@ -128,12 +131,14 @@ function formatAction(action) { const formattedAction = { plugin: capitalizeWords(action.tool) || action.tool, - input: action.toolInput, + input: action.toolInput?.input || action.toolInput, thought: action.log.includes('Thought: ') ? action.log.split('\n')[0].replace('Thought: ', '') : action.log.split('\n')[0] }; + formattedAction.thought = getString(formattedAction.thought); + if (action.tool.toLowerCase() === 'self-reflection' || formattedAction.plugin === 'N/A') { formattedAction.inputStr = `{\n\tthought: ${formattedAction.input}${ !formattedAction.thought.includes(formattedAction.input) @@ -142,7 +147,9 @@ function formatAction(action) { }\n}`; formattedAction.inputStr = formattedAction.inputStr.replace('N/A - ', ''); } else { - formattedAction.inputStr = `{\n\tplugin: ${formattedAction.plugin}\n\tinput: ${formattedAction.input}\n\tthought: ${formattedAction.thought}\n}`; + const hasThought = formattedAction.thought.length > 0; + const thought = hasThought ? `\n\tthought: ${formattedAction.thought}` : ''; + formattedAction.inputStr = `{\n\tplugin: ${formattedAction.plugin}\n\tinput: ${formattedAction.input}\n${thought}}`; } return formattedAction;