OpenAPI 工具

智能体可以通过提供 OpenAPI 架构,利用 OpenAPI 工具连接到外部 API。代理将代表您调用 API。

限制:

  • 每个 OpenAPI 工具只能有一个操作或函数。

示例架构:

openapi: 3.0.0
info:
  title: Simple Pets API
  version: 1.0.0
servers:
  - url: 'https://api.pet-service-example.com/v1'
paths:
  /pets/{petId}:
    get:
      summary: Return a pet by ID.
      operationId: getPet
      parameters:
        - in: path
          name: petId
          required: true
          description: Pet id
          schema:
            type: integer
      responses:
        200:
          description: OK
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      parameters:
        - name: petName
          in: query
          required: false
          description: Pet name
          schema:
            type: string
        - name: label
          in: query
          description: Pet label
          style: form
          explode: true
          required: false
          schema:
            type: array
            items:
              type: string
        - name: X-OWNER
          in: header
          description: Optional pet owner provided in the HTTP header
          required: false
          schema:
            type: string
        - name: X-SESSION
          in: header
          description: session id
          required: true
          schema:
            type: string
          x-ces-session-context: $context.session_id
      responses:
        '200':
          description: An array of pets
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
    post:
      summary: Create a new pet
      operationId: createPet
      requestBody:
        description: Pet to add to the store
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '201':
          description: Pet created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        owner:
          type: string
        label:
          type: array
          items:
            type: string

注入会话上下文变量

您可以将变量从会话上下文(例如会话 ID)注入到 OpenAPI 请求中。在参数定义中使用 x-ces-session-context 扩展字段。该值应为 context 对象中变量的路径。 如需了解详情,请参阅会话上下文变量。

例如:

      parameters:
        - name: X-SESSION
          in: header
          description: session id
          required: true
          schema:
            type: string
          # This extension injects the session ID
          x-ces-session-context: $context.session_id

安全注意事项

配置 OpenAPI 工具时,请务必了解权限的处理方式:

  • 工具执行身份:OpenAPI 工具执行的操作是使用授予 CX Agent Studio 服务账号的权限执行的,而不是使用与代理互动的最终用户的直接权限。
  • 传递访问风险: 这意味着,如果 CX Agent Studio 服务账号有权调用 Cloud Run functions, 则该工具可能会调用该服务账号可访问的任何函数, 即使最终用户没有直接访问权限也是如此。

为降低与此行为相关的潜在风险,请执行以下操作:

  1. 使用专用项目:我们强烈建议您在专用项目中部署智能体应用,并将其与其他关键资源或敏感功能分开。这种隔离限制了 CX Agent Studio 服务账号可用的权限范围。
  2. 实施 VPC Service Controls:利用 VPC Service Controls 为敏感服务定义服务边界。这样,您就可以控制对 Cloud Run functions 等服务的访问权限,并防止 CX Agent Studio 服务账号发出不必要的调用。
  3. 遵循最小权限原则:确保仅向 CX Agent Studio 服务账号授予其预期功能所需的最低限度 Identity and Access Management 角色和权限。

API 身份验证

调用外部 API 时,支持以下身份验证选项:

服务代理 ID 令牌

CX Agent Studio 可以使用 Customer Engagement Suite 服务代理(格式为:service-{PROJECT_NUMBER}@gcp-sa-ces.iam.gserviceaccount.com)生成 ID 令牌。当 CX Agent Studio 调用外部 API 时,该令牌会添加到授权 HTTP 标头中。

如果 Cloud Run functions 和 Cloud Run 服务与代理位于同一项目中,则您无需额外的 IAM 权限即可调用它们。如果它们位于不同的项目中,则在您向服务代理地址授予 roles/cloudfunctions.invoker 和 roles/run.invoker 角色后,可以使用 ID 令牌访问 Cloud Run functions 和 Cloud Run 服务。

服务账号身份验证

服务账号可用于对任何支持它的 Google API 的工具请求进行身份验证。 提供您的服务账号邮箱。

如果您还没有服务账号,请创建一个服务账号。

由于服务账号是主账号,因此您可以为服务账号授予角色(就像为任何其他主账号授予角色一样),以便服务账号能够访问您项目中的资源。服务账号邮箱将用于生成访问令牌,该令牌将在工具请求的 Authorization 标头中发送。

配置工具以使用服务账号的用户必须具有以下权限:

  • roles/iam.serviceAccountUser

为了让 Dialogflow CX 生成令牌,Customer Engagement Suite 服务代理必须具有以下权限:

  • roles/iam.serviceAccountTokenCreator

服务账号还必须具有访问托管该工具的服务的权限。

OAuth

提供您所用服务的 OAuth 信息。

如果您要为 Google 服务使用 OAuth,则需要为该服务配置 OAuth。

API 密钥

您可以通过提供密钥名称、请求位置(标头或查询字符串)和 API 密钥来配置 API 密钥身份验证,以便 CX Agent Studio 在请求中传递 API 密钥。使用 Secret Manager 密钥版本提供 API 密钥。

Secret Manager 身份验证

您可以使用 Secret Manager 将凭证存储为 Secret。以下是使用密钥对工具进行身份验证的必要步骤:

  1. 如果您还没有密钥,请创建密钥。
  2. 向 Customer Engagement Suite Service Agent 授予新 Secret 的 Secret Manager Secret Accessor (roles/secretmanager.secretAccessor) 角色。
  3. 将凭证复制到剪贴板。
  4. 向 Secret 添加新的 Secret 版本。将您的凭证粘贴为 Secret 值。
    • 省略末尾的所有换行符。
  5. 复制您刚刚添加的 Secret 版本的名称。名称格式为 projects/{project_id}/secrets/{secret_id}/versions/{version_id}。将此 Secret 版本用于工具配置。

不记名令牌

您可以配置不记名令牌身份验证,以便在 OpenAPI 工具请求的 Authorization 标头中传递存储在会话上下文变量中的不记名令牌。当代理需要将从最终用户收集的令牌传递给另一个 API 时,此选项非常有用。

采用以下格式指定会话上下文变量:$context.variables.VARIABLE_NAME,其中 VARIABLE_NAME 是存储不记名令牌的变量的名称。