feat!: Support different logic for streaming in RunnableFunction #394
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is common to need to map the output value of a previous runnable to a new value that conforms to the input requirements of the next runnable.
Runnable.mapInput
,Runnable.mapInputStream
,Runnable.getItemFromMap
, andRunnable.getMapFromInput
are the easiest way to do that with minimal boilerplate. However, sometimes you may need more control over the input and output values. This is whereRunnable.fromFunction
comes in.The main differences between
Runnable.mapInput
andRunnable.fromFunction
are:Runnable.fromFunction
allows you to define separate logic for invoke vs stream.Runnable.fromFunction
allows you to access the invocation options.In the following example, we use
Runnable.fromFunction
to log the output value of the previousRunnable
. Note that we have print different messages depending on whether the chain is invoked or streamed.When we invoke the chain, we get the following output:
When we stream the chain, we get the following output:
Migration guide
For most of the cases, you can just use
Runnable.mapInput
where you were usingRunnable.fromFunction
. Eg.:Before:
After::
If you use tear-off instead of passing a lamda, now you can call it directly. Eg.:
Before:
After::
In case you need to access the invocation options, or you need different logic for invoke vs stream then you can make use of
Runnable.fromFunction
as shown in the example above.For more info, check the LangChain Expression Language docs.