我是这项技术的新手,但是我想在。net应用程序中使用Watson的API会话。我如何在。net中调用沃森云服务?
我认为IBM对'简单'的理解相当遥远。他们的示例应用程序相当晦涩。最重要的是,他们最近烧掉/弃用了旧的API。下面是新的API描述。你需要先获得一些watsone证书。
您应该能够像使用其他RESTful API一样使用v1会话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();
}
}
}
您可以使用REST接口调用任何Watson Cloud服务,如前面的答案所示。只要确保正确格式化JSON有效负载,您需要的所有信息都在Conversation API Reference中。
也就是说,有一个。net SDK,尽管它可能还不成熟。你可以在GitHub上的Watson开发者云上看到所有当前的sdk和实用程序。