ASTRO轻应用 ASTROZERO-如何开发推荐类AI应用:在脚本中使用AI服务

时间:2024-04-25 15:58:40

在脚本中使用AI服务

  1. 推荐模型发布后,在图1中单击“使用”,可使用该推荐模型进行推荐。

    系统支持使用服务编排或者脚本来调用推荐服务。

  2. 选择使用该推荐服务的应用,选择页签“在Script中使用”,单击“开发”。

    图6 在脚本中使用
    系统会自动为所选应用创建好调用该AI的脚本,代码示例如下:
    import * as aienable from 'aienable';
    
    export class Input {
        @action.param({ type: "Any", required: false, description: "name" })
        name: string;  // name为已发布的预测/推荐服务名称
        @action.param({ type: "Any", required: false, description: "input" })
        dataInputs: JSON;
    }
    
    export class Output {
        @action.param({ type: "Any" })
        result: JSON;
    }
    
    export class testAIEnable {
        @action.method({ input: "Input", output: "Output", description: "do a operation" })
        run(input: Input): void {
            let client = aienable.newClient("recommendation_services")
            let result = client.recommend(input.name, input.dataInputs);
            console.log(result)
        }
    }

  3. 单击编辑器上方的,保存脚本。
  4. 单击编辑器上方的,运行脚本。
  5. 在页面底部“输入参数”页签输入请求参数,在图6中“输入参数详情”区域单击获取,给入参即相关字段配上测试值。
  6. 单击测试窗口右上角的,执行脚本。

    其中,“recipients_id”为5配置的接收人对象字段值,该场景表示接受推荐内容的客户ID。以下输入参数中“fff7466a253c0e59499ea943462c10f9”和“677ea793bab5253b52c7f73f0e120b74”为有效的客户ID,“6775ba6a3e92020abc8e6cd6e19e867a”为无效的客户ID,即该客户ID不存在。

    “N”表示给接收人推荐的产品数量。

    {
    	"name": "test_02",
    	"dataInputs": {
    		"recipients_id": [
    			"fff7466a253c0e59499ea943462c10f9",
    			"677ea793bab5253b52c7f73f0e120b74",
    			"6775ba6a3e92020abc8e6cd6e19e867a"
    		],
    		"N": 3
    	}
    }

    输出以下结果,表示调用推荐服务成功,给有效客户ID的推荐内容为5配置的产品ID。其中“rating”值为预测接收人购买该产品的概率。这里看出,当输入无效客户ID时,不会推荐相关内容。

    {
         resCode: '0', 
         resMsg: '成功', 
         result: {
             677ea793bab5253b52c7f73f0e120b74: [ {
                     aienable__productId__CST: '5727b4e9463bfa0b43bf97397c4b7c44', 
                     rating: 0.9780523919210801
                 }, {
                     aienable__productId__CST: '58e3dad74a022a8e41571be503143e5b', 
                     rating: 0.9343108103239535
                 }, {
                     aienable__productId__CST: '58efb9b638561ce132216a9a612513e2', 
                     rating: 0.9228962093643385
                 } ], 
             fff7466a253c0e59499ea943462c10f9: [ {
                     aienable__productId__CST: '5727b4e9463bfa0b43bf97397c4b7c44', 
                     rating: 1
                 }, {
                     aienable__productId__CST: '57f2bc497c1a3ebe41ba7a06d78ed159', 
                     rating: 0.979109618457509
                 }, {
                     aienable__productId__CST: '574597aaf385996112490308e37399ce', 
                     rating: 0.9502617871072747
                 } ]
         }
     }

  7. 单击编辑页面上方的,启用脚本。
support.huaweicloud.com/usermanual-astrozero/astrozero_05_1202.html