Apigee API Hub tools for ADK¶
适用于 ADK 的 Apigee API Hub 工具¶
ApiHubToolset lets you turn any documented API from Apigee API hub into a tool with a few lines of code. This section shows you to step by step instructions including setting up authentication for a secure connection to your APIs.
ApiHubToolset 让您只需几行代码就可以将 Apigee API 集线器中的任何文档化 API 转换为工具。本部分向您展示分步说明,包括为安全连接到您的 API 设置身份验证。
Prerequisites 先决条件
- Install ADK
- 安装 ADK
- Install Google Cloud CLI.
- 安装 Google Cloud CLI。
- Apigee API hub instance with documented (i.e. OpenAPI spec) APIs
- 具有文档化(即 OpenAPI 规范)API 的 Apigee API 集线器 实例
- Set up your project structure and create required files
- 设置您的项目结构并创建必需的文件
Create an API Hub Toolset¶
创建 API 集线器工具集¶
Note: This tutorial includes an agent creation. If you already have an agent, you only need to follow a subset of these steps.
注意:本教程包括智能体创建。如果您已经有智能体,只需遵循这些步骤的子集。
- Get your access token, so that APIHubToolset can fetch spec from API Hub API. In your terminal run the following command
-
获取您的访问令牌,以便 APIHubToolset 可以从 API 集线器 API 获取规范。 在终端中运行以下命令
-
Ensure that account used has required permissions. You can use pre-defined role
roles/apihub.vieweror assign following permissions: -
确保使用的账户具有所需的权限。您可以使用预定义的角色
roles/apihub.viewer或分配以下权限:- apihub.specs.get (required)
- apihub.apis.get (optional)
- apihub.apis.list (optional)
- apihub.versions.get (optional)
- apihub.versions.list (optional)
- apihub.specs.list (optional)
-
Create a tool with
APIHubToolset. Add to below totools.py -
使用
APIHubToolset创建工具。将以下内容添加到tools.pyIf your API requires authentication, you must configure authentication for tool. The following code sample demonstrates how to configure an API key. ADK supports token based auth (API Key, Bearer token), service account, and OpenID Connect. We will soon add support for various OAuth2 flows.
如果您的 API 需要身份验证,您必须为工具配置身份验证。以下代码示例演示如何配置 API 密钥。ADK 支持基于令牌的身份验证(API 密钥、Bearer 令牌)、服务账户和 OpenID Connect。我们将很快添加对各种 OAuth2 流程的支持。
from google.adk.tools.openapi_tool.auth.auth_helpers import token_to_scheme_credential from google.adk.tools.apihub_tool.apihub_toolset import APIHubToolset # Provide authentication for your APIs. Not required if your APIs don't required authentication. # 为您的 API 提供身份验证。如果您的 API 不需要身份验证,则不需要此项。 auth_scheme, auth_credential = token_to_scheme_credential( "apikey", "query", "apikey", apikey_credential_str ) sample_toolset = APIHubToolset( name="apihub-sample-tool", description="Sample Tool", access_token="...", # Copy your access token generated in step 1 # 复制您在步骤 1 中生成的访问令牌 apihub_resource_name="...", # API Hub resource name / API 集线器资源名称 auth_scheme=auth_scheme, auth_credential=auth_credential, )For production deployment we recommend using a service account instead of an access token. In the code snippet above, use
service_account_json=service_account_cred_json_strand provide your security account credentials instead of a token.对于生产部署,我们建议使用服务账户而不是访问令牌。在上面的代码片段中,使用
service_account_json=service_account_cred_json_str并提供您的安全账户凭据而不是令牌。For apihub_resource_name, if you know to specific ID of to OpenAPI Spec being used for your API, use
`projects/my-project-id/locations/us-west1/apis/my-api-id/versions/version-id/specs/spec-id`. If you would like to Toolset to automatically pull to first available spec from to API, use`projects/my-project-id/locations/us-west1/apis/my-api-id`对于 apihub_resource_name,如果您知道为您的 API 使用的 OpenAPI 规范的特定 ID,请使用
projects/my-project-id/locations/us-west1/apis/my-api-id/versions/version-id/specs/spec-id。如果您希望工具集自动从 API 拉取第一个可用的规范,请使用projects/my-project-id/locations/us-west1/apis/my-api-id。 -
Create your agent file Agent.py and add to created tools to your agent definition:
-
创建您的智能体文件 Agent.py 并将创建的工具添加到您的智能体定义:
-
Configure your
__init__.pyto expose your agent -
配置您的
__init__.py以暴露您的智能体: -
Start to Google ADK Web UI and try your agent:
-
启动 Google ADK Web UI 并尝试您的智能体:
Then go to http://localhost:8000 to try your agent from Web UI. - 然后前往 http://localhost:8000 从 Web UI 尝试您的智能体。