Custom Actions are powerful building blocks that enable you to create custom functionality while running independently. They can handle everything from simple data processing to complex third-party integrations.

Creating a Custom Tool without Authentication

1

Install required libraries

pip install composio-openai openai
2

Import libraries

from composio_openai import ComposioToolSet, action
from openai import OpenAI

openai_client = OpenAI()
toolset = ComposioToolSet()
3

Creating a custom action

Below are examples of creating a custom action called my_custom_action (Python) & myCustomAction (JavaScript) to make cowsay say whatever you want it to say.

You need to add the action, input parameters & return content description, this is what the LLM will use to understand the action.

@action(toolname="cow", requires=["cowsay"])
def my_custom_action(message: str) -> str:
    """
    Cow will say whatever you want it to say.

    :param message: Message to be displayed
    :return greeting: Formatted message.
    """
    import cowsay
    
    return cowsay.get_output_string("cow", message)
4

Executing the Custom Action

Executing the custom action using LLM. Learn how to execute the custom action without LLM here.

tools = toolset.get_tools(actions=[my_custom_action])

task = "Say 'AI is the future' using cowsay"

response = openai_client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=
    [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": task},
    ],
)

result = toolset.handle_tool_calls(response)
print(result)

Output from executing Custom Action

  ________________
| AI is the future |
  ================
                \
                 \
                   ^__^
                   (oo)\_______
                   (__)\       )\/\
                       ||----w |
                       ||     ||

Why Use Custom Tools or Actions?

Custom Tools or Actions provide several advantages:

  • Data privacy: Execution happens on the user’s machine, ensuring sensitive data doesn’t leave the local environment
  • Flexibility: Users can create and customize as many tools and actions as needed
  • Compatibility: Custom actions can be integrated seamlessly across various Composio-supported platforms