-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(@langchain/core): support of ToolRuntime #9316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 865079a The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this a lot better! Still not sold on the idea of needing more tool overloads, see my comment.
I've got a radical idea that appeases the encapsulation goblin inside of me + lets us avoid one of the deadly sins of TS types (for your consideration):
const definition = createAgentDefinition({
stateSchema: z.object(),
contextSchema: z.object(),
store
});
// runtime can be auto-typed to include schemas + store
const myTool1 = definition.tool((state, runtime) => {}, {});
const agent = createAgent({
model: "foobar",
tools: [myTool1],
definition
});| export type ToolRuntime< | ||
| TState = unknown, | ||
| TContext = unknown | ||
| > = RunnableConfig & { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think we want this to extend ToolRunnableConfig? (since additional properties like toolCall get added within the tool invocation)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't quite use ToolRunnableConfig as it makes context an optional property which it isn't it every case, so I extended ToolRuntime with toolCall to match the same type.
| | DynamicTool<ToolOutputT>; | ||
|
|
||
| // Overloads with ToolRuntime as CallOptions | ||
| export function tool< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of these overloads if TState + TContext is only used in the runtime arg? Would say something different if we were recommending the usage of this should be by passing in TState + TContext as generic args, but I don't think that's what we want either. E.g. this should be possible w/o overloads?
const myTool = tool((state, runtime: ToolRuntime<...>) => {}, {})There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This are unfortunately required because the runtime type is otherwise defined as:
options:
| CallOptions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| (Record<string, any> & CallOptions)
) => RunOutput | Promise<RunOutput>;which fails because of:
Type 'Record<string, any>' is missing the following properties from type '{ state: { userId: string; }; toolCallId: string; config: ToolRunnableConfig<Record<string, any>, any>; context: { db: { foo: string; }; }; store: BaseStore<string, unknown> | null; writer: ((chunk: unknown) => void) | null; }': state, toolCallId, config, context, and 2 more.ts(2769)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems much more in line w/ what we have in python, and looks like a less significant change. nice!
d063853 to
a4f6660
Compare
Add changeset for ToolRuntime support in langchain.
After trying to support state and context aware tools through
contextSchemaandstateSchemaas tool arguments, here is a 2nd approach:This patch attempts to add more overwrites to the
toolfunction to allow users to define the second tool function argument to be aToolRuntime. Note that specific ToolRuntime parameters are only applied (in ToolNode) when using the tool viacreateAgent.