Skip to content

Instantly share code, notes, and snippets.

@ma-zah
Created September 29, 2023 15:39
Show Gist options
  • Save ma-zah/97d2ce8a94b05528d9e7dccac07fb3db to your computer and use it in GitHub Desktop.
Save ma-zah/97d2ce8a94b05528d9e7dccac07fb3db to your computer and use it in GitHub Desktop.
on type safety in LangChain TS
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