-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lcel): Add support for Runnable.mapInput() (#229)
A `RunnableMapInput` allows you to map the input to a different value. You can create a `RunnableMapInput` using the `Runnable.mapInput` static method. When you call `invoke` on a `RunnableMapInput`, it will take the input it receives and returns the output returned by the given `inputMapper` function. Example: ```dart final agent = Agent.fromRunnable( Runnable.mapInput( (final AgentPlanInput planInput) => <String, dynamic>{ 'input': planInput.inputs['input'], 'agent_scratchpad': buildScratchpad(planInput.intermediateSteps), }, ).pipe(prompt).pipe(model).pipe(outputParser), tools: [tool], ); ```
- Loading branch information
1 parent
7330cfc
commit 7cc832c
Showing
6 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import '../base.dart'; | ||
import 'base.dart'; | ||
|
||
/// {@template runnable_map_input} | ||
/// A [RunnableMapInput] allows you to map the input to a different value. | ||
/// | ||
/// You can create a [RunnableMapInput] using the [Runnable.mapInput] static | ||
/// method. | ||
/// | ||
/// When you call [invoke] on a [RunnableMapInput], it will take the | ||
/// input it receives and returns the output returned by the given | ||
/// [inputMapper] function. | ||
/// | ||
/// Example: | ||
/// | ||
/// ```dart | ||
/// final agent = Agent.fromRunnable( | ||
/// Runnable.mapInput( | ||
/// (final AgentPlanInput planInput) => <String, dynamic>{ | ||
/// 'input': planInput.inputs['input'], | ||
/// 'agent_scratchpad': buildScratchpad(planInput.intermediateSteps), | ||
/// }, | ||
/// ).pipe(prompt).pipe(model).pipe(outputParser), | ||
/// tools: [tool], | ||
/// ); | ||
/// ``` | ||
/// {@endtemplate} | ||
class RunnableMapInput<RunInput extends Object, RunOutput extends Object> | ||
extends Runnable<RunInput, BaseLangChainOptions, RunOutput> { | ||
/// {@macro runnable_map_from_input_items} | ||
const RunnableMapInput(this.inputMapper); | ||
|
||
/// A function that maps [RunInput] to [RunOutput]. | ||
final RunOutput Function(RunInput input) inputMapper; | ||
|
||
/// Invokes the [RunnableMapInput] on the given [input]. | ||
/// | ||
/// - [input] - the input to invoke the [RunnableMapInput] on. | ||
/// - [options] - not used. | ||
@override | ||
Future<RunOutput> invoke( | ||
final RunInput input, { | ||
final BaseLangChainOptions? options, | ||
}) async { | ||
return inputMapper(input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:langchain/langchain.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('RunnableMapInput tests', () { | ||
test('RunnableMapInput from Runnable.getItemFromMap', () async { | ||
final chain = Runnable.mapInput<ChainValues, ChainValues>( | ||
(final input) => { | ||
'input': '${input['foo']}${input['bar']}', | ||
}, | ||
); | ||
|
||
final res = await chain.invoke({'foo': 'foo1', 'bar': 'bar1'}); | ||
expect(res, {'input': 'foo1bar1'}); | ||
}); | ||
|
||
test('Streaming RunnableMapInput', () async { | ||
final chain = Runnable.mapInput<ChainValues, ChainValues>( | ||
(final input) => { | ||
'input': '${input['foo']}${input['bar']}', | ||
}, | ||
); | ||
final stream = chain.stream({'foo': 'foo1', 'bar': 'bar1'}); | ||
|
||
final streamList = await stream.toList(); | ||
expect(streamList.length, 1); | ||
|
||
final item = streamList.first; | ||
expect(item, {'input': 'foo1bar1'}); | ||
}); | ||
}); | ||
} |