feat: add more ai reply options

This commit is contained in:
Muhsin Keloth
2023-07-06 12:05:40 +05:30
parent 92aecb1262
commit b698a332e0
4 changed files with 81 additions and 6 deletions
@@ -1,5 +1,6 @@
module Enterprise::Integrations::OpenaiProcessorService
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion label_suggestion].freeze
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion fix_spelling_grammar shorten expand make_friendly make_formal simplify
label_suggestion].freeze
CACHEABLE_EVENTS = %w[label_suggestion].freeze
def label_suggestion_message
+56 -4
View File
@@ -11,16 +11,68 @@ class Integrations::Openai::ProcessorService < Integrations::OpenaiProcessorServ
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("You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone. " \
'Reply in the user\'s language.')
end
def fix_spelling_grammar_body
build_api_call_body('You are a helpful support agent. Please fix the spelling and grammar of the following response. ' \
'Reply in the user\'s language.')
end
def shorten_body
build_api_call_body("You are a helpful support agent. Please shorten the following response. Reply in the user's language.")
end
def expand_body
build_api_call_body("You are a helpful support agent. Please expand the following response. Reply in the user's language.")
end
def make_friendly_body
build_api_call_body("You are a helpful support agent.Please make the following response more friendly. Reply in the user's language.")
end
def make_formal_body
build_api_call_body("You are a helpful support agent. Please make the following response more formal. Reply in the user's language.")
end
def simplify_body
build_api_call_body("You are a helpful support agent. Please simplify the following response. Reply in the user's language.")
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
+1 -1
View File
@@ -6,7 +6,7 @@ class Integrations::OpenaiProcessorService
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!]
@@ -153,5 +153,27 @@ RSpec.describe Integrations::Openai::ProcessorService do
expect(subject.perform).to be_nil
end
end
context 'when event name is fix_spelling_grammar' do
let(:event) { { 'name' => 'fix_spelling_grammar', 'data' => { 'content' => 'This is a test' } } }
it 'returns the corrected text' do
request_body = {
'model' => 'gpt-3.5-turbo',
'messages' => [
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please fix the spelling and grammar of the following response. ' \
'Reply in the user\'s language.' },
{ 'role' => 'user', 'content' => event['data']['content'] }
]
}.to_json
stub_request(:post, 'https://api.openai.com/v1/chat/completions')
.with(body: request_body, headers: expected_headers)
.to_return(status: 200, body: openai_response, headers: {})
result = subject.perform
expect(result).to eq('This is a reply from openai.')
end
end
end
end