以编程方式向 Luis 添加意图

Add Intents to Luis programmatically

我需要使用 c# 代码将 Intents 添加到我的 Luis 应用程序。在 MS 文档中,只有以编程方式添加话语的示例。但是我试着自己做。所以我在浏览器中导航到我的应用程序,然后添加了一个新的 Intent 并检查了网络选项卡并得到了这样的 Post 请求:

乌里: https://westus.api.cognitive.microsoft.com/luis/api/v2.0/apps/App_ID/versions/0.1/intents

这样的请求正文(其中 "testIntent" 是添加的意图的名称):

{
  "name": "testIntent"
}

然后我写了下面的代码:

class Program
{ 
      static void Main(string[] args)
      {
                AddIntent().Wait();
      }

      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);
             }
       }

       async static Task AddIntent()
       {
            string uri = "https://westus.api.cognitive.microsoft.com/luis/api/v2.0/apps/App_ID/versions/0.1/intents";
            string requestBody = File.ReadAllText("json.json");
            var response = await SendPost(uri, requestBody);
            var result = await response.Content.ReadAsStringAsync();
            Console.WriteLine(JsonFormatter.Format(result));
       }
}

json.json 文件如下所示:

[
    {
      "name": "Intent from c# code!"
    }
]

我遇到错误:

{
    "error": {
        "code": "BadArgument",
        "message": "Failed to parse classifier creation object. Parameter name: classifierCreateObject"
    }
}

请在此处查看 LUIS API 文档,了解 Create Intent 方法:https://westus.dev.cognitive.microsoft.com/docs/services/5890b47c39e2bb17b84a55ff/operations/5890b47c39e2bb052c5b9c0c

正文应如下所示:

{
    "name": "BookFlight"
}

在您的例子中,您提供了一个包含 1 个项目的数组。

更正它,它将起作用(见下文):