mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 03:01:10 +03:00
Code cleanup and minor fixes.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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 Parser (Experimental)'
|
||||
|
||||
export const applyOutputParser = async (response: string, outputParser: BaseOutputParser | undefined): Promise<string> => {
|
||||
if (outputParser) {
|
||||
const parsedResponse = await outputParser.parse(response)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('**** parsedResponse ****', parsedResponse)
|
||||
if (typeof parsedResponse === 'object') {
|
||||
return JSON.stringify(parsedResponse)
|
||||
} else {
|
||||
return parsedResponse as string
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getBaseClasses, ICommonObject, 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
|
||||
@@ -21,7 +22,7 @@ class CSVListOutputParser implements INode {
|
||||
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 = 'Output Parser'
|
||||
this.category = CATEGORY
|
||||
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
|
||||
this.inputs = []
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getBaseClasses, ICommonObject, 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
|
||||
@@ -21,7 +22,7 @@ class CustomListOutputParser implements INode {
|
||||
this.type = 'CustomListOutputParser'
|
||||
this.description = 'Parse the output of an LLM call as a list of values.'
|
||||
this.icon = 'list.png'
|
||||
this.category = 'Output Parser'
|
||||
this.category = CATEGORY
|
||||
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
|
||||
this.inputs = [
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getBaseClasses, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
|
||||
import { BaseOutputParser } from 'langchain/schema/output_parser'
|
||||
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
|
||||
import { CATEGORY } from '../OutputParserHelpers'
|
||||
|
||||
class StructuredOutputParser implements INode {
|
||||
label: string
|
||||
@@ -21,8 +22,9 @@ class StructuredOutputParser implements INode {
|
||||
this.type = 'StructuredOutputParser'
|
||||
this.description = 'Parse the output of an LLM call into a given (JSON) structure.'
|
||||
this.icon = 'structure.png'
|
||||
this.category = 'Output Parser'
|
||||
this.category = CATEGORY
|
||||
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
|
||||
//TODO: To extend the structureType to ZodSchema
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Structure Type',
|
||||
@@ -32,10 +34,6 @@ class StructuredOutputParser implements INode {
|
||||
{
|
||||
label: 'Names And Descriptions',
|
||||
name: 'fromNamesAndDescriptions'
|
||||
},
|
||||
{
|
||||
label: 'Zod Schema',
|
||||
name: 'fromZodSchema'
|
||||
}
|
||||
],
|
||||
default: 'fromNamesAndDescriptions'
|
||||
@@ -59,18 +57,15 @@ class StructuredOutputParser implements INode {
|
||||
const structureType = nodeData.inputs?.structureType as string
|
||||
const structure = nodeData.inputs?.structure as string
|
||||
let parsedStructure: any | undefined = undefined
|
||||
if (structure) {
|
||||
if (structure && structureType === 'fromNamesAndDescriptions') {
|
||||
try {
|
||||
parsedStructure = JSON.parse(structure)
|
||||
if (structureType === 'fromZodSchema') {
|
||||
return LangchainStructuredOutputParser.fromZodSchema(parsedStructure)
|
||||
} else {
|
||||
return LangchainStructuredOutputParser.fromNamesAndDescriptions(parsedStructure)
|
||||
}
|
||||
return LangchainStructuredOutputParser.fromNamesAndDescriptions(parsedStructure)
|
||||
} catch (exception) {
|
||||
throw new Error('Invalid JSON in StructuredOutputParser: ' + exception)
|
||||
}
|
||||
}
|
||||
throw new Error('Error creating OutputParser.')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user