-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
2,265 additions
and
10 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
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,6 @@ | ||
Requirement already satisfied: pytest in ./.venv-3.9/lib/python3.9/site-packages (7.4.4) | ||
Requirement already satisfied: iniconfig in ./.venv-3.9/lib/python3.9/site-packages (from pytest) (2.0.0) | ||
Requirement already satisfied: packaging in ./.venv-3.9/lib/python3.9/site-packages (from pytest) (24.2) | ||
Requirement already satisfied: pluggy<2.0,>=0.12 in ./.venv-3.9/lib/python3.9/site-packages (from pytest) (1.5.0) | ||
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in ./.venv-3.9/lib/python3.9/site-packages (from pytest) (1.2.2) | ||
Requirement already satisfied: tomli>=1.0.0 in ./.venv-3.9/lib/python3.9/site-packages (from pytest) (2.2.1) |
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,12 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .crewai import CrewAIInteroperability | ||
from .interoperability import Interoperability | ||
from .interoperable import Interoperable | ||
from .langchain import LangChainInteroperability | ||
from .pydantic_ai import PydanticAIInteroperability | ||
from .registry import register_interoperable_class | ||
|
||
__all__ = ["Interoperability", "Interoperable", "register_interoperable_class"] |
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,7 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .crewai import CrewAIInteroperability | ||
|
||
__all__ = ["CrewAIInteroperability"] |
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,83 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import re | ||
import sys | ||
from typing import Any, Optional | ||
|
||
from ...tools import Tool | ||
from ..registry import register_interoperable_class | ||
|
||
__all__ = ["CrewAIInteroperability"] | ||
|
||
|
||
def _sanitize_name(s: str) -> str: | ||
return re.sub(r"\W|^(?=\d)", "_", s) | ||
|
||
|
||
@register_interoperable_class("crewai") | ||
class CrewAIInteroperability: | ||
""" | ||
A class implementing the `Interoperable` protocol for converting CrewAI tools | ||
to a general `Tool` format. | ||
This class takes a `CrewAITool` and converts it into a standard `Tool` object. | ||
""" | ||
|
||
@classmethod | ||
def convert_tool(cls, tool: Any, **kwargs: Any) -> Tool: | ||
""" | ||
Converts a given CrewAI tool into a general `Tool` format. | ||
This method ensures that the provided tool is a valid `CrewAITool`, sanitizes | ||
the tool's name, processes its description, and prepares a function to interact | ||
with the tool's arguments. It then returns a standardized `Tool` object. | ||
Args: | ||
tool (Any): The tool to convert, expected to be an instance of `CrewAITool`. | ||
**kwargs (Any): Additional arguments, which are not supported by this method. | ||
Returns: | ||
Tool: A standardized `Tool` object converted from the CrewAI tool. | ||
Raises: | ||
ValueError: If the provided tool is not an instance of `CrewAITool`, or if | ||
any additional arguments are passed. | ||
""" | ||
from crewai.tools import BaseTool as CrewAITool | ||
|
||
if not isinstance(tool, CrewAITool): | ||
raise ValueError(f"Expected an instance of `crewai.tools.BaseTool`, got {type(tool)}") | ||
if kwargs: | ||
raise ValueError(f"The CrewAIInteroperability does not support any additional arguments, got {kwargs}") | ||
|
||
# needed for type checking | ||
crewai_tool: CrewAITool = tool # type: ignore[no-any-unimported] | ||
|
||
name = _sanitize_name(crewai_tool.name) | ||
description = ( | ||
crewai_tool.description.split("Tool Description: ")[-1] | ||
+ " (IMPORTANT: When using arguments, put them all in an `args` dictionary)" | ||
) | ||
|
||
def func(args: crewai_tool.args_schema) -> Any: # type: ignore[no-any-unimported] | ||
return crewai_tool.run(**args.model_dump()) | ||
|
||
return Tool( | ||
name=name, | ||
description=description, | ||
func=func, | ||
) | ||
|
||
@classmethod | ||
def get_unsupported_reason(cls) -> Optional[str]: | ||
if sys.version_info < (3, 10) or sys.version_info >= (3, 13): | ||
return "This submodule is only supported for Python versions 3.10, 3.11, and 3.12" | ||
|
||
try: | ||
import crewai.tools | ||
except ImportError: | ||
return "Please install `interop-crewai` extra to use this module:\n\n\tpip install ag2[interop-crewai]" | ||
|
||
return None |
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,73 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import Any, Dict, List, Type | ||
|
||
from ..tools import Tool | ||
from .interoperable import Interoperable | ||
from .registry import InteroperableRegistry | ||
|
||
__all__ = ["Interoperable"] | ||
|
||
|
||
class Interoperability: | ||
""" | ||
A class to handle interoperability between different tool types. | ||
This class allows the conversion of tools to various interoperability classes and provides functionality | ||
for retrieving and registering interoperability classes. | ||
""" | ||
|
||
registry = InteroperableRegistry.get_instance() | ||
|
||
@classmethod | ||
def convert_tool(cls, *, tool: Any, type: str, **kwargs: Any) -> Tool: | ||
""" | ||
Converts a given tool to an instance of a specified interoperability type. | ||
Args: | ||
tool (Any): The tool object to be converted. | ||
type (str): The type of interoperability to convert the tool to. | ||
**kwargs (Any): Additional arguments to be passed during conversion. | ||
Returns: | ||
Tool: The converted tool. | ||
Raises: | ||
ValueError: If the interoperability class for the provided type is not found. | ||
""" | ||
interop = cls.get_interoperability_class(type) | ||
return interop.convert_tool(tool, **kwargs) | ||
|
||
@classmethod | ||
def get_interoperability_class(cls, type: str) -> Type[Interoperable]: | ||
""" | ||
Retrieves the interoperability class corresponding to the specified type. | ||
Args: | ||
type (str): The type of the interoperability class to retrieve. | ||
Returns: | ||
Type[Interoperable]: The interoperability class type. | ||
Raises: | ||
ValueError: If no interoperability class is found for the provided type. | ||
""" | ||
supported_types = cls.registry.get_supported_types() | ||
if type not in supported_types: | ||
supported_types_formated = ", ".join(["'t'" for t in supported_types]) | ||
raise ValueError( | ||
f"Interoperability class {type} is not supported, supported types: {supported_types_formated}" | ||
) | ||
|
||
return cls.registry.get_class(type) | ||
|
||
@classmethod | ||
def get_supported_types(cls) -> List[str]: | ||
""" | ||
Returns a sorted list of all supported interoperability types. | ||
Returns: | ||
List[str]: A sorted list of strings representing the supported interoperability types. | ||
""" | ||
return sorted(cls.registry.get_supported_types()) |
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,46 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import Any, Optional, Protocol, runtime_checkable | ||
|
||
from ..tools import Tool | ||
|
||
__all__ = ["Interoperable"] | ||
|
||
|
||
@runtime_checkable | ||
class Interoperable(Protocol): | ||
""" | ||
A Protocol defining the interoperability interface for tool conversion. | ||
This protocol ensures that any class implementing it provides the method | ||
`convert_tool` to convert a given tool into a desired format or type. | ||
""" | ||
|
||
@classmethod | ||
def convert_tool(cls, tool: Any, **kwargs: Any) -> Tool: | ||
""" | ||
Converts a given tool to a desired format or type. | ||
This method should be implemented by any class adhering to the `Interoperable` protocol. | ||
Args: | ||
tool (Any): The tool object to be converted. | ||
**kwargs (Any): Additional parameters to pass during the conversion process. | ||
Returns: | ||
Tool: The converted tool in the desired format or type. | ||
""" | ||
... | ||
|
||
@classmethod | ||
def get_unsupported_reason(cls) -> Optional[str]: | ||
"""Returns the reason for the tool being unsupported. | ||
This method should be implemented by any class adhering to the `Interoperable` protocol. | ||
Returns: | ||
str: The reason for the interoperability class being unsupported. | ||
""" | ||
... |
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,7 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .langchain import LangChainInteroperability | ||
|
||
__all__ = ["LangChainInteroperability"] |
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,76 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import sys | ||
from typing import Any, Optional | ||
|
||
from ...tools import Tool | ||
from ..registry import register_interoperable_class | ||
|
||
__all__ = ["LangChainInteroperability"] | ||
|
||
|
||
@register_interoperable_class("langchain") | ||
class LangChainInteroperability: | ||
""" | ||
A class implementing the `Interoperable` protocol for converting Langchain tools | ||
into a general `Tool` format. | ||
This class takes a `LangchainTool` and converts it into a standard `Tool` object, | ||
ensuring compatibility between Langchain tools and other systems that expect | ||
the `Tool` format. | ||
""" | ||
|
||
@classmethod | ||
def convert_tool(cls, tool: Any, **kwargs: Any) -> Tool: | ||
""" | ||
Converts a given Langchain tool into a general `Tool` format. | ||
This method verifies that the provided tool is a valid `LangchainTool`, | ||
processes the tool's input and description, and returns a standardized | ||
`Tool` object. | ||
Args: | ||
tool (Any): The tool to convert, expected to be an instance of `LangchainTool`. | ||
**kwargs (Any): Additional arguments, which are not supported by this method. | ||
Returns: | ||
Tool: A standardized `Tool` object converted from the Langchain tool. | ||
Raises: | ||
ValueError: If the provided tool is not an instance of `LangchainTool`, or if | ||
any additional arguments are passed. | ||
""" | ||
from langchain_core.tools import BaseTool as LangchainTool | ||
|
||
if not isinstance(tool, LangchainTool): | ||
raise ValueError(f"Expected an instance of `langchain_core.tools.BaseTool`, got {type(tool)}") | ||
if kwargs: | ||
raise ValueError(f"The LangchainInteroperability does not support any additional arguments, got {kwargs}") | ||
|
||
# needed for type checking | ||
langchain_tool: LangchainTool = tool # type: ignore | ||
|
||
def func(tool_input: langchain_tool.args_schema) -> Any: # type: ignore | ||
return langchain_tool.run(tool_input.model_dump()) | ||
|
||
return Tool( | ||
name=langchain_tool.name, | ||
description=langchain_tool.description, | ||
func=func, | ||
) | ||
|
||
@classmethod | ||
def get_unsupported_reason(cls) -> Optional[str]: | ||
if sys.version_info < (3, 9): | ||
return "This submodule is only supported for Python versions 3.9 and above" | ||
|
||
try: | ||
import langchain_core.tools | ||
except ImportError: | ||
return ( | ||
"Please install `interop-langchain` extra to use this module:\n\n\tpip install ag2[interop-langchain]" | ||
) | ||
|
||
return None |
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,7 @@ | ||
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .pydantic_ai import PydanticAIInteroperability | ||
|
||
__all__ = ["PydanticAIInteroperability"] |
Oops, something went wrong.