Skip to content

Commit

Permalink
Phidata plugin[ONLY] (#431)
Browse files Browse the repository at this point in the history
No changes other than the phidata plugin
  • Loading branch information
sawradip authored Aug 11, 2024
1 parent 4bc7fa8 commit a983e63
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
55 changes: 55 additions & 0 deletions python/plugins/phidata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## 🚀🔗 Leveraging PhiData with Composio

Facilitate the integration of PhiData with Composio to empower LLMs to directly interact with external applications & knowledge base, broadening their capabilities and application scope.

### Objective

- **Automate starring a GitHub repository** using conversational instructions via PhiData Tool Calls.

### Installation and Setup

Ensure you have the necessary packages installed and connect your GitHub account to allow your agents to utilize GitHub functionalities.

```bash
# Install Composio LangChain package
pip install composio-phidata

# Connect your GitHub account
composio-cli add github

# View available applications you can connect with
composio-cli show-apps
```

### Usage Steps

#### 1. Import Base Packages

Prepare your environment by initializing necessary imports from PhiData.

```python
from phi.assistant import Assistant
```

### Step 2: Integrating GitHub Tools with Composio

This step involves fetching and integrating GitHub tools provided by Composio, enabling enhanced functionality for LangChain operations.
```python
from composio_phidata import ComposioToolSet, Action

toolset = ComposioToolSet()
composio_tools = toolset.get_actions(actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER])
```

### Step 3: Agent Execution

This step involves configuring and executing the assistant to carry out actions, such as starring a GitHub repository.

```python
my_task = "Star a repo composiohq/composio on GitHub"

# Create a chat completion request to decide on the action
assistant = Assistant(tools=composio_tools, show_tool_calls=True)

assistant.print_response("Can you star ComposioHQ/composio repo?")
```
13 changes: 13 additions & 0 deletions python/plugins/phidata/composio_phidata/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from composio_phidata.toolset import ComposioToolSet

from composio import Action, App, Tag, Trigger, WorkspaceType


__all__ = (
"Action",
"App",
"Tag",
"ComposioToolSet",
"WorkspaceType",
"Trigger",
)
115 changes: 115 additions & 0 deletions python/plugins/phidata/composio_phidata/toolset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""
PhiData tool spec.
"""
import json
import typing as t

from phi.tools.function import Function
from pydantic import validate_call

from composio import Action, ActionType, AppType, TagType, WorkspaceConfigType
from composio.constants import DEFAULT_ENTITY_ID

from composio_openai import ComposioToolSet as BaseComposioToolSet


class ComposioToolSet(BaseComposioToolSet):
"""
Composio toolset for Phidata framework.
"""

def __init__(
self,
api_key: t.Optional[str] = None,
base_url: t.Optional[str] = None,
entity_id: str = DEFAULT_ENTITY_ID,
output_in_file: bool = False,
workspace_config: t.Optional[WorkspaceConfigType] = None,
workspace_id: t.Optional[str] = None,
) -> None:
"""
Initialize composio toolset.
:param api_key: Composio API key
:param base_url: Base URL for the Composio API server
:param entity_id: Entity ID for making function calls
:param output_in_file: Whether to write output to a file
"""
super().__init__(
api_key,
base_url,
entity_id=entity_id,
output_in_file=output_in_file,
workspace_config=workspace_config,
workspace_id=workspace_id,
)
self._runtime = "phidata"

def _wrap_tool(
self,
schema: t.Dict,
entity_id: t.Optional[str] = None,
) -> Function:
"""
Wrap composio tool as Phidata `Function` object.
"""
name = schema["name"]
description = schema["description"]
parameters = schema["parameters"]

def function(**kwargs: t.Any) -> str:
"""Composio tool wrapped as Phidata `Function`."""
return json.dumps(
self.execute_action(
action=Action(value=name),
params=kwargs,
entity_id=entity_id or self.entity_id,
)
)

return Function(
name=name,
description=description,
parameters=parameters,
entrypoint=validate_call(function),
)

def get_actions(
self,
actions: t.Sequence[ActionType],
) -> t.List[Function]:
"""
Get composio tools wrapped as Phidata `Function` objects.
:param actions: List of actions to wrap
:param entity_id: Entity ID to use for executing function calls.
:return: Composio tools wrapped as `Function` objects
"""
return [
self._wrap_tool(
schema=schema.model_dump(exclude_none=True),
entity_id=self.entity_id,
)
for schema in self.get_action_schemas(actions=actions)
]

def get_tools(
self,
apps: t.Sequence[AppType],
tags: t.Optional[t.List[TagType]] = None,
) -> t.List[Function]:
"""
Get composio tools wrapped as Lyzr `Function` 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 `Function` objects
"""
return [
self._wrap_tool(
schema=schema.model_dump(exclude_none=True),
entity_id=self.entity_id,
)
for schema in self.get_action_schemas(apps=apps, tags=tags)
]
13 changes: 13 additions & 0 deletions python/plugins/phidata/phidata_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from composio_phidata import Action, ComposioToolSet
from phi.assistant import Assistant


toolset = ComposioToolSet()
composio_tools = toolset.get_actions(
actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER]
)

assistant = Assistant(tools=composio_tools, show_tool_calls=True)

assistant.print_response("Can you start sawradip/sawradip repo?")
# pprint(tool)
27 changes: 27 additions & 0 deletions python/plugins/phidata/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Setup configuration for Composio PhiData plugin.
"""

from pathlib import Path

from setuptools import setup


setup(
name="composio_phidata",
version="0.4.1",
author="Sawradip",
author_email="[email protected]",
description="Use Composio to get an array of tools with your Phidata Plugin.",
long_description=(Path(__file__).parent / "README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
url="https://github.com/ComposioHQ/composio",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
python_requires=">=3.9,<4",
install_requires=["composio_==0.4.1"],
include_package_data=True,
)

0 comments on commit a983e63

Please sign in to comment.