Skip to content

Commit

Permalink
Cv2 5086 smooch nlu to presto 2 (#2019)
Browse files Browse the repository at this point in the history
* Cv2 5082 article indexing to presto (#1994)

* CV2-5087 move Articles side effecting saves to to it via presto

* CV2-5082 move article indexing to presto

* resolve test errors

* updates for broken tests

* small tweak

* set to sync

* more fixes

* rename function and revert request

* add response suppression and move to specific path for side effecting requests

* extend similar media to allow for temporary texts

* fix broken test fixture

* revert back to async

* fix another test

* fixes per PR review

* fixes per PR review

* more fixes after review

* CV2-5086 second attempt on clean Smooch NLU to Presto branch

* fix broken test stubs

* fix typo brought over from previous PR

* alias and rename per caio review

* fix syntax

* Mayyyyybe its the alias?

* fix old reference

* move another stale function reference

* Replace with alias

* symbolize aliased method names

* Revert to proper function
  • Loading branch information
DGaffney authored Sep 11, 2024
1 parent 14ce78d commit 543a117
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
10 changes: 3 additions & 7 deletions app/lib/smooch_nlu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ def update_keywords(language, keywords, keyword, operation, doc_id, context)
}
if operation == 'add' && !keywords.include?(keyword)
keywords << keyword
alegre_operation = 'post'
alegre_params = common_alegre_params.merge({ text: keyword, models: ALEGRE_MODELS_AND_THRESHOLDS.keys })
Bot::Alegre.index_sync_with_params(common_alegre_params.merge({ text: keyword, models: ALEGRE_MODELS_AND_THRESHOLDS.keys }), "text")
elsif operation == 'remove'
keywords -= [keyword]
alegre_operation = 'delete'
alegre_params = common_alegre_params.merge({ quiet: true })
Bot::Alegre.request_delete_from_raw(common_alegre_params.merge({ quiet: true }), "text")
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 +87,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.query_sync_with_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
12 changes: 10 additions & 2 deletions app/models/concerns/alegre_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,19 @@ def index_async_with_params(params, type, suppress_search_response=true)
request("post", async_path_for_type(type), params.merge(suppress_search_response: suppress_search_response))
end

def get_sync_with_params(params, type)
def index_async_with_params(params, type)
request("post", async_path_for_type(type), params)
end

def index_sync_with_params(params, type)
query_sync_with_params(params, type)
end

def query_sync_with_params(params, type)
request("post", sync_path_for_type(type), params)
end

def get_async_with_params(params, type)
def query_async_with_params(params, type)
request("post", async_path_for_type(type), params)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def self.search_by_similarity(text, language, team_id)
language: language
}
}
response = Bot::Alegre.get_async_with_params(params, "text")
response = Bot::Alegre.query_async_with_params(params, "text")
results = response['result'].to_a.sort_by{ |result| result['_score'] }
explainer_ids = results.collect{ |result| result.dig('_source', 'context', 'explainer_id').to_i }.uniq.first(3)
explainer_ids.empty? ? Explainer.none : Explainer.where(team_id: team_id, id: explainer_ids)
Expand Down
4 changes: 2 additions & 2 deletions app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def attach_to_similar_request!(alegre_limit = 20)
models_thresholds = self.text_similarity_settings.reject{ |_k, v| v['min_words'] > words }
if models_thresholds.count > 0
params = { text: media.quote, models: models_thresholds.keys, per_model_threshold: models_thresholds.transform_values{ |v| v['threshold'] }, limit: alegre_limit, context: context }
similar_request_id = ::Bot::Alegre.get_sync_with_params(params, "text")&.dig('result').to_a.collect{ |result| result&.dig('_source', 'context', 'request_id').to_i }.find{ |id| id != 0 && id < self.id }
similar_request_id = ::Bot::Alegre.query_sync_with_params(params, "text")&.dig('result').to_a.collect{ |result| result&.dig('_source', 'context', 'request_id').to_i }.find{ |id| id != 0 && id < self.id }
end
# elsif ['UploadedImage', 'UploadedAudio', 'UploadedVideo'].include?(media.type)
# threshold = 0.85 #FIXME: Should be feed setting
Expand Down Expand Up @@ -194,7 +194,7 @@ def self.send_to_alegre(id)
models: request.text_similarity_settings.keys(),
context: context
}
::Bot::Alegre.get_async_with_params(params, "text")
::Bot::Alegre.index_async_with_params(params, "text")
# elsif ['UploadedImage', 'UploadedAudio', 'UploadedVideo'].include?(media.type)
# type = media.type.gsub(/^Uploaded/, '').downcase
# url = media.file&.file&.public_url
Expand Down
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/text' && 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/text' && (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

0 comments on commit 543a117

Please sign in to comment.