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

refactor: deactivate caching if a component is part of a cycle #3694

Merged
merged 32 commits into from
Oct 2, 2024

Conversation

ogabrielluiz
Copy link
Contributor

This pull request adds cycle detection and caching functionality to the Vertex class. It introduces the has_cycle_edges method to check if a vertex has any cycle edges. Additionally, it includes the apply_on_outputs method to apply functions to the outputs of a vertex. The pull request also adds a utility function, find_cycle_vertices, to find all vertices that are part of cycles in a directed graph. The instantiate_component method is updated to use the initialize.loading.instantiate_class function for custom component instantiation. Unit tests are added for the find_cycle_vertices utility function. The set_cache_to_vertices_in_cycle method is introduced to enable caching for vertices involved in cycles. The vertex instantiation process is refactored for better code organization.

@github-actions github-actions bot added the refactor Maintenance tasks and housekeeping label Sep 5, 2024
Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-3694.dmtpw4p5recq1.amplifyapp.com

@ogabrielluiz ogabrielluiz marked this pull request as ready for review September 6, 2024 17:42
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Sep 6, 2024
@ogabrielluiz ogabrielluiz marked this pull request as draft September 6, 2024 21:04
@ogabrielluiz ogabrielluiz force-pushed the feat/disablecacheincycle branch 2 times, most recently from 5b41b5c to c091958 Compare September 23, 2024 20:05
@ogabrielluiz ogabrielluiz marked this pull request as ready for review September 23, 2024 21:04
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Sep 24, 2024
@@ -1632,7 +1657,7 @@ def build_edge(self, edge: EdgeData) -> CycleEdge | Edge:
raise ValueError(f"Source vertex {edge['source']} not found")
if target is None:
raise ValueError(f"Target vertex {edge['target']} not found")
if (source.id, target.id) in self.cycles:
if any(v in self.cycle_vertices for v in [source.id, target.id]):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phact this is what made things start working.



@pytest.mark.api_key_required
def test_updated_graph_with_prompts():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phact Check out this test to understand a bit more what is happening.

chat_output = ChatOutput(_id="chat_output")
chat_output.set(input_value=text_output.text_response)

graph = Graph(chat_input, chat_output)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not specific to this PR but how come you need to pass an end component to Graph? Wouldn't it eventually get added through the adjacency lists?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. This is kinda weird still. We went for a syntax similar to Keras' Functional API.
The end shouldn't be necessary but it has its uses still, I just didn't mind going over it yet.

The `has_cycle_edges` method is added to the `Vertex` class to check if the vertex has any cycle edges. Additionally, the `instantiate_component` method is updated to use the `initialize.loading.instantiate_class` function for custom component instantiation.
- Implement `find_cycle_vertices` function to identify all vertices that are part of cycles in a directed graph.
- Utilize depth-first search (DFS) to detect cycles and collect vertices involved in those cycles.
- Introduced `_set_cache_to_vertices_in_cycle` method to enable caching for vertices involved in cycles.
- Added `find_cycle_vertices` import to support the new method.
- Refactored vertex instantiation into `_instantiate_components_in_vertices` method for better code organization.
Refactor the `_set_cache_to_vertices_in_cycle` method to improve caching logic for vertices involved in cycles. Instead of setting the `cache` attribute to `True`, it is now set to `False` for better clarity and consistency. This change ensures that the cache is properly handled for vertices in cycles.
…and add new test case

- Removed the `entry_point` parameter from all test cases for `find_cycle_vertices`.
- Added a new parameterized test case `test_handle_two_inputs_in_cycle` to verify handling of cycles with two inputs.
…ntegration

- Introduce `test_updated_graph_with_prompts` to validate graph cyclicity and execution.
- Integrate `PromptComponent`, `OpenAIModelComponent`, and `ConditionalRouterComponent` in the test.
- Ensure graph execution with a maximum of 20 iterations and cache disabled.
- Validate the presence of expected output vertices in the results.
- Introduced `default_value` to handle cases where edges are cycles and target parameters are present.
- Ensured that `default_value` is returned if defined, preventing errors when the component is not built.
…date assertions

- Simplified component initialization using method chaining.
- Corrected router input and message parameters to use openai_component_1.
- Updated assertions to check for correct output IDs.
- Introduced `_cycle_vertices` attribute to store vertices involved in cycles.
- Added `cycle_vertices` property to compute and cache cycle vertices.
- Updated edge creation logic to use `cycle_vertices` for cycle detection.
- Changed router operator from "equals" to "contains".
- Consolidated chat output to a single component.
- Updated graph construction to use a single chat output.
- Replaced `_snapshot` with `get_snapshot` for graph state capture.
- Adjusted assertions to reflect the updated graph structure and outputs.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Oct 1, 2024
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Oct 2, 2024
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Oct 2, 2024
@ogabrielluiz ogabrielluiz enabled auto-merge (squash) October 2, 2024 17:57
@ogabrielluiz ogabrielluiz merged commit 4221fa4 into main Oct 2, 2024
29 checks passed
@ogabrielluiz ogabrielluiz deleted the feat/disablecacheincycle branch October 2, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lgtm This PR has been approved by a maintainer refactor Maintenance tasks and housekeeping size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants