Skip to content

Instantly share code, notes, and snippets.

@ma-zah
Created September 29, 2023 15:35
Show Gist options
  • Save ma-zah/1e2e2e80f5885ad439a6ae76c2b3e1c5 to your computer and use it in GitHub Desktop.
Save ma-zah/1e2e2e80f5885ad439a6ae76c2b3e1c5 to your computer and use it in GitHub Desktop.
on type safety in LangChain TS
async safePlanNextStep(previousSteps: AgentStep[]): Promise<AgentAction> {
try {
// This will return either an AgentAction or AgentFinish through outputParser.parse
output = await this.agent.plan(steps, inputs);
} catch (e) {
if (e instanceof OutputParserException) {
let observation;
const text = e.message;
if (this.handleParsingErrors === true) {
observation = "Invalid or incomplete response";
} else if (typeof this.handleParsingErrors === "string") {
observation = this.handleParsingErrors;
} else if (typeof this.handleParsingErrors === "function") {
observation = this.handleParsingErrors(e);
} else {
throw e;
}
output = {
tool: "_Exception",
toolInput: observation,
log: text,
} as AgentAction;
} else {
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment