将应用程序导出为 UCS 微服务

UCS Tools 允许将应用程序导出为微服务,以便可以重用这些应用程序(以及使用的微服务及其配置)。这可以通过文本表示和 CLI 工具以及 UCS Studio 完成。

我们将针对 入门 部分中显示的 Text ChatbotSpeech Chatbot 应用程序演示这一点。将从包含 BotMaker Speech ControllerBotMaker Dialog ManagerRiva Speech Skills 微服务的应用程序创建一个新的微服务。这个新的微服务可以与 BotMaker Text WebApp 微服务一起重用以创建 Text Chatbot,或与 BotMaker Speech WebApp 微服务一起重用以创建 Speech Chatbot

使用 CLI 工具

将应用程序导出为微服务

UCS AppBuilder CLI 工具的 app export-ms 命令可用于此目的。它需要应用程序 YAML 文件和一个 Export Information 文件。此文件包含用户导出应用程序作为微服务所需的其他信息。

首先,使用以下内容创建应用程序 YAML ucf-bot-app.yaml

specVersion: 2.5.0

version: 0.0.1

doc: ''

name: ucf-bot-app

description: Bot microservice application

dependencies:
- ucf.svc.riva.speech-skills:2.0.0
- ucf.svc.botmaker.dialog-manager:2.0.0
- ucf.svc.botmaker.speech-controller:2.0.0

components:
- name: Riva Speech Skills
  type: ucf.svc.riva.speech-skills
  parameters:
    buildModelEngines: 'true'
    ngcModelConfigs:
    - nvidia/riva/rmir_asr_citrinet_1024_en_us_str:2.6.0
    - nvidia/riva/rmir_tts_fastpitch_hifigan_en_us:2.6.0
    - eevaigoeixww/ucf-11-ea-release/misty_text_classification:1.7.3-ea
    - eevaigoeixww/ucf-11-ea-release/intent_slot_weather:1.7.3-ea
    - eevaigoeixww/ucf-11-ea-release/intent_slot_poi:1.7.3-ea
    - eevaigoeixww/ucf-11-ea-release/intent_slot_smalltalk:1.7.3-ea
    - nvidia/riva/rmir_nlp_question_answering_bert_base:2.6.0
    - nvidia/riva/rmir_nlp_named_entity_recognition_bert_base:2.6.0
  secrets:
    ngc-api-key-secret: ngc-api-key

- name: BotMaker Dialog Manager
  type: ucf.svc.botmaker.dialog-manager
  parameters:
    botNgcPath: eevaigoeixww/ucf-11-ea-release/misty_bot:2.0.0-ea-x86_64
    botConfigName: misty_bot_config.yaml
    botConfig:
      fulfillments:
        weather:
          parameters:
            api-key: XXXXXXXXXXXXXXXXXXXXX
        poi:
          parameters:
            api-key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            here-api-key: XXXXXXXXXXXXXXXXXXXXXX
        map:
          parameters:
            api-key: XXXXXXXXXXXXXXXXXXXXX
  secrets:
    ngc-api-key-secret: ngc-api-key

- name: BotMaker Speech Controller
  type: ucf.svc.botmaker.speech-controller
  parameters:
    pipeline: speech_lite
    wordBoostFilePath: ''
  secrets:
    ngc-api-key-secret: ngc-api-key

connections:
  BotMaker Dialog Manager/riva-speech: Riva Speech Skills/riva-speech-api
  BotMaker Dialog Manager/triton-grpc: Riva Speech Skills/triton-grpc
  BotMaker Speech Controller/riva: Riva Speech Skills/riva-speech-api
  BotMaker Speech Controller/dialog-manager: BotMaker Dialog Manager/dialog-manager

secrets:
  ngc-api-key:
    k8sSecret:
      secretName: ngc-api-key-secret
      key: NGC_API_KEY

接下来,使用以下内容创建名为 export-info.yaml 的导出信息文件

specVersion: 2.5.0

msInfo:
  name: ucf.svc.my-bot
  version: 0.0.1
  description: Chatbot microservice with smalltalk, weather and maps support
  displayName: My Bot
  category:
    functional: Speech AI
    industry: General

parameters:
- name: imagePullSecrets
  mapsTo:
  - microservice: BotMaker Dialog Manager
    parameter: imagePullSecrets
  - microservice: BotMaker Speech Controller
    parameter: imagePullSecrets
  - microservice: Riva Speech Skills
    parameter: imagePullSecrets

secrets:
- name: ngc-api-key

ingress-endpoints:
- name: speech-controller-api
  mapsTo: BotMaker Speech Controller/grpc-api
- name: dialog-manager
  mapsTo: BotMaker Dialog Manager/dialog-manager

导出信息包含要创建的微服务的基本信息、参数、端点和应用程序中要导出的各个微服务的密钥以及如何映射它们。

在上面的示例中,必须导出 BotMaker Speech Controller 微服务的 grpc-api 入口端点并命名为 speech-controller-api。添加一个名为 imagePullSecrets 的参数,并将其映射到三个微服务的 imagePullSecrets 参数。导出应用程序中的 ngc-api-key 密钥,作为要创建的微服务的密钥要求。

现在所有必需的信息都可用了,可以导出应用程序了。这可以通过运行以下命令来完成

$ ucf_app_builder_cli app export-ms ucf-bot-app.yaml -e export-info.yaml
2023-04-20 13:44:05,679 - AppBuilder - INFO - Syncing any missing service versions to cache...
2023-04-20 13:44:16,463 - AppBuilder - INFO - Validating application ...
2023-04-20 13:44:16,550 - AppBuilder - INFO - Building application ucf-bot-app-0.0.1 ...
2023-04-20 13:44:17,329 - AppBuilder - INFO - Application Information written to /tmp/tmp__oanp37/app_info.yaml
2023-04-20 13:44:17,333 - AppBuilder - INFO - Application compliance report generated at /tmp/tmp__oanp37/compliance_report.json
2023-04-20 13:44:17,334 - AppBuilder - INFO - Application Helm Chart generated in /tmp/tmp__oanp37
2023-04-20 13:44:17,475 - AppBuilder - INFO - Existing chart mentioned in manifest. Skipping helm chart generation
2023-04-20 13:44:17,806 - AppBuilder - INFO - MS spec generated in ucf-bot-app-ms/output/msspec
2023-04-20 13:44:22,468 - AppBuilder - WARNING - Mandatory compliance checks failed. Check ucf-bot-app-ms/output/compliance_test_logs.txt for more information
2023-04-20 13:44:22,853 - AppBuilder - INFO - MS spec generated in ucf-bot-app-ms/output/msspec
2023-04-20 13:44:22,862 - AppBuilder - INFO - Building test application 'dev'
2023-04-20 13:44:22,868 - AppBuilder - INFO - Syncing any missing service versions to cache...
2023-04-20 13:44:22,869 - AppBuilder - INFO - Validating application ...
2023-04-20 13:44:22,873 - AppBuilder - INFO - Building application my-bot-dev-0.0.1 ...
2023-04-20 13:44:23,201 - AppBuilder - INFO - Application Information written to ucf-bot-app-ms/output/tests/dev/app_info.yaml
2023-04-20 13:44:23,205 - AppBuilder - INFO - Application compliance report generated at ucf-bot-app-ms/output/tests/dev/compliance_report.json
2023-04-20 13:44:23,205 - AppBuilder - INFO - Application Helm Chart generated in ucf-bot-app-ms/output/tests/dev
2023-04-20 13:44:23,205 - AppBuilder - INFO - Tests generated in ucf-bot-app-ms/output/tests
2023-04-20 13:44:23,340 - Registry - INFO - Generating test helm chart packages...
2023-04-20 13:44:23,552 - AppBuilder - INFO - Added microservice 'ucf.svc.my-bot:0.0.1' to local repository
2023-04-20 13:44:23,553 - AppBuilder - INFO - Microservice created in ucf-bot-app-ms
2023-04-20 13:44:23,566 - AppBuilder - INFO - App exported as Microservice

如果成功,日志将显示生成微服务构建器输入的位置 (ucf-bot-app-ms)。

导出功能创建模板测试应用程序、README、变更日志和手动合规性结果输入。可以根据需要更新这些内容,并且可以重建微服务。

在应用程序中使用新的微服务

创建一个新的名为 speech-chatbot-app.yaml 的应用程序 YAML 文件,内容如下

specVersion: 2.5.0

version: 0.0.1

doc: ''

name: speech-chatbot

description: speech-chatbot

dependencies:
- ucf.svc.my-bot:0.0.1
- ucf.svc.botmaker.speech-web-app:2.0.0

components:
- name: My Bot
  type: ucf.svc.my-bot
  parameters:
    imagePullSecrets:
    - name: ngc-docker-reg-secret
  secrets:
    ngc-api-key: ngc-api-key

- name: BotMaker Speech Web App
  type: ucf.svc.botmaker.speech-web-app
  parameters:
    imagePullSecrets:
    - name: ngc-docker-reg-secret

connections:
  BotMaker Speech Web App/speech-controller: My Bot/speech-controller-api

secrets:
  ngc-api-key:
    k8sSecret:
      secretName: ngc-api-key-secret
      key: NGC_API_KEY

在此应用程序中,使用了新创建的微服务。已设置导出的 imagePullSecrets 参数,导出的 speech-controller-api 入口端点已连接,并且已设置导出的 ngc-api-key 密钥。此应用程序的行为应与入门指南中的 Speech Chatbot 应用程序相同。

使用以下命令构建应用程序

$ ucf_app_builder_cli app build speech-chatbot-app.yaml
2023-04-20 14:02:07,410 - AppBuilder - INFO - Syncing any missing service versions to cache...
2023-04-20 14:02:07,411 - AppBuilder - INFO - Validating application ...
2023-04-20 14:02:07,415 - AppBuilder - INFO - Building application speech-chatbot-0.0.1 ...
2023-04-20 14:02:11,056 - AppBuilder - INFO - Application Information written to speech-chatbot-0.0.1/app_info.yaml
2023-04-20 14:02:11,071 - AppBuilder - INFO - Application compliance report generated at speech-chatbot-0.0.1/compliance_report.json
2023-04-20 14:02:11,072 - AppBuilder - INFO - Application Helm Chart generated in speech-chatbot-0.0.1
2023-04-20 14:02:11,072 - AppBuilder - INFO - App built

应用程序 Helm Chart 将在 speech-chatbot-0.0.1 中构建。按照 部署参考应用程序 中的描述部署应用程序并与之交互。

导出信息文件格式

以下是导出信息文件的预期格式

# UCS MS Spec Version the file follows.
specVersion: 2.5.0

# Basic information for the microservice to be created
msInfo:
  # Name for the microservice. Must start with `ucf.svc.` prefix
  # Optional. If not specified, will be set to ucf.svc.<app-name>
  name: ucf.svc.my-bot

  # Version for the microservice. Must follow semantic versioning
  # Optional. If not specified, will be set to version of the application
  version: 0.0.1

  # Short description of the microservice.
  # Optional. If not specified, will be set to the description of the application.
  description: my-bot

  # User friendly display name for the microservice.
  # Optional. If not specified, will be created from the application name.
  displayName: My Bot

  # Category for the microservice.
  # Optional. If not specified, will be set to blank
  category:
    functional: Speech AI
    industry: Geneal

# Add parameters for the microservice to be exported and map them to
# parameters of individual microservices in the application.
parameters:
- name: botNgcPath  # Name of parameter in exported application.
  # List of individual microservice parameters this parameter maps to
  mapsTo:
  - microservice: BotMaker Dialog Manager  # Name of the microservice in the application. Must be same as component name in the application.
    parameter: botNgcPath                  # Parameter of the microservice in the application

- name: imagePullSecrets
  mapsTo:
  # Single parameter can map to multiple parameters
  - microservice: BotMaker Dialog Manager
    parameter: imagePullSecrets
  - microservice: BotMaker Speech Controller
    parameter: imagePullSecrets

# List of secrets added in the application to be added as secret requirements of
# microservice to be exported
secrets:
- name: ngc-api-key   # Name must be same as what is added under the `secrets` section
                      # in the application.

# List of ingress endpoints of microservices in the application to be
# added in the microservice to be exported.
ingress-endpoints:
- name: speech-controller-api                  # Name to set for ingress endpoint to be added in
                                               # microservice to be exported
  mapsTo: BotMaker Speech Controller/grpc-api  # The actual microservice ingress endpoint it maps to.
                                               # Format: <component-name-in-app>/<name-of-ingress-endpoint-in-component>

- name: dialog-manager
  mapsTo: BotMaker Dialog Manager/dialog-manager

# List of egress endpoints to be added in the microservice to be exported and
# the egress endpoints of the microservices in the app it maps to.
egress-endpoints:
- name: riva-speech-api  # Name of egress endpoint in the exported microservice.

  # Can map to multiple egress endpoints in the application.
  mapsTo:
  - BotMaker Dialog Manager/riva-speech  # Format: <name-of-component-in-app>/<name-of-egress-endpoint>
  - BotMaker Speech Controller/riva

- name: riva-triton-grpc
  mapsTo:
  - BotMaker Dialog Manager/triton-grpc

使用 Studio

将应用程序导出为微服务

1. 按照 入门 - Speech Chatbot 部分中的步骤首先创建应用程序。

  1. 通过首先选择 Speech Web App 微服务,然后右键单击以打开上下文菜单并单击“删除”,来删除它。

UCS Studio - Export App as MS - Delete Web App
  1. 添加一个 Export Ingress Endpoint 组件以导出 BotMaker Speech Controller grpc-api 入口端点。重命名组件。

    speech-controller-api 并将其连接到 grpc-api 端点。这将导出 BotMaker Speech Controller grpc-api 作为新导出的微服务中的 speech-controller-api。对 BotMaker Dialog Manager dialog-manager 入口端点执行相同的操作。

UCS Studio - Export App as MS - Export Ingress Endpoint
  1. 对于三个微服务中的每一个,选择微服务,打开右侧 属性 窗口中的 Common MS Params 部分,然后选中 imagePullSecrets 旁边的复选框。这将添加 imagePullSecrets 作为新导出微服务的参数,并将其映射到三个微服务的 imagePullSecrets 参数。

UCS Studio - Export App as MS - Export Parameter
  1. 接下来,右键单击“图形画布”中的空白区域以打开上下文菜单,然后选择 Graph Configurator。选择 Secrets 部分,然后选中 ngc-api-key-secret 密钥的 Export in Microservice 复选框。这会将 ngc-api-key-secret 添加为要创建的微服务中的密钥要求,并将其映射到正在使用此密钥的微服务。

UCS Studio - Export App as MS - Export Secret
  1. 从操作工具栏中,选择 Export the current app as microservice 选项。这将启动 Export as Microservice 对话框。更新要为将要创建的微服务设置的基本信息,然后单击对话框右下角的 Export as Microservice 按钮。这将打开一个文件浏览器。导航到应在其中创建新微服务的 Microservice Builder Input 的目录,然后单击“确定”。这将启动导出过程。

UCS Studio - Export App as MS - Export Secret
  1. 完成后,应该看到成功对话框以及创建 Microservice Builder Input 的路径。新的微服务现在也应该在左侧的微服务列表中可用。

UCS Studio - Export App as MS - Export Success

UCS Studio - Export App as MS - MS List

在应用程序中使用新的微服务

  1. 关闭所有打开的图形并创建一个新图形。从左侧的微服务列表中,将 BotMaker Speech Web AppMy Bot 微服务添加到图形中。将 speech-controller 端点连接到 speech-controller-api 端点。

UCS Studio - Export App as MS - Create App
  1. 将以下内容保存到文件(例如 params.yaml),并从文件中加载参数,如 从文件加载参数 中所示。

'My Bot':
  imagePullSecrets:
    - name: ngc-docker-reg-secret

'BotMaker Speech Web App':
  imagePullSecrets:
    - name: ngc-docker-reg-secret
  1. 创建一个新的 ngc-api-key 密钥,并将其分配给 My Bot 微服务,如 UCS 文本机器人应用程序 - 设置密钥 中所示。

  2. 按照 UCS 文本机器人应用程序 应用程序部分中 构建参考应用程序 中的说明构建应用程序。

  3. 按照 部署参考应用程序 中的描述部署应用程序并与之交互。