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

Add tools for Rewind #168

Closed
wants to merge 2 commits into from
Closed

Add tools for Rewind #168

wants to merge 2 commits into from

Conversation

kaavee315
Copy link
Contributor

@kaavee315 kaavee315 commented Jun 17, 2024

PR Type

Enhancement, Tests


Description

  • Added OpenAIChat action to interact with OpenAI's API, supporting both text and image inputs.
  • Implemented ScreenCapture action to capture and save screenshots.
  • Added Notify action to send local notifications on MacOS.
  • Updated TriggerPayloadPropertyModel and TriggerPayloadModel to include optional type and anyOf fields.
  • Registered System tool in LocalToolHandler.
  • Updated example script to use App.SYSTEM tools instead of App.MATHEMATICAL.
  • Added System tool with ScreenCapture and Notify actions.
  • Added Mathematical tool with Calculator action.

Changes walkthrough 📝

Relevant files
Enhancement
11 files
openai.py
Add OpenAI Chat action with text and image support             

composio/local_tools/llm/actions/openai.py

  • Added OpenAIChatRequest and OpenAIChatResponse models.
  • Implemented OpenAIChat action for interacting with OpenAI's API.
  • Included functionality to handle text and image inputs.
  • +50/-0   
    screencapture.py
    Add ScreenCapture action for taking screenshots                   

    composio/local_tools/system/actions/screencapture.py

  • Added ScreenCaptureRequest and ScreenCaptureResponse models.
  • Implemented ScreenCapture action to capture and save screenshots.
  • +38/-0   
    notify.py
    Add Notify action for local notifications on MacOS             

    composio/local_tools/system/actions/notify.py

  • Added NotifyRequest and NotifyResponse models.
  • Implemented Notify action to send local notifications on MacOS.
  • +40/-0   
    __init__.py
    Update TriggerPayload models to support optional type and anyOf fields

    composio/client/init.py

  • Added TypeModel class.
  • Updated TriggerPayloadPropertyModel and TriggerPayloadModel to include
    optional type and anyOf fields.
  • +6/-3     
    local_handler.py
    Register System tool in LocalToolHandler                                 

    composio/client/local_handler.py

  • Imported System tool.
  • Registered System tool in LocalToolHandler.
  • +2/-0     
    tool.py
    Add System tool with ScreenCapture and Notify actions       

    composio/local_tools/system/tool.py

    • Added System tool with ScreenCapture and Notify actions.
    +16/-0   
    tool.py
    Add Mathematical tool with Calculator action                         

    composio/local_tools/llm/tool.py

    • Added Mathematical tool with Calculator action.
    +17/-0   
    __init__.py
    Initialize system actions module with ScreenCapture and Notify

    composio/local_tools/system/actions/init.py

    • Imported ScreenCapture and Notify actions.
    +3/-0     
    __init__.py
    Initialize LLM actions module with OpenAIChat                       

    composio/local_tools/llm/actions/init.py

    • Imported OpenAIChat action.
    +1/-0     
    __init__.py
    Initialize LLM module with Mathematical tool                         

    composio/local_tools/llm/init.py

    • Imported Mathematical tool.
    +1/-0     
    __init__.py
    Initialize system module with System tool                               

    composio/local_tools/system/init.py

    • Imported System tool.
    +1/-0     
    Tests
    1 files
    langchain_math.py
    Update example to use SYSTEM tools instead of MATHEMATICAL

    examples/local_tools/langchain_math.py

    • Changed toolset from App.MATHEMATICAL to App.SYSTEM.
    +1/-1     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 4
    🧪 Relevant tests No
    🔒 Security concerns No
    ⚡ Key issues to review Possible Bug:
    In the OpenAIChat action, the same message is appended twice to the messages list if request_data.image_path is provided. This could lead to unintended behavior or duplicate messages being sent to the OpenAI API.
    Error Handling:
    The ScreenCapture action in screencapture.py catches all exceptions and logs them, but it might be beneficial to handle specific exceptions differently or provide more detailed error messages to the user.
    Platform Specific Code:
    The Notify action in notify.py is specifically designed for MacOS, using osascript. This limits the portability of the code. Consider implementing a cross-platform solution or clearly documenting the platform limitation.

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Return the actual response text from OpenAI instead of a hardcoded string

    The execute method should return the actual response text from OpenAI instead of a
    hardcoded string.

    composio/local_tools/llm/actions/openai.py [50]

    -return {"execution_details": {"executed": True}, "response_data": {"text": "Hello"}}
    +return {"execution_details": {"executed": True}, "response_data": {"text": response.choices[0].message['content']}}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 10

    Why: Returning the actual response from the API is crucial for the functionality of the method, making this change critical to meet the expected behavior.

    10
    Maintainability
    Correct the class docstring to accurately describe the System tool

    Update the class docstring to accurately reflect the purpose of the System tool, as it
    currently mentions "Mathematical Tools for LLM" which seems incorrect.

    composio/local_tools/system/tool.py [9]

    -Mathematical Tools for LLM
    +System Tools for various system actions
     
    • Apply this suggestion
    Suggestion importance[1-10]: 10

    Why: The suggestion addresses a critical documentation error that could lead to confusion about the tool's purpose, thus improving clarity and maintainability.

    10
    Possible issue
    Add exception handling to the execute method to ensure robustness when making API calls

    The execute method should handle potential exceptions when making API calls to OpenAI to
    ensure robustness.

    composio/local_tools/llm/actions/openai.py [33-50]

    -client = openai.OpenAI()
    -messages = [ChatCompletionUserMessageParam(content=request_data.message, role="user")]
    -if request_data.image_path:
    -    image_file = client.files.create(
    -        file=open(request_data.image_path, "rb"),
    -        purpose="vision",
    +try:
    +    client = openai.OpenAI()
    +    messages = [ChatCompletionUserMessageParam(content=request_data.message, role="user")]
    +    if request_data.image_path:
    +        image_file = client.files.create(
    +            file=open(request_data.image_path, "rb"),
    +            purpose="vision",
    +        )
    +        messages.append(ChatCompletionUserMessageParam(content=f"data:image/jpeg;base64,{image_file.data.base64}", role="user"))
    +    response = client.chat.completions.create(
    +        model="gpt-4-turbo",
    +        messages=[
    +            ChatCompletionUserMessageParam(content=request_data.message, role="user"),
    +        ],
         )
    -    messages.append(ChatCompletionUserMessageParam(content=f"data:image/jpeg;base64,{image_file.data.base64}", role="user"))
    -response = client.chat.completions.create(
    -    model="gpt-4-turbo",
    -    messages=[
    -        ChatCompletionUserMessageParam(content=request_data.message, role="user"),
    -    ],
    -)
    -return {"execution_details": {"executed": True}, "response_data": {"text": "Hello"}}
    +    return {"execution_details": {"executed": True}, "response_data": {"text": response.choices[0].message['content']}}
    +except Exception as e:
    +    return {"execution_details": {"executed": False}, "error": str(e)}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding exception handling is essential for robustness, especially in production environments where API calls can fail due to numerous reasons.

    9
    Ensure the directory for the screenshot file path exists before saving the screenshot

    Consider adding a check to ensure the file_path directory exists before attempting to save
    the screenshot. This will prevent potential errors if the directory does not exist.

    composio/local_tools/system/actions/screencapture.py [31]

    +import os
    +os.makedirs(os.path.dirname(file_path), exist_ok=True)
     screenshot.save(file_path)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies a potential error scenario where the directory might not exist, which is crucial for file operations and can prevent runtime errors.

    8
    Add a check to ensure the tools variable is not empty before printing

    Add a check to ensure that the tools variable is not empty before printing, to avoid
    potential issues if no tools are returned.

    examples/local_tools/langchain_math.py [16]

    -print(tools)
    +if tools:
    +    print(tools)
    +else:
    +    print("No tools available.")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: This suggestion improves the robustness of the code by handling cases where no tools are available, which enhances the user experience by providing clearer feedback.

    6
    Best practice
    Rename the method to avoid confusion with the built-in name attribute of enums

    The name property method should be renamed to something more descriptive to avoid
    confusion with the built-in name attribute of enums.

    composio/client/enums.py [14-16]

    -def name(self) -> str:
    +def trigger_name(self) -> str:
         """Returns trigger name."""
         return self.value[0]
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Renaming the method is crucial to avoid confusion with the built-in name attribute, which can lead to subtle bugs or misunderstandings in the codebase.

    8
    Ensure the notification command raises an exception if it fails by using subprocess.run with check=True

    Use subprocess.run with check=True to ensure that an exception is raised if the
    notification command fails, allowing for better error handling.

    composio/local_tools/system/actions/notify.py [32]

    -subprocess.run(command, shell=True)
    +subprocess.run(command, shell=True, check=True)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This is a valid improvement for error handling in subprocess operations, ensuring that failures in the notification command are caught and can be handled appropriately.

    7
    Performance
    Use a set for membership testing to improve performance

    The is_local property method should use a set for membership testing instead of a list for
    better performance.

    composio/client/enums.py [424]

    -return self.value.lower() in ["mathematical", "localworkspace", "cmdmanagertool", "historykeeper", "ragtool", "webtool", "greptile", "submitpatchtool", "system"]
    +local_apps = {"mathematical", "localworkspace", "cmdmanagertool", "historykeeper", "ragtool", "webtool", "greptile", "submitpatchtool", "system"}
    +return self.value.lower() in local_apps
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Using a set for membership testing is a valid performance optimization, especially if the list of items grows larger.

    6

    @kaavee315 kaavee315 marked this pull request as draft June 28, 2024 09:17
    @kaavee315 kaavee315 closed this Jul 2, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants