From 17d68b833d71e9bd15f700e6e96db6d4c848ddee Mon Sep 17 00:00:00 2001 From: Devin Gaffney Date: Thu, 29 Aug 2024 07:16:20 -0700 Subject: [PATCH] CV2-5080 update request model alegre calls to use presto-based alegre querying --- app/models/request.rb | 4 ++-- test/models/request_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/request.rb b/app/models/request.rb index 81c9e58346..97ea43cc7a 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -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.request('post', '/text/similarity/search/', params)&.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.get_async_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 @@ -194,7 +194,7 @@ def self.send_to_alegre(id) models: request.text_similarity_settings.keys(), context: context } - ::Bot::Alegre.request('post', '/text/similarity/', params) + ::Bot::Alegre.get_async_with_params(params, "text") # elsif ['UploadedImage', 'UploadedAudio', 'UploadedVideo'].include?(media.type) # type = media.type.gsub(/^Uploaded/, '').downcase # url = media.file&.file&.public_url diff --git a/test/models/request_test.rb b/test/models/request_test.rb index 354bd184ec..22f0dec18e 100644 --- a/test/models/request_test.rb +++ b/test/models/request_test.rb @@ -139,7 +139,7 @@ def setup m2 = Media.create! type: 'Claim', quote: 'Foo bar foo bar 2' r2 = create_request media: m2, feed: f response = { 'result' => [{ '_source' => { 'context' => { 'request_id' => r1.id } } }] } - Bot::Alegre.stubs(:request).with('post', '/text/similarity/search/', { text: 'Foo bar foo bar 2', models: [::Bot::Alegre::ELASTICSEARCH_MODEL, ::Bot::Alegre::MEAN_TOKENS_MODEL], per_model_threshold: {::Bot::Alegre::ELASTICSEARCH_MODEL => 0.85, ::Bot::Alegre::MEAN_TOKENS_MODEL => 0.9}, limit: 20, context: { feed_id: f.id } }).returns(response) + Bot::Alegre.stubs(:request).with('post', '/similarity/async/text/', { text: 'Foo bar foo bar 2', models: [::Bot::Alegre::ELASTICSEARCH_MODEL, ::Bot::Alegre::MEAN_TOKENS_MODEL], per_model_threshold: {::Bot::Alegre::ELASTICSEARCH_MODEL => 0.85, ::Bot::Alegre::MEAN_TOKENS_MODEL => 0.9}, limit: 20, context: { feed_id: f.id } }).returns(response) r2.attach_to_similar_request! #Alegre should be called with ES and vector model for request with 4 or more words assert_equal r1, r2.reload.similar_to_request @@ -155,7 +155,7 @@ def setup m2 = Media.create! type: 'Claim', quote: 'Foo bar 2' r2 = create_request media: m2, feed: f response = { 'result' => [{ '_source' => { 'context' => { 'request_id' => r1.id } } }] } - Bot::Alegre.stubs(:request).with('post', '/text/similarity/search/', { text: 'Foo bar 2', models: [::Bot::Alegre::MEAN_TOKENS_MODEL], per_model_threshold: {::Bot::Alegre::MEAN_TOKENS_MODEL => 0.9}, limit: 20, context: { feed_id: f.id } }).returns(response) + Bot::Alegre.stubs(:request).with('post', '/similarity/async/text/', { text: 'Foo bar 2', models: [::Bot::Alegre::MEAN_TOKENS_MODEL], per_model_threshold: {::Bot::Alegre::MEAN_TOKENS_MODEL => 0.9}, limit: 20, context: { feed_id: f.id } }).returns(response) r2.attach_to_similar_request! #Alegre should only be called with vector models for 2 or 3 word request assert_equal r1, r2.reload.similar_to_request