RunnableMap<RunInput extends Object> constructor

const RunnableMap<RunInput extends Object>(
  1. 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.

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.

Implementation

const RunnableMap(this.steps)
    : super(defaultOptions: const RunnableOptions());