RunnableSequence<RunInput extends Object?, RunOutput extends Object?> class
A RunnableSequence allows you to run multiple Runnable objects sequentially, passing the output of the previous Runnable to the next one.
You can create a RunnableSequence in several ways:
- Calling Runnable.pipe method which takes another Runnable as an argument. E.g.:
final chain = promptTemplate.pipe(chatModel);
- Using the
|
operator. This is a convenience method that calls Runnable.pipe under the hood (note that it offers less type safety than Runnable.pipe because of Dart limitations). E.g.:
final chain = promptTemplate | chatModel;
- Using the Runnable.fromList static method with a list of Runnable, which will run in sequence when invoked. E.g.:
final chain = Runnable.fromList([promptTemplate, chatModel]);
When you call invoke on a RunnableSequence, it will invoke each Runnable in the sequence in order, passing the output of the previous Runnable to the next one. The output of the last Runnable in the sequence is returned.
You can think of RunnableSequence as the replacement for
SequentialChain
.
Example:
final openaiApiKey = Platform.environment['OPENAI_API_KEY'];
final model = ChatOpenAI(apiKey: openaiApiKey);
final promptTemplate = ChatPromptTemplate.fromTemplate(
'Tell me a joke about {topic}',
);
// The following three chains are equivalent:
final chain1 = promptTemplate | model | StringOutputParser();
final chain2 = promptTemplate.pipe(model).pipe(StringOutputParser());
final chain3 = Runnable.fromList(
[promptTemplate, model, StringOutputParser()],
);
final res = await chain1.invoke({'topic': 'bears'});
print(res);
// Why don't bears wear shoes? Because they have bear feet!
- Inheritance
-
- Object
- Runnable<
RunInput, RunnableOptions, RunOutput> - RunnableSequence
- Available extensions
Constructors
-
RunnableSequence({required Runnable<
RunInput, RunnableOptions, Object?> first, List<Runnable< middle = const [], required Runnable<Object?, RunnableOptions, Object?> >Object?, RunnableOptions, RunOutput> last}) -
A RunnableSequence allows you to run multiple Runnable objects
sequentially, passing the output of the previous Runnable to the next one.
const
Properties
- defaultOptions → RunnableOptions
-
The default options to use when invoking the Runnable.
finalinherited
-
first
→ Runnable<
RunInput, RunnableOptions, Object?> -
The first Runnable in the RunnableSequence.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
last
→ Runnable<
Object?, RunnableOptions, RunOutput> -
The last Runnable in the RunnableSequence.
final
-
middle
→ List<
Runnable< Object?, RunnableOptions, Object?> > -
The middle Runnables in the RunnableSequence.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
steps
→ List<
Runnable< Object?, RunnableOptions, Object?> > -
Returns a list of all the Runnables in the RunnableSequence.
no setter
Methods
-
batch(
List< RunInput> inputs, {List<RunnableOptions> ? options}) → Future<List< RunOutput> > -
Batches the invocation of the Runnable on the given
inputs
.inherited -
bind(
RunnableOptions options) → RunnableBinding< RunInput, RunnableOptions, RunOutput> -
Binds the Runnable to the given
options
.inherited -
close(
) → void -
Cleans up any resources associated with it the Runnable.
override
-
getCompatibleOptions(
RunnableOptions? options) → RunnableOptions? -
Returns the given
options
if they are compatible with the Runnable, otherwise returnsnull
.inherited -
invoke(
RunInput input, {RunnableOptions? options}) → Future< RunOutput> -
Invokes the RunnableSequence on the given
input
.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pipe<
NewRunOutput extends Object?, NewCallOptions extends RunnableOptions> (Runnable< RunOutput, NewCallOptions, NewRunOutput> next) → RunnableSequence<RunInput, NewRunOutput> -
Pipes the output of this RunnableSequence into another Runnable.
override
-
stream(
RunInput input, {RunnableOptions? options}) → Stream< RunOutput> -
Streams the output of invoking the Runnable on the given
input
.override -
streamFromInputStream(
Stream< RunInput> inputStream, {RunnableOptions? options}) → Stream<RunOutput> -
Streams the output of invoking the Runnable on the given
inputStream
.override -
toString(
) → String -
A string representation of this object.
inherited
-
withFallbacks(
List< Runnable< fallbacks) → RunnableWithFallback<RunInput, RunnableOptions, RunOutput> >RunInput, RunOutput> -
Adds fallback runnables to be invoked if the primary runnable fails.
inherited
-
withRetry(
{int maxRetries = 3, FutureOr< bool> retryIf(Object e)?, List<Duration?> ? delayDurations, bool addJitter = false}) → RunnableRetry<RunInput, RunOutput> -
Adds retry logic to an existing runnable.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator |(
Runnable< RunOutput, RunnableOptions, NewRunOutput> next) → RunnableSequence<RunInput, NewRunOutput> -
Available on Runnable<
Pipes the output of this Runnable into another Runnable.RunInput, CallOptions, RunOutput> , provided by the RunnableX extension
Static Methods
-
from(
List< Runnable< runnables) → RunnableSequence<Object?, RunnableOptions, Object?> >Object?, Object?> - Creates a RunnableSequence from a list of Runnables.