跳转到内容

用于 ADK 的 Google Cloud 应用集成工具

在 ADK 中得到支持。Python v0.1.0Java v0.3.0

使用(某种工具、方法等);凭借 ApplicationIntegrationToolset通过使用 Integration Connectors 提供的 100 多个预构建的连接器(这些连接器支持 Salesforce、ServiceNow、JIRA、SAP 等系统),您可以轻松地为您的员工提供安全且受管控的访问企业应用程序的权限。

该解决方案同时支持本地部署(on-premise)和 SaaS 模式的应用程序。此外,您还可以通过将应用程序集成流程自动化功能作为工具提供给 ADK(Application Development Kit)代理,从而将这些自动化流程转化为可执行的工作流程(agent-based workflows)。

在应用程序集成(Application Integration)中,联合搜索(Federated Search)功能允许您使用 ADK 代理同时查询多个企业应用程序和数据源。

通过这个视频教程,了解 ADK 联合搜索(ADK Federated Search)在应用程序集成(Application Integration)中的具体实现方式。

先决条件

1. 安装 ADK(Adobe Digital Kit)。

按照文档中的步骤安装代理开发工具包(Agent Development Kit)。 安装指南.

2. 安装命令行工具(CLI)。

安装该软件/程序。 Google Cloud CLI要使用该工具并使用默认的凭据,请运行以下命令:

gcloud config set project <project-id>
gcloud auth application-default login
gcloud auth application-default set-quota-project <project-id>

替换 <project-id> 使用您的 Google Cloud 项目的唯一 ID。

3. 配置应用程序集成工作流程,并发布相应的连接工具(Connection Tool)。

使用现有的(资源或解决方案)。 应用程序集成(Application Integration) “工作流程(workflow)或……” 集成连接器(Integrations Connector) 您希望与您的代理使用的连接类型。您也可以创建一个新的连接类型。 应用程序集成工作流程(Application Integration Workflow) 或者一个 连接(connection).

导入并发布该内容。 连接工具(Connection Tool) 来自模板库(template library)。

注意:要使用 Integration Connectors 中提供的连接器,您需要在与您的连接相同的区域内配置“应用程序集成”(Application Integration)服务。

4. 创建项目结构

设置您的项目结构,并创建所需的文件:

project_root_folder
├── .env
└── my_agent
    ├── __init__.py
    ├── agent.py
    └── tools.py

在运行代理程序时,请务必确保其能够正常启动并运行。 adk web from the project_root_folder.

设置您的项目结构,并创建所需的文件:

  project_root_folder
  └── my_agent
      ├── agent.java
      └── pom.xml

在运行代理程序时,请确保从相应的命令行或脚本中执行相应的命令。 project_root_folder.

5. 设置角色和权限

为了获取设置所需的相关权限…… ApplicationIntegrationToolset为了使用这些功能,您的项目必须具有以下 IAM 角色(这些角色同时适用于 Integration Connector 和 Application Integration Workflow):

- roles/integrations.integrationEditor
- roles/connectors.invoker
- roles/secretmanager.secretAccessor

注意: 在使用 Agent Runtime 进行部署时,请不要使用……(请根据实际情况填写具体的禁止操作内容)。 roles/integrations.integrationInvoker因为这可能会导致 403 错误,请使用(正确的方法)。 roles/integrations.integrationEditor 相反。

使用集成连接器(Integration Connectors)。

使用该功能,将您的代理(agent)连接到企业级应用程序中。 集成连接器(Integration Connectors).

在开始之前……

注意: The ExecuteConnection 当您在某个特定区域配置“应用程序集成”(Application Integration)功能时,相应的集成服务通常会自动创建。如果…… ExecuteConnection 该内容在数据库或系统中并不存在。 集成列表要创建它,您必须按照以下步骤操作:

  1. 要使用 Integration Connectors 中提供的连接器,请点击…… 快速设置 和 provision(供应;准备;规定) 应用程序集成应位于与您的网络连接相同的区域内。

Google Cloud Tools

  1. 前往…… 连接工具(Connection Tool) 从模板库中选择一个模板,然后点击“使用”(或类似的按钮)。 USE TEMPLATE.

    Google Cloud Tools

  2. 请输入集成名称(Integration Name): ExecuteConnection (必须使用这个确切的集成名称。) 接下来,选择与您的连接区域相匹配的区域,然后点击“确定”。 CREATE.

  3. 点击 PUBLISH 要将该集成功能发布到…… 应用程序集成(Application Integration) editor.

    Google Cloud Tools

创建一个应用程序集成工具集(Application Integration Toolset)

要为 Integration Connectors 创建一个应用程序集成工具集,请按照以下步骤操作:

  1. 创建一个工具,用于…… ApplicationIntegrationToolset 在……里面;在……期间 tools.py file:

    from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset
    
    connector_tool = ApplicationIntegrationToolset(
        project="test-project", # TODO: replace with GCP project of the connection
        location="us-central1", #TODO: replace with location of the connection
        connection="test-connection", #TODO: replace with connection name
        entity_operations={"Entity_One": ["LIST","CREATE"], "Entity_Two": []},#empty list for actions means all operations on the entity are supported.
        actions=["action1"], #TODO: replace with actions
        service_account_json='{...}', # optional. Stringified json for service account key
        tool_name_prefix="tool_prefix2",
        tool_instructions="..."
    )
    

    注意:

    from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset
    
    connector_tool = ApplicationIntegrationToolset(
        project="YOUR_PROJECT_ID",
        location="YOUR_LOCATION", # e.g., "us-central1"
        connection="YOUR_CONNECTION_NAME",
        connection_template_override="YOUR_CUSTOM_INTEGRATION_NAME",
    )
    

    ApplicationIntegrationToolset 支持 auth_scheme 和 auth_credential for 动态 OAuth2 认证 用于集成连接器(Integration Connectors)。要使用该工具,请创建一个类似这样的工具。 tools.py file:

    from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset
    from google.adk.tools.openapi_tool.auth.auth_helpers import dict_to_auth_scheme
    from google.adk.auth import AuthCredential
    from google.adk.auth import AuthCredentialTypes
    from google.adk.auth import OAuth2Auth
    
    oauth2_data_google_cloud = {
      "type": "oauth2",
      "flows": {
          "authorizationCode": {
              "authorizationUrl": "https://accounts.google.com/o/oauth2/auth",
              "tokenUrl": "https://oauth2.googleapis.com/token",
              "scopes": {
                  "https://www.googleapis.com/auth/cloud-platform": (
                      "View and manage your data across Google Cloud Platform"
                      " services"
                  ),
                  "https://www.googleapis.com/auth/calendar.readonly": "View your calendars"
              },
          }
      },
    }
    
    oauth_scheme = dict_to_auth_scheme(oauth2_data_google_cloud)
    
    auth_credential = AuthCredential(
      auth_type=AuthCredentialTypes.OAUTH2,
      oauth2=OAuth2Auth(
          client_id="...", #TODO: replace with client_id
          client_secret="...", #TODO: replace with client_secret
      ),
    )
    
    connector_tool = ApplicationIntegrationToolset(
        project="test-project", # TODO: replace with GCP project of the connection
        location="us-central1", #TODO: replace with location of the connection
        connection="test-connection", #TODO: replace with connection name
        entity_operations={"Entity_One": ["LIST","CREATE"], "Entity_Two": []},#empty list for actions means all operations on the entity are supported.
        actions=["GET_calendars/%7BcalendarId%7D/events"], #TODO: replace with actions. this one is for list events
        service_account_json='{...}', # optional. Stringified json for service account key
        tool_name_prefix="tool_prefix2",
        tool_instructions="...",
        auth_scheme=oauth_scheme,
        auth_credential=auth_credential
    )
    
  2. 更新: agent.py 将这个工具文件添加到您的代理程序中:

    from google.adk.agents.llm_agent import LlmAgent
    from .tools import connector_tool
    
    root_agent = LlmAgent(
        model='gemini-flash-latest',
        name='connector_agent',
        instruction="Help user, leverage the tools you have access to",
        tools=[connector_tool],
    )
    
  3. 配置 __init__.py “to expose your agent” 的意思是:公开或暴露你的代理(agent);让你的代理被其他人或系统看到或使用。

    from . import agent
    
  4. 启动 Google ADK Web UI,并使用您的代理(agent)进行操作。

    # make sure to run `adk web` from your project_root_folder
    adk web
    

完成上述步骤后,请前往: http://localhost:8000然后进行选择。 my\_agent 代理(该名称与代理文件夹的名称相同)。

使用应用程序集成工作流(Application Integration Workflows)

使用现有的(资源或解决方案)。 应用程序集成(Application Integration) 可以将工作流(workflow)作为工具来帮助您的代理(agent)完成工作,或者创建一个新的工作流。

1. 创建一个工具。

要创建一个工具…… ApplicationIntegrationToolset 在……里面;在……期间 tools.py 对于文件(file),请使用以下代码:

    integration_tool = ApplicationIntegrationToolset(
        project="test-project", # TODO: replace with GCP project of the connection
        location="us-central1", #TODO: replace with location of the connection
        integration="test-integration", #TODO: replace with integration name
        triggers=["api_trigger/test_trigger"],#TODO: replace with trigger id(s). Empty list would mean all api triggers in the integration to be considered.
        service_account_json='{...}', #optional. Stringified json for service account key
    )

注意: 您可以提供一个服务账户来代替使用默认的凭据。为此,请生成一个服务账户。 服务账户密钥(Service Account Key) 并提供正确的信息。 应用程序集成(Application Integration)与集成连接器(Integration Connector)相关的 IAM 角色(IAM Roles) 该权限属于服务账户(service account)。有关 IAM 角色的更多详细信息,请参阅相关文档。 先决条件 section.

注意: tool_name_prefix 和 tool_instructions 仅当您满足以下条件时才适用: (请在此处填写具体的条件或要求。) connection=在……上;关于…… integration= 这些路径虽然被系统接受,但实际上会被系统默默地忽略(即系统不会对这些路径采取任何操作)。

要创建一个工具…… ApplicationIntegrationToolset 在……里面;在……期间 tools.java 对于文件(file),请使用以下代码:

    import com.google.adk.tools.applicationintegrationtoolset.ApplicationIntegrationToolset;
    import com.google.common.collect.ImmutableList;
    import com.google.common.collect.ImmutableMap;

    public class Tools {
        private static ApplicationIntegrationToolset integrationTool;
        private static ApplicationIntegrationToolset connectionsTool;

        static {
            integrationTool = new ApplicationIntegrationToolset(
                    "test-project",
                    "us-central1",
                    "test-integration",
                    ImmutableList.of("api_trigger/test-api"),
                    null,
                    null,
                    null,
                    "{...}",
                    "tool_prefix1",
                    "...");

            connectionsTool = new ApplicationIntegrationToolset(
                    "test-project",
                    "us-central1",
                    null,
                    null,
                    "test-connection",
                    ImmutableMap.of("Issue", ImmutableList.of("GET")),
                    ImmutableList.of("ExecuteCustomQuery"),
                    "{...}",
                    "tool_prefix",
                    "...");
        }
    }

注意: 您可以提供一个服务账户来代替使用默认的凭据。为此,请生成一个服务账户。 服务账户密钥(Service Account Key) 并提供正确的信息。 应用程序集成(Application Integration)与集成连接器(Integration Connector)相关的 IAM 角色(IAM Roles) 该权限属于服务账户(service account)。有关 IAM 角色的更多详细信息,请参阅相关文档。 先决条件 section.

2. 将该工具添加到您的代理程序中。

要更新…… agent.py 将此工具文件添加到您的代理程序中,并使用以下代码进行配置:

    from google.adk.agents.llm_agent import LlmAgent
    from .tools import integration_tool, connector_tool

    root_agent = LlmAgent(
        model='gemini-flash-latest',
        name='integration_agent',
        instruction="Help user, leverage the tools you have access to",
        tools=[integration_tool],
    )

要更新…… agent.java 将此工具文件添加到您的代理程序中,并使用以下代码进行配置:

    import com.google.adk.agents.LlmAgent;
    import com.google.adk.tools.BaseTool;
    import com.google.common.collect.ImmutableList;

      public class MyAgent {
          public static void main(String[] args) {
              // Assuming Tools class is defined as in the previous step
              ImmutableList<BaseTool> tools = ImmutableList.<BaseTool>builder()
                      .add(Tools.integrationTool)
                      .add(Tools.connectionsTool)
                      .build();

              // Finally, create your agent with the tools generated automatically.
              LlmAgent rootAgent = LlmAgent.builder()
                      .name("science-teacher")
                      .description("Science teacher agent")
                      .model("gemini-flash-latest")
                      .instruction(
                              "Help user, leverage the tools you have access to."
                      )
                      .tools(tools)
                      .build();

              // You can now use rootAgent to interact with the LLM
              // For example, you can start a conversation with the agent.
          }
      }

注意: 要查找某个连接所支持的所有实体和操作列表,请使用以下连接器 API: listActions, listEntityTypes.

3. 公开您的代理程序(agent)。

要配置…… __init__.py 要公开您的代理(agent),请使用以下代码:

    from . import agent

4. 使用您的代理(agent)。

要启动 Google ADK Web UI 并使用您的代理,请使用以下命令:

    # make sure to run `adk web` from your project_root_folder
    adk web
完成上述步骤后,请前往: http://localhost:8000...然后选择 my_agent 代理(该名称与代理文件夹的名称相同)。

要启动 Google ADK Web UI 并使用您的代理,请使用以下命令:

    mvn install

    mvn exec:java \
        -Dexec.mainClass="com.google.adk.web.AdkWebServer" \
        -Dexec.args="--adk.agents.source-dir=src/main/java" \
        -Dexec.classpathScope="compile"

完成上述步骤后,请前往: http://localhost:8000...然后选择 my_agent 代理(该名称与代理文件夹的名称相同)。