-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
core[minor]: Runnable with message history #3437
Merged
Merged
Changes from 18 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
df9a9f1
Runnable with message history
bracesproul a22855e
cr
bracesproul 66ed823
Merge branch 'main' into brace/runnable-message-history
bracesproul add083f
cr
bracesproul 05b71ea
adds withListeners method to runnables/callbacks
bracesproul 2cf91d5
added entrypoint for root listener file
bracesproul 21ce187
cr
bracesproul a8b2dbd
cr
bracesproul 48e1f7e
cr
bracesproul a56cd19
cr
bracesproul 89d689d
cr
bracesproul 9fd9eaf
Merge branch 'brace/with-listeners' of https://github.com/langchain-a…
bracesproul be79ae6
support async listeners
bracesproul ad795af
Merge branch 'brace/with-listeners' of https://github.com/langchain-a…
bracesproul b71e30d
allow for run or run and config as args to listener funcs
bracesproul b7575a4
Merge branch 'brace/with-listeners' of https://github.com/langchain-a…
bracesproul e8432ac
cr
bracesproul 00ab504
chore: lint files
bracesproul 08698aa
cr
bracesproul b055337
cr
bracesproul 8e23d5d
eslint disbale any
bracesproul a5576a8
Merge branch 'brace/with-listeners' of https://github.com/langchain-a…
bracesproul 485b915
Merge branch 'main' into brace/with-listeners
bracesproul 4394aa0
Merge branch 'main' into brace/with-listeners
bracesproul 670f4f1
update types
bracesproul 0a09cda
Merge branch 'main' into brace/with-listeners
bracesproul 3b76fc4
Merge branch 'main' into brace/runnable-message-history
bracesproul bc8cdbf
Merge branch 'main' into brace/with-listeners
bracesproul 7eb2ef9
Merge branch 'brace/with-listeners' of https://github.com/langchain-a…
bracesproul cec10ac
cr
bracesproul c9d3402
cr
bracesproul 12b2d9b
merge main
bracesproul b465628
cr
bracesproul 6df6e5d
Merge branch 'main' into brace/runnable-message-history
bracesproul dcbc640
cr
bracesproul 5e113e9
cr
bracesproul 1708764
Merge branch 'main' into brace/runnable-message-history
bracesproul 52ceeea
cr
bracesproul f3f6c79
Style
jacoblee93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,14 @@ import { | |
} from "../tracers/log_stream.js"; | ||
import { Serializable } from "../load/serializable.js"; | ||
import { IterableReadableStream } from "../utils/stream.js"; | ||
import { RunnableConfig, getCallbackMangerForConfig } from "./config.js"; | ||
import { | ||
RunnableConfig, | ||
getCallbackMangerForConfig, | ||
mergeConfigs, | ||
} from "./config.js"; | ||
import { AsyncCaller } from "../utils/async_caller.js"; | ||
import { Run } from "../tracers/base.js"; | ||
import { RootListenersTracer } from "../tracers/root_listener.js"; | ||
|
||
export type RunnableFunc<RunInput, RunOutput> = ( | ||
input: RunInput | ||
|
@@ -549,6 +555,54 @@ export abstract class Runnable< | |
static isRunnable(thing: any): thing is Runnable { | ||
return thing ? thing.lc_runnable : false; | ||
} | ||
|
||
/** | ||
* Bind lifecycle listeners to a Runnable, returning a new Runnable. | ||
* The Run object contains information about the run, including its id, | ||
* type, input, output, error, startTime, endTime, and any tags or metadata | ||
* added to the run. | ||
* | ||
* @param {Object} params - The object containing the callback functions. | ||
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object. | ||
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object. | ||
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object. | ||
*/ | ||
withListeners({ | ||
onStart, | ||
onEnd, | ||
onError, | ||
}: { | ||
onStart?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
onEnd?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
onError?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
}): Runnable<RunInput, RunOutput, CallOptions> { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return new RunnableBinding<RunInput, RunOutput, CallOptions>({ | ||
bound: this, | ||
config: {}, | ||
configFactories: [ | ||
(config) => ({ | ||
callbacks: [ | ||
new RootListenersTracer({ | ||
config, | ||
onStart, | ||
onEnd, | ||
onError, | ||
}), | ||
], | ||
}), | ||
], | ||
}); | ||
} | ||
} | ||
|
||
export type RunnableBindingArgs< | ||
|
@@ -557,8 +611,9 @@ export type RunnableBindingArgs< | |
CallOptions extends RunnableConfig | ||
> = { | ||
bound: Runnable<RunInput, RunOutput, CallOptions>; | ||
kwargs: Partial<CallOptions>; | ||
kwargs?: Partial<CallOptions>; | ||
config: RunnableConfig; | ||
configFactories?: Array<(config: RunnableConfig) => RunnableConfig>; | ||
}; | ||
|
||
/** | ||
|
@@ -581,31 +636,33 @@ export class RunnableBinding< | |
|
||
config: RunnableConfig; | ||
|
||
protected kwargs: Partial<CallOptions>; | ||
protected kwargs?: Partial<CallOptions>; | ||
|
||
configFactories?: Array< | ||
(config: RunnableConfig) => RunnableConfig | Promise<RunnableConfig> | ||
>; | ||
|
||
constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>) { | ||
super(fields); | ||
this.bound = fields.bound; | ||
this.kwargs = fields.kwargs; | ||
this.config = fields.config; | ||
this.configFactories = fields.configFactories; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
_mergeConfig(options?: Record<string, any>) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const copy: Record<string, any> = { ...this.config }; | ||
if (options) { | ||
for (const key of Object.keys(options)) { | ||
if (key === "metadata") { | ||
copy[key] = { ...copy[key], ...options[key] }; | ||
} else if (key === "tags") { | ||
copy[key] = (copy[key] ?? []).concat(options[key] ?? []); | ||
} else { | ||
copy[key] = options[key] ?? copy[key]; | ||
} | ||
} | ||
} | ||
return copy as Partial<CallOptions>; | ||
async _mergeConfig( | ||
options?: Record<string, any> | ||
): Promise<Partial<CallOptions>> { | ||
const config = mergeConfigs(this.config, options); | ||
return mergeConfigs( | ||
config, | ||
...(this.configFactories | ||
? await Promise.all( | ||
this.configFactories.map(async (f) => await f(config)) | ||
) | ||
: []) | ||
); | ||
} | ||
|
||
bind( | ||
|
@@ -645,7 +702,7 @@ export class RunnableBinding< | |
): Promise<RunOutput> { | ||
return this.bound.invoke( | ||
input, | ||
this._mergeConfig({ ...options, ...this.kwargs }) | ||
await this._mergeConfig({ ...options, ...this.kwargs }) | ||
); | ||
} | ||
|
||
|
@@ -673,13 +730,16 @@ export class RunnableBinding< | |
batchOptions?: RunnableBatchOptions | ||
): Promise<(RunOutput | Error)[]> { | ||
const mergedOptions = Array.isArray(options) | ||
? options.map((individualOption) => | ||
this._mergeConfig({ | ||
...individualOption, | ||
...this.kwargs, | ||
}) | ||
? await Promise.all( | ||
options.map( | ||
async (individualOption) => | ||
await this._mergeConfig({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No race condition right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't believe so, no. |
||
...individualOption, | ||
...this.kwargs, | ||
}) | ||
) | ||
) | ||
: this._mergeConfig({ ...options, ...this.kwargs }); | ||
: await this._mergeConfig({ ...options, ...this.kwargs }); | ||
return this.bound.batch(inputs, mergedOptions, batchOptions); | ||
} | ||
|
||
|
@@ -689,7 +749,7 @@ export class RunnableBinding< | |
) { | ||
yield* this.bound._streamIterator( | ||
input, | ||
this._mergeConfig({ ...options, ...this.kwargs }) | ||
await this._mergeConfig({ ...options, ...this.kwargs }) | ||
); | ||
} | ||
|
||
|
@@ -699,7 +759,7 @@ export class RunnableBinding< | |
): Promise<IterableReadableStream<RunOutput>> { | ||
return this.bound.stream( | ||
input, | ||
this._mergeConfig({ ...options, ...this.kwargs }) | ||
await this._mergeConfig({ ...options, ...this.kwargs }) | ||
); | ||
} | ||
|
||
|
@@ -710,7 +770,7 @@ export class RunnableBinding< | |
): AsyncGenerator<RunOutput> { | ||
yield* this.bound.transform( | ||
generator, | ||
this._mergeConfig({ ...options, ...this.kwargs }) | ||
await this._mergeConfig({ ...options, ...this.kwargs }) | ||
); | ||
} | ||
|
||
|
@@ -721,6 +781,55 @@ export class RunnableBinding< | |
): thing is RunnableBinding<any, any, any> { | ||
return thing.bound && Runnable.isRunnable(thing.bound); | ||
} | ||
|
||
/** | ||
* Bind lifecycle listeners to a Runnable, returning a new Runnable. | ||
* The Run object contains information about the run, including its id, | ||
* type, input, output, error, startTime, endTime, and any tags or metadata | ||
* added to the run. | ||
* | ||
* @param {Object} params - The object containing the callback functions. | ||
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object. | ||
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object. | ||
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object. | ||
*/ | ||
withListeners({ | ||
onStart, | ||
onEnd, | ||
onError, | ||
}: { | ||
onStart?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
onEnd?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
onError?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
}): Runnable<RunInput, RunOutput, CallOptions> { | ||
// | ||
return new RunnableBinding<RunInput, RunOutput, CallOptions>({ | ||
bound: this.bound, | ||
kwargs: this.kwargs, | ||
config: this.config, | ||
configFactories: [ | ||
(config) => ({ | ||
callbacks: [ | ||
new RootListenersTracer({ | ||
config, | ||
onStart, | ||
onEnd, | ||
onError, | ||
}), | ||
], | ||
}), | ||
], | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -789,6 +898,41 @@ export class RunnableEach< | |
this._patchConfig(config, runManager?.getChild()) | ||
); | ||
} | ||
|
||
/** | ||
* Bind lifecycle listeners to a Runnable, returning a new Runnable. | ||
* The Run object contains information about the run, including its id, | ||
* type, input, output, error, startTime, endTime, and any tags or metadata | ||
* added to the run. | ||
* | ||
* @param {Object} params - The object containing the callback functions. | ||
* @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object. | ||
* @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object. | ||
* @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object. | ||
*/ | ||
withListeners({ | ||
onStart, | ||
onEnd, | ||
onError, | ||
}: { | ||
onStart?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
onEnd?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
onError?: { | ||
(run: Run): void | Promise<void>; | ||
(run: Run, config: RunnableConfig): void | Promise<void>; | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
}): Runnable<any, any, CallOptions> { | ||
return new RunnableEach<RunInputItem, RunOutputItem, CallOptions>({ | ||
bound: this.bound.withListeners({ onStart, onEnd, onError }), | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
f
->factoryMethod
or something