feat: Add more options for AI reply suggestions (#7493)

This commit is contained in:
Muhsin Keloth
2023-07-13 10:26:25 +05:30
committed by GitHub
parent 7c080fa9fa
commit 19ff738211
4 changed files with 210 additions and 6 deletions

View File

@@ -1,4 +1,6 @@
class Integrations::Openai::ProcessorService < Integrations::OpenaiBaseService
AGENT_INSTRUCTION = 'You are a helpful support agent.'.freeze
LANGUAGE_INSTRUCTION = 'Reply in the user\'s language.'.freeze
def reply_suggestion_message
make_api_call(reply_suggestion_body)
end
@@ -11,16 +13,68 @@ class Integrations::Openai::ProcessorService < Integrations::OpenaiBaseService
make_api_call(rephrase_body)
end
def fix_spelling_grammar_message
make_api_call(fix_spelling_grammar_body)
end
def shorten_message
make_api_call(shorten_body)
end
def expand_message
make_api_call(expand_body)
end
def make_friendly_message
make_api_call(make_friendly_body)
end
def make_formal_message
make_api_call(make_formal_body)
end
def simplify_message
make_api_call(simplify_body)
end
private
def rephrase_body
build_api_call_body("#{AGENT_INSTRUCTION} Please rephrase the following response to a more #{event['data']['tone']} tone. " \
"#{LANGUAGE_INSTRUCTION}")
end
def fix_spelling_grammar_body
build_api_call_body("#{AGENT_INSTRUCTION} Please fix the spelling and grammar of the following response. " \
"#{LANGUAGE_INSTRUCTION}")
end
def shorten_body
build_api_call_body("#{AGENT_INSTRUCTION} Please shorten the following response. #{LANGUAGE_INSTRUCTION}")
end
def expand_body
build_api_call_body("#{AGENT_INSTRUCTION} Please expand the following response. #{LANGUAGE_INSTRUCTION}")
end
def make_friendly_body
build_api_call_body("#{AGENT_INSTRUCTION} Please make the following response more friendly. #{LANGUAGE_INSTRUCTION}")
end
def make_formal_body
build_api_call_body("#{AGENT_INSTRUCTION} Please make the following response more formal. #{LANGUAGE_INSTRUCTION}")
end
def simplify_body
build_api_call_body("#{AGENT_INSTRUCTION} Please simplify the following response. #{LANGUAGE_INSTRUCTION}")
end
def build_api_call_body(system_content, user_content = event['data']['content'])
{
model: GPT_MODEL,
messages: [
{ role: 'system',
content: "You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone. " \
"Reply in the user's language." },
{ role: 'user', content: event['data']['content'] }
{ role: 'system', content: system_content },
{ role: 'user', content: user_content }
]
}.to_json
end

View File

@@ -6,7 +6,7 @@ class Integrations::OpenaiBaseService
API_URL = 'https://api.openai.com/v1/chat/completions'.freeze
GPT_MODEL = 'gpt-3.5-turbo'.freeze
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion].freeze
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion fix_spelling_grammar shorten expand make_friendly make_formal simplify].freeze
CACHEABLE_EVENTS = %w[].freeze
pattr_initialize [:hook!, :event!]