Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add a no_collect: bool=False param to RunnableLambda, enables per-chunk processing instead of whole collected input #21413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rhighs
Copy link

@rhighs rhighs commented May 8, 2024

This PR introduces a new param no_collect: bool to RunnableLambda.__init__. This flags allows for the _transform and _atransform methods to not collect input before processing but rather to process each individual chunk via self.func/self.afunc. This is particularly useful when we want to keep the input stream flowing as it gets transformed. One specific issue this implementation has solved on my part is being able to have a streamed conversation chain whilst being able to save the final chain output in a context memory. This is a problem some other ppl have encountered in this issue: #11945

Example usage:

def generate_values(input: Iterator[Any]) -> Iterator[int]:
    yield 1
    yield 2
    yield 3
    yield 4

def square(input: Input) -> int:
    n = cast(int, input)
    return n * n

expected_values = [1, 4, 9, 16]
runnable = RunnableGenerator(generate_values) | RunnableLambda(
   square, no_collect=True
)
for i, value in enumerate(runnable.stream({"input": "some_input"})):
    assert value == expected_values[i]

twitter: @rhighs_

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label May 8, 2024
Copy link

vercel bot commented May 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview May 8, 2024 8:15am

@dosubot dosubot bot added the 🤖:improvement Medium size change to existing code to handle new use-cases label May 8, 2024
@ccurme
Copy link
Collaborator

ccurme commented May 8, 2024

cc @nfcampos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants