From b455950ddd27d107553b93071c44b43c7a05fe62 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Tue, 12 Dec 2023 18:10:34 -0800 Subject: [PATCH] core[patch],community[patch]: Make traced DynamicTool runs use tool name (#3635) * Make traced DynamicTool runs use tool name * Revert test * Fix typo --- langchain-core/src/tools.ts | 2 +- libs/langchain-community/src/tools/dynamic.ts | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/langchain-core/src/tools.ts b/langchain-core/src/tools.ts index eeedab18ecd8..84b5311eac39 100644 --- a/langchain-core/src/tools.ts +++ b/langchain-core/src/tools.ts @@ -150,7 +150,7 @@ export abstract class Tool extends StructuredTool { */ call( arg: string | undefined | z.input, - callbacks?: Callbacks + callbacks?: Callbacks | RunnableConfig ): Promise { return super.call( typeof arg === "string" || !arg ? { input: arg } : arg, diff --git a/libs/langchain-community/src/tools/dynamic.ts b/libs/langchain-community/src/tools/dynamic.ts index eac8b7593534..007540a2e084 100644 --- a/libs/langchain-community/src/tools/dynamic.ts +++ b/libs/langchain-community/src/tools/dynamic.ts @@ -1,6 +1,11 @@ import { z } from "zod"; -import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager"; +import { + CallbackManagerForToolRun, + Callbacks, + parseCallbackConfigArg, +} from "@langchain/core/callbacks/manager"; import { StructuredTool, Tool, type ToolParams } from "@langchain/core/tools"; +import { RunnableConfig } from "@langchain/core/runnables"; export interface BaseDynamicToolInput extends ToolParams { name: string; @@ -54,6 +59,17 @@ export class DynamicTool extends Tool { this.returnDirect = fields.returnDirect ?? this.returnDirect; } + async call( + arg: string | undefined | z.input, + configArg?: RunnableConfig | Callbacks + ): Promise { + const config = parseCallbackConfigArg(configArg); + if (config.runName === undefined) { + config.runName = this.name; + } + return super.call(arg, config); + } + /** @ignore */ async _call( input: string, @@ -94,6 +110,19 @@ export class DynamicStructuredTool< this.schema = fields.schema; } + async call( + arg: z.output, + configArg?: RunnableConfig | Callbacks, + /** @deprecated */ + tags?: string[] + ): Promise { + const config = parseCallbackConfigArg(configArg); + if (config.runName === undefined) { + config.runName = this.name; + } + return super.call(arg, config, tags); + } + protected _call( arg: z.output, runManager?: CallbackManagerForToolRun