feat(Structured Output Parser Node): Refactor Output Parsers and Improve Error Handling (#11148)

This commit is contained in:
oleg
2024-10-22 10:46:58 +02:00
committed by GitHub
parent 4dde772814
commit 45274f2e7f
20 changed files with 1061 additions and 383 deletions

View File

@@ -1,15 +1,11 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { OutputFixingParser } from 'langchain/output_parsers';
import type { BaseOutputParser } from '@langchain/core/output_parsers';
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import { logWrapper } from '../../../utils/logWrapper';
import { NodeConnectionType } from 'n8n-workflow';
import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow';
import {
N8nOutputFixingParser,
type N8nStructuredOutputParser,
} from '../../../utils/output_parsers/N8nOutputParser';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
export class OutputParserAutofixing implements INodeType {
@@ -75,12 +71,12 @@ export class OutputParserAutofixing implements INodeType {
const outputParser = (await this.getInputConnectionData(
NodeConnectionType.AiOutputParser,
itemIndex,
)) as BaseOutputParser;
)) as N8nStructuredOutputParser;
const parser = OutputFixingParser.fromLLM(model, outputParser);
const parser = new N8nOutputFixingParser(this, model, outputParser);
return {
response: logWrapper(parser, this),
response: parser,
};
}
}

View File

@@ -0,0 +1,120 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import type { MockProxy } from 'jest-mock-extended';
import { mock } from 'jest-mock-extended';
import { normalizeItems } from 'n8n-core';
import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow';
import { ApplicationError, NodeConnectionType } from 'n8n-workflow';
import { N8nOutputFixingParser } from '../../../../utils/output_parsers/N8nOutputParser';
import type { N8nStructuredOutputParser } from '../../../../utils/output_parsers/N8nOutputParser';
import { OutputParserAutofixing } from '../OutputParserAutofixing.node';
describe('OutputParserAutofixing', () => {
let outputParser: OutputParserAutofixing;
let thisArg: MockProxy<IExecuteFunctions>;
let mockModel: MockProxy<BaseLanguageModel>;
let mockStructuredOutputParser: MockProxy<N8nStructuredOutputParser>;
beforeEach(() => {
outputParser = new OutputParserAutofixing();
thisArg = mock<IExecuteFunctions>({
helpers: { normalizeItems },
});
mockModel = mock<BaseLanguageModel>();
mockStructuredOutputParser = mock<N8nStructuredOutputParser>();
thisArg.getWorkflowDataProxy.mockReturnValue(mock<IWorkflowDataProxyData>({ $input: mock() }));
thisArg.addInputData.mockReturnValue({ index: 0 });
thisArg.addOutputData.mockReturnValue();
thisArg.getInputConnectionData.mockImplementation(async (type: NodeConnectionType) => {
if (type === NodeConnectionType.AiLanguageModel) return mockModel;
if (type === NodeConnectionType.AiOutputParser) return mockStructuredOutputParser;
throw new ApplicationError('Unexpected connection type');
});
});
afterEach(() => {
jest.clearAllMocks();
});
function getMockedRetryChain(output: string) {
return jest.fn().mockReturnValue({
invoke: jest.fn().mockResolvedValue({
content: output,
}),
});
}
it('should successfully parse valid output without needing to fix it', async () => {
const validOutput = { name: 'Alice', age: 25 };
mockStructuredOutputParser.parse.mockResolvedValueOnce(validOutput);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
// Ensure the response contains the output-fixing parser
expect(response).toBeDefined();
expect(response).toBeInstanceOf(N8nOutputFixingParser);
const result = await response.parse('{"name": "Alice", "age": 25}');
// Validate that the parser succeeds without retry
expect(result).toEqual(validOutput);
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(1); // Only one call to parse
});
it('should throw an error when both structured parser and fixing parser fail', async () => {
mockStructuredOutputParser.parse
.mockRejectedValueOnce(new Error('Invalid JSON')) // First attempt fails
.mockRejectedValueOnce(new Error('Fixing attempt failed')); // Second attempt fails
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain('{}');
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Fixing attempt failed');
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2);
});
it('should reject on the first attempt and succeed on retry with the parsed content', async () => {
const validOutput = { name: 'Bob', age: 28 };
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Invalid JSON'));
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain(JSON.stringify(validOutput));
mockStructuredOutputParser.parse.mockResolvedValueOnce(validOutput);
const result = await response.parse('Invalid JSON string');
expect(result).toEqual(validOutput);
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2); // First fails, second succeeds
});
it('should handle non-JSON formatted response from fixing parser', async () => {
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Invalid JSON'));
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nOutputFixingParser;
};
response.getRetryChain = getMockedRetryChain('This is not JSON');
mockStructuredOutputParser.parse.mockRejectedValueOnce(new Error('Unexpected token'));
// Expect the structured parser to throw an error on invalid JSON from retry
await expect(response.parse('Invalid JSON string')).rejects.toThrow('Unexpected token');
expect(mockStructuredOutputParser.parse).toHaveBeenCalledTimes(2); // First fails, second tries and fails
});
});