Share via


如何使用 REST API 更新 LUIS 模型

重要

LUIS 將於 2025 年 10 月 1 日淘汰,而自 2023 年 4 月 1 日開始,您將無法建立新的 LUIS 資源。 建議移轉 LUIS 應用程式交談語言理解,以享有產品持續支援和多語言功能的優點。

在本文中,您會將範例語句新增至 Pizza 應用程式,並且訓練應用程式。 範例語句是對應到意圖的交談使用者文字。 您可以藉由提供與意圖相關的範例語句,教導 LUIS 哪些種類的使用者提供文字屬於哪種意圖。

參考文件 | 範例

必要條件

範例語句 JSON 檔案

範例表達會遵循特定的格式。

text 欄位包含範例語句的文字。 intentName 欄位必須對應到 LUIS 應用程式中的現有意圖名稱。 entityLabels 是必填欄位。 如果您不想要標示任何實體,請提供空白陣列。

如果 entityLabels 陣列不是空的,startCharIndexendCharIndex 必須標示 entityName 欄位中參考的實體。 索引以零為起始。 如果您在文字中的某個空格開始或結束標籤,新增語句的 API 呼叫將會失敗。

[
  {
    "text": "order a pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "order a large pepperoni pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 14,
        "endCharIndex": 28
      },
      {
        "entityName": "Size",
        "startCharIndex": 8,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "I want two large pepperoni pizzas on thin crust",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 17,
        "endCharIndex": 32
      },
      {
        "entityName": "Size",
        "startCharIndex": 11,
        "endCharIndex": 15
      },
      {
        "entityName": "Quantity",
        "startCharIndex": 7,
        "endCharIndex": 9
      },
      {
        "entityName": "Crust",
        "startCharIndex": 37,
        "endCharIndex": 46
      }
    ]
  }
]

建立披薩應用程式

建立 Pizza 應用程式。

  1. 選取 pizza-app-for-luis-v6.json 以顯示 pizza-app-for-luis.json 檔案的 GitHub 頁面。
  2. 以滑鼠右鍵按一下或長點選 [原始] 按鈕,然後選取 [另存連結]pizza-app-for-luis.json 儲存到您的電腦。
  3. 登入 LUIS 入口網站
  4. 選取我的應用程式
  5. 在 [我的應用程式] 頁面中,選取 [+ 新增對話應用程式]
  6. 選取 [匯入為 JSON]
  7. 在 [匯入新的應用程式] 對話方塊中,選取 [選擇檔案] 按鈕。
  8. 選取您下載的 pizza-app-for-luis.json 檔案,然後選取 [開啟]
  9. 在 [匯入新的應用程式] 對話方塊的 [名稱] 欄位中,輸入您 Pizza 應用程式的名稱,然後選取 [完成] 按鈕。

應用程式將會匯入。

如果您看到 [如何建立有效的 LUIS 應用程式] 對話方塊,請關閉對話方塊。

將 Pizza 應用程式定型並發佈

您應該會看到 [意圖] 頁面,其中包含 Pizza 應用程式中的意圖清單。

  1. 在 LUIS 網站的右上方,選取 [定型] 按鈕。

    訓練按鈕

  2. [定型] 按鈕停用時,定型即完成。

若要在聊天機器人或其他用戶端應用程式中收到 LUIS 預測,則須將應用程式發佈到預測端點。

  1. 選取右上方導覽列中的 [發佈]

    發佈至端點之按鈕的螢幕快照。

  2. 選取 [生產位置],然後選取 [完成]

    LUIS 發佈至端點的螢幕快照。

  3. 選取通知中的 [存取您的端點 URL],以移至 [Azure 資源] 頁面。 只有在具有與應用程式相關聯的預測資源時,您才能夠看到 URL。 您也可以按一下 [管理] 以尋找 [Azure 資源] 頁面。

    顯示應用程式已發佈之訊息的螢幕快照。

將撰寫資源新增至 Pizza 應用程式

  1. 選取 [管理]
  2. 選取 [Azure 資源]
  3. 選取 [製作資源]
  4. 選取 [變更製作資源]

如果您有撰寫資源,請輸入租用戶名稱訂用帳戶名稱,以及您撰寫資源的 LUIS 資源名稱

如果您沒有撰寫資源:

  1. 選取 [建立新的資源]
  2. 輸入租用戶名稱資源名稱訂用帳戶名稱Azure 資源群組名稱

您的 Pizza 應用程式現在已準備好可以使用。

針對您的 Pizza 應用程式記錄存取值

若要使用您新的 Pizza 應用程式,將需要 Pizza 應用程式的應用程式識別碼、撰寫金鑰和撰寫端點。 若要取得預測,您需要個別的預測端點和預測金鑰。

若要尋找這些值:

  1. 從 [意圖] 頁面上,選取 [管理]
  2. 從 [應用程式設定] 頁面上,記錄 [應用程式識別碼]
  3. 選取 [Azure 資源]
  4. 選取 [製作資源]
  5. 從 [撰寫資源] 和 [預測資源] 索引標籤中,記下主要金鑰。 此值是您的撰寫金鑰。
  6. 記錄 [端點 URL]。 此值是您的撰寫端點。

以程式設計方式變更模型

  1. 建立以 C# 語言為目標的新主控台應用程式,其專案和資料夾名稱為 csharp-model-with-rest

    dotnet new console -lang C# -n csharp-model-with-rest
    
  2. 變更為您建立的 csharp-model-with-rest 目錄,並使用下列命令安裝必要的相依性:

    cd csharp-model-with-rest
    dotnet add package System.Net.Http
    dotnet add package JsonFormatterPlus
    
  3. 使用下列程式碼覆寫 Program.cs:

    //
    // This quickstart shows how to add utterances to a LUIS model using the REST APIs.
    //
    
    using System;
    using System.IO;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using System.Linq;
    
    // 3rd party NuGet packages
    using JsonFormatterPlus;
    
    namespace AddUtterances
    {
        class Program
        {
            //////////
            // Values to modify.
    
            // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
            static string appID = "PASTE_YOUR_LUIS_APP_ID_HERE";
    
            // YOUR-AUTHORING-KEY: Your LUIS authoring key, 32 character value.
            static string authoringKey = "PASTE_YOUR_LUIS_AUTHORING_SUBSCRIPTION_KEY_HERE";
    
            // YOUR-AUTHORING-ENDPOINT: Replace this endpoint with your authoring key endpoint.
            // For example, "https://your-resource-name.cognitiveservices.azure.com/"
            static string authoringEndpoint = "PASTE_YOUR_LUIS_AUTHORING_ENDPOINT_HERE";
    
            // NOTE: Replace this your version number.
            static string appVersion = "0.1";
            //////////
    
            static string host = String.Format("{0}luis/authoring/v3.0-preview/apps/{1}/versions/{2}/", authoringEndpoint, appID, appVersion);
    
            // GET request with authentication
            async static Task<HttpResponseMessage> SendGet(string uri)
            {
                using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method = HttpMethod.Get;
                    request.RequestUri = new Uri(uri);
                    request.Headers.Add("Ocp-Apim-Subscription-Key", authoringKey);
                    return await client.SendAsync(request);
                }
            }
    
            // POST request with authentication
            async static Task<HttpResponseMessage> SendPost(string uri, string requestBody)
            {
                using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method = HttpMethod.Post;
                    request.RequestUri = new Uri(uri);
    
                    if (!String.IsNullOrEmpty(requestBody))
                    {
                        request.Content = new StringContent(requestBody, Encoding.UTF8, "text/json");
                    }
    
                    request.Headers.Add("Ocp-Apim-Subscription-Key", authoringKey);
                    return await client.SendAsync(request);
                }
            }
    
            // Add utterances as string with POST request
            async static Task AddUtterances(string utterances)
            {
                string uri = host + "examples";
    
                var response = await SendPost(uri, utterances);
                var result = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Added utterances.");
                Console.WriteLine(JsonFormatter.Format(result));
            }
    
            // Train app after adding utterances
            async static Task Train()
            {
                string uri = host  + "train";
    
                var response = await SendPost(uri, null);
                var result = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Sent training request.");
                Console.WriteLine(JsonFormatter.Format(result));
            }
    
            // Check status of training
            async static Task Status()
            {
                var response = await SendGet(host  + "train");
                var result = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Requested training status.");
                Console.WriteLine(JsonFormatter.Format(result));
            }
    
            // Add utterances, train, check status
            static void Main(string[] args)
            {
                string utterances = @"
                [
                    {
                        'text': 'order a pizza',
                        'intentName': 'ModifyOrder',
                        'entityLabels': [
                            {
                                'entityName': 'Order',
                                'startCharIndex': 6,
                                'endCharIndex': 12
                            }
                        ]
                    },
                    {
                        'text': 'order a large pepperoni pizza',
                        'intentName': 'ModifyOrder',
                        'entityLabels': [
                            {
                                'entityName': 'Order',
                                'startCharIndex': 6,
                                'endCharIndex': 28
                            },
                            {
                                'entityName': 'FullPizzaWithModifiers',
                                'startCharIndex': 6,
                                'endCharIndex': 28
                            },
                            {
                                'entityName': 'PizzaType',
                                'startCharIndex': 14,
                                'endCharIndex': 28
                            },
                            {
                                'entityName': 'Size',
                                'startCharIndex': 8,
                                'endCharIndex': 12
                            }
                        ]
                    },
                    {
                        'text': 'I want two large pepperoni pizzas on thin crust',
                        'intentName': 'ModifyOrder',
                        'entityLabels': [
                            {
                                'entityName': 'Order',
                                'startCharIndex': 7,
                                'endCharIndex': 46
                            },
                            {
                                'entityName': 'FullPizzaWithModifiers',
                                'startCharIndex': 7,
                                'endCharIndex': 46
                            },
                            {
                                'entityName': 'PizzaType',
                                'startCharIndex': 17,
                                'endCharIndex': 32
                            },
                            {
                                'entityName': 'Size',
                                'startCharIndex': 11,
                                'endCharIndex': 15
                            },
                            {
                                'entityName': 'Quantity',
                                'startCharIndex': 7,
                                'endCharIndex': 9
                            },
                            {
                                'entityName': 'Crust',
                                'startCharIndex': 37,
                                'endCharIndex': 46
                            }
                        ]
                    }
                ]
                ";
    
                AddUtterances(utterances).Wait();
                Train().Wait();
                Status().Wait();
            }
        }
    }
    
  4. 使用您自己的值取代以 YOUR- 開頭的值。

    資訊 目的
    YOUR-APP-ID 您的 LUIS 應用程式識別碼。
    YOUR-AUTHORING-KEY 您的 32 字元撰寫金鑰。
    YOUR-AUTHORING-ENDPOINT 您的撰寫 URL 端點。 例如: https://replace-with-your-resource-name.api.cognitive.microsoft.com/ 。 您已在建立資源時設定資源名稱。

    在 LUIS 入口網站中,您可以在 [Azure 資源] 頁面上的 [管理] 區段中看到指派的金鑰和端點。 在相同的 [管理] 區段中,您可以在 [應用程式設定] 頁面上取得應用程式識別碼。

    重要

    完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

  5. 建置主控台應用程式。

    dotnet build
    
  6. 執行主控台應用程式 (Console Application)。

    dotnet run
    
  7. 檢閱撰寫回應:

    Added utterances.
    [
        {
            "value": {
                "ExampleId": 1137150691,
                "UtteranceText": "order a pizza"
            },
            "hasError": false
        },
        {
            "value": {
                "ExampleId": 1137150692,
                "UtteranceText": "order a large pepperoni pizza"
            },
            "hasError": false
        },
        {
            "value": {
                "ExampleId": 1137150693,
                "UtteranceText": "i want two large pepperoni pizzas on thin crust"
            },
            "hasError": false
        }
    ]
    Sent training request.
    {
        "statusId": 9,
        "status": "Queued"
    }
    Requested training status.
    [
        {
            "modelId": "edb46abf-0000-41ab-beb2-a41a0fe1630f",
            "details": {
                "statusId": 9,
                "status": "Queued",
                "exampleCount": 0
            }
        },
        {
            "modelId": "a5030be2-616c-4648-bf2f-380fa9417d37",
            "details": {
                "statusId": 9,
                "status": "Queued",
                "exampleCount": 0
            }
        },
        {
            "modelId": "3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9",
            "details": {
                "statusId": 9,
                "status": "Queued",
                "exampleCount": 0
            }
        },
        {
            "modelId": "e4b6704b-1636-474c-9459-fe9ccbeba51c",
            "details": {
                "statusId": 9,
                "status": "Queued",
                "exampleCount": 0
            }
        },
        {
            "modelId": "031d3777-2a00-4a7a-9323-9a3280a30000",
            "details": {
                "statusId": 9,
                "status": "Queued",
                "exampleCount": 0
            }
        },
        {
            "modelId": "9250e7a1-06eb-4413-9432-ae132ed32583",
            "details": {
                "statusId": 9,
                "status": "Queued",
                "exampleCount": 0
            }
        }
    ]
    

清除資源

完成本快速入門時,請從檔案系統中刪除專案資料夾。

下一步

應用程式的最佳做法

參考文件 | 範例

必要條件

範例語句 JSON 檔案

範例表達會遵循特定的格式。

text 欄位包含範例語句的文字。 intentName 欄位必須對應到 LUIS 應用程式中的現有意圖名稱。 entityLabels 是必填欄位。 如果您不想要標示任何實體,請提供空白陣列。

如果 entityLabels 陣列不是空的,startCharIndexendCharIndex 必須標示 entityName 欄位中參考的實體。 索引以零為起始。 如果您在文字中的某個空格開始或結束標籤,新增語句的 API 呼叫將會失敗。

[
  {
    "text": "order a pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "order a large pepperoni pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 14,
        "endCharIndex": 28
      },
      {
        "entityName": "Size",
        "startCharIndex": 8,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "I want two large pepperoni pizzas on thin crust",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 17,
        "endCharIndex": 32
      },
      {
        "entityName": "Size",
        "startCharIndex": 11,
        "endCharIndex": 15
      },
      {
        "entityName": "Quantity",
        "startCharIndex": 7,
        "endCharIndex": 9
      },
      {
        "entityName": "Crust",
        "startCharIndex": 37,
        "endCharIndex": 46
      }
    ]
  }
]

以程式設計方式變更模型

  1. 建立新資料夾來存放您的 Java 專案,例如 java-model-with-rest

  2. 建立名為 lib 的子目錄,在下列的 Java 程式庫中複製並貼到 lib 子目錄:

  3. 建立名為 Model.java 的新檔案。 新增下列程式碼:

    //
    // This quickstart shows how to add utterances to a LUIS model using the REST APIs.
    //
    
    import java.io.*;
    import java.net.URI;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    // To compile, execute this command at the console:
    //      Windows: javac -cp ";lib/*" Model.java
    //      macOs: javac -cp ":lib/*" Model.java
    //      Linux: javac -cp ":lib/*" Model.java
    
    // To run, execute this command at the console:
    //      Windows: java -cp ";lib/*" Model
    //      macOs: java -cp ":lib/*" Model
    //      Linux: java -cp ":lib/*" Model
    
    public class Model
    {
        public static void main(String[] args)
        {
            try
            {
                //////////
                // Values to modify.
    
                // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
                String AppId = "PASTE_YOUR_LUIS_APP_ID_HERE";
    
                // YOUR-AUTHORING-KEY: Your LUIS authoring key, 32 character value.
                String Key = "PASTE_YOUR_LUIS_AUTHORING_SUBSCRIPTION_KEY_HERE";
    
                // YOUR-AUTHORING-ENDPOINT: Replace this with your authoring key endpoint.
                // For example, "https://your-resource-name.cognitiveservices.azure.com/"
                String Endpoint = "PASTE_YOUR_LUIS_AUTHORING_ENDPOINT_HERE";
    
                // NOTE: Replace this your version number. The Pizza app uses a version number of "0.1".
                String Version = "0.1";
                //////////
    
                // The list of utterances to add, in JSON format.
                String Utterances = "[{'text': 'order a pizza', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 6, 'endCharIndex': 12}]}, {'text': 'order a large pepperoni pizza', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 6, 'endCharIndex': 28}, {'entityName': 'FullPizzaWithModifiers', 'startCharIndex': 6, 'endCharIndex': 28}, {'entityName': 'PizzaType', 'startCharIndex': 14, 'endCharIndex': 28}, {'entityName': 'Size', 'startCharIndex': 8, 'endCharIndex': 12}]}, {'text': 'I want two large pepperoni pizzas on thin crust', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 7, 'endCharIndex': 46}, {'entityName': 'FullPizzaWithModifiers', 'startCharIndex': 7, 'endCharIndex': 46}, {'entityName': 'PizzaType', 'startCharIndex': 17, 'endCharIndex': 32}, {'entityName': 'Size', 'startCharIndex': 11, 'endCharIndex': 15}, {'entityName': 'Quantity', 'startCharIndex': 7, 'endCharIndex': 9}, {'entityName': 'Crust', 'startCharIndex': 37, 'endCharIndex': 46}]}]";
    
                // Create the URLs for uploading example utterances and for training.
                URIBuilder addUtteranceURL = new URIBuilder(Endpoint + "luis/authoring/v3.0-preview/apps/" + AppId + "/versions/" + Version + "/examples");
                URIBuilder trainURL = new URIBuilder(Endpoint + "luis/authoring/v3.0-preview/apps/" + AppId + "/versions/" + Version + "/train");
                URI addUtterancesURI = addUtteranceURL.build();
                URI trainURI = trainURL.build();
    
    
                // Add the utterances.
    
                // Create the request.
                HttpClient addUtterancesClient = HttpClients.createDefault();
                HttpPost addUtterancesRequest = new HttpPost(addUtterancesURI);
    
                // Add the headers.
                addUtterancesRequest.setHeader("Ocp-Apim-Subscription-Key",Key);
                addUtterancesRequest.setHeader("Content-type","application/json");
    
                // Add the body.
                StringEntity stringEntity = new StringEntity(Utterances);
                addUtterancesRequest.setEntity(stringEntity);
    
                // Execute the request and obtain the response.
                HttpResponse addUtterancesResponse = addUtterancesClient.execute(addUtterancesRequest);
                HttpEntity addUtterancesEntity = addUtterancesResponse.getEntity();
    
                // Print the response on the console.
                if (addUtterancesEntity != null)
                {
                    System.out.println(EntityUtils.toString(addUtterancesEntity));
                }
    
    
                // Train the model.
    
                // Create the request.
                HttpClient trainClient = HttpClients.createDefault();
                HttpPost trainRequest = new HttpPost(trainURI);
    
                // Add the headers.
                trainRequest.setHeader("Ocp-Apim-Subscription-Key",Key);
                trainRequest.setHeader("Content-type","application/json");
    
                // Execute the request and obtain the response.
                HttpResponse trainResponse = trainClient.execute(trainRequest);
                HttpEntity trainEntity = trainResponse.getEntity();
    
                // Print the response on the console.
                if (trainEntity != null)
                {
                    System.out.println(EntityUtils.toString(trainEntity));
                }
    
    
                // Get the training status.
    
    
                // Create the request.
                HttpClient trainStatusClient = HttpClients.createDefault();
                HttpGet trainStatusRequest = new HttpGet(trainURI);
    
                // Add the headers.
                trainStatusRequest.setHeader("Ocp-Apim-Subscription-Key",Key);
                trainStatusRequest.setHeader("Content-type","application/json");
    
                // Execute the request and obtain the response.
                HttpResponse trainStatusResponse = trainStatusClient.execute(trainStatusRequest);
                HttpEntity trainStatusEntity = trainStatusResponse.getEntity();
    
                // Print the response on the console.
                if (trainStatusEntity != null)
                {
                    System.out.println(EntityUtils.toString(trainStatusEntity));
                }
            }
    
            // Display errors if they occur.
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    
  4. 使用您自己的值取代以 YOUR- 開頭的值。

    資訊 目的
    YOUR-APP-ID 您的 LUIS 應用程式識別碼。
    YOUR-AUTHORING-KEY 您的 32 字元撰寫金鑰。
    YOUR-AUTHORING-ENDPOINT 您的撰寫 URL 端點。 例如: https://replace-with-your-resource-name.api.cognitive.microsoft.com/ 。 您已在建立資源時設定資源名稱。

    在 LUIS 入口網站中,您可以在 [Azure 資源] 頁面上的 [管理] 區段中看到指派的金鑰和端點。 在相同的 [管理] 區段中,您可以在 [應用程式設定] 頁面上取得應用程式識別碼。

    重要

    完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

  5. 在您建立 Model.java 檔案所在的相同目錄中使用命令提示字元,輸入下列命令以編譯 Java 檔案:

    • 如果您使用的是 Windows,請使用此命令:javac -cp ";lib/*" Model.java
    • 如果您使用 macOS 或 Linux,請使用此命令:javac -cp ":lib/*" Model.java
  6. 藉由在命令提示字元中輸入下列文字,從命令列執行 Java 應用程式:

    • 如果您使用的是 Windows,請使用此命令:java -cp ";lib/*" Model
    • 如果您使用 macOS 或 Linux,請使用此命令:java -cp ":lib/*" Model
  7. 檢閱撰寫回應:

    [{"value":{"ExampleId":1137150691,"UtteranceText":"order a pizza"},"hasError":false},{"value":{"ExampleId":1137150692,"UtteranceText":"order a large pepperoni pizza"},"hasError":false},{"value":{"ExampleId":1137150693,"UtteranceText":"i want two large pepperoni pizzas on thin crust"},"hasError":false}]
    {"statusId":9,"status":"Queued"}
    [{"modelId":"edb46abf-0000-41ab-beb2-a41a0fe1630f","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"a5030be2-616c-4648-bf2f-380fa9417d37","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"e4b6704b-1636-474c-9459-fe9ccbeba51c","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"031d3777-2a00-4a7a-9323-9a3280a30000","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"9250e7a1-06eb-4413-9432-ae132ed32583","details":{"statusId":3,"status":"InProgress","exampleCount":0,"progressSubstatus":"CollectingData"}}]
    

    以下是針對可讀性格式化的輸出:

    [
      {
        "value": {
          "ExampleId": 1137150691,
          "UtteranceText": "order a pizza"
        },
        "hasError": false
      },
      {
        "value": {
          "ExampleId": 1137150692,
          "UtteranceText": "order a large pepperoni pizza"
        },
        "hasError": false
      },
      {
        "value": {
          "ExampleId": 1137150693,
          "UtteranceText": "i want two large pepperoni pizzas on thin crust"
        },
        "hasError": false
      }
    ]
    {
      "statusId": 9,
      "status": "Queued"
    }
    [
      {
        "modelId": "edb46abf-0000-41ab-beb2-a41a0fe1630f",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "a5030be2-616c-4648-bf2f-380fa9417d37",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "e4b6704b-1636-474c-9459-fe9ccbeba51c",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "031d3777-2a00-4a7a-9323-9a3280a30000",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "9250e7a1-06eb-4413-9432-ae132ed32583",
        "details": {
          "statusId": 3,
          "status": "InProgress",
          "exampleCount": 0,
          "progressSubstatus": "CollectingData"
        }
      }
    ]
    

清除資源

完成本快速入門時,請從檔案系統中刪除專案資料夾。

下一步

應用程式的最佳做法

參考文件 | 範例

必要條件

範例語句 JSON 檔案

範例表達會遵循特定的格式。

text 欄位包含範例語句的文字。 intentName 欄位必須對應到 LUIS 應用程式中的現有意圖名稱。 entityLabels 是必填欄位。 如果您不想要標示任何實體,請提供空白陣列。

如果 entityLabels 陣列不是空的,startCharIndexendCharIndex 必須標示 entityName 欄位中參考的實體。 索引以零為起始。 如果您在文字中的某個空格開始或結束標籤,新增語句的 API 呼叫將會失敗。

[
  {
    "text": "order a pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "order a large pepperoni pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 14,
        "endCharIndex": 28
      },
      {
        "entityName": "Size",
        "startCharIndex": 8,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "I want two large pepperoni pizzas on thin crust",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 17,
        "endCharIndex": 32
      },
      {
        "entityName": "Size",
        "startCharIndex": 11,
        "endCharIndex": 15
      },
      {
        "entityName": "Quantity",
        "startCharIndex": 7,
        "endCharIndex": 9
      },
      {
        "entityName": "Crust",
        "startCharIndex": 37,
        "endCharIndex": 46
      }
    ]
  }
]

建立披薩應用程式

建立 Pizza 應用程式。

  1. 選取 pizza-app-for-luis-v6.json 以顯示 pizza-app-for-luis.json 檔案的 GitHub 頁面。
  2. 以滑鼠右鍵按一下或長點選 [原始] 按鈕,然後選取 [另存連結]pizza-app-for-luis.json 儲存到您的電腦。
  3. 登入 LUIS 入口網站
  4. 選取我的應用程式
  5. 在 [我的應用程式] 頁面中,選取 [+ 新增對話應用程式]
  6. 選取 [匯入為 JSON]
  7. 在 [匯入新的應用程式] 對話方塊中,選取 [選擇檔案] 按鈕。
  8. 選取您下載的 pizza-app-for-luis.json 檔案,然後選取 [開啟]
  9. 在 [匯入新的應用程式] 對話方塊的 [名稱] 欄位中,輸入您 Pizza 應用程式的名稱,然後選取 [完成] 按鈕。

應用程式將會匯入。

如果您看到 [如何建立有效的 LUIS 應用程式] 對話方塊,請關閉對話方塊。

將 Pizza 應用程式定型並發佈

您應該會看到 [意圖] 頁面,其中包含 Pizza 應用程式中的意圖清單。

  1. 在 LUIS 網站的右上方,選取 [定型] 按鈕。

    訓練按鈕

  2. [定型] 按鈕停用時,定型即完成。

若要在聊天機器人或其他用戶端應用程式中收到 LUIS 預測,則須將應用程式發佈到預測端點。

  1. 選取右上方導覽列中的 [發佈]

    發佈至端點之按鈕的螢幕快照。

  2. 選取 [生產位置],然後選取 [完成]

    LUIS 發佈至端點的螢幕快照。

  3. 選取通知中的 [存取您的端點 URL],以移至 [Azure 資源] 頁面。 只有在具有與應用程式相關聯的預測資源時,您才能夠看到 URL。 您也可以按一下 [管理] 以尋找 [Azure 資源] 頁面。

    顯示應用程式已發佈之訊息的螢幕快照。

將撰寫資源新增至 Pizza 應用程式

  1. 選取 [管理]
  2. 選取 [Azure 資源]
  3. 選取 [製作資源]
  4. 選取 [變更製作資源]

如果您有撰寫資源,請輸入租用戶名稱訂用帳戶名稱,以及您撰寫資源的 LUIS 資源名稱

如果您沒有撰寫資源:

  1. 選取 [建立新的資源]
  2. 輸入租用戶名稱資源名稱訂用帳戶名稱Azure 資源群組名稱

您的 Pizza 應用程式現在已準備好可以使用。

針對您的 Pizza 應用程式記錄存取值

若要使用您新的 Pizza 應用程式,將需要 Pizza 應用程式的應用程式識別碼、撰寫金鑰和撰寫端點。 若要取得預測,您需要個別的預測端點和預測金鑰。

若要尋找這些值:

  1. 從 [意圖] 頁面上,選取 [管理]
  2. 從 [應用程式設定] 頁面上,記錄 [應用程式識別碼]
  3. 選取 [Azure 資源]
  4. 選取 [製作資源]
  5. 從 [撰寫資源] 和 [預測資源] 索引標籤中,記下主要金鑰。 此值是您的撰寫金鑰。
  6. 記錄 [端點 URL]。 此值是您的撰寫端點。

以程式設計方式變更模型

  1. 建立名為 predict.go 的新檔案。 新增下列程式碼:

    //
    // This quickstart shows how to add utterances to a LUIS model using the REST APIs.
    //
    
    // dependencies
    package main
    import (
        "fmt"
        "net/http"
        "io/ioutil"
        "log"
        "strings"
    )
    
    // main function
    func main() {
    
        //////////
        // Values to modify.
    
        // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
        var appID = "PASTE_YOUR_LUIS_APP_ID_HERE"
    
        // YOUR-AUTHORING-KEY: Your LUIS authoring key, 32 character value.
        var authoringKey = "PASTE_YOUR_LUIS_AUTHORING_SUBSCRIPTION_KEY_HERE"
    
        //  YOUR-AUTHORING-ENDPOINT: Replace this with your authoring key endpoint.
        // For example, "https://your-resource-name.cognitiveservices.azure.com/"
        var endpoint = "PASTE_YOUR_LUIS_AUTHORING_ENDPOINT_HERE"
    
        // NOTE: Replace this your version number. The Pizza app uses a version number of "0.1".
        var version = "0.1"
        //////////
    
        var exampleUtterances = "[{'text': 'order a pizza', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 6, 'endCharIndex': 12}]}, {'text': 'order a large pepperoni pizza', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 6, 'endCharIndex': 28}, {'entityName': 'FullPizzaWithModifiers', 'startCharIndex': 6, 'endCharIndex': 28}, {'entityName': 'PizzaType', 'startCharIndex': 14, 'endCharIndex': 28}, {'entityName': 'Size', 'startCharIndex': 8, 'endCharIndex': 12}]}, {'text': 'I want two large pepperoni pizzas on thin crust', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 7, 'endCharIndex': 46}, {'entityName': 'FullPizzaWithModifiers', 'startCharIndex': 7, 'endCharIndex': 46}, {'entityName': 'PizzaType', 'startCharIndex': 17, 'endCharIndex': 32}, {'entityName': 'Size', 'startCharIndex': 11, 'endCharIndex': 15}, {'entityName': 'Quantity', 'startCharIndex': 7, 'endCharIndex': 9}, {'entityName': 'Crust', 'startCharIndex': 37, 'endCharIndex': 46}]}]"
    
        fmt.Println("add example utterances requested")
        addUtterance(authoringKey, appID, version, exampleUtterances, endpoint)
    
        fmt.Println("training selected")
        requestTraining(authoringKey, appID, version, endpoint)
    
        fmt.Println("training status selected")
        getTrainingStatus(authoringKey, appID, version, endpoint)
    }
    
    // Send the list of utterances to the model.
    func addUtterance(authoringKey string, appID string,  version string, labeledExampleUtterances string, endpoint string){
    
        var authoringUrl = fmt.Sprintf("%sluis/authoring/v3.0-preview/apps/%s/versions/%s/examples", endpoint, appID, version)
    
        httpRequest("POST", authoringUrl, authoringKey, labeledExampleUtterances)
    }
    
    // Request training.
    func requestTraining(authoringKey string, appID string,  version string, endpoint string){
    
        trainApp("POST", authoringKey, appID, version, endpoint)
    }
    
    
    func trainApp(httpVerb string, authoringKey string, appID string,  version string, endpoint string){
    
        var authoringUrl = fmt.Sprintf("%sluis/authoring/v3.0-preview/apps/%s/versions/%s/train", endpoint, appID, version)
    
        httpRequest(httpVerb,authoringUrl, authoringKey, "")
    }
    
    
    func getTrainingStatus(authoringKey string, appID string, version string, endpoint string){
    
        trainApp("GET", authoringKey, appID, version, endpoint)
    }
    
    // generic HTTP request
    // includes setting header with authoring key
    func httpRequest(httpVerb string, url string, authoringKey string, body string){
    
        client := &http.Client{}
    
        request, err := http.NewRequest(httpVerb, url, strings.NewReader(body))
        request.Header.Add("Ocp-Apim-Subscription-Key", authoringKey)
    
        fmt.Println("body")
        fmt.Println(body)
    
        response, err := client.Do(request)
    
        if err != nil {
            log.Fatal(err)
        } else {
            defer response.Body.Close()
    
            contents, err := ioutil.ReadAll(response.Body)
    
            if err != nil {
                log.Fatal(err)
            }
    
            fmt.Println("   ", response.StatusCode)
            fmt.Println(string(contents))
        }
    }
    
  2. 使用您自己的值取代以 YOUR- 開頭的值。

    資訊 目的
    YOUR-APP-ID 您的 LUIS 應用程式識別碼。
    YOUR-AUTHORING-KEY 您的 32 字元撰寫金鑰。
    YOUR-AUTHORING-ENDPOINT 您的撰寫 URL 端點。 例如: https://replace-with-your-resource-name.api.cognitive.microsoft.com/ 。 您已在建立資源時設定資源名稱。

    在 LUIS 入口網站中,您可以在 [Azure 資源] 頁面上的 [管理] 區段中看到指派的金鑰和端點。 在相同的 [管理] 區段中,您可以在 [應用程式設定] 頁面上取得應用程式識別碼。

    重要

    完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

  3. 在您建立檔案所在的相同目錄中使用命令提示字元,輸入下列命令以編譯 Go 檔案:

    go build model.go
    
  4. 藉由在命令提示字元中輸入下列文字,從命令列執行 Go 應用程式:

    go run model.go
    
  5. 檢閱撰寫回應:

    add example utterances requested
    body
    [{'text': 'order a pizza', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 6, 'endCharIndex': 12}]}, {'text': 'order a large pepperoni pizza', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 6, 'endCharIndex': 28}, {'entityName': 'FullPizzaWithModifiers', 'startCharIndex': 6, 'endCharIndex': 28}, {'entityName': 'PizzaType', 'startCharIndex': 14, 'endCharIndex': 28}, {'entityName': 'Size', 'startCharIndex': 8, 'endCharIndex': 12}]}, {'text': 'I want two large pepperoni pizzas on thin crust', 'intentName': 'ModifyOrder', 'entityLabels': [{'entityName': 'Order', 'startCharIndex': 7, 'endCharIndex': 46}, {'entityName': 'FullPizzaWithModifiers', 'startCharIndex': 7, 'endCharIndex': 46}, {'entityName': 'PizzaType', 'startCharIndex': 17, 'endCharIndex': 32}, {'entityName': 'Size', 'startCharIndex': 11, 'endCharIndex': 15}, {'entityName': 'Quantity', 'startCharIndex': 7, 'endCharIndex': 9}, {'entityName': 'Crust', 'startCharIndex': 37, 'endCharIndex': 46}]}]
        201
    [{"value":{"ExampleId":1137150691,"UtteranceText":"order a pizza"},"hasError":false},{"value":{"ExampleId":1137150692,"UtteranceText":"order a large pepperoni pizza"},"hasError":false},{"value":{"ExampleId":1137150693,"UtteranceText":"i want two large pepperoni pizzas on thin crust"},"hasError":false}]
    training selected
    body
    
        202
    {"statusId":9,"status":"Queued"}
    training status selected
    body
    
        200
    [{"modelId":"edb46abf-0000-41ab-beb2-a41a0fe1630f","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"a5030be2-616c-4648-bf2f-380fa9417d37","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"e4b6704b-1636-474c-9459-fe9ccbeba51c","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"031d3777-2a00-4a7a-9323-9a3280a30000","details":{"statusId":9,"status":"Queued","exampleCount":0}},{"modelId":"9250e7a1-06eb-4413-9432-ae132ed32583","details":{"statusId":9,"status":"Queued","exampleCount":0}}]
    

    以下是針對可讀性格式化的輸出:

    add example utterances requested
    body
    [
      {
        'text': 'order a pizza',
        'intentName': 'ModifyOrder',
        'entityLabels': [
          {
            'entityName': 'Order',
            'startCharIndex': 6,
            'endCharIndex': 12
          }
        ]
      },
      {
        'text': 'order a large pepperoni pizza',
        'intentName': 'ModifyOrder',
        'entityLabels': [
          {
            'entityName': 'Order',
            'startCharIndex': 6,
            'endCharIndex': 28
          },
          {
            'entityName': 'FullPizzaWithModifiers',
            'startCharIndex': 6,
            'endCharIndex': 28
          },
          {
            'entityName': 'PizzaType',
            'startCharIndex': 14,
            'endCharIndex': 28
          },
          {
            'entityName': 'Size',
            'startCharIndex': 8,
            'endCharIndex': 12
          }
        ]
      },
      {
        'text': 'I want two large pepperoni pizzas on thin crust',
        'intentName': 'ModifyOrder',
        'entityLabels': [
          {
            'entityName': 'Order',
            'startCharIndex': 7,
            'endCharIndex': 46
          },
          {
            'entityName': 'FullPizzaWithModifiers',
            'startCharIndex': 7,
            'endCharIndex': 46
          },
          {
            'entityName': 'PizzaType',
            'startCharIndex': 17,
            'endCharIndex': 32
          },
          {
            'entityName': 'Size',
            'startCharIndex': 11,
            'endCharIndex': 15
          },
          {
            'entityName': 'Quantity',
            'startCharIndex': 7,
            'endCharIndex': 9
          },
          {
            'entityName': 'Crust',
            'startCharIndex': 37,
            'endCharIndex': 46
          }
        ]
      }
    ]
    
        201
    [
      {
        "value": {
          "ExampleId": 1137150691,
          "UtteranceText": "order a pizza"
        },
        "hasError": false
      },
      {
        "value": {
          "ExampleId": 1137150692,
          "UtteranceText": "order a large pepperoni pizza"
        },
        "hasError": false
      },
      {
        "value": {
          "ExampleId": 1137150693,
          "UtteranceText": "i want two large pepperoni pizzas on thin crust"
        },
        "hasError": false
      }
    ]
    training selected
    body
    
        202
    {
      "statusId": 9,
      "status": "Queued"
    }
    training status selected
    body
    
        200
    [
      {
        "modelId": "edb46abf-0000-41ab-beb2-a41a0fe1630f",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "a5030be2-616c-4648-bf2f-380fa9417d37",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "e4b6704b-1636-474c-9459-fe9ccbeba51c",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "031d3777-2a00-4a7a-9323-9a3280a30000",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "9250e7a1-06eb-4413-9432-ae132ed32583",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      }
    ]
    

清除資源

您完成本快速入門時,請從檔案系統中刪除該檔案。

下一步

應用程式的最佳做法

參考文件 | 範例

必要條件

範例語句 JSON 檔案

範例表達會遵循特定的格式。

text 欄位包含範例語句的文字。 intentName 欄位必須對應到 LUIS 應用程式中的現有意圖名稱。 entityLabels 是必填欄位。 如果您不想要標示任何實體,請提供空白陣列。

如果 entityLabels 陣列不是空的,startCharIndexendCharIndex 必須標示 entityName 欄位中參考的實體。 索引以零為起始。 如果您在文字中的某個空格開始或結束標籤,新增語句的 API 呼叫將會失敗。

[
  {
    "text": "order a pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "order a large pepperoni pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 14,
        "endCharIndex": 28
      },
      {
        "entityName": "Size",
        "startCharIndex": 8,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "I want two large pepperoni pizzas on thin crust",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 17,
        "endCharIndex": 32
      },
      {
        "entityName": "Size",
        "startCharIndex": 11,
        "endCharIndex": 15
      },
      {
        "entityName": "Quantity",
        "startCharIndex": 7,
        "endCharIndex": 9
      },
      {
        "entityName": "Crust",
        "startCharIndex": 37,
        "endCharIndex": 46
      }
    ]
  }
]

建立 Node.js 專案

  1. 建立新資料夾來存放您的 Node.js 專案,例如 node-model-with-rest

  2. 開啟新的命令提示字元,瀏覽至您所建立的資料夾,然後執行下列命令:

    npm init
    

    在每個提示字元下,按 ENTER 接受預設值。

  3. 輸入下列命令來安裝 request-promise 模組:

    npm install --save request
    npm install --save request-promise
    npm install --save querystring
    

以程式設計方式變更模型

  1. 建立名為 model.js 的新檔案。 新增下列程式碼:

    //
    // This quickstart shows how to add utterances to a LUIS model using the REST APIs.
    //
    
    var request = require('request-promise');
    
    //////////
    // Values to modify.
    
    // YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
    const LUIS_appId = "PASTE_YOUR_LUIS_APP_ID_HERE";
    
    // YOUR-AUTHORING-KEY: Your LUIS authoring key, 32 character value.
    const LUIS_authoringKey = "PASTE_YOUR_LUIS_AUTHORING_SUBSCRIPTION_KEY_HERE";
    
    // YOUR-AUTHORING-ENDPOINT: Replace this with your authoring key endpoint.
    // For example, "https://your-resource-name.cognitiveservices.azure.com/"
    const LUIS_endpoint = "PASTE_YOUR_LUIS_AUTHORING_ENDPOINT_HERE";
    
    // NOTE: Replace this your version number. The Pizza app uses a version number of "0.1".
    const LUIS_versionId = "0.1";
    //////////
    
    const addUtterancesURI = `${LUIS_endpoint}luis/authoring/v3.0-preview/apps/${LUIS_appId}/versions/${LUIS_versionId}/examples`;
    const addTrainURI = `${LUIS_endpoint}luis/authoring/v3.0-preview/apps/${LUIS_appId}/versions/${LUIS_versionId}/train`;
    
    const utterances = [
        {
            'text': 'order a pizza',
            'intentName': 'ModifyOrder',
            'entityLabels': [
                {
                    'entityName': 'Order',
                    'startCharIndex': 6,
                    'endCharIndex': 12
                }
            ]
        },
        {
            'text': 'order a large pepperoni pizza',
            'intentName': 'ModifyOrder',
            'entityLabels': [
                {
                    'entityName': 'Order',
                    'startCharIndex': 6,
                    'endCharIndex': 28
                },
                {
                    'entityName': 'FullPizzaWithModifiers',
                    'startCharIndex': 6,
                    'endCharIndex': 28
                },
                {
                    'entityName': 'PizzaType',
                    'startCharIndex': 14,
                    'endCharIndex': 28
                },
                {
                    'entityName': 'Size',
                    'startCharIndex': 8,
                    'endCharIndex': 12
                }
            ]
        },
        {
            'text': 'I want two large pepperoni pizzas on thin crust',
            'intentName': 'ModifyOrder',
            'entityLabels': [
                {
                    'entityName': 'Order',
                    'startCharIndex': 7,
                    'endCharIndex': 46
                },
                {
                    'entityName': 'FullPizzaWithModifiers',
                    'startCharIndex': 7,
                    'endCharIndex': 46
                },
                {
                    'entityName': 'PizzaType',
                    'startCharIndex': 17,
                    'endCharIndex': 32
                },
                {
                    'entityName': 'Size',
                    'startCharIndex': 11,
                    'endCharIndex': 15
                },
                {
                    'entityName': 'Quantity',
                    'startCharIndex': 7,
                    'endCharIndex': 9
                },
                {
                    'entityName': 'Crust',
                    'startCharIndex': 37,
                    'endCharIndex': 46
                }
            ]
        }
    ];
    
    // Main function.
    const main = async() =>{
    
        await addUtterances(utterances);
        await train("POST");
        await train("GET");
    
    }
    
    // Adds the utterances to the model.
    const addUtterances = async (utterances) => {
    
        const options = {
            uri: addUtterancesURI,
            method: 'POST',
            headers: {
                'Ocp-Apim-Subscription-Key': LUIS_authoringKey
            },
            json: true,
            body: utterances
        };
    
        const response = await request(options)
        console.log("addUtterance:\n" + JSON.stringify(response, null, 2));
    }
    
    // With verb === "POST", sends a training request.
    // With verb === "GET", obtains the training status.
    const train = async (verb) => {
    
        const options = {
            uri: addTrainURI,
            method: verb,
            headers: {
                'Ocp-Apim-Subscription-Key': LUIS_authoringKey
            },
            json: true,
            body: null // The body can be empty for a training request
        };
    
        const response = await request(options)
        console.log("train " + verb + ":\n" + JSON.stringify(response, null, 2));
    }
    
    // MAIN
    main().then(() => console.log("done")).catch((err)=> console.log(err));
    
  2. 使用您自己的值取代以 YOUR- 開頭的值。

    資訊 目的
    YOUR-APP-ID 您的 LUIS 應用程式識別碼。
    YOUR-AUTHORING-KEY 您的 32 字元撰寫金鑰。
    YOUR-AUTHORING-ENDPOINT 您的撰寫 URL 端點。 例如: https://replace-with-your-resource-name.api.cognitive.microsoft.com/ 。 您已在建立資源時設定資源名稱。

    在 LUIS 入口網站中,您可以在 [Azure 資源] 頁面上的 [管理] 區段中看到指派的金鑰和端點。 在相同的 [管理] 區段中,您可以在 [應用程式設定] 頁面上取得應用程式識別碼。

    重要

    完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

  3. 在命令提示字元中輸入下列命令,以執行專案:

    node model.js
    
  4. 檢閱撰寫回應:

    addUtterance:
    [
      {
        "value": {
          "ExampleId": 1137150691,
          "UtteranceText": "order a pizza"
        },
        "hasError": false
      },
      {
        "value": {
          "ExampleId": 1137150692,
          "UtteranceText": "order a large pepperoni pizza"
        },
        "hasError": false
      },
      {
        "value": {
          "ExampleId": 1137150693,
          "UtteranceText": "i want two large pepperoni pizzas on thin crust"
        },
        "hasError": false
      }
    ]
    train POST:
    {
      "statusId": 9,
      "status": "Queued"
    }
    train GET:
    [
      {
        "modelId": "edb46abf-0000-41ab-beb2-a41a0fe1630f",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "a5030be2-616c-4648-bf2f-380fa9417d37",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "e4b6704b-1636-474c-9459-fe9ccbeba51c",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "031d3777-2a00-4a7a-9323-9a3280a30000",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      },
      {
        "modelId": "9250e7a1-06eb-4413-9432-ae132ed32583",
        "details": {
          "statusId": 9,
          "status": "Queued",
          "exampleCount": 0
        }
      }
    ]
    done
    

清除資源

完成本快速入門時,請從檔案系統中刪除專案資料夾。

下一步

應用程式的最佳做法

參考文件 | 範例

必要條件

範例語句 JSON 檔案

範例表達會遵循特定的格式。

text 欄位包含範例語句的文字。 intentName 欄位必須對應到 LUIS 應用程式中的現有意圖名稱。 entityLabels 是必填欄位。 如果您不想要標示任何實體,請提供空白陣列。

如果 entityLabels 陣列不是空的,startCharIndexendCharIndex 必須標示 entityName 欄位中參考的實體。 索引以零為起始。 如果您在文字中的某個空格開始或結束標籤,新增語句的 API 呼叫將會失敗。

[
  {
    "text": "order a pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "order a large pepperoni pizza",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 6,
        "endCharIndex": 28
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 14,
        "endCharIndex": 28
      },
      {
        "entityName": "Size",
        "startCharIndex": 8,
        "endCharIndex": 12
      }
    ]
  },
  {
    "text": "I want two large pepperoni pizzas on thin crust",
    "intentName": "ModifyOrder",
    "entityLabels": [
      {
        "entityName": "Order",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "FullPizzaWithModifiers",
        "startCharIndex": 7,
        "endCharIndex": 46
      },
      {
        "entityName": "PizzaType",
        "startCharIndex": 17,
        "endCharIndex": 32
      },
      {
        "entityName": "Size",
        "startCharIndex": 11,
        "endCharIndex": 15
      },
      {
        "entityName": "Quantity",
        "startCharIndex": 7,
        "endCharIndex": 9
      },
      {
        "entityName": "Crust",
        "startCharIndex": 37,
        "endCharIndex": 46
      }
    ]
  }
]

建立披薩應用程式

建立 Pizza 應用程式。

  1. 選取 pizza-app-for-luis-v6.json 以顯示 pizza-app-for-luis.json 檔案的 GitHub 頁面。
  2. 以滑鼠右鍵按一下或長點選 [原始] 按鈕,然後選取 [另存連結]pizza-app-for-luis.json 儲存到您的電腦。
  3. 登入 LUIS 入口網站
  4. 選取我的應用程式
  5. 在 [我的應用程式] 頁面中,選取 [+ 新增對話應用程式]
  6. 選取 [匯入為 JSON]
  7. 在 [匯入新的應用程式] 對話方塊中,選取 [選擇檔案] 按鈕。
  8. 選取您下載的 pizza-app-for-luis.json 檔案,然後選取 [開啟]
  9. 在 [匯入新的應用程式] 對話方塊的 [名稱] 欄位中,輸入您 Pizza 應用程式的名稱,然後選取 [完成] 按鈕。

應用程式將會匯入。

如果您看到 [如何建立有效的 LUIS 應用程式] 對話方塊,請關閉對話方塊。

將 Pizza 應用程式定型並發佈

您應該會看到 [意圖] 頁面,其中包含 Pizza 應用程式中的意圖清單。

  1. 在 LUIS 網站的右上方,選取 [定型] 按鈕。

    訓練按鈕

  2. [定型] 按鈕停用時,定型即完成。

若要在聊天機器人或其他用戶端應用程式中收到 LUIS 預測,則須將應用程式發佈到預測端點。

  1. 選取右上方導覽列中的 [發佈]

    發佈至端點之按鈕的螢幕快照。

  2. 選取 [生產位置],然後選取 [完成]

    LUIS 發佈至端點的螢幕快照。

  3. 選取通知中的 [存取您的端點 URL],以移至 [Azure 資源] 頁面。 只有在具有與應用程式相關聯的預測資源時,您才能夠看到 URL。 您也可以按一下 [管理] 以尋找 [Azure 資源] 頁面。

    顯示應用程式已發佈之訊息的螢幕快照。

將撰寫資源新增至 Pizza 應用程式

  1. 選取 [管理]
  2. 選取 [Azure 資源]
  3. 選取 [製作資源]
  4. 選取 [變更製作資源]

如果您有撰寫資源,請輸入租用戶名稱訂用帳戶名稱,以及您撰寫資源的 LUIS 資源名稱

如果您沒有撰寫資源:

  1. 選取 [建立新的資源]
  2. 輸入租用戶名稱資源名稱訂用帳戶名稱Azure 資源群組名稱

您的 Pizza 應用程式現在已準備好可以使用。

針對您的 Pizza 應用程式記錄存取值

若要使用您新的 Pizza 應用程式,將需要 Pizza 應用程式的應用程式識別碼、撰寫金鑰和撰寫端點。 若要取得預測,您需要個別的預測端點和預測金鑰。

若要尋找這些值:

  1. 從 [意圖] 頁面上,選取 [管理]
  2. 從 [應用程式設定] 頁面上,記錄 [應用程式識別碼]
  3. 選取 [Azure 資源]
  4. 選取 [製作資源]
  5. 從 [撰寫資源] 和 [預測資源] 索引標籤中,記下主要金鑰。 此值是您的撰寫金鑰。
  6. 記錄 [端點 URL]。 此值是您的撰寫端點。

以程式設計方式變更模型

  1. 建立名為 model.py 的新檔案。 新增下列程式碼:

    ########### Python 3.6 #############
    
    #
    # This quickstart shows how to add utterances to a LUIS model using the REST APIs.
    #
    
    import requests
    
    try:
    
        ##########
        # Values to modify.
    
        # YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
        appId = "PASTE_YOUR_LUIS_APP_ID_HERE"
    
        # YOUR-AUTHORING-KEY: Your LUIS authoring key, 32 character value.
        authoring_key = "PASTE_YOUR_LUIS_AUTHORING_SUBSCRIPTION_KEY_HERE"
    
        # YOUR-AUTHORING-ENDPOINT: Replace this with your authoring key endpoint.
        # For example, "https://your-resource-name.cognitiveservices.azure.com/"
        authoring_endpoint = "PASTE_YOUR_LUIS_AUTHORING_ENDPOINT_HERE"
    
        # The version number of your LUIS app
        app_version = "0.1"
        ##########
    
        # The headers to use in this REST call.
        headers = {'Ocp-Apim-Subscription-Key': authoring_key}
    
        # The URL parameters to use in this REST call.
        params ={
            #'timezoneOffset': '0',
            #'verbose': 'true',
            #'show-all-intents': 'true',
            #'spellCheck': 'false',
            #'staging': 'false'
        }
    
        # List of example utterances to send to the LUIS app.
        data = """[
        {
            'text': 'order a pizza',
            'intentName': 'ModifyOrder',
            'entityLabels': [
                {
                    'entityName': 'Order',
                    'startCharIndex': 6,
                    'endCharIndex': 12
                }
            ]
        },
        {
            'text': 'order a large pepperoni pizza',
            'intentName': 'ModifyOrder',
            'entityLabels': [
                {
                    'entityName': 'Order',
                    'startCharIndex': 6,
                    'endCharIndex': 28
                },
                {
                    'entityName': 'FullPizzaWithModifiers',
                    'startCharIndex': 6,
                    'endCharIndex': 28
                },
                {
                    'entityName': 'PizzaType',
                    'startCharIndex': 14,
                    'endCharIndex': 28
                },
                {
                    'entityName': 'Size',
                    'startCharIndex': 8,
                    'endCharIndex': 12
                }
            ]
        },
        {
            'text': 'I want two large pepperoni pizzas on thin crust',
            'intentName': 'ModifyOrder',
            'entityLabels': [
                {
                    'entityName': 'Order',
                    'startCharIndex': 7,
                    'endCharIndex': 46
                },
                {
                    'entityName': 'FullPizzaWithModifiers',
                    'startCharIndex': 7,
                    'endCharIndex': 46
                },
                {
                    'entityName': 'PizzaType',
                    'startCharIndex': 17,
                    'endCharIndex': 32
                },
                {
                    'entityName': 'Size',
                    'startCharIndex': 11,
                    'endCharIndex': 15
                },
                {
                    'entityName': 'Quantity',
                    'startCharIndex': 7,
                    'endCharIndex': 9
                },
                {
                    'entityName': 'Crust',
                    'startCharIndex': 37,
                    'endCharIndex': 46
                }
            ]
        }
    ]
    """
    
    
        # Make the REST call to POST the list of example utterances.
        response = requests.post(f'{authoring_endpoint}luis/authoring/v3.0-preview/apps/{appId}/versions/{app_version}/examples',
            headers=headers, params=params, data=data)
    
        # Display the results on the console.
        print('Add the list of utterances:')
        print(response.json())
    
    
        # Make the REST call to initiate a training session.
        response = requests.post(f'{authoring_endpoint}luis/authoring/v3.0-preview/apps/{appId}/versions/{app_version}/train',
            headers=headers, params=params, data=None)
    
        # Display the results on the console.
        print('Request training:')
        print(response.json())
    
    
        # Make the REST call to request the status of training.
        response = requests.get(f'{authoring_endpoint}luis/authoring/v3.0-preview/apps/{appId}/versions/{app_version}/train',
            headers=headers, params=params, data=None)
    
        # Display the results on the console.
        print('Request training status:')
        print(response.json())
    
    
    except Exception as e:
        # Display the error string.
        print(f'{e}')
    
  2. 使用您自己的值取代以 YOUR- 開頭的值。

    資訊 目的
    YOUR-APP-ID 您的 LUIS 應用程式識別碼。
    YOUR-AUTHORING-KEY 您的 32 字元撰寫金鑰。
    YOUR-AUTHORING-ENDPOINT 您的撰寫 URL 端點。 例如: https://replace-with-your-resource-name.api.cognitive.microsoft.com/ 。 您已在建立資源時設定資源名稱。

    在 LUIS 入口網站中,您可以在 [Azure 資源] 頁面上的 [管理] 區段中看到指派的金鑰和端點。 在相同的 [管理] 區段中,您可以在 [應用程式設定] 頁面上取得應用程式識別碼。

    重要

    完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

  3. 在您建立檔案所在的相同目錄中使用命令提示字元,輸入下列命令以執行檔案:

    python model.py
    
  4. 檢閱撰寫回應:

    Add the list of utterances:
    [{'value': {'ExampleId': 1137150691, 'UtteranceText': 'order a pizza'}, 'hasError': False}, {'value': {'ExampleId': 1137150692, 'UtteranceText': 'order a large pepperoni pizza'}, 'hasError': False}, {'value': {'ExampleId': 1137150693, 'UtteranceText': 'i want two large pepperoni pizzas on thin crust'}, 'hasError': False}]
    Request training:
    {'statusId': 9, 'status': 'Queued'}
    Request training status:
    [{'modelId': 'edb46abf-0000-41ab-beb2-a41a0fe1630f', 'details': {'statusId': 3, 'status': 'InProgress', 'exampleCount': 0, 'progressSubstatus': 'CollectingData'}}, {'modelId': 'a5030be2-616c-4648-bf2f-380fa9417d37', 'details': {'statusId': 3, 'status': 'InProgress', 'exampleCount': 0, 'progressSubstatus': 'CollectingData'}}, {'modelId': '3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9', 'details': {'statusId': 3, 'status': 'InProgress', 'exampleCount': 0, 'progressSubstatus': 'CollectingData'}}, {'modelId': 'e4b6704b-1636-474c-9459-fe9ccbeba51c', 'details': {'statusId': 3, 'status': 'InProgress', 'exampleCount': 0, 'progressSubstatus': 'CollectingData'}}, {'modelId': '031d3777-2a00-4a7a-9323-9a3280a30000', 'details': {'statusId': 3, 'status': 'InProgress', 'exampleCount': 0, 'progressSubstatus': 'CollectingData'}}, {'modelId': '9250e7a1-06eb-4413-9432-ae132ed32583', 'details': {'statusId': 3, 'status': 'InProgress', 'exampleCount': 0, 'progressSubstatus': 'CollectingData'}}]
    

    以下是針對可讀性格式化的輸出:

    Add the list of utterances:
    [
      {
        'value': {
          'ExampleId': 1137150691,
          'UtteranceText': 'order a pizza'
        },
        'hasError': False
      },
      {
        'value': {
          'ExampleId': 1137150692,
          'UtteranceText': 'order a large pepperoni pizza'
        },
        'hasError': False
      },
      {
        'value': {
          'ExampleId': 1137150693,
          'UtteranceText': 'i want two large pepperoni pizzas on thin crust'
        },
        'hasError': False
      }
    ]
    
    Request training:
    {
      'statusId': 9,
      'status': 'Queued'
    }
    
    Request training status:
    [
      {
        'modelId': 'edb46abf-0000-41ab-beb2-a41a0fe1630f',
        'details': {
          'statusId': 3,
          'status': 'InProgress',
          'exampleCount': 0,
          'progressSubstatus': 'CollectingData'
        }
      },
      {
        'modelId': 'a5030be2-616c-4648-bf2f-380fa9417d37',
        'details': {
          'statusId': 3,
          'status': 'InProgress',
          'exampleCount': 0,
          'progressSubstatus': 'CollectingData'
        }
      },
      {
        'modelId': '3f2b1f31-a3c3-4fbd-8182-e9d9dbc120b9',
        'details': {
          'statusId': 3,
          'status': 'InProgress',
          'exampleCount': 0,
          'progressSubstatus': 'CollectingData'
        }
      },
      {
        'modelId': 'e4b6704b-1636-474c-9459-fe9ccbeba51c',
        'details': {
          'statusId': 3,
          'status': 'InProgress',
          'exampleCount': 0,
          'progressSubstatus': 'CollectingData'
        }
      },
      {
        'modelId': '031d3777-2a00-4a7a-9323-9a3280a30000',
        'details': {
          'statusId': 3,
          'status': 'InProgress',
          'exampleCount': 0,
          'progressSubstatus': 'CollectingData'
        }
      },
      {
        'modelId': '9250e7a1-06eb-4413-9432-ae132ed32583',
        'details': {
          'statusId': 3,
          'status': 'InProgress',
          'exampleCount': 0,
          'progressSubstatus': 'CollectingData'
        }
      }
    ]
    

清除資源

您完成本快速入門時,請從檔案系統中刪除該檔案。

下一步

應用程式的最佳做法