fix: Continue on fail / error output support for chains and agents (#9078)

This commit is contained in:
Michael Kret
2024-04-09 15:06:12 +03:00
committed by GitHub
parent 8c2622549b
commit f62800cd72
9 changed files with 469 additions and 388 deletions

View File

@@ -79,36 +79,45 @@ export async function conversationalAgentExecute(
const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let input;
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 (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);

View File

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

View File

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

View File

@@ -80,36 +80,45 @@ export async function reActAgentAgentExecute(
const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let input;
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 (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);

View File

@@ -41,106 +41,115 @@ export async function sqlAgentAgentExecute(
const returnData: INodeExecutionData[] = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
let input;
if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('input', i) as string;
} else {
input = getPromptInputByType({
ctx: this,
i,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}
try {
const item = items[i];
let input;
if (this.getNode().typeVersion <= 1.2) {
input = this.getNodeParameter('input', i) as string;
} else {
input = getPromptInputByType({
ctx: this,
i,
inputKey: 'text',
promptTypeKey: 'promptType',
});
}
if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The prompt parameter is empty.');
}
if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The prompt parameter is empty.');
}
const options = this.getNodeParameter('options', i, {});
const selectedDataSource = this.getNodeParameter('dataSource', i, 'sqlite') as
| 'mysql'
| 'postgres'
| 'sqlite';
const options = this.getNodeParameter('options', i, {});
const selectedDataSource = this.getNodeParameter('dataSource', i, 'sqlite') as
| 'mysql'
| 'postgres'
| 'sqlite';
const includedSampleRows = options.includedSampleRows as number;
const includedTablesArray = parseTablesString((options.includedTables as string) ?? '');
const ignoredTablesArray = parseTablesString((options.ignoredTables as string) ?? '');
const includedSampleRows = options.includedSampleRows as number;
const includedTablesArray = parseTablesString((options.includedTables as string) ?? '');
const ignoredTablesArray = parseTablesString((options.ignoredTables as string) ?? '');
let dataSource: DataSource | null = null;
if (selectedDataSource === 'sqlite') {
if (!item.binary) {
let dataSource: DataSource | null = null;
if (selectedDataSource === 'sqlite') {
if (!item.binary) {
throw new NodeOperationError(
this.getNode(),
'No binary data found, please connect a binary to the input if you want to use SQLite as data source',
);
}
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i, 'data');
dataSource = await getSqliteDataSource.call(this, item.binary, binaryPropertyName);
}
if (selectedDataSource === 'postgres') {
dataSource = await getPostgresDataSource.call(this);
}
if (selectedDataSource === 'mysql') {
dataSource = await getMysqlDataSource.call(this);
}
if (!dataSource) {
throw new NodeOperationError(
this.getNode(),
'No binary data found, please connect a binary to the input if you want to use SQLite as data source',
'No data source found, please configure data source',
);
}
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i, 'data');
dataSource = await getSqliteDataSource.call(this, item.binary, binaryPropertyName);
}
const agentOptions: SqlCreatePromptArgs = {
topK: (options.topK as number) ?? 10,
prefix: (options.prefixPrompt as string) ?? SQL_PREFIX,
suffix: (options.suffixPrompt as string) ?? SQL_SUFFIX,
inputVariables: ['chatHistory', 'input', 'agent_scratchpad'],
};
if (selectedDataSource === 'postgres') {
dataSource = await getPostgresDataSource.call(this);
}
if (selectedDataSource === 'mysql') {
dataSource = await getMysqlDataSource.call(this);
}
if (!dataSource) {
throw new NodeOperationError(
this.getNode(),
'No data source found, please configure data source',
);
}
const agentOptions: SqlCreatePromptArgs = {
topK: (options.topK as number) ?? 10,
prefix: (options.prefixPrompt as string) ?? SQL_PREFIX,
suffix: (options.suffixPrompt as string) ?? SQL_SUFFIX,
inputVariables: ['chatHistory', 'input', 'agent_scratchpad'],
};
const dbInstance = await SqlDatabase.fromDataSourceParams({
appDataSource: dataSource,
includesTables: includedTablesArray.length > 0 ? includedTablesArray : undefined,
ignoreTables: ignoredTablesArray.length > 0 ? ignoredTablesArray : undefined,
sampleRowsInTableInfo: includedSampleRows ?? 3,
});
const toolkit = new SqlToolkit(dbInstance, model);
const agentExecutor = createSqlAgent(model, toolkit, agentOptions);
const memory = (await this.getInputConnectionData(NodeConnectionType.AiMemory, 0)) as
| BaseChatMemory
| undefined;
agentExecutor.memory = memory;
let chatHistory = '';
if (memory) {
const messages = await memory.chatHistory.getMessages();
chatHistory = serializeChatHistory(messages);
}
let response: IDataObject;
try {
response = await agentExecutor.withConfig(getTracingConfig(this)).invoke({
input,
signal: this.getExecutionCancelSignal(),
chatHistory,
const dbInstance = await SqlDatabase.fromDataSourceParams({
appDataSource: dataSource,
includesTables: includedTablesArray.length > 0 ? includedTablesArray : undefined,
ignoreTables: ignoredTablesArray.length > 0 ? ignoredTablesArray : undefined,
sampleRowsInTableInfo: includedSampleRows ?? 3,
});
} catch (error) {
if ((error.message as IDataObject)?.output) {
response = error.message as IDataObject;
} else {
throw new NodeOperationError(this.getNode(), error.message as string, { itemIndex: i });
}
}
returnData.push({ json: response });
const toolkit = new SqlToolkit(dbInstance, model);
const agentExecutor = createSqlAgent(model, toolkit, agentOptions);
const memory = (await this.getInputConnectionData(NodeConnectionType.AiMemory, 0)) as
| BaseChatMemory
| undefined;
agentExecutor.memory = memory;
let chatHistory = '';
if (memory) {
const messages = await memory.chatHistory.getMessages();
chatHistory = serializeChatHistory(messages);
}
let response: IDataObject;
try {
response = await agentExecutor.withConfig(getTracingConfig(this)).invoke({
input,
signal: this.getExecutionCancelSignal(),
chatHistory,
});
} catch (error) {
if ((error.message as IDataObject)?.output) {
response = error.message as IDataObject;
} else {
throw new NodeOperationError(this.getNode(), error.message as string, { itemIndex: i });
}
}
returnData.push({ json: response });
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: i } });
continue;
}
throw error;
}
}
return await this.prepareOutputData(returnData);

View File

@@ -320,67 +320,76 @@ export class OpenAiAssistant implements INodeType {
const returnData: INodeExecutionData[] = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const input = this.getNodeParameter('text', itemIndex) as string;
const assistantId = this.getNodeParameter('assistantId', itemIndex, '') as string;
const nativeTools = this.getNodeParameter('nativeTools', itemIndex, []) as Array<
'code_interpreter' | 'retrieval'
>;
try {
const input = this.getNodeParameter('text', itemIndex) as string;
const assistantId = this.getNodeParameter('assistantId', itemIndex, '') as string;
const nativeTools = this.getNodeParameter('nativeTools', itemIndex, []) as Array<
'code_interpreter' | 'retrieval'
>;
const options = this.getNodeParameter('options', itemIndex, {}) as {
baseURL?: string;
maxRetries: number;
timeout: number;
};
const options = this.getNodeParameter('options', itemIndex, {}) as {
baseURL?: string;
maxRetries: number;
timeout: number;
};
if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The text parameter is empty.');
}
if (input === undefined) {
throw new NodeOperationError(this.getNode(), 'The text parameter is empty.');
}
const client = new OpenAIClient({
apiKey: credentials.apiKey as string,
maxRetries: options.maxRetries ?? 2,
timeout: options.timeout ?? 10000,
baseURL: options.baseURL,
});
let agent;
const nativeToolsParsed: OpenAIToolType = nativeTools.map((tool) => ({ type: tool }));
const transformedConnectedTools = tools?.map(formatToOpenAIAssistantTool) ?? [];
const newTools = [...transformedConnectedTools, ...nativeToolsParsed];
// Existing agent, update tools with currently assigned
if (assistantId) {
agent = new OpenAIAssistantRunnable({ assistantId, client, asAgent: true });
await client.beta.assistants.update(assistantId, {
tools: newTools,
const client = new OpenAIClient({
apiKey: credentials.apiKey as string,
maxRetries: options.maxRetries ?? 2,
timeout: options.timeout ?? 10000,
baseURL: options.baseURL,
});
} else {
const name = this.getNodeParameter('name', itemIndex, '') as string;
const instructions = this.getNodeParameter('instructions', itemIndex, '') as string;
const model = this.getNodeParameter('model', itemIndex, 'gpt-3.5-turbo-1106') as string;
let agent;
const nativeToolsParsed: OpenAIToolType = nativeTools.map((tool) => ({ type: tool }));
const transformedConnectedTools = tools?.map(formatToOpenAIAssistantTool) ?? [];
const newTools = [...transformedConnectedTools, ...nativeToolsParsed];
agent = await OpenAIAssistantRunnable.createAssistant({
model,
client,
instructions,
name,
tools: newTools,
asAgent: true,
// Existing agent, update tools with currently assigned
if (assistantId) {
agent = new OpenAIAssistantRunnable({ assistantId, client, asAgent: true });
await client.beta.assistants.update(assistantId, {
tools: newTools,
});
} else {
const name = this.getNodeParameter('name', itemIndex, '') as string;
const instructions = this.getNodeParameter('instructions', itemIndex, '') as string;
const model = this.getNodeParameter('model', itemIndex, 'gpt-3.5-turbo-1106') as string;
agent = await OpenAIAssistantRunnable.createAssistant({
model,
client,
instructions,
name,
tools: newTools,
asAgent: true,
});
}
const agentExecutor = AgentExecutor.fromAgentAndTools({
agent,
tools,
});
const response = await agentExecutor.withConfig(getTracingConfig(this)).invoke({
content: input,
signal: this.getExecutionCancelSignal(),
timeout: options.timeout ?? 10000,
});
returnData.push({ json: response });
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
continue;
}
throw error;
}
const agentExecutor = AgentExecutor.fromAgentAndTools({
agent,
tools,
});
const response = await agentExecutor.withConfig(getTracingConfig(this)).invoke({
content: input,
signal: this.getExecutionCancelSignal(),
timeout: options.timeout ?? 10000,
});
returnData.push({ json: response });
}
return await this.prepareOutputData(returnData);