Skip to content

Commit

Permalink
Merge pull request #70 from SamparkAI/feat/entity-id-arg
Browse files Browse the repository at this point in the history
Entity id argument usage on plugins
  • Loading branch information
angrybayblade authored May 30, 2024
2 parents 8a969ea + 7e16769 commit e4398cf
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 39 deletions.
12 changes: 10 additions & 2 deletions plugins/autogen/composio_autogen/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def register_tools(
caller: t.Optional[ConversableAgent] = None,
executor: t.Optional[ConversableAgent] = None,
tags: t.Optional[t.Sequence[Tag]] = None,
entity_id: t.Optional[str] = None,
) -> None:
"""
Register tools to the proxy agents.
Expand All @@ -57,6 +58,7 @@ def register_tools(
:param caller: Caller agent.
:param executor: Executor agent.
:param tags: Filter by the list of given Tags.
:param entity_id: Entity ID to use for executing function calls.
"""
if isinstance(tools, App):
tools = [tools]
Expand All @@ -79,20 +81,23 @@ def register_tools(
),
caller=caller,
executor=executor,
entity_id=entity_id or self.entity_id,
)

def register_actions(
self,
actions: t.Sequence[Action],
caller: t.Optional[ConversableAgent] = None,
executor: t.Optional[ConversableAgent] = None,
entity_id: t.Optional[str] = None,
):
"""
Register tools to the proxy agents.
:param actions: List of tools to register.
:param caller: Caller agent.
:param executor: Executor agent.
:param entity_id: Entity ID to use for executing function calls.
"""

caller = caller or self.caller
Expand All @@ -113,6 +118,7 @@ def register_actions(
),
caller=caller,
executor=executor,
entity_id=entity_id or self.entity_id,
)

def _process_function_name_for_registration(
Expand All @@ -134,7 +140,9 @@ def _register_schema_to_autogen(
schema: t.Dict,
caller: ConversableAgent,
executor: ConversableAgent,
):
entity_id: t.Optional[str] = None,
) -> None:
"""Register action schema to autogen registry."""
name = schema["name"]
appName = schema["appName"]
description = schema["description"]
Expand All @@ -147,7 +155,7 @@ def execute_action(**kwargs: t.Any) -> t.Dict:
name=name,
),
params=kwargs,
entity_id=self.entity_id,
entity_id=entity_id or self.entity_id,
)

function = types.FunctionType(
Expand Down
14 changes: 7 additions & 7 deletions plugins/claude/composio_claude/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,42 +136,42 @@ def get_tools(
def execute_tool_call(
self,
tool_call: ToolUseBlock,
entity_id: str = DEFAULT_ENTITY_ID,
entity_id: t.Optional[str] = None,
) -> t.Dict:
"""
Execute a tool call.
:param tool_call: Tool call metadata.
:param entity_id: Entity ID.
:param entity_id: Entity ID to use for executing function calls.
:return: Object containing output data from the tool call.
"""
return self.execute_action(
action=Action.from_action(name=tool_call.name),
params=t.cast(t.Dict, tool_call.input),
entity_id=entity_id,
entity_id=entity_id or self.entity_id,
)

def handle_tool_calls(
self,
llm_response: ToolsBetaMessage,
entity_id: str = DEFAULT_ENTITY_ID,
entity_id: t.Optional[str] = None,
) -> t.List[t.Dict]:
"""
Handle tool calls from OpenAI chat completion object.
:param response: Chat completion object from
openai.OpenAI.chat.completions.create function call
:param entity_id: Entity ID.
:param entity_id: Entity ID to use for executing function calls.
:return: A list of output objects from the function calls.
"""
entity_id = self.validate_entity_id(entity_id)
entity_id = self.validate_entity_id(entity_id or self.entity_id)
outputs = []
for content in llm_response.content:
if isinstance(content, ToolUseBlock):
outputs.append(
self.execute_tool_call(
tool_call=content,
entity_id=entity_id,
entity_id=entity_id or self.entity_id,
)
)
return outputs
32 changes: 25 additions & 7 deletions plugins/griptape/composio_griptape/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def __init__(
entity_id=entity_id,
)

def _wrap_tool(self, schema: t.Dict) -> t.Type[BaseTool]:
def _wrap_tool(
self,
schema: t.Dict,
entity_id: t.Optional[str] = None,
) -> BaseTool:
"""Wrap Composio tool as GripTape `BaseTool` object"""
app = schema["appName"]
name = schema["name"]
Expand All @@ -90,11 +94,11 @@ def _wrap_tool(self, schema: t.Dict) -> t.Type[BaseTool]:
schema_dict[schema_key] = schema_dtype

def _execute_task(params: t.Dict) -> t.Dict:
"""Auxialiry method for executing task."""
"""Placeholder method for executing task."""
return self.execute_action(
action=Action.from_app_and_action(app=app, name=name),
params=params,
entity_id=self.entity_id,
entity_id=entity_id or self.entity_id,
)

class GripTapeTool(BaseTool):
Expand Down Expand Up @@ -124,35 +128,49 @@ def manifest(self) -> t.Dict:
}

name = "".join(map(lambda x: x.title(), name.split("_"))) + "Client"
return type(name, (GripTapeTool,), {})
cls = type(name, (GripTapeTool,), {})
return cls()

def get_actions(self, actions: t.Sequence[Action]) -> t.List[BaseTool]:
def get_actions(
self,
actions: t.Sequence[Action],
entity_id: t.Optional[str] = None,
) -> t.List[BaseTool]:
"""
Get composio tools wrapped as GripTape `BaseTool` type objects.
:param actions: List of actions to wrap
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `BaseTool` objects
"""

return [
self._wrap_tool(schema=tool.model_dump(exclude_none=True))()
self._wrap_tool(
schema=tool.model_dump(exclude_none=True),
entity_id=entity_id,
)
for tool in self.client.actions.get(actions=actions)
]

def get_tools(
self,
apps: t.Sequence[App],
tags: t.Optional[t.List[t.Union[str, Tag]]] = None,
entity_id: t.Optional[str] = None,
) -> t.List[BaseTool]:
"""
Get composio tools wrapped as GripTape `BaseTool` type objects.
:param apps: List of apps to wrap
:param tags: Filter the apps by given tags
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `BaseTool` objects
"""

return [
self._wrap_tool(schema=tool.model_dump(exclude_none=True))()
self._wrap_tool(
schema=tool.model_dump(exclude_none=True),
entity_id=entity_id,
)
for tool in self.client.actions.get(apps=apps, tags=tags)
]
8 changes: 4 additions & 4 deletions plugins/julep/composio_julep/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ def __init__(
def handle_tool_calls(
self,
response: ChatResponse,
entity_id: str = "default",
entity_id: str = DEFAULT_ENTITY_ID,
) -> t.List[t.Dict]:
"""
Handle tool calls from Julep chat client object.
:param response: Chat completion object from
julep.Client.sessions.chat function call
:param entity_id: Entity ID.
:param entity_id: Entity ID to use for executing function calls.
:return: A list of output objects from the function calls.
"""
entity_id = self.validate_entity_id(entity_id)
entity_id = self.validate_entity_id(entity_id or self.entity_id)
outputs = []

for _responses in response.response:
Expand All @@ -57,7 +57,7 @@ def handle_tool_calls(
self.execute_action(
action=Action.from_action(name=function["name"]),
params=json.loads(function["arguments"]),
entity_id=self.entity_id,
entity_id=entity_id or self.entity_id,
)
)
except json.JSONDecodeError:
Expand Down
26 changes: 22 additions & 4 deletions plugins/langchain/composio_langchain/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def __init__(
entity_id=entity_id,
)

def _wrap_tool(self, schema: t.Dict[str, t.Any]) -> StructuredTool:
def _wrap_tool(
self,
schema: t.Dict[str, t.Any],
entity_id: t.Optional[str] = None,
) -> StructuredTool:
"""Wraps composio tool as Langchain StructuredTool object."""
app = schema["appName"]
action = schema["name"]
Expand All @@ -89,6 +93,7 @@ def function(**kwargs: t.Any) -> t.Dict:
name=action,
),
params=kwargs,
entity_id=entity_id or self.entity_id,
)

parameters = json_schema_to_model(
Expand All @@ -114,33 +119,46 @@ def function(**kwargs: t.Any) -> t.Dict:
func=action_func,
)

def get_actions(self, actions: t.Sequence[Action]) -> t.Sequence[StructuredTool]:
def get_actions(
self,
actions: t.Sequence[Action],
entity_id: t.Optional[str] = None,
) -> t.Sequence[StructuredTool]:
"""
Get composio tools wrapped as Langchain StructuredTool objects.
:param actions: List of actions to wrap
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `StructuredTool` objects
"""

return [
self._wrap_tool(schema=tool.model_dump(exclude_none=True))
self._wrap_tool(
schema=tool.model_dump(exclude_none=True),
entity_id=entity_id or self.entity_id,
)
for tool in self.client.actions.get(actions=actions)
]

def get_tools(
self,
apps: t.Sequence[App],
tags: t.Optional[t.List[t.Union[str, Tag]]] = None,
entity_id: t.Optional[str] = None,
) -> t.Sequence[StructuredTool]:
"""
Get composio tools wrapped as Langchain StructuredTool objects.
:param apps: List of apps to wrap
:param tags: Filter the apps by given tags
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `StructuredTool` objects
"""

return [
self._wrap_tool(schema=tool.model_dump(exclude_none=True))
self._wrap_tool(
schema=tool.model_dump(exclude_none=True),
entity_id=entity_id or self.entity_id,
)
for tool in self.client.actions.get(apps=apps, tags=tags)
]
27 changes: 22 additions & 5 deletions plugins/lyzr/composio_lyzr/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def __init__(
entity_id=entity_id,
)

def _wrap_tool(self, schema: t.Dict) -> Tool:
def _wrap_tool(
self,
schema: t.Dict,
entity_id: t.Optional[str] = None,
) -> Tool:
"""
Wrap composio tool as Lyzr `Tool` object.
"""
Expand All @@ -58,7 +62,7 @@ def function(**kwargs: t.Any) -> t.Dict:
name=name,
),
params=kwargs,
entity_id=self.entity_id,
entity_id=entity_id or self.entity_id,
)

action_func = types.FunctionType(
Expand Down Expand Up @@ -86,31 +90,44 @@ def function(**kwargs: t.Any) -> t.Dict:
default_params={},
)

def get_actions(self, actions: t.Sequence[Action]) -> t.List[Tool]:
def get_actions(
self,
actions: t.Sequence[Action],
entity_id: t.Optional[str] = None,
) -> t.List[Tool]:
"""
Get composio tools wrapped as Lyzr `Tool` objects.
:param actions: List of actions to wrap
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `Tool` objects
"""
return [
self._wrap_tool(schema=schema.model_dump(exclude_none=True))
self._wrap_tool(
schema=schema.model_dump(exclude_none=True),
entity_id=entity_id or self.entity_id,
)
for schema in self.client.actions.get(actions=actions)
]

def get_tools(
self,
apps: t.Sequence[App],
tags: t.Optional[t.List[t.Union[str, Tag]]] = None,
entity_id: t.Optional[str] = None,
) -> t.Sequence[Tool]:
"""
Get composio tools wrapped as Lyzr `Tool` objects.
:param apps: List of apps to wrap
:param tags: Filter the apps by given tags
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `Tool` objects
"""
return [
self._wrap_tool(schema=schema.model_dump(exclude_none=True))
self._wrap_tool(
schema=schema.model_dump(exclude_none=True),
entity_id=entity_id or self.entity_id,
)
for schema in self.client.actions.get(apps=apps, tags=tags)
]
Loading

0 comments on commit e4398cf

Please sign in to comment.