【问题标题】:Example of IBM Watson Conversation API client in c#/.netc#/.net 中的 IBM Watson Conversation API 客户端示例
【发布时间】:2017-03-22 11:30:32
【问题描述】:

我是这项技术的新手,但我想在 .NET 应用程序中使用 Watson 的 API 对话。如何在 .NET 中调用 Watson Cloud 服务?

【问题讨论】:

  • 请改为在西班牙语网站上发布。这个网站只有英文。
  • 支持 preguntarlo aqui.
  • 需要明确的是,这将是一个糟糕的问题,如果用英语提出仍然不清楚或过于宽泛。
  • Stack Overflow 是针对您在使用编程工具时遇到的问题,而不是针对如何使用这些工具的建议。有关在此处提出哪些问题的提示,请参阅stackoverflow.com/help/on-topic。对于代码示例,请查看常见的疑点:GitHub 或供应商的教程和启用developer.ibm.com 或产品文档console.ng.bluemix.net/docs
  • 好的,让我们开始吧... :) 现在听起来好点了吗?

标签: c# rest ibm-cloud ibm-watson watson


【解决方案1】:

我认为 IBM 对“简单”的理解相当遥远。他们的sample apps 相当晦涩。最重要的是,他们最近烧毁/弃用了他们的旧 API。这是new API description。您需要先获得一些 watsone 凭据。

您应该能够像使用任何其他 RESTful API 一样使用 v1 Converstaions API。我喜欢 Flurl 来完成这项任务。

namespace WhatsOn
{
    using System;
    using System.Text;
    using System.Linq;
    using System.Threading.Tasks;
    using Flurl;
    using Flurl.Http;
    using Newtonsoft.Json;

    public class Program
    {
        public static void Main()
        {
            TalkToWatson().Wait();
        }

        public static async Task TalkToWatson()
        {
            var baseurl = "https://gateway.watsonplatform.net/conversation/api";
            var workspace = "25dfa8a0-0263-471b-8980-317e68c30488";
            var username = "...get your own...";
            var password = "...get your own...";
            var context = null as object;
            var input = Console.ReadLine();
            var message = new { input = new { text = input }, context };

            var resp = await baseurl
                .AppendPathSegments("v1", "workspaces", workspace, "message")
                .SetQueryParam("version","2016-11-21")
                .WithBasicAuth(username, password)
                .AllowAnyHttpStatus()
                .PostJsonAsync(message);

            var json = await resp.Content.ReadAsStringAsync();

            var answer = new
            {
                intents = default(object),
                entities = default(object),
                input = default(object),
                output = new
                {
                    text = default(string[])
                },
                context = default(object)
            };

            answer = JsonConvert.DeserializeAnonymousType(json, answer);

            var output = answer?.output?.text?.Aggregate(
                new StringBuilder(),
                (sb,l) => sb.AppendLine(l),
                sb => sb.ToString());

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine($"{resp.StatusCode}: {output}");

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(json);
            Console.ResetColor();
        }        
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 REST 接口调用任何 Watson Cloud 服务,如上一个答案所示。请务必正确格式化 JSON 有效负载,您需要的所有信息都在 Conversation API Reference 中。

    话虽如此,有一个SDK for .NET,尽管它可能还不成熟。您可以在 GitHub 上的 Watson Developer Cloud 上查看所有当前的 SDK 和实用程序。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多