refactor(ChatAgent.js, handlers.js): improve logging format and add support for functionsAgent

- Improve logging format in ChatAgent.js by adding more details to the log
- Add support for functionsAgent in ChatAgent.js to format the log differently
- Improve formatAction function in handlers.js to handle empty thoughts and add support for functionsAgent
This commit is contained in:
Daniel Avila 2023-06-14 07:23:11 -04:00 committed by Danny Avila
parent 6e183b91e1
commit 71d812403e
2 changed files with 16 additions and 5 deletions

View file

@ -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;

View file

@ -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;