Merge pull request #1942 from FlowiseAI/bugfix/XML-Ouput-Parser

Bugfix/Update xml output parser
This commit is contained in:
Henry Heng 2024-03-13 15:47:19 +08:00 committed by GitHub
commit 534d6e4bbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 2 deletions

View File

@ -5,12 +5,11 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models'
import { RunnableSequence } from '@langchain/core/runnables' import { RunnableSequence } from '@langchain/core/runnables'
import { Tool } from '@langchain/core/tools' import { Tool } from '@langchain/core/tools'
import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder } from '@langchain/core/prompts' import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder } from '@langchain/core/prompts'
import { XMLAgentOutputParser } from 'langchain/agents/xml/output_parser'
import { formatLogToMessage } from 'langchain/agents/format_scratchpad/log_to_message' import { formatLogToMessage } from 'langchain/agents/format_scratchpad/log_to_message'
import { getBaseClasses } from '../../../src/utils' import { getBaseClasses } from '../../../src/utils'
import { FlowiseMemory, ICommonObject, IMessage, INode, INodeData, INodeParams } from '../../../src/Interface' import { FlowiseMemory, ICommonObject, IMessage, INode, INodeData, INodeParams } from '../../../src/Interface'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler' import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import { AgentExecutor } from '../../../src/agents' import { AgentExecutor, XMLAgentOutputParser } from '../../../src/agents'
import { Moderation, checkInputs } from '../../moderation/Moderation' import { Moderation, checkInputs } from '../../moderation/Moderation'
import { formatResponse } from '../../outputparsers/OutputParserHelpers' import { formatResponse } from '../../outputparsers/OutputParserHelpers'

View File

@ -779,3 +779,36 @@ Final Answer: the final answer to the original input question`
}) })
} }
} }
export class XMLAgentOutputParser extends AgentActionOutputParser {
lc_namespace = ['langchain', 'agents', 'xml']
static lc_name() {
return 'XMLAgentOutputParser'
}
/**
* Parses the output text from the agent and returns an AgentAction or
* AgentFinish object.
* @param text The output text from the agent.
* @returns An AgentAction or AgentFinish object.
*/
async parse(text: string): Promise<AgentAction | AgentFinish> {
if (text.includes('</tool>')) {
const [tool, toolInput] = text.split('</tool>')
const _tool = tool.split('<tool>')[1]
const _toolInput = toolInput.split('<tool_input>')[1]
return { tool: _tool, toolInput: _toolInput, log: text }
} else if (text.includes('<final_answer>')) {
const [, answer] = text.split('<final_answer>')
return { returnValues: { output: answer }, log: text }
} else {
// Instead of throwing Error, we return a AgentFinish object
return { returnValues: { output: text }, log: text }
}
}
getFormatInstructions(): string {
throw new Error('getFormatInstructions not implemented inside XMLAgentOutputParser.')
}
}