Skip to content

Commit

Permalink
CV2-5086 second attempt on clean Smooch NLU to Presto branch
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Sep 3, 2024
1 parent ff95711 commit 23d9b0f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 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.get_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.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
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

0 comments on commit 23d9b0f

Please sign in to comment.