update xml output parser

This commit is contained in:
Henry
2024-03-13 15:11:22 +08:00
parent 56b610cfa2
commit 5f69a0652e
2 changed files with 34 additions and 2 deletions
+33
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.')
}
}