forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request FlowiseAI#1115 from vinodkiran/FEATURE/output-parsers
New Feature - Output Parsers
- Loading branch information
Showing
27 changed files
with
2,174 additions
and
768 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/components/nodes/outputparsers/CSVListOutputParser/CSVListOutputParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { getBaseClasses, INode, INodeData, INodeParams } from '../../../src' | ||
import { BaseOutputParser } from 'langchain/schema/output_parser' | ||
import { CommaSeparatedListOutputParser } from 'langchain/output_parsers' | ||
import { CATEGORY } from '../OutputParserHelpers' | ||
|
||
class CSVListOutputParser implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
credential: INodeParams | ||
|
||
constructor() { | ||
this.label = 'CSV Output Parser' | ||
this.name = 'csvOutputParser' | ||
this.version = 1.0 | ||
this.type = 'CSVListOutputParser' | ||
this.description = 'Parse the output of an LLM call as a comma-separated list of values' | ||
this.icon = 'csv.png' | ||
this.category = CATEGORY | ||
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)] | ||
this.inputs = [ | ||
{ | ||
label: 'Autofix', | ||
name: 'autofixParser', | ||
type: 'boolean', | ||
optional: true, | ||
description: 'In the event that the first call fails, will make another call to the model to fix any errors.' | ||
} | ||
] | ||
} | ||
|
||
async init(nodeData: INodeData): Promise<any> { | ||
const autoFix = nodeData.inputs?.autofixParser as boolean | ||
|
||
const commaSeparatedListOutputParser = new CommaSeparatedListOutputParser() | ||
Object.defineProperty(commaSeparatedListOutputParser, 'autoFix', { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: autoFix | ||
}) | ||
return commaSeparatedListOutputParser | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: CSVListOutputParser } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions
71
packages/components/nodes/outputparsers/CustomListOutputParser/CustomListOutputParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { getBaseClasses, INode, INodeData, INodeParams } from '../../../src' | ||
import { BaseOutputParser } from 'langchain/schema/output_parser' | ||
import { CustomListOutputParser as LangchainCustomListOutputParser } from 'langchain/output_parsers' | ||
import { CATEGORY } from '../OutputParserHelpers' | ||
|
||
class CustomListOutputParser implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
credential: INodeParams | ||
|
||
constructor() { | ||
this.label = 'Custom List Output Parser' | ||
this.name = 'customListOutputParser' | ||
this.version = 1.0 | ||
this.type = 'CustomListOutputParser' | ||
this.description = 'Parse the output of an LLM call as a list of values.' | ||
this.icon = 'list.png' | ||
this.category = CATEGORY | ||
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)] | ||
this.inputs = [ | ||
{ | ||
label: 'Length', | ||
name: 'length', | ||
type: 'number', | ||
default: 5, | ||
step: 1, | ||
description: 'Number of values to return' | ||
}, | ||
{ | ||
label: 'Separator', | ||
name: 'separator', | ||
type: 'string', | ||
description: 'Separator between values', | ||
default: ',' | ||
}, | ||
{ | ||
label: 'Autofix', | ||
name: 'autofixParser', | ||
type: 'boolean', | ||
optional: true, | ||
description: 'In the event that the first call fails, will make another call to the model to fix any errors.' | ||
} | ||
] | ||
} | ||
|
||
async init(nodeData: INodeData): Promise<any> { | ||
const separator = nodeData.inputs?.separator as string | ||
const lengthStr = nodeData.inputs?.length as string | ||
const autoFix = nodeData.inputs?.autofixParser as boolean | ||
let length = 5 | ||
if (lengthStr) length = parseInt(lengthStr, 10) | ||
|
||
const parser = new LangchainCustomListOutputParser({ length: length, separator: separator }) | ||
Object.defineProperty(parser, 'autoFix', { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: autoFix | ||
}) | ||
return parser | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: CustomListOutputParser } |
Binary file added
BIN
+4.88 KB
packages/components/nodes/outputparsers/CustomListOutputParser/list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions
46
packages/components/nodes/outputparsers/OutputParserHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BaseOutputParser } from 'langchain/schema/output_parser' | ||
import { LLMChain } from 'langchain/chains' | ||
import { BaseLanguageModel } from 'langchain/base_language' | ||
import { ICommonObject } from '../../src' | ||
import { ChatPromptTemplate, FewShotPromptTemplate, PromptTemplate, SystemMessagePromptTemplate } from 'langchain/prompts' | ||
|
||
export const CATEGORY = 'Output Parsers' | ||
|
||
export const formatResponse = (response: string | object): string | object => { | ||
if (typeof response === 'object') { | ||
return { json: response } | ||
} | ||
return response | ||
} | ||
|
||
export const injectOutputParser = ( | ||
outputParser: BaseOutputParser<unknown>, | ||
chain: LLMChain<string, BaseLanguageModel>, | ||
promptValues: ICommonObject | undefined = undefined | ||
) => { | ||
if (outputParser && chain.prompt) { | ||
const formatInstructions = outputParser.getFormatInstructions() | ||
if (chain.prompt instanceof PromptTemplate) { | ||
let pt = chain.prompt | ||
pt.template = pt.template + '\n{format_instructions}' | ||
chain.prompt.partialVariables = { format_instructions: formatInstructions } | ||
} else if (chain.prompt instanceof ChatPromptTemplate) { | ||
let pt = chain.prompt | ||
pt.promptMessages.forEach((msg) => { | ||
if (msg instanceof SystemMessagePromptTemplate) { | ||
;(msg.prompt as any).partialVariables = { format_instructions: outputParser.getFormatInstructions() } | ||
;(msg.prompt as any).template = ((msg.prompt as any).template + '\n{format_instructions}') as string | ||
} | ||
}) | ||
} else if (chain.prompt instanceof FewShotPromptTemplate) { | ||
chain.prompt.examplePrompt.partialVariables = { format_instructions: formatInstructions } | ||
chain.prompt.examplePrompt.template = chain.prompt.examplePrompt.template + '\n{format_instructions}' | ||
} | ||
|
||
chain.prompt.inputVariables.push('format_instructions') | ||
if (promptValues) { | ||
promptValues = { ...promptValues, format_instructions: outputParser.getFormatInstructions() } | ||
} | ||
} | ||
return promptValues | ||
} |
Oops, something went wrong.