class NluAdapter::Adapters::WatsonAssistant
IBM Watson Assistant wrapper class
Public Class Methods
new(options = {})
click to toggle source
Constructor
# File lib/nlu_adapter/watson_assistant.rb, line 14 def initialize(options = {}) @url = options[:url] || "https://gateway-lon.watsonplatform.net/assistant/api" @version = options[:version] || "2018-09-20" @workspace_id = ENV["WATSON_WORKSPACE_ID"] begin @assistant = AssistantV1.new( iam_apikey: ENV["WATSON_API_KEY"], version: @version, url: @url ) rescue WatsonApiException => ex puts "Error: #{ex.inspect}" end end
Public Instance Methods
create_intent(intent)
click to toggle source
Given an Intent
object, create/update it in Dialogflow
@param intent [Intent] Intent
object @return [Intent] Intent
object
@todo convert response -> Intent
# File lib/nlu_adapter/watson_assistant.rb, line 101 def create_intent(intent) #check: to create / update i = intent.to_h if !intent.id response = @assistant.create_intent( workspace_id: @workspace_id, intent: i[:name], description: i[:description], examples: i[:examples] ) else response = @assistant.update_intent( workspace_id: @workspace_id, intent: intent.id, new_examples: i[:examples], new_description: i[:description], new_intent: i[:name] ) end end
get_intent(name)
click to toggle source
Get an instance of Intent
, if it exists else nil
@param name [String] name of the intent @return [Intent] intent object
# File lib/nlu_adapter/watson_assistant.rb, line 58 def get_intent(name) begin response = @assistant.get_intent( workspace_id: @workspace_id, intent: name, export: true ) return Intent.new({id: response.result['intent'], #for is update check name: response.result['intent'], description: response.result['description'], utterences: response.result['examples'].map { |e| e['text']} }) rescue WatsonApiException => ex puts "Error: #{ex.inspect}" end return nil end
new_intent(name, description, utterences = [])
click to toggle source
Get a new instance of Intent
@param name [String] name of the intent @param utterences [Array] phrases for training @return [Intent] Intent
object
# File lib/nlu_adapter/watson_assistant.rb, line 83 def new_intent(name, description, utterences = []) i = get_intent(name) i = Intent.new if !i i.name = name i.description = description i.utterences = utterences return i end
parse(text)
click to toggle source
Understand a given text
@param text [String] a text to parse using the NLU provider @return [Json] return the identified intent name
# File lib/nlu_adapter/watson_assistant.rb, line 34 def parse(text) intent_name = :NO_INTENT_FOUND begin response = @assistant.message( workspace_id: @workspace_id, input: { text: text } ) unless response.nil? || response.result.nil? || response.result["intents"].nil? || response.result["intents"][0].nil? intent_name = response.result["intents"][0]["intent"] end rescue WatsonApiException => ex puts "Error: #{ex.inspect}" end return { intent_name: intent_name} end