-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 autogen tools new version #1190
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,45 @@ | ||
""" | ||
Autogen demo using get_tools() approach. | ||
""" | ||
|
||
import asyncio | ||
import os | ||
|
||
import dotenv | ||
from autogen_agentchat.agents import AssistantAgent | ||
from autogen_core import CancellationToken | ||
from autogen_ext.models.openai import OpenAIChatCompletionClient | ||
|
||
from composio_autogen import App, ComposioToolSet | ||
|
||
|
||
async def main(): | ||
# Initialize toolset and get tools | ||
composio_toolset = ComposioToolSet() | ||
tools = composio_toolset.get_tools(apps=[App.GITHUB]) | ||
|
||
model_client = OpenAIChatCompletionClient( | ||
model="gpt-4", | ||
api_key=os.environ["OPENAI_API_KEY"], | ||
) | ||
# Create assistant agent with tools | ||
assistant = AssistantAgent( | ||
name="github_assistant", | ||
system_message=( | ||
"You are an AI assistant that helps with GitHub tasks. " | ||
"Use the provided tools to interact with GitHub." | ||
), | ||
model_client=model_client, | ||
tools=list(tools), | ||
) | ||
|
||
# Define task and initiate chat | ||
task = "Star the repository composiohq/composio on GitHub" | ||
result = await assistant.run(task=task, cancellation_token=CancellationToken()) | ||
print(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Load environment variables from .env | ||
dotenv.load_dotenv() | ||
asyncio.run(main()) |
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 |
---|---|---|
|
@@ -6,9 +6,11 @@ | |
import autogen | ||
import typing_extensions as te | ||
from autogen.agentchat.conversable_agent import ConversableAgent | ||
from autogen_core.tools import FunctionTool | ||
|
||
from composio import Action, ActionType, AppType, TagType | ||
from composio.tools import ComposioToolSet as BaseComposioToolSet | ||
from composio.tools.toolset import ProcessorsType | ||
from composio.utils.shared import get_signature_format_from_schema_params | ||
|
||
|
||
|
@@ -137,10 +139,14 @@ def execute_action(**kwargs: t.Any) -> t.Dict: | |
), | ||
closure=execute_action.__closure__, | ||
) | ||
function.__signature__ = Signature( # type: ignore | ||
parameters=get_signature_format_from_schema_params( | ||
schema_params=schema["parameters"], | ||
), | ||
params = get_signature_format_from_schema_params( | ||
schema_params=schema["parameters"], | ||
) | ||
setattr(function, "__signature__", Signature(parameters=params)) | ||
setattr( | ||
function, | ||
"__annotations__", | ||
{p.name: p.annotation for p in params} | {"return": t.Dict[str, t.Any]}, | ||
) | ||
function.__doc__ = ( | ||
description if description else f"Action {name} from {appName}" | ||
|
@@ -154,3 +160,102 @@ def execute_action(**kwargs: t.Any) -> t.Dict: | |
), | ||
description=description if description else f"Action {name} from {appName}", | ||
) | ||
|
||
def _wrap_tool( | ||
self, | ||
schema: t.Dict[str, t.Any], | ||
entity_id: t.Optional[str] = None, | ||
) -> FunctionTool: | ||
""" | ||
Wraps a composio action as an Autogen FunctionTool. | ||
|
||
Args: | ||
schema: The action schema to wrap | ||
entity_id: Optional entity ID for executing function calls | ||
|
||
Returns: | ||
FunctionTool: Wrapped function as an Autogen FunctionTool | ||
""" | ||
name = schema["name"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding validation for the schema parameters in |
||
description = schema["description"] or f"Action {name} from {schema['appName']}" | ||
|
||
def execute_action(**kwargs: t.Any) -> t.Dict: | ||
"""Placeholder function for executing action.""" | ||
return self.execute_action( | ||
action=Action(value=name), | ||
params=kwargs, | ||
entity_id=entity_id or self.entity_id, | ||
_check_requested_actions=True, | ||
) | ||
|
||
# Create function with proper signature | ||
function = types.FunctionType( | ||
code=execute_action.__code__, | ||
globals=globals(), | ||
name=self._process_function_name_for_registration(input_string=name), | ||
closure=execute_action.__closure__, | ||
) | ||
|
||
# Set signature and annotations | ||
params = get_signature_format_from_schema_params( | ||
schema_params=schema["parameters"] | ||
) | ||
setattr(function, "__signature__", Signature(parameters=params)) | ||
setattr( | ||
function, | ||
"__annotations__", | ||
{p.name: p.annotation for p in params} | {"return": t.Dict[str, t.Any]}, | ||
) | ||
function.__doc__ = description | ||
|
||
return FunctionTool( | ||
func=function, | ||
description=description, | ||
name=self._process_function_name_for_registration(input_string=name), | ||
) | ||
|
||
def get_tools( | ||
self, | ||
actions: t.Optional[t.Sequence[ActionType]] = None, | ||
apps: t.Optional[t.Sequence[AppType]] = None, | ||
tags: t.Optional[t.List[TagType]] = None, | ||
entity_id: t.Optional[str] = None, | ||
*, | ||
processors: t.Optional[ProcessorsType] = None, | ||
check_connected_accounts: bool = True, | ||
) -> t.Sequence[FunctionTool]: | ||
""" | ||
Get composio tools as Autogen FunctionTool objects. | ||
|
||
Args: | ||
actions: List of actions to wrap | ||
apps: List of apps to wrap | ||
tags: Filter apps by given tags | ||
entity_id: Entity ID for function wrapper | ||
processors: Optional dict of processors to merge | ||
check_connected_accounts: Whether to check for connected accounts | ||
|
||
Returns: | ||
List of Autogen FunctionTool objects | ||
""" | ||
self.validate_tools(apps=apps, actions=actions, tags=tags) | ||
if processors is not None: | ||
self._merge_processors(processors) | ||
|
||
tools = [ | ||
self._wrap_tool( | ||
schema=tool.model_dump( | ||
exclude_none=True, | ||
), | ||
entity_id=entity_id or self.entity_id, | ||
) | ||
for tool in self.get_action_schemas( | ||
actions=actions, | ||
apps=apps, | ||
tags=tags, | ||
check_connected_accounts=check_connected_accounts, | ||
_populate_requested=True, | ||
) | ||
] | ||
|
||
return tools |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding error handling for environment variable loading. If .env file is missing or required variables are not set, the code will fail silently.