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

Improve UX for action processor #709

Merged
merged 12 commits into from
Oct 18, 2024
Merged

Improve UX for action processor #709

merged 12 commits into from
Oct 18, 2024

Conversation

tushar-composio
Copy link
Collaborator

@tushar-composio tushar-composio commented Oct 15, 2024

Adds a processors field on the execute_action and get_tools methods, and deprecates the use of processors field in the ComposioToolSet constructor.


Important

Adds processors parameter to execute_action and get_tools, deprecating its use in ComposioToolSet constructor, with tests for new functionality.

  • Behavior:
    • Adds processors parameter to execute_action and get_tools methods in ComposioToolSet and derived classes.
    • Deprecates processors parameter in ComposioToolSet constructor.
  • Methods:
    • Updates execute_action in toolset.py to merge processors if provided.
    • Updates get_tools in toolset.py and derived classes to handle processors.
  • Deprecation:
    • Adds deprecation warning for processors in ComposioToolSet constructor.
  • Tests:
    • Adds tests in test_toolset.py for new processors handling in execute_action and get_tools methods.

This description was created by Ellipsis for fe574c6. It will automatically update as commits are pushed.

Copy link

github-actions bot commented Oct 15, 2024

This comment was generated by github-actions[bot]!

JS SDK Coverage Report

📊 Coverage report for JS SDK can be found at the following URL:
https://pub-92e668239ab84bfd80ee07d61e9d2f40.r2.dev/coverage-11399715390/coverage/index.html

📁 Test report folder can be found at the following URL:
https://pub-92e668239ab84bfd80ee07d61e9d2f40.r2.dev/html-report-11399715390/html-report/report.html

@tushar-composio tushar-composio marked this pull request as ready for review October 17, 2024 09:03
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Reviewed everything up to d1b40fc in 24 seconds

More details
  • Looked at 492 lines of code in 11 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/tests/test_tools/test_toolset.py:158
  • Draft comment:
    The test for the processors field in ComposioToolSet constructor should include a check for the deprecation warning. Use pytest.warns to ensure the warning is raised.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable:
    The comment suggests a change that is not clearly required by the current code. There is no evidence in the code that a deprecation warning is expected or should be tested for. Without strong evidence that this is necessary, the comment should be removed.
    I might be missing some context about the ComposioToolSet class and whether there is a known deprecation warning that should be tested for. However, based on the provided code, there is no indication of this.
    The absence of any deprecation-related code or comments in the provided diff suggests that the comment is speculative. Without clear evidence, it is safer to remove the comment.
    Remove the comment as it suggests a speculative change without strong evidence from the code.

Workflow ID: wflow_jHYJFUUhDwR047kN


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on 36e2317 in 13 seconds

More details
  • Looked at 41 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/tests/test_tools/test_toolset.py:11
  • Draft comment:
    Organize imports according to PEP 8: standard library imports, related third-party imports, and local application/library-specific imports.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The import order should follow PEP 8 guidelines for better readability and organization.

Workflow ID: wflow_gcy0Sdepb2WAsE3z


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on bf13af4 in 17 seconds

More details
  • Looked at 20 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/tests/test_tools/test_toolset.py:21
  • Draft comment:
    The docstring mentions find_actions_by_tags, but this test is for get_action_schemas. Please update the docstring to reflect the correct method being tested.
  • Reason this comment was not posted:
    Comment was on unchanged code.

Workflow ID: wflow_xxl9Spt2xKkq7Eyn


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on 2fc40f3 in 14 seconds

More details
  • Looked at 12 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 0 drafted comments based on config settings.

Workflow ID: wflow_UMZVp5y2XX8B8yQN


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on 431ce02 in 14 seconds

More details
  • Looked at 21 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/tox.ini:127
  • Draft comment:
    Uncommenting the pip install plugins/langchain line means this plugin will be installed during the test setup. Ensure this is necessary for the tests being run, as it could introduce dependency conflicts or unnecessary installations.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The change in the tox.ini file is minor and involves uncommenting a line to install a plugin. This is a straightforward change, but it should be noted that uncommenting this line means the plugin will be installed during the test environment setup, which could have implications if there are dependency conflicts or if the plugin is not needed for all tests.

Workflow ID: wflow_G8CkGfQVxIbiDWMs


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on 8d2ce33 in 21 seconds

More details
  • Looked at 19 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/tests/test_tools/test_toolset.py:215
  • Draft comment:
    The test test_processors_on_get_tools is not testing the processors field in get_tools() method as described. Instead, it is testing the execute_action method. This is a logical error in the test case.
  • Reason this comment was not posted:
    Comment was on unchanged code.

Workflow ID: wflow_ShZRi0l4HRFr96jT


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

python/tox.ini Outdated
@@ -124,14 +124,15 @@ commands =

; TODO: Extract plugin tests separately
; Installing separately because of the dependency conflicts
pip install plugins/langchain
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pip install plugins/langchain
pip install plugins/langchain --no-deps

And add the langchain plugin dependencies here - https://github.com/ComposioHQ/composio/blob/ENG-1726/python/tox.ini#L89

Copy link
Collaborator

Choose a reason for hiding this comment

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

Reason is, tox installs the current top-level package (composio-core) in this case before running the tests and running pip install plugins/langchain might overwrite the composio-core installation which might lead to tests breaking.

Comment on lines 608 to 613
# Users may not respect our type annotations and return something that isn't a dict.
# If that happens we should show a friendly error message.
if not isinstance(data, t.Dict):
raise TypeError(
f"Expected {type_}-processor to return 'dict', got {type(data).__name__!r}"
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would suggest log a warning instead raising error since this can interrupt the agent execution and agent might get stuck in a loop, also as long as the return object is JSON serialisable there won't be any issue so just a warning should be fine. We can improve the documentation around this.

for processor_type in self._processors.keys():
if processor_type in processors:
processor_type = t.cast(
te.Literal["pre", "post", "schema"], processor_type
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can extract te.Literal["pre", "post", "schema"] as ProcessorType

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

❌ Changes requested. Incremental review on fe574c6 in 38 seconds

More details
  • Looked at 141 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 drafted comments based on config settings.
1. python/composio/tools/toolset.py:611
  • Draft comment:
    The deprecation warning message for 'processors' could be clearer. Consider rephrasing to: "The 'processors' parameter in the ToolSet constructor is deprecated. Please provide it to the 'get_tools()' or 'execute_action()' methods instead."
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The deprecation warning for the 'processors' parameter in the ComposioToolSet constructor is correctly implemented, but the warning message could be improved for clarity.
2. python/composio/tools/toolset.py:661
  • Draft comment:
    The check for processor_type in self._processors is unnecessary since existing_processors is initialized if not present. You can simplify the code by directly updating existing_processors.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The _merge_processors method can be simplified by removing the unnecessary check for processor_type in self._processors, as it is already being initialized if not present.

Workflow ID: wflow_LfSkGQnw8vExuZVv


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

python/composio/tools/toolset.py Show resolved Hide resolved
@tushar-composio tushar-composio enabled auto-merge (squash) October 18, 2024 09:21
@angrybayblade angrybayblade merged commit 4f3bb98 into master Oct 18, 2024
6 of 7 checks passed
@angrybayblade angrybayblade deleted the ENG-1726 branch October 18, 2024 09:41
@@ -67,22 +67,28 @@
P = te.ParamSpec("P")

_KeyType = t.Union[AppType, ActionType]
_ProcessorType = t.Callable[[t.Dict], t.Dict]
_CallableType = t.Callable[[t.Dict], t.Dict]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider using a more specific type for _CallableType. Instead of t.Callable[[t.Dict], t.Dict], you could use t.Callable[[t.Dict[str, t.Any]], t.Dict[str, t.Any]] to be more precise about the input and output types.

else {"post": {}, "pre": {}, "schema": {}}
)
if processors is not None:
warnings.warn(
Copy link
Collaborator

Choose a reason for hiding this comment

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

The warning message here could be more specific. Instead of just mentioning 'processors', it could say "Setting 'processors' directly on the ToolSet instance is deprecated..."

@@ -628,6 +650,22 @@ def _process_schema_properties(self, action: Action, properties: t.Dict) -> t.Di
type_="schema",
)

def _merge_processors(self, processors: ProcessorsType) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding a type hint for the processors parameter in the _merge_processors method signature.

# Improperly defined processors
preprocessor_called = postprocessor_called = False

def weird_postprocessor(reponse: dict) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The variable name reponse in the weird_postprocessor function is misspelled. It should be response.

@shreysingla11
Copy link
Collaborator

I've completed the review of this pull request. Overall, the changes look good and improve the functionality of the toolset. Here's a summary of my findings:

  1. The code introduces a new processors parameter in various methods, which enhances flexibility.
  2. There are some minor improvements that could be made in type annotations and variable naming.
  3. The deprecation warning for setting processors directly on the ToolSet instance is a good practice.

In terms of code quality, I'd rate this PR as 8/10. The changes are well-structured and improve the existing codebase. The addition of new tests is particularly commendable. There are just a few minor points that could be addressed to make it even better.

Great job on this PR!

@@ -628,6 +650,22 @@ def _process_schema_properties(self, action: Action, properties: t.Dict) -> t.Di
type_="schema",
)

def _merge_processors(self, processors: ProcessorsType) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding a type hint for the processors parameter in the _merge_processors method to improve code readability and maintainability.

# Improperly defined processors
preprocessor_called = postprocessor_called = False

def weird_postprocessor(reponse: dict) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The weird_postprocessor function in the test case doesn't follow the expected signature for a post-processor. It should return a dict, but it's currently returning None. Consider updating the function to return the modified response.

@shreysingla11
Copy link
Collaborator

I've completed the review of the pull request. Overall, the changes look good and improve the functionality of the toolset by adding support for processors in various methods. Here's a summary of my observations:

  1. The implementation of processors in the ComposioToolSet class and its subclasses is consistent and well-structured.
  2. The deprecation warning for setting processors in the constructor is a good approach for maintaining backward compatibility while encouraging the use of the new method.
  3. The test cases cover various scenarios, including the happy path and edge cases with improperly defined processors.

There are a couple of minor suggestions for improvement:

  1. Consider adding a type hint for the processors parameter in the _merge_processors method to improve code readability.
  2. In the test case for the weird_postprocessor, it might be helpful to add a comment explaining that the function intentionally doesn't follow the expected signature to test the warning system.

Overall, the code quality is good, and the changes seem to enhance the flexibility and usability of the toolset. Good job!


MetadataType = t.Dict[_KeyType, t.Dict]
ParamType = t.TypeVar("ParamType")

# Enable deprecation warnings
warnings.simplefilter("always", DeprecationWarning)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding a type hint for the warnings module to improve code readability and maintainability.

warnings.simplefilter("always", DeprecationWarning)


ProcessorType = te.Literal["pre", "post", "schema"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

The ProcessorType type alias is defined but not used in this file. Consider removing it if it's not needed, or use it to improve type checking in the relevant parts of the code.

@@ -628,6 +650,22 @@ def _process_schema_properties(self, action: Action, properties: t.Dict) -> t.Di
type_="schema",
)

def _merge_processors(self, processors: ProcessorsType) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The _merge_processors method modifies the self._processors dictionary in-place. Consider returning a new dictionary instead of modifying the existing one to maintain immutability and prevent potential side effects.

# Improperly defined processors
preprocessor_called = postprocessor_called = False

def weird_postprocessor(reponse: dict) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The weird_postprocessor function is defined but not used in the test. Consider removing it or using it in a test case to ensure it behaves as expected.

@shreysingla11
Copy link
Collaborator

I've completed the review of the PR. Here's a summary of the changes and my observations:

  1. The PR introduces a new processors parameter to the get_tools() and execute_action() methods, allowing for more flexible pre and post-processing of requests and responses.

  2. A deprecation warning has been added for setting processors on the ToolSet initialization, encouraging users to use the new method-level processors instead.

  3. The changes are well-tested, with new test cases added to cover the new functionality.

  4. The code quality is generally good, with clear naming conventions and logical structure.

Some minor suggestions for improvement:

  • Consider adding type hints for imported modules like warnings.
  • The ProcessorType type alias is defined but not used; consider removing it or using it more extensively.
  • The _merge_processors method modifies state in-place; consider a more functional approach.
  • In the test file, there's an unused weird_postprocessor function that could be removed or utilized in a test case.

Overall, this PR seems to improve the flexibility and usability of the toolset. The changes are well-implemented and thoroughly tested. With a few minor adjustments, this PR would be ready for merging.

@@ -628,6 +650,22 @@ def _process_schema_properties(self, action: Action, properties: t.Dict) -> t.Di
type_="schema",
)

def _merge_processors(self, processors: ProcessorsType) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the _merge_processors method, consider adding a type check for the processors parameter to ensure it's of type ProcessorsType. This will help catch potential errors early and improve type safety.

self._processors[processor_type] = existing_processors

existing_processors.update(new_processors)

@_record_action_if_available
def execute_action(
self,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding type hints for the processors parameter in the execute_action method. This will improve type checking and make the method signature more consistent with other parts of the codebase.

@@ -628,6 +650,22 @@ def _process_schema_properties(self, action: Action, properties: t.Dict) -> t.Di
type_="schema",
)

def _merge_processors(self, processors: ProcessorsType) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding a docstring for the new _merge_processors method to explain its purpose and usage. This will help other developers understand the method's functionality and how to use it correctly.

@@ -593,6 +608,13 @@ def _process(
f" through: {processor.__name__}"
)
data = processor(data)
# Users may not respect our type annotations and return something that isn't a dict.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding more comprehensive error handling for cases where processors might fail or return unexpected types. While you've added a warning for unexpected return types, it might be beneficial to have a more robust error handling strategy, possibly including custom exceptions or fallback behavior.

@shreysingla11
Copy link
Collaborator

Overall, this pull request introduces valuable improvements to the processor functionality in the ComposioToolSet. The addition of the ProcessorsType, the _merge_processors method, and the optional processors parameter in execute_action enhances the flexibility of the toolset.

Here's a summary of the changes and some suggestions for further improvement:

  1. The new processor functionality allows for more flexible configuration at different levels (toolset initialization, execute_action, and get_tools).
  2. The deprecation warning for the old method of setting processors is a good practice for maintaining backwards compatibility.
  3. The updates to all plugin files to include the new processors parameter in their get_tools method ensures consistency across the codebase.
  4. The new tests in test_toolset.py provide good coverage for the new functionality.

Suggestions for further improvement:

  1. Consider adding type hints for the processors parameter in the execute_action method for consistency and improved type checking.
  2. Add a docstring for the new _merge_processors method to explain its purpose and usage.
  3. Consider implementing more comprehensive error handling for cases where processors might fail or return unexpected types.
  4. Provide clear documentation and examples in the README or documentation files on how to use the new processors functionality.
  5. Consider adding performance benchmarks to ensure that the addition of processors doesn't significantly impact the toolset's performance.
  6. Look into a more comprehensive solution for managing plugin dependencies in the future, as the current approach of installing langchain separately might not scale well for multiple plugins.

Overall, this is a solid improvement to the codebase that enhances its flexibility and maintainability. With a few minor additions to documentation and error handling, it will be even more robust and user-friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants