Skip to content

Commit

Permalink
CV2-5086 Move Smooch NLU to presto
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Aug 18, 2024
1 parent fea6cea commit 0b96781
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
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
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
37 changes: 30 additions & 7 deletions app/models/concerns/alegre_v2.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'active_support/concern'
class AlegreTimeoutError < StandardError; end
class TemporaryProjectMedia
attr_accessor :team_id, :id, :url, :type
attr_accessor :team_id, :id, :url, :text, :type
def media
media_type_map = {
"claim" => "Claim",
Expand Down Expand Up @@ -55,11 +55,18 @@ def sync_path_for_type(type)
end

def async_path(project_media)
"/similarity/async/#{get_type(project_media)}"
self.async_path_for_type(get_type(project_media))
end

def async_path_for_type(type)
"/similarity/async/#{type}"
end

def delete_path(project_media)
type = get_type(project_media)
self.delete_path_for_type(get_type(project_media))
end

def delete_path_for_type(type)
"/#{type}/similarity/"
end

Expand Down Expand Up @@ -122,6 +129,10 @@ def request(method, path, params, retries=3)
end
end

def request_delete_from_raw(params, type)
request("delete", delete_path_for_type(type), params)
end

def request_delete(data, project_media)
request("delete", delete_path(project_media), data)
end
Expand All @@ -148,18 +159,22 @@ def get_type(project_media)
type
end

def content_hash_for_value(value)
Digest::MD5.hexdigest(value)
end

def content_hash(project_media, field)
if Bot::Alegre::ALL_TEXT_SIMILARITY_FIELDS.include?(field)
Digest::MD5.hexdigest(project_media.send(field))
content_hash_for_value(project_media.send(field))
else
if project_media.is_link?
return Digest::MD5.hexdigest(project_media.media.url)
return content_hash_for_value(project_media.media.url)
elsif project_media.is_a?(TemporaryProjectMedia)
return Rails.cache.read("url_sha:#{project_media.url}")
elsif !project_media.is_text?
return project_media.media.file.filename.split(".").first
else
return Digest::MD5.hexdigest(project_media.send(field).to_s)
return content_hash_for_value(project_media.send(field).to_s)
end
end
end
Expand Down Expand Up @@ -267,6 +282,14 @@ def store_package_text(project_media, field, params)
generic_package_text(project_media, field, params)
end

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

def get_async_raw_params(params, type)
request("post", async_path_for_type(type), params)
end

def get_sync(project_media, field=nil, params={})
request_sync(
store_package(project_media, field, params),
Expand Down Expand Up @@ -537,4 +560,4 @@ def restrict_contexts(project_media, project_media_id_scores)
}]
end
end
end
end

0 comments on commit 0b96781

Please sign in to comment.