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

@@ -104,5 +104,154 @@ RSpec.describe Integrations::Openai::ProcessorService do
expect(result).to eq('This is a reply from openai.')
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