feat: Ability to improve drafts in the editor using GPT integration (#6957)

ref: https://github.com/chatwoot/chatwoot/issues/6436
fixes: https://linear.app/chatwoot/issue/CW-1552/ability-to-rephrase-text-in-the-editor-using-gpt-integration

---------

Co-authored-by: Sojan <sojan@pepalo.com>
Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
This commit is contained in:
Muhsin Keloth
2023-04-24 23:52:23 +05:30
committed by GitHub
parent f6e0453bb2
commit 92fa9c4fdc
22 changed files with 480 additions and 7 deletions

View File

@@ -28,6 +28,18 @@ RSpec.describe 'Integration Apps API', type: :request do
expect(apps['action']).to be_nil
end
it 'will not return sensitive information for openai app for agents' do
openai = create(:integrations_hook, :openai, account: account)
get api_v1_account_integrations_apps_url(account),
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
app = JSON.parse(response.body)['payload'].find { |int_app| int_app['id'] == openai.app.id }
expect(app['hooks'].first['settings']).to be_nil
end
it 'returns all active apps with sensitive information if user is an admin' do
first_app = Integrations::App.all.find(&:active?)
get api_v1_account_integrations_apps_url(account),
@@ -53,6 +65,18 @@ RSpec.describe 'Integration Apps API', type: :request do
expect(slack_app['action']).to include('client_id=client_id')
end
end
it 'will return sensitive information for openai app for admins' do
openai = create(:integrations_hook, :openai, account: account)
get api_v1_account_integrations_apps_url(account),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
app = JSON.parse(response.body)['payload'].find { |int_app| int_app['id'] == openai.app.id }
expect(app['hooks'].first['settings']).not_to be_nil
end
end
end
@@ -65,7 +89,8 @@ RSpec.describe 'Integration Apps API', type: :request do
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) }
let(:admin) { create(:user, account: account, role: :administrator) }
it 'returns details of the app' do
get api_v1_account_integrations_app_url(account_id: account.id, id: 'slack'),
@@ -77,6 +102,30 @@ RSpec.describe 'Integration Apps API', type: :request do
expect(app['id']).to eql('slack')
expect(app['name']).to eql('Slack')
end
it 'will not return sensitive information for openai app for agents' do
openai = create(:integrations_hook, :openai, account: account)
get api_v1_account_integrations_app_url(account_id: account.id, id: openai.app.id),
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
app = JSON.parse(response.body)
expect(app['hooks'].first['settings']).to be_nil
end
it 'will return sensitive information for openai app for admins' do
openai = create(:integrations_hook, :openai, account: account)
get api_v1_account_integrations_app_url(account_id: account.id, id: openai.app.id),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
app = JSON.parse(response.body)
expect(app['hooks'].first['settings']).not_to be_nil
end
end
end
end

View File

@@ -77,6 +77,33 @@ RSpec.describe 'Integration Hooks API', type: :request do
end
end
describe 'POST /api/v1/accounts/{account.id}/integrations/hooks/{hook_id}/process_event' do
let(:hook) { create(:integrations_hook, account: account) }
let(:params) { { event: 'rephrase', payload: { test: 'test' } } }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post process_event_api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
params: params,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'will process the events' do
post process_event_api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
params: params,
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.parsed_body['message']).to eq('No processor found')
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/integrations/hooks/{hook_id}' do
let(:hook) { create(:integrations_hook, account: account) }

View File

@@ -21,5 +21,10 @@ FactoryBot.define do
app_id { 'google_translate' }
settings { { project_id: 'test', credentials: {} } }
end
trait :openai do
app_id { 'openai' }
settings { { api_key: 'api_key' } }
end
end
end

View File

@@ -0,0 +1,51 @@
require 'rails_helper'
RSpec.describe Integrations::Openai::ProcessorService do
subject { described_class.new(hook: hook, event: event) }
let(:hook) { create(:integrations_hook, :openai) }
let(:expected_headers) { { 'Authorization' => "Bearer #{hook.settings['api_key']}" } }
let(:openai_response) do
{
'choices' => [
{
'message' => {
'content' => 'This is a rephrased test message.'
}
}
]
}.to_json
end
describe '#perform' do
context 'when event name is rephrase' do
let(:event) { { 'name' => 'rephrase', 'data' => { 'tone' => 'friendly', 'content' => 'This is a test message' } } }
it 'returns the rephrased message using the tone in data' do
request_body = {
'model' => 'gpt-3.5-turbo',
'messages' => [
{ 'role' => 'system',
'content' => "You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone." },
{ '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 rephrased test message.')
end
end
context 'when event name is not rephrase' do
let(:event) { { 'name' => 'unknown', 'data' => {} } }
it 'returns nil' do
expect(subject.perform).to be_nil
end
end
end
end

View File

@@ -27,4 +27,25 @@ RSpec.describe Integrations::Hook, type: :model do
end
end
end
describe 'process_event' do
let(:account) { create(:account) }
let(:params) { { event: 'rephrase', payload: { test: 'test' } } }
it 'returns no processor found for hooks with out processor defined' do
hook = create(:integrations_hook, account: account)
expect(hook.process_event(params)).to eq('No processor found')
end
it 'returns results from procesor for openai hook' do
hook = create(:integrations_hook, :openai, account: account)
openai_double = double
allow(Integrations::Openai::ProcessorService).to receive(:new).and_return(openai_double)
allow(openai_double).to receive(:perform).and_return('test')
expect(hook.process_event(params)).to eq('test')
expect(Integrations::Openai::ProcessorService).to have_received(:new).with(event: params, hook: hook)
expect(openai_double).to have_received(:perform)
end
end
end