mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 20:18:08 +00:00
feat: Add more options for AI reply suggestions (#7493)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
module Enterprise::Integrations::OpenaiProcessorService
|
module Enterprise::Integrations::OpenaiProcessorService
|
||||||
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion label_suggestion].freeze
|
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion label_suggestion fix_spelling_grammar shorten expand
|
||||||
|
make_friendly make_formal simplify].freeze
|
||||||
CACHEABLE_EVENTS = %w[label_suggestion].freeze
|
CACHEABLE_EVENTS = %w[label_suggestion].freeze
|
||||||
|
|
||||||
def label_suggestion_message
|
def label_suggestion_message
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
class Integrations::Openai::ProcessorService < Integrations::OpenaiBaseService
|
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
|
def reply_suggestion_message
|
||||||
make_api_call(reply_suggestion_body)
|
make_api_call(reply_suggestion_body)
|
||||||
end
|
end
|
||||||
@@ -11,16 +13,68 @@ class Integrations::Openai::ProcessorService < Integrations::OpenaiBaseService
|
|||||||
make_api_call(rephrase_body)
|
make_api_call(rephrase_body)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def rephrase_body
|
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,
|
model: GPT_MODEL,
|
||||||
messages: [
|
messages: [
|
||||||
{ role: 'system',
|
{ role: 'system', content: system_content },
|
||||||
content: "You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone. " \
|
{ role: 'user', content: user_content }
|
||||||
"Reply in the user's language." },
|
|
||||||
{ role: 'user', content: event['data']['content'] }
|
|
||||||
]
|
]
|
||||||
}.to_json
|
}.to_json
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class Integrations::OpenaiBaseService
|
|||||||
API_URL = 'https://api.openai.com/v1/chat/completions'.freeze
|
API_URL = 'https://api.openai.com/v1/chat/completions'.freeze
|
||||||
GPT_MODEL = 'gpt-3.5-turbo'.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
|
CACHEABLE_EVENTS = %w[].freeze
|
||||||
|
|
||||||
pattr_initialize [:hook!, :event!]
|
pattr_initialize [:hook!, :event!]
|
||||||
|
|||||||
@@ -104,5 +104,154 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq('This is a reply from openai.')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when event name is label_suggestion with no labels' do
|
||||||
|
let(:event) { { 'name' => 'label_suggestion', 'data' => { 'conversation_display_id' => conversation.display_id } } }
|
||||||
|
|
||||||
|
it 'returns nil' do
|
||||||
|
result = subject.perform
|
||||||
|
expect(result).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when event name is not one that can be processed' do
|
||||||
|
let(:event) { { 'name' => 'unknown', 'data' => {} } }
|
||||||
|
|
||||||
|
it 'returns nil' 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
|
||||||
|
|
||||||
|
context 'when event name is shorten' do
|
||||||
|
let(:event) { { 'name' => 'shorten', 'data' => { 'content' => 'This is a test' } } }
|
||||||
|
|
||||||
|
it 'returns the shortened text' do
|
||||||
|
request_body = {
|
||||||
|
'model' => 'gpt-3.5-turbo',
|
||||||
|
'messages' => [
|
||||||
|
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please shorten 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
|
||||||
|
|
||||||
|
context 'when event name is expand' do
|
||||||
|
let(:event) { { 'name' => 'expand', 'data' => { 'content' => 'help you' } } }
|
||||||
|
|
||||||
|
it 'returns the expanded text' do
|
||||||
|
request_body = {
|
||||||
|
'model' => 'gpt-3.5-turbo',
|
||||||
|
'messages' => [
|
||||||
|
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please expand 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
|
||||||
|
|
||||||
|
context 'when event name is make_friendly' do
|
||||||
|
let(:event) { { 'name' => 'make_friendly', 'data' => { 'content' => 'This is a test' } } }
|
||||||
|
|
||||||
|
it 'returns the friendly text' do
|
||||||
|
request_body = {
|
||||||
|
'model' => 'gpt-3.5-turbo',
|
||||||
|
'messages' => [
|
||||||
|
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please make the following response more friendly. ' \
|
||||||
|
'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
|
||||||
|
|
||||||
|
context 'when event name is make_formal' do
|
||||||
|
let(:event) { { 'name' => 'make_formal', 'data' => { 'content' => 'This is a test' } } }
|
||||||
|
|
||||||
|
it 'returns the formal text' do
|
||||||
|
request_body = {
|
||||||
|
'model' => 'gpt-3.5-turbo',
|
||||||
|
'messages' => [
|
||||||
|
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please make the following response more formal. ' \
|
||||||
|
'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
|
||||||
|
|
||||||
|
context 'when event name is simplify' do
|
||||||
|
let(:event) { { 'name' => 'simplify', 'data' => { 'content' => 'This is a test' } } }
|
||||||
|
|
||||||
|
it 'returns the simplified text' do
|
||||||
|
request_body = {
|
||||||
|
'model' => 'gpt-3.5-turbo',
|
||||||
|
'messages' => [
|
||||||
|
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please simplify 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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user