Skip to content

Commit

Permalink
core[patch],community[patch]: Make traced DynamicTool runs use tool n…
Browse files Browse the repository at this point in the history
…ame (#3635)

* Make traced DynamicTool runs use tool name

* Revert test

* Fix typo
  • Loading branch information
jacoblee93 authored Dec 13, 2023
1 parent c5608e8 commit b455950
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion langchain-core/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export abstract class Tool extends StructuredTool {
*/
call(
arg: string | undefined | z.input<this["schema"]>,
callbacks?: Callbacks
callbacks?: Callbacks | RunnableConfig
): Promise<string> {
return super.call(
typeof arg === "string" || !arg ? { input: arg } : arg,
Expand Down
31 changes: 30 additions & 1 deletion libs/langchain-community/src/tools/dynamic.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -54,6 +59,17 @@ export class DynamicTool extends Tool {
this.returnDirect = fields.returnDirect ?? this.returnDirect;
}

async call(
arg: string | undefined | z.input<this["schema"]>,
configArg?: RunnableConfig | Callbacks
): Promise<string> {
const config = parseCallbackConfigArg(configArg);
if (config.runName === undefined) {
config.runName = this.name;
}
return super.call(arg, config);
}

/** @ignore */
async _call(
input: string,
Expand Down Expand Up @@ -94,6 +110,19 @@ export class DynamicStructuredTool<
this.schema = fields.schema;
}

async call(
arg: z.output<T>,
configArg?: RunnableConfig | Callbacks,
/** @deprecated */
tags?: string[]
): Promise<string> {
const config = parseCallbackConfigArg(configArg);
if (config.runName === undefined) {
config.runName = this.name;
}
return super.call(arg, config, tags);
}

protected _call(
arg: z.output<T>,
runManager?: CallbackManagerForToolRun
Expand Down

1 comment on commit b455950

@vercel
Copy link

@vercel vercel bot commented on b455950 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.