-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No changes other than the phidata plugin
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |