Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CV2-5086 Move Smooch NLU to presto #1996

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/lib/smooch_nlu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def update_keywords(language, keywords, keyword, operation, doc_id, context)
keywords << keyword
alegre_operation = 'post'
alegre_params = common_alegre_params.merge({ text: keyword, models: ALEGRE_MODELS_AND_THRESHOLDS.keys })
Bot::Alegre.get_sync_raw_params(alegre_params, "text") if alegre_operation && alegre_params
elsif operation == 'remove'
keywords -= [keyword]
alegre_operation = 'delete'
alegre_params = common_alegre_params.merge({ quiet: true })
Bot::Alegre.request_delete_from_raw(alegre_params, "text") if alegre_operation && alegre_params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified further, it seems... you don't need alegre_operation anymore, since the calls were moved to the condition blocks... right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right! Updated.

end
# FIXME: Add error handling and better logging
Bot::Alegre.request(alegre_operation, '/text/similarity/', alegre_params) if alegre_operation && alegre_params
keywords
end

Expand Down Expand Up @@ -91,7 +91,7 @@ def self.alegre_matches_from_message(message, language, context, alegre_result_k
language: language,
}.merge(context)
}
response = Bot::Alegre.request('post', '/text/similarity/search/', params)
response = Bot::Alegre.get_sync_raw_params(params, "text")

# One approach would be to take the option that has the most matches
# Unfortunately this approach is influenced by the number of keywords per option
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/alegre_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,4 @@ def restrict_contexts(project_media, project_media_id_scores)
}]
end
end
end
end
10 changes: 5 additions & 5 deletions test/lib/smooch_nlu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create_team_with_smooch_bot_installed
team = create_team_with_smooch_bot_installed
nlu = SmoochNlu.new(team.slug)
nlu.enable!
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.once
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/similarity/sync/text' }.once
nlu.add_keyword_to_menu_option('en', 'main', 0, 'subscribe')
expected_output = {
'en' => {
Expand All @@ -85,17 +85,17 @@ def create_team_with_smooch_bot_installed
end

test 'should add keyword if it does not exist' do
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.once
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/similarity/sync/text' }.once
team = create_team_with_smooch_bot_installed
SmoochNlu.new(team.slug).add_keyword_to_menu_option('en', 'main', 0, 'subscribe to the newsletter')
end

test 'should not add keyword if it exists' do
team = create_team_with_smooch_bot_installed
nlu = SmoochNlu.new(team.slug)
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.once
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/similarity/sync/text' }.once
nlu.add_keyword_to_menu_option('en', 'main', 0, 'subscribe to the newsletter')
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/text/similarity/' }.never
Bot::Alegre.expects(:request).with{ |x, y, _z| x == 'post' && y == '/similarity/sync/text' }.never
nlu.add_keyword_to_menu_option('en', 'main', 0, 'subscribe to the newsletter')
end

Expand All @@ -114,7 +114,7 @@ def create_team_with_smooch_bot_installed
end

test 'should return a menu option if NLU is enabled' do
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/search/' && z[:text] =~ /newsletter/ }.returns({ 'result' => [
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/text' && z[:text] =~ /newsletter/ }.returns({ 'result' => [
{ '_score' => 0.9, '_source' => { 'context' => { 'menu_option_id' => 'test' } } },
]})
team = create_team_with_smooch_bot_installed
Expand Down
14 changes: 7 additions & 7 deletions test/models/bot/smooch_6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,9 @@ def send_message_outside_24_hours_window(template, pm = nil)

test 'should process menu option using NLU' do
# Mock any call to Alegre like `POST /text/similarity/` with a "text" parameter that contains "want"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/' && z[:text] =~ /want/ }.returns(true)
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/text' && z[:text] =~ /want/ }.returns(true)
# Mock any call to Alegre like `GET /text/similarity/` with a "text" parameter that does not contain "want"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/search/' && (z[:text] =~ /want/).nil? }.returns({ 'result' => [] })
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/text' && (z[:text] =~ /want/).nil? }.returns({ 'result' => [] })

# Enable NLU and add a couple of keywords for the newsletter menu option
nlu = SmoochNlu.new(@team.slug)
Expand All @@ -824,7 +824,7 @@ def send_message_outside_24_hours_window(template, pm = nil)
subscription_option_id = @installation.get_smooch_workflows[0]['smooch_state_main']['smooch_menu_options'][2]['smooch_menu_option_id']

# Mock a call to Alegre like `GET /text/similarity/` with a "text" parameter that contains "want"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/search/' && z[:text] =~ /want/ }.returns({ 'result' => [
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/text' && z[:text] =~ /want/ }.returns({ 'result' => [
{ '_score' => 0.9, '_source' => { 'context' => { 'menu_option_id' => subscription_option_id } } },
{ '_score' => 0.2, '_source' => { 'context' => { 'menu_option_id' => query_option_id } } }
]})
Expand All @@ -838,7 +838,7 @@ def send_message_outside_24_hours_window(template, pm = nil)
assert_state 'main'

# Mock a call to Alegre like `GET /text/similarity/` with a "text" parameter that contains "want"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/search/' && z[:text] =~ /want/ }.returns({ 'result' => [
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/text' && z[:text] =~ /want/ }.returns({ 'result' => [
{ '_score' => 0.96, '_source' => { 'context' => { 'menu_option_id' => subscription_option_id } } },
{ '_score' => 0.91, '_source' => { 'context' => { 'menu_option_id' => query_option_id } } }
]})
Expand Down Expand Up @@ -877,9 +877,9 @@ def send_message_outside_24_hours_window(template, pm = nil)
Sidekiq::Testing.fake! do
WebMock.disable_net_connect! allow: /#{CheckConfig.get('elasticsearch_host')}|#{CheckConfig.get('storage_endpoint')}/
# Mock any call to Alegre like `POST /text/similarity/` with a "text" parameter that contains "who are you"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/' && z[:text] =~ /who are you/ }.returns(true)
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/test' && z[:text] =~ /who are you/ }.returns(true)
# Mock any call to Alegre like `GET /text/similarity/` with a "text" parameter that does not contain "who are you"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/search/' && (z[:text] =~ /who are you/).nil? }.returns({ 'result' => [] })
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/test' && (z[:text] =~ /who are you/).nil? }.returns({ 'result' => [] })

# Enable NLU and add a couple of keywords to a new "About Us" resource
nlu = SmoochNlu.new(@team.slug)
Expand All @@ -889,7 +889,7 @@ def send_message_outside_24_hours_window(template, pm = nil)
r.add_keyword('who are you')

# Mock a call to Alegre like `GET /text/similarity/` with a "text" parameter that contains "who are you"
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/text/similarity/search/' && z[:text] =~ /who are you/ }.returns({ 'result' => [
Bot::Alegre.stubs(:request).with{ |x, y, z| x == 'post' && y == '/similarity/sync/text' && z[:text] =~ /who are you/ }.returns({ 'result' => [
{ '_score' => 0.9, '_source' => { 'context' => { 'resource_id' => 0 } } },
{ '_score' => 0.8, '_source' => { 'context' => { 'resource_id' => r.id } } }
]})
Expand Down
Loading