RunnableSequence<RunInput extends Object?, RunOutput extends Object?> constructor

const RunnableSequence<RunInput extends Object?, RunOutput extends Object?>({
  1. required Runnable<RunInput, RunnableOptions, Object?> first,
  2. List<Runnable<Object?, RunnableOptions, Object?>> middle = const [],
  3. required Runnable<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.

You can create a RunnableSequence in several ways:

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;
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!

Implementation

const RunnableSequence({
  required this.first,
  this.middle = const [],
  required this.last,
}) : super(defaultOptions: const RunnableOptions());