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,9 +1,17 @@
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { HumanMessage } from '@langchain/core/messages';
import {
ApplicationError,
NodeApiError,
NodeConnectionType,
NodeOperationError,
} from 'n8n-workflow';
AIMessagePromptTemplate,
PromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
ChatPromptTemplate,
} from '@langchain/core/prompts';
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { ChatOllama } from '@langchain/ollama';
import { LLMChain } from 'langchain/chains';
import { CombiningOutputParser } from 'langchain/output_parsers';
import type {
IBinaryData,
IDataObject,
@@ -12,28 +20,17 @@ import type {
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
ApplicationError,
NodeApiError,
NodeConnectionType,
NodeOperationError,
} from 'n8n-workflow';
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
import {
AIMessagePromptTemplate,
PromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
ChatPromptTemplate,
} from '@langchain/core/prompts';
import type { BaseOutputParser } from '@langchain/core/output_parsers';
import { CombiningOutputParser } from 'langchain/output_parsers';
import { LLMChain } from 'langchain/chains';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { HumanMessage } from '@langchain/core/messages';
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { ChatOllama } from '@langchain/ollama';
import { getPromptInputByType, isChatInstance } from '../../../utils/helpers';
import type { N8nOutputParser } from '../../../utils/output_parsers/N8nOutputParser';
import { getOptionalOutputParsers } from '../../../utils/output_parsers/N8nOutputParser';
import { getTemplateNoticeField } from '../../../utils/sharedFields';
import {
getOptionalOutputParsers,
getPromptInputByType,
isChatInstance,
} from '../../../utils/helpers';
import { getTracingConfig } from '../../../utils/tracing';
import {
getCustomErrorMessage as getCustomOpenAiErrorMessage,
@@ -189,7 +186,7 @@ async function getChain(
itemIndex: number,
query: string,
llm: BaseLanguageModel,
outputParsers: BaseOutputParser[],
outputParsers: N8nOutputParser[],
messages?: MessagesTemplate[],
): Promise<unknown[]> {
const chatTemplate: ChatPromptTemplate | PromptTemplate = await getChainPromptTemplate(