-
-
Save ma-zah/97d2ce8a94b05528d9e7dccac07fb3db to your computer and use it in GitHub Desktop.
on type safety in LangChain 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
async safeCallTool(action: AgentAction): Promise<AgentStep> { | |
const tool = | |
action.tool === "_Exception" | |
? new ExceptionTool() | |
: toolsByName[action.tool?.toLowerCase()]; | |
let observation; | |
try { | |
observation = tool | |
? await tool.call(action.toolInput) | |
: `${action.tool} is not a valid tool, try another one.`; | |
} catch (e) { | |
if (e instanceof ToolInputParsingException) { | |
if (this.handleParsingErrors === true) { | |
observation = "Invalid or incomplete tool input. Please try again."; | |
} else if (typeof this.handleParsingErrors === "string") { | |
observation = this.handleParsingErrors; | |
} else if (typeof this.handleParsingErrors === "function") { | |
observation = this.handleParsingErrors(e); | |
} else { | |
throw e; | |
} | |
observation = await new ExceptionTool().call( | |
observation, | |
runManager?.getChild() | |
); | |
return { action, observation: observation ?? "" }; | |
} | |
} | |
return { action, observation: observation ?? "" }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment