-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
feat: add dataframe to Loop component, fixes loop input error #6996
Open
rodrigosnader
wants to merge
20
commits into
main
Choose a base branch
from
loop_component_dataframe_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b8e5aba
add dataframe support for the loop component
rodrigosnader fb4228b
[autofix.ci] apply automated fixes
autofix-ci[bot] 48daea6
[autofix.ci] apply automated fixes (attempt 2/3)
autofix-ci[bot] c026756
Merge branch 'main' into loop_component_dataframe_support
edwinjosechittilappilly 59105f1
Merge branch 'main' into loop_component_dataframe_support
italojohnny d333703
fix: starter project
italojohnny 5224c33
Merge branch 'main' into loop_component_dataframe_support
edwinjosechittilappilly 4852752
update loop component and tests
edwinjosechittilappilly 463257b
Merge branch 'main' into loop_component_dataframe_support
edwinjosechittilappilly 70f9524
[autofix.ci] apply automated fixes
autofix-ci[bot] 235b128
[autofix.ci] apply automated fixes (attempt 2/3)
autofix-ci[bot] 7fa0b0b
Merge branch 'main' into loop_component_dataframe_support
edwinjosechittilappilly 3f4db93
update logic
edwinjosechittilappilly a1d0904
Update loop_basic.py
edwinjosechittilappilly 9678729
Update LoopTest.json
edwinjosechittilappilly ebf6e61
Update Research Translation Loop.json
edwinjosechittilappilly bfe6e48
fix lint
edwinjosechittilappilly 0b324d2
format fix
edwinjosechittilappilly 5a357b6
[autofix.ci] apply automated fixes
autofix-ci[bot] fc9cbef
Merge branch 'main' into loop_component_dataframe_support
edwinjosechittilappilly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
116 changes: 116 additions & 0 deletions
116
src/backend/base/langflow/components/logic/loop_basic.py
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,116 @@ | ||
from langflow.custom import Component | ||
from langflow.io import HandleInput, Output | ||
from langflow.schema import Data | ||
from langflow.schema.dataframe import DataFrame | ||
|
||
|
||
class BasicLoopComponent(Component): | ||
display_name = "Loop" | ||
description = ( | ||
"Iterates over a list of Data objects, outputting one item at a time and aggregating results from loop inputs." | ||
) | ||
icon = "infinity" | ||
|
||
inputs = [ | ||
HandleInput( | ||
name="data", | ||
display_name="Data or DataFrame", | ||
info="The initial list of Data objects or DataFrame to iterate over.", | ||
input_types=["Data", "DataFrame"], | ||
), | ||
] | ||
|
||
outputs = [ | ||
Output(display_name="Item", name="item", method="item_output", allows_loop=True), | ||
Output(display_name="Done", name="done", method="done_output"), | ||
] | ||
|
||
def initialize_data(self) -> None: | ||
"""Initialize the data list, context index, and aggregated list.""" | ||
if self.ctx.get(f"{self._id}_initialized", False): | ||
return | ||
|
||
# Ensure data is a list of Data objects | ||
data_list = self._validate_data(self.data) | ||
|
||
# Store the initial data and context variables | ||
self.update_ctx( | ||
{ | ||
f"{self._id}_data": data_list, | ||
f"{self._id}_index": 0, | ||
f"{self._id}_aggregated": [], | ||
f"{self._id}_initialized": True, | ||
} | ||
) | ||
|
||
def _validate_data(self, data): | ||
"""Validate and return a list of Data objects.""" | ||
if isinstance(data, DataFrame): | ||
return data.to_data_list() | ||
if isinstance(data, Data): | ||
return [data] | ||
if isinstance(data, list) and all(isinstance(item, Data) for item in data): | ||
return data | ||
msg = "The 'data' input must be a DataFrame, a list of Data objects, or a single Data object." | ||
raise TypeError(msg) | ||
|
||
def evaluate_stop_loop(self) -> bool: | ||
"""Evaluate whether to stop item or done output.""" | ||
current_index = self.ctx.get(f"{self._id}_index", 0) | ||
data_length = len(self.ctx.get(f"{self._id}_data", [])) | ||
return current_index > data_length | ||
|
||
def item_output(self) -> Data: | ||
"""Output the next item in the list or stop if done.""" | ||
self.initialize_data() | ||
current_item = Data(text="") | ||
|
||
if self.evaluate_stop_loop(): | ||
self.stop("item") | ||
return Data(text="") | ||
|
||
# Get data list and current index | ||
data_list, current_index = self.loop_variables() | ||
if current_index < len(data_list): | ||
# Output current item and increment index | ||
try: | ||
current_item = data_list[current_index] | ||
except IndexError: | ||
current_item = Data(text="") | ||
self.aggregated_output() | ||
self.update_ctx({f"{self._id}_index": current_index + 1}) | ||
return current_item | ||
|
||
def done_output(self) -> DataFrame: | ||
"""Trigger the done output when iteration is complete.""" | ||
self.initialize_data() | ||
|
||
if self.evaluate_stop_loop(): | ||
self.stop("item") | ||
self.start("done") | ||
|
||
aggregated = self.ctx.get(f"{self._id}_aggregated", []) | ||
|
||
return DataFrame(aggregated) | ||
self.stop("done") | ||
return DataFrame([]) | ||
|
||
def loop_variables(self): | ||
"""Retrieve loop variables from context.""" | ||
return ( | ||
self.ctx.get(f"{self._id}_data", []), | ||
self.ctx.get(f"{self._id}_index", 0), | ||
) | ||
|
||
def aggregated_output(self) -> list[Data]: | ||
"""Return the aggregated list once all items are processed.""" | ||
self.initialize_data() | ||
|
||
# Get data list and aggregated list | ||
data_list = self.ctx.get(f"{self._id}_data", []) | ||
aggregated = self.ctx.get(f"{self._id}_aggregated", []) | ||
loop_input = self.get_loop_output_value("item") | ||
if loop_input is not None and not isinstance(loop_input, str) and len(aggregated) <= len(data_list): | ||
aggregated.append(loop_input) | ||
self.update_ctx({f"{self._id}_aggregated": aggregated}) | ||
return aggregated |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using '>= data_length' instead of '> data_length' so that the loop terminates correctly when the current index equals the length of the data list.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.