【问题标题】:how to consume IBM watson conversation api from javascript and C#如何从 javascript 和 C# 使用 IBM watson 对话 api
【发布时间】:2017-05-10 06:53:25
【问题描述】:

如何通过 javascript 和 C# 使用 IBM watson 对话 api?

我尝试了以下代码,但它不起作用:

if (string.IsNullOrEmpty(context)) req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}";
                else req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}, \"context\": \"" + context + "\"";
                using (var handler = new HttpClientHandler
                {
                    Credentials = _NetCredential
                })
                using (var client = new HttpClient(handler))
                {
                    var cont = new HttpRequestMessage();
                    cont.Content = new StringContent(req.ToString(), Encoding.UTF8, "application/json");
                    var result = await client.PostAsync(_Server, cont.Content);
                    return await result.Content.ReadAsStringAsync();
                }

【问题讨论】:

标签: javascript c# watson-conversation watson


【解决方案1】:

IBM Watson API 使用 REST 调用,您可以简单地使用任何语言并使用 REST 来调用您想要的 Watson API。

但是,最近,IBM Developers 创建了要在您的代码中使用的包,并且您可以使用 IBM Developers 的库来构建具有此 link 的应用程序。

查看official 示例:

//import librarys
using IBM.WatsonDeveloperCloud.Conversation.v1;
using IBM.WatsonDeveloperCloud.Conversation.v1.Model;
using System;

namespace IBM.WatsonDeveloperCloud.Conversation.Example
{
    public class ConversationServiceExample
    {
        private ConversationService _conversation = new ConversationService();
        private string _workspaceID;
        private string _inputString = "Turn on the winshield wipers";

        //set username, password with Service Crentials
        public ConversationServiceExample(string username, string password, string workspaceID)
        {
            _conversation.SetCredential(username, password);
            _workspaceID = workspaceID; //workspace_id from your conversation created

            Message(); //send message
        }

【讨论】:

    【解决方案2】:

    您可以使用 .NET SDK 库连接到 Watson。 https://github.com/watson-developer-cloud/dotnet-standard-sdk

    但是,如果您想通过 C# 中的 REST API 进行连接。这是文档https://www.ibm.com/watson/developercloud/conversation/api/v1/

    因此,根据文档发送消息的快速肮脏示例将是这样的。

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://gateway.watsonplatform.net/conversation/api/v1/workspaces/25dfa8a0-0263-471b-8980-317e68c30488/message?version=2017-04-21");
    
                var json = "{\"input\": {\"text\": \"Turn on the lights\"}, \"context\": {\"conversation_id\": \"1b7b67c0-90ed-45dc-8508-9488bc483d5b\", \"system\": {\"dialog_stack\":[{\"dialog_node\":\"root\"}], \"dialog_turn_counter\": 1, \"dialog_request_counter\": 1}}}";
    
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                var result = client.PostAsync(url, content).Result;
    

    这个问题讨论了使用 jQuery $.ajax 调用 Watson REST API。 Use IBM watson API with jquery's $.ajax 如果您想使用 jQuery 从 javascript 调用,这可能会有所帮助。

    【讨论】:

      【解决方案3】:

      使用NugetGithub 上提供的Watson Developer Cloud .NET Standard SDK。

      您将需要在 Bluemix 上创建一个 Conversation service 的实例并创建一个 Workspace 以与之交互。

      在您的项目中,您可以使用您的工作区 ID 实例化该实例的服务和消息。

      // create a Language Translator Service instance
      ConversationService _conversation = new ConversationService();
      
      // set the credentials
      _conversation.SetCredential("<username>", "<password>");
      
      //  create message request
      MessageRequest messageRequest = new MessageRequest()
      {
        Input = new InputData()
        {
          Text = "<input-string>"
        }
      };
      
      //  send a message to the conversation instance
      var result = _conversation.Message("<workspace-id>", messageRequest);
      

      【讨论】:

        【解决方案4】:

        我必须将版本 ("2018-02-16") 添加到 ConversationService 构造函数,并通过构造函数传递凭据以使其对我有用(基于@taj 的示例):

        using IBM.WatsonDeveloperCloud.Conversation.v1.Model;
        using IBM.WatsonDeveloperCloud.Conversation.v1;
        
        static void Main(string[] args)
        {
            // create a Language Translator Service instance
            ConversationService _conversation = new ConversationService("<username>", "<password>", "<version>");
        
            //  create message request
            MessageRequest messageRequest = new MessageRequest()
            {
                Input = new InputData()
                {
                    Text = "<input-string>"
                }
            };
        
            //  send a message to the conversation instance
            var result = _conversation.Message("<workspace-id>", messageRequest);
        }
        

        【讨论】:

          猜你喜欢
          • 2017-03-17
          • 2017-03-31
          • 1970-01-01
          • 2019-08-08
          • 2018-06-20
          • 1970-01-01
          • 2016-09-20
          • 1970-01-01
          • 2017-08-23
          相关资源
          最近更新 更多