From af5b8ad18b3fed04e94790cabe4fa26da0b9919e Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 10 Jul 2024 15:31:09 -0700 Subject: [PATCH] RunnableToolLike --- langchain-core/src/runnables/tool.ts | 35 +++++++++++++++++++++++++++- langchain-core/src/tools.ts | 4 +--- langchain-core/src/types/zod.ts | 4 ++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 langchain-core/src/types/zod.ts diff --git a/langchain-core/src/runnables/tool.ts b/langchain-core/src/runnables/tool.ts index b1a990019f7f..eec6be2a61a6 100644 --- a/langchain-core/src/runnables/tool.ts +++ b/langchain-core/src/runnables/tool.ts @@ -1 +1,34 @@ -// todo implement \ No newline at end of file +import { ZodAny } from "../types/zod.js"; +import { RunnableLambda } from "./base.js"; +import { RunnableConfig } from "./config.js"; + +export interface RunnableToolLikeFields { + name?: string; + + description?: string; + + schema: RunInput; + + func: + | ((input: RunInput, config?: RunnableConfig) => RunOutput) + | ((input: RunInput, config?: RunnableConfig) => Promise); +} + +export class RunnableToolLike< + RunInput extends ZodAny, + RunOutput = string +> extends RunnableLambda { + description?: string; + + schema: RunInput; + + constructor(fields: RunnableToolLikeFields) { + super({ + func: fields.func, + }); + + this.name = fields.name; + this.description = fields.description; + this.schema = fields.schema; + } +} diff --git a/langchain-core/src/tools.ts b/langchain-core/src/tools.ts index 252571ca084a..5adc4d27b4e4 100644 --- a/langchain-core/src/tools.ts +++ b/langchain-core/src/tools.ts @@ -11,9 +11,7 @@ import { } from "./language_models/base.js"; import { ensureConfig, type RunnableConfig } from "./runnables/config.js"; import type { RunnableFunc, RunnableInterface } from "./runnables/base.js"; - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -type ZodAny = z.ZodObject; +import { ZodAny } from "./types/zod.js"; /** * Parameters for the Tool classes. diff --git a/langchain-core/src/types/zod.ts b/langchain-core/src/types/zod.ts new file mode 100644 index 000000000000..d864170ddafa --- /dev/null +++ b/langchain-core/src/types/zod.ts @@ -0,0 +1,4 @@ +import type { z } from "zod"; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type ZodAny = z.ZodObject;