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

Adding GraphQL mutations to add and remove NLU keywords to/from tipline menu options #1707

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions app/graph/mutations/nlu_mutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module NluMutations
class ToggleKeywordInTiplineMenu < Mutations::BaseMutation
argument :language, GraphQL::Types::String, required: true
argument :keyword, GraphQL::Types::String, required: true
argument :menu, GraphQL::Types::String, required: true # "main" or "secondary"
argument :menu_option_index, GraphQL::Types::Int, required: true # zero-based... the order is the same displayed in the tipline and in the tipline settings page

field :success, GraphQL::Types::Boolean, null: true

def resolve(language:, menu:, menu_option_index:, keyword:)
begin
if User.current.is_admin
nlu = SmoochNlu.new(Team.current.slug)
nlu.enable!
if toggle == :add
nlu.add_keyword_to_menu_option(language, menu, menu_option_index, keyword)
elsif toggle == :remove
nlu.remove_keyword_from_menu_option(language, menu, menu_option_index, keyword)
end
{ success: true }
else
{ success: false }
end
rescue
{ success: false }
end
end
end

class AddKeywordToTiplineMenu < ToggleKeywordInTiplineMenu
def toggle
:add
end
end

class RemoveKeywordFromTiplineMenu < ToggleKeywordInTiplineMenu
def toggle
:remove
end
end
end
3 changes: 3 additions & 0 deletions app/graph/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,7 @@ class MutationType < BaseObject
field :destroyTiplineResource, mutation: TiplineResourceMutations::Destroy

field :sendTiplineMessage, mutation: TiplineMessageMutations::Send

field :addNluKeywordToTiplineMenu, mutation: NluMutations::AddKeywordToTiplineMenu
field :removeNluKeywordFromTiplineMenu, mutation: NluMutations::RemoveKeywordFromTiplineMenu
end
2 changes: 1 addition & 1 deletion app/lib/smooch_nlu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SmoochBotNotInstalledError < ::ArgumentError
# FIXME: Make it more flexible
# FIXME: Once we support paraphrase-multilingual-mpnet-base-v2 make it the only model used
ALEGRE_MODELS_AND_THRESHOLDS = {
# Bot::Alegre::ELASTICSEARCH_MODEL => 0.8, Sometimes this is easier for local development
# Bot::Alegre::ELASTICSEARCH_MODEL => 0.8 # , Sometimes this is easier for local development
Bot::Alegre::OPENAI_ADA_MODEL => 0.8,
Bot::Alegre::MEAN_TOKENS_MODEL => 0.6
}
Expand Down
7 changes: 4 additions & 3 deletions app/lib/smooch_nlu_menus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def remove_keyword_from_menu_option(language, menu, menu_option_index, keyword)
update_menu_option_keywords(language, menu, menu_option_index, keyword, 'remove')
end

def list_menu_keywords(languages = nil, menus = nil)
def list_menu_keywords(languages = nil, menus = nil, include_empty = true)
if languages.nil?
languages = @smooch_bot_installation.get_smooch_workflows.map { |w| w['smooch_workflow_language'] }
elsif languages.is_a? String
Expand All @@ -33,12 +33,13 @@ def list_menu_keywords(languages = nil, menus = nil)
output[language][menu] = []
i = 0
workflow.fetch("smooch_state_#{menu}",{}).fetch('smooch_menu_options', []).each do |option|
keywords = option.dig('smooch_menu_option_nlu_keywords').to_a
output[language][menu] << {
'index' => i,
'title' => option.dig('smooch_menu_option_label'),
'keywords' => option.dig('smooch_menu_option_nlu_keywords').to_a,
'keywords' => keywords,
'id' => option.dig('smooch_menu_option_id'),
}
} if include_empty || !keywords.blank?
i += 1
end
end
Expand Down
62 changes: 62 additions & 0 deletions lib/relay.idl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ type AddFilesToTaskPayload {
task: Task
}

"""
Autogenerated input type of AddKeywordToTiplineMenu
"""
input AddKeywordToTiplineMenuInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
keyword: String!
language: String!
menu: String!
menuOptionIndex: Int!
}

"""
Autogenerated return type of AddKeywordToTiplineMenu
"""
type AddKeywordToTiplineMenuPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
success: Boolean
}

type Annotation implements Node {
annotated_id: String
annotated_type: String
Expand Down Expand Up @@ -8911,6 +8936,12 @@ type MutationType {
"""
input: AddFilesToTaskInput!
): AddFilesToTaskPayload
addNluKeywordToTiplineMenu(
"""
Parameters for AddKeywordToTiplineMenu
"""
input: AddKeywordToTiplineMenuInput!
): AddKeywordToTiplineMenuPayload

"""
Allow multiple items to be marked as read or unread.
Expand Down Expand Up @@ -9737,6 +9768,12 @@ type MutationType {
"""
input: RemoveFilesFromTaskInput!
): RemoveFilesFromTaskPayload
removeNluKeywordFromTiplineMenu(
"""
Parameters for RemoveKeywordFromTiplineMenu
"""
input: RemoveKeywordFromTiplineMenuInput!
): RemoveKeywordFromTiplineMenuPayload
replaceProjectMedia(
"""
Parameters for ReplaceProjectMedia
Expand Down Expand Up @@ -11858,6 +11895,31 @@ type RemoveFilesFromTaskPayload {
task: Task
}

"""
Autogenerated input type of RemoveKeywordFromTiplineMenu
"""
input RemoveKeywordFromTiplineMenuInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
keyword: String!
language: String!
menu: String!
menuOptionIndex: Int!
}

"""
Autogenerated return type of RemoveKeywordFromTiplineMenu
"""
type RemoveKeywordFromTiplineMenuPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
success: Boolean
}

"""
Autogenerated input type of ReplaceProjectMedia
"""
Expand Down
Loading
Loading