RunnableFunction<RunInput extends Object, RunOutput extends Object> class

A RunnableFunction allows you to run a Dart function as part of a chain.

You can create a RunnableFunction using the Runnable.fromFunction static method.

When you call invoke on a RunnableFunction, it will invoke the function, passing the input to it. The output of the function is returned.

Example:

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

final promptTemplate = ChatPromptTemplate.fromTemplate(
  'How much is {a} + {b}?',
);

final chain = Runnable.fromMap({
      'a': Runnable.fromFunction((
        final Map<String, String> input,
        final options,
      ) async {
        final foo = input['foo'] ?? '';
        return '${foo.length}';
      }),
      'b': Runnable.fromFunction((
        final Map<String, String> input,
        final options,
      ) async {
        final foo = input['foo'] ?? '';
        final bar = input['bar'] ?? '';
        return '${bar.length * foo.length}';
      }),
    }) |
    promptTemplate |
    model |
    StringOutputParser();

final res = await chain.invoke({'foo': 'foo', 'bar': 'bar'});
print(res);
// 3 + 9 = 12
Inheritance
Available extensions

Constructors

RunnableFunction({FutureOr<RunOutput> invoke(RunInput input, RunnableOptions? options)?, Stream<RunOutput> stream(Stream<RunInput> inputStream, RunnableOptions? options)?, RunnableOptions defaultOptions = const RunnableOptions()})
A RunnableFunction allows you to run a Dart function as part of a chain.
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

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.
inherited
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<RunOutput>
Invokes the RunnableFunction 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 Runnable into another Runnable using a RunnableSequence.
inherited
stream(RunInput input, {RunnableOptions? options}) Stream<RunOutput>
Streams the input through the RunnableFunction.
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<RunInput, RunnableOptions, RunOutput>> fallbacks) RunnableWithFallback<RunInput, RunOutput>
Adds fallback runnables to be invoked if the primary runnable fails.
inherited

Operators

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