Skip to content

Commit

Permalink
fix: Continue on fail / error output support for chains and agents (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored Apr 9, 2024
1 parent 8c26225 commit f62800c
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,45 @@ export async function conversationalAgentExecute(

const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let input;

if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
try {
let input;

if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
continue;
}

throw error;
}

returnData.push({ json: response });
}

return await this.prepareOutputData(returnData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,44 @@ export async function openAiFunctionsAgentExecute(

const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let input;
if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
try {
let input;
if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
continue;
}

throw error;
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
}

return await this.prepareOutputData(returnData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,44 @@ export async function planAndExecuteAgentExecute(

const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let input;
if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
try {
let input;
if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
continue;
}

throw error;
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
}

return await this.prepareOutputData(returnData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,45 @@ export async function reActAgentAgentExecute(

const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let input;

if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
try {
let input;

if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('text', itemIndex) as string;
} else {
input = getPromptInputByType({
ctx: this,
i: itemIndex,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
continue;
}

throw error;
}

if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The ‘text‘ parameter is empty.');
}

if (prompt) {
input = (await prompt.invoke({ input })).value;
}

let response = await agentExecutor
.withConfig(getTracingConfig(this))
.invoke({ input, outputParsers });

if (outputParser) {
response = { output: await outputParser.parse(response.output as string) };
}

returnData.push({ json: response });
}

return await this.prepareOutputData(returnData);
Expand Down
Loading

0 comments on commit f62800c

Please sign in to comment.