RunnableMap<RunInput extends Object> class

A RunnableMap allows you to run multiple Runnable objects in parallel on the same input returning a map of the results.

You can create a RunnableMap using the Runnable.fromMap static method.

When you call invoke on a RunnableMap, it will invoke each Runnable in the map in parallel, passing the same input to each one. The output of each Runnable is returned in a map, where the keys are the names of the outputs.

Example:

final openaiApiKey = Platform.environment['OPENAI_API_KEY'];
final model = ChatOpenAI(apiKey: openaiApiKey);

final promptTemplate1 = ChatPromptTemplate.fromTemplate(
  'What is the city {person} is from?',
);
final promptTemplate2 = ChatPromptTemplate.fromTemplate(
  'How old is {person}?',
);
final promptTemplate3 = ChatPromptTemplate.fromTemplate(
  'Is {city} a good city for a {age} years old person?',
);
const stringOutputParser = StringOutputParser<ChatResult>();

final chain = Runnable.fromMap({
  'city': promptTemplate1 | model | stringOutputParser,
  'age': promptTemplate2 | model | stringOutputParser,
}) | promptTemplate3 | model | stringOutputParser;

final res = await chain.invoke({'person': 'Elon Musk'});
print(res);
// It is subjective to determine whether Pretoria, South Africa, is a good
// city for a 50-year-old person as it depends on individual preferences and needs.
Inheritance

Constructors

RunnableMap(Map<String, Runnable<RunInput, RunnableOptions, Object>> steps)
A RunnableMap allows you to run multiple Runnable objects in parallel on the same input returning a map of the results.
const

Properties

defaultOptions RunnableOptions
The default options to use when invoking the Runnable.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
steps Map<String, Runnable<RunInput, RunnableOptions, Object>>
The map of Runnable objects to run in parallel.
final

Methods

batch(List<RunInput> inputs, {List<RunnableOptions>? options}) Future<List<Map<String, dynamic>>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(RunnableOptions options) RunnableBinding<RunInput, RunnableOptions, Map<String, dynamic>>
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 returns null.
inherited
invoke(RunInput input, {RunnableOptions? options}) Future<Map<String, dynamic>>
Invokes the RunnableMap 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<Map<String, dynamic>, NewCallOptions, NewRunOutput> next) RunnableSequence<RunInput, NewRunOutput>
Pipes the output of this Runnable into another Runnable using a RunnableSequence.
inherited
stream(RunInput input, {RunnableOptions? options}) Stream<Map<String, dynamic>>
Streams the output of invoking the Runnable on the given input.
override
streamFromInputStream(Stream<RunInput> inputStream, {RunnableOptions? options}) Stream<Map<String, dynamic>>
Streams the output of invoking the Runnable on the given inputStream.
override
toString() String
A string representation of this object.
inherited
withFallbacks(List<Runnable<RunInput, RunnableOptions, Map<String, dynamic>>> fallbacks) RunnableWithFallback<RunInput, Map<String, dynamic>>
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, Map<String, dynamic>>
Adds retry logic to an existing runnable.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited