Skip to content

GoogleDriveToolkit

autogen.tools.experimental.google.drive.GoogleDriveToolkit #

GoogleDriveToolkit(*, credentials, download_folder, exclude=None, api_version='v3')

Bases: Toolkit, GoogleToolkitProtocol

A tool map for Google Drive.

Initialize the Google Drive tool map.

PARAMETER DESCRIPTION
credentials

The Google OAuth2 credentials.

TYPE: Credentials

download_folder

The folder to download files to.

TYPE: Union[Path, str]

exclude

The tool names to exclude.

TYPE: Optional[list[Literal['list_drive_files_and_folders', 'download_file_from_drive']]] DEFAULT: None

api_version

The Google Drive API version to use."

TYPE: str DEFAULT: 'v3'

Source code in autogen/tools/experimental/google/drive/toolkit.py
def __init__(  # type: ignore[no-any-unimported]
    self,
    *,
    credentials: "Credentials",
    download_folder: Union[Path, str],
    exclude: Optional[list[Literal["list_drive_files_and_folders", "download_file_from_drive"]]] = None,
    api_version: str = "v3",
) -> None:
    """Initialize the Google Drive tool map.

    Args:
        credentials: The Google OAuth2 credentials.
        download_folder: The folder to download files to.
        exclude: The tool names to exclude.
        api_version: The Google Drive API version to use."
    """
    self.service = build(serviceName="drive", version=api_version, credentials=credentials)

    if isinstance(download_folder, str):
        download_folder = Path(download_folder)
    download_folder.mkdir(parents=True, exist_ok=True)

    @tool(description="List files and folders in a Google Drive")
    def list_drive_files_and_folders(
        page_size: Annotated[int, "The number of files to list per page."] = 10,
        folder_id: Annotated[
            Optional[str],
            "The ID of the folder to list files from. If not provided, lists all files in the root folder.",
        ] = None,
    ) -> list[GoogleFileInfo]:
        return list_files_and_folders(service=self.service, page_size=page_size, folder_id=folder_id)

    @tool(description="download a file from Google Drive")
    def download_file_from_drive(
        file_info: Annotated[GoogleFileInfo, "The file info to download."],
    ) -> str:
        return download_file(
            service=self.service,
            file_id=file_info.id,
            file_name=file_info.name,
            mime_type=file_info.mime_type,
            download_folder=download_folder,
        )

    if exclude is None:
        exclude = []

    tools = [tool for tool in [list_drive_files_and_folders, download_file_from_drive] if tool.name not in exclude]
    super().__init__(tools=tools)

toolkit instance-attribute #

toolkit = {name: _83vfor tool in tools}

tools property #

tools

Get the list of tools in the set.

service instance-attribute #

service = build(serviceName='drive', version=api_version, credentials=credentials)

register_for_llm #

register_for_llm(agent)

Register the tools in the set with an LLM agent.

PARAMETER DESCRIPTION
agent

The LLM agent to register the tools with.

TYPE: ConversableAgent

Source code in autogen/tools/toolkit.py
def register_for_llm(self, agent: "ConversableAgent") -> None:
    """Register the tools in the set with an LLM agent.

    Args:
        agent (ConversableAgent): The LLM agent to register the tools with.
    """
    for tool in self.toolkit.values():
        tool.register_for_llm(agent)

register_for_execution #

register_for_execution(agent)

Register the tools in the set with an agent for

PARAMETER DESCRIPTION
agent

The agent to register the tools with.

TYPE: ConversableAgent

Source code in autogen/tools/toolkit.py
def register_for_execution(self, agent: "ConversableAgent") -> None:
    """Register the tools in the set with an agent for

    Args:
        agent (ConversableAgent): The agent to register the tools with.
    """
    for tool in self.toolkit.values():
        tool.register_for_execution(agent)

get_tool #

get_tool(tool_name)

Get a tool from the set by name.

PARAMETER DESCRIPTION
tool_name

The name of the tool to get.

TYPE: str

RETURNS DESCRIPTION
Tool

The tool with the given name.

TYPE: Tool

Source code in autogen/tools/toolkit.py
def get_tool(self, tool_name: str) -> Tool:
    """Get a tool from the set by name.

    Args:
        tool_name (str): The name of the tool to get.

    Returns:
        Tool: The tool with the given name.
    """
    if tool_name in self.toolkit:
        return self.toolkit[tool_name]

    raise ValueError(f"Tool '{tool_name}' not found in Toolkit.")

set_tool #

set_tool(tool)

Set a tool in the set.

PARAMETER DESCRIPTION
tool

The tool to set.

TYPE: Tool

Source code in autogen/tools/toolkit.py
def set_tool(self, tool: Tool) -> None:
    """Set a tool in the set.

    Args:
        tool (Tool): The tool to set.
    """
    self.toolkit[tool.name] = tool

remove_tool #

remove_tool(tool_name)

Remove a tool from the set by name.

PARAMETER DESCRIPTION
tool_name

The name of the tool to remove.

TYPE: str

Source code in autogen/tools/toolkit.py
def remove_tool(self, tool_name: str) -> None:
    """Remove a tool from the set by name.

    Args:
        tool_name (str): The name of the tool to remove.
    """
    if tool_name in self.toolkit:
        del self.toolkit[tool_name]
    else:
        raise ValueError(f"Tool '{tool_name}' not found in Toolkit.")

recommended_scopes classmethod #

recommended_scopes()

Return the recommended scopes manatory for using tools from this tool map.

Source code in autogen/tools/experimental/google/drive/toolkit.py
@classmethod
def recommended_scopes(cls) -> list[str]:
    """Return the recommended scopes manatory for using tools from this tool map."""
    return [
        "https://www.googleapis.com/auth/drive.readonly",
    ]