npm 包 node-red-node-watson-ucg-custom 使用教程

阅读时长 7 分钟读完

在前端开发中,使用一些现有的 npm 包可以提高开发效率和代码质量。本文将介绍一个 npm 包 node-red-node-watson-ucg-custom,它是一个可以用来执行 Watson 基于 UC Gateway 的自定义 API 调用的节点。

背景介绍

Watson 是 IBM 推出的人工智能平台,提供了多种 AI 技术,如语音、文本、视觉、数据分析等。UC Gateway 是一个解决方案,用于轻松创建集成企业核心应用系统和外部 API/服务的应用程序。

node-red-node-watson-ucg-custom 是一个基于 node.js 的 npm 包,可以将 Watson 和 UC Gateway 集成,以便使用 Watson 能力来执行自定义 API。

安装

使用以下命令可以安装 node-red-node-watson-ucg-custom:

使用

在 node-red 中,我们可以使用 node-red-node-watson-ucg-custom 来创建一个 Watson 自定义 API 调用的节点。

创建节点

在 node-red 的界面上右击空白处,选择“导入” -> “节点”,将下载好的 node-red-node-watson-ucg-custom.json 文件导入即可。

配置节点

在创建好节点后,我们需要进行节点的配置。点击节点,打开节点配置框,在“配置”一栏中,我们需要填写以下信息:

  • Endpoint URL:UC Gateway API 的 URL 地址,格式为 https://{hostname}:{port}/{context_root}/{api_version}/{resource}/{action}。
  • Request method:UC Gateway API 的请求方法,如 GET、POST、PUT、DELETE 等。
  • Auth token:UC Gateway 的认证令牌,可以在 UC Gateway 上生成。
  • Jsonata expression:UC Gateway API 所返回的 JSON 数据,我们需要进行处理后再发送到 Watson 的参数,可以使用 jsonata 语法转换数据格式。

测试节点

在完成节点的配置后,我们可以对节点进行测试。点击节点,打开节点控制台,在“输入”一栏中输入 UC Gateway API 的参数,并点击“发送”按钮,可以查看节点输出的结果。

整合 Watson

在测试成功后,我们可以在节点中使用 Watson 的 API。我们需要在节点配置中填写以下信息:

  • Username:Watson 身份验证的用户名。
  • Password:Watson 身份验证的密码。
  • Service name:Watson 能力的名称。
  • Parameters:向 Watson API 发送的参数。
  • Output type:Watson API 的输出类型,如 text、image、json 等。

示例代码

以下是一个使用 node-red-node-watson-ucg-custom 的示例代码,它可以将 UC Gateway API 的返回结果发送到 Watson 的语言翻译 API,并最终将译文输出到控制台。

-- -------------------- ---- -------
--
    ----- ------------------
    ------- ---------
    ---- -----------------
    ------- ---
    -------- --
        ---- ---------
    -- -
        ---- --------
        ----- -----
    ---
    --------- ---
    ---------- ---
    ------- ------
    ------------ ----
    -------- ---
    ---------- ---
    -------------- -------
    ---- ----
    ---- ----
    -------- -
        -------------------
    -
-- -
    ----- ------------------
    ------- ----------------
    ---- -----------------
    ------- ------------ -----
    ----------- ---------------------------------------------------------------------------
    --------- -------
    ------- ----------
    ------- -----------
    ------------- -----------------------------------
    ---------- -- ---------------- ---
    ---- ----
    ---- ----
    -------- -
        ------------------
    -
-- -
    ----- -----------------
    ------- ---------
    ---- -----------------
    ------- --------- ------------
    --------------- ----------------------
    ----------- ----------
    ----------- -----------
    -------------- -------
    --------- ------------
    ------------- -- ------- - ------- ---
    ----------- -----
    ---- ----
    ---- ----
    -------- -
        -------------------
    -
-- -
    ----- -----------------
    ------- ---------
    ---- -----------------
    ------- ---
    -------- --
        ---- ------
        ---- ----------
        ----- ------
        ----- -----------------------------------
        ------ ---------
    ---
    --------- ---
    ----------- ---
    ------- ---
    ----- ---
    ------ ------
    ---- ----
    ---- ----
    -------- -
        ------------------
    -
-- -
    ----- ------------------
    ------- --------
    ---- -----------------
    ------- ---
    --------- -----
    ------------ -----
    ---------- ------
    ----------- ------
    ----------- ----------
    ------------- ------
    ------------ ---
    ------------- -------
    ---- ----
    ---- ----
    -------- --
--

总结

在本文中,我们介绍了 npm 包 node-red-node-watson-ucg-custom 的用途、安装和使用方法。它可以将 Watson 和 UC Gateway 集成,以便使用 Watson 能力来执行自定义 API 调用,并提供了一个示例代码。通过此文的学习,读者可以更好的了解如何使用 node-red-node-watson-ucg-custom 来实现企业应用的开发。

来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005665c81e8991b448e2809

纠错
反馈