Class: NluAdapter::Adapters::WatsonAssistant

Inherits:
Object
  • Object
show all
Includes:
IBMWatson, ParseHelper
Defined in:
lib/nlu_adapter/watson_assistant.rb

Overview

IBM Watson Assistant wrapper class

Defined Under Namespace

Classes: Intent

Instance Method Summary collapse

Methods included from ParseHelper

#bulk_parse, #parse_test, #parse_test_report

Constructor Details

#initialize(options = {}) ⇒ WatsonAssistant

Constructor



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#create_intent(intent) ⇒ Intent

TODO:

convert response -> Intent

Given an Intent object, create/update it in Dialogflow

Parameters:

  • intent (Intent)

    Intent object

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Intent

Get an instance of Intent, if it exists else nil

Parameters:

  • name (String)

    name of the intent

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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 = []) ⇒ Intent

Get a new instance of Intent

Parameters:

  • name (String)

    name of the intent

  • utterences (Array) (defaults to: [])

    phrases for training

Returns:



83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Json

Understand a given text

Parameters:

  • text (String)

    a text to parse using the NLU provider

Returns:

  • (Json)

    return the identified intent name



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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