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 support for disabling remote enum fetching #603

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ fmt: format-code

.PHONY: check-code
check-code:
tox -e isort-check
tox -e black-check
tox -e flake8
tox -e mypy
tox -e pylint
tox run-parallel -e isort-check,black-check,flake8,mypy,pylint

.PHONY: chk
chk: check-code
Expand Down
15 changes: 15 additions & 0 deletions python/composio/client/enums/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Enum helper base.
"""

import os
import typing as t
import warnings
from pathlib import Path
Expand All @@ -25,6 +26,10 @@
ACTIONS_CACHE = LOCAL_CACHE_DIRECTORY / "actions"
TRIGGERS_CACHE = LOCAL_CACHE_DIRECTORY / "triggers"

NO_REMOTE_ENUM_FETCHING = (
os.environ.get("COMPOSIO_NO_REMOTE_ENUM_FETCHING", "false") == "true"
)


class EnumStringNotFound(ComposioSDKError):
"""Raise when user provides invalid enum string."""
Expand Down Expand Up @@ -197,6 +202,16 @@ def _cache_from_local(self) -> t.Optional[EntityType]:
return None

def _cache_from_remote(self) -> EntityType:
if NO_REMOTE_ENUM_FETCHING:
raise ComposioSDKError(
message=(
f"No metadata found for enum `{self.slug}`, "
"You might be trying to use an app or action "
"that is deprecated, run `composio apps update` "
"and try again"
)
)

from composio.client import Composio # pylint: disable=import-outside-toplevel
from composio.client.endpoints import ( # pylint: disable=import-outside-toplevel
v2,
Expand Down
17 changes: 13 additions & 4 deletions python/plugins/claude/claude_demo.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
"""
Anthropic claude demo.
Anthropic claude demo.7
"""

import anthropic
import dotenv

from composio_claude import App, ComposioToolset
from composio_claude import App, ComposioToolSet


# Load environment variables from .env
dotenv.load_dotenv()

# Initialize tools.
claude_client = anthropic.Anthropic()
composio_toolset = ComposioToolset()
composio_toolset = ComposioToolSet()

# Define task.
task = "Star a repo composiohq/composio on GitHub"

# Get GitHub tools that are pre-configured
actions = composio_toolset.get_tools(apps=[App.GITHUB])

# Get executor
try:
# anthropic<0.27.0
executor = claude_client.beta.tools
except AttributeError:
# anthropic>=0.27.0
executor = claude_client


# Get response from the LLM
response = claude_client.beta.tools.messages.create(
response = executor.messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
tools=actions,
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/claude/composio_claude/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from composio import Action, App, Tag, Trigger, WorkspaceType

from composio_claude.toolset import ComposioToolset
from composio_claude.toolset import ComposioToolSet


ComposioToolset = ComposioToolSet


__all__ = (
Expand All @@ -10,4 +13,5 @@
"Trigger",
"WorkspaceType",
"ComposioToolset",
"ComposioToolSet",
)
17 changes: 12 additions & 5 deletions python/plugins/claude/composio_claude/toolset.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import typing as t

import typing_extensions as te
from anthropic.types.beta.tools import ToolUseBlock, ToolsBetaMessage
from anthropic.types.beta.tools.tool_param import ToolParam


try:
from anthropic.types.beta.tools import ToolUseBlock, ToolsBetaMessage
from anthropic.types.beta.tools.tool_param import ToolParam
except ModuleNotFoundError:
from anthropic.types.tool_use_block import ToolUseBlock
from anthropic.types.tool_param import ToolParam
from anthropic.types.message import Message as ToolsBetaMessage

from composio import Action, ActionType, AppType, TagType
from composio.constants import DEFAULT_ENTITY_ID
from composio.tools import ComposioToolSet as BaseComposioToolSet
from composio.tools.schema import ClaudeSchema, SchemaType


class ComposioToolset(
class ComposioToolSet(
BaseComposioToolSet,
runtime="claude",
description_char_limit=1024,
Expand All @@ -22,15 +29,15 @@ class ComposioToolset(
```python
import anthropic
import dotenv
from composio_claude import App, ComposioToolset
from composio_claude import App, ComposioToolSet


# Load environment variables from .env
dotenv.load_dotenv()

# Initialize tools.
claude_client = anthropic.Anthropic()
composio_tools = ComposioToolset()
composio_tools = ComposioToolSet()

# Define task.
task = "Star a repo composiohq/composio on GitHub"
Expand Down
25 changes: 24 additions & 1 deletion python/tests/test_client/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from typing import Dict, List
from unittest import mock

import pytest
from pydantic import BaseModel

from composio import action
from composio.client.enums import Action, App, Tag, Trigger
from composio.client.enums import Action, App, Tag, Trigger, base
from composio.exceptions import ComposioSDKError
from composio.tools.base.local import LocalAction, LocalTool


Expand Down Expand Up @@ -83,6 +85,27 @@ def test_load_remote_trigger(self, _patch) -> None:
assert enum.slug == Trigger.GITHUB_COMMIT_EVENT.slug


class TestDisableRemoteCaching:
def setup_method(self) -> None:
base.NO_REMOTE_ENUM_FETCHING = True

def teardown_method(self) -> None:
base.NO_REMOTE_ENUM_FETCHING = False

def test_error(self) -> None:
"""Test `NO_REMOTE_ENUM_FETCHING` set to True."""
enum = Action.GITHUB_META_ROOT
with pytest.raises(
ComposioSDKError,
match=(
"No metadata found for enum `GITHUB_META_ROOT`, You might be "
"trying to use an app or action that is deprecated, run "
"`composio apps update` and try again"
),
):
enum._cache_from_remote() # pylint: disable=protected-access


def test_tag_enum() -> None:
"""Test `Tag` enum."""
tag = Tag("ASANA_ALLOCATIONS")
Expand Down
5 changes: 4 additions & 1 deletion python/tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@
"values": ["composio_output/CODEINTERPRETER_GET_FILE_CMD_default_", ""],
},
"env": {"OPENAI_API_KEY": OPENAI_API_KEY, "COMPOSIO_API_KEY": COMPOSIO_API_KEY},
"cwd": EXAMPLES_PATH / "quickstarters" / "sql_agent" / "sql_agent_plotter_crewai",
"cwd": EXAMPLES_PATH
/ "quickstarters"
/ "sql_agent"
/ "sql_agent_plotter_crewai",
},
"multi_entity_api_key": {
"plugin": "langchain",
Expand Down
Loading