From 92fa9c4fdca3bdfad6480bcd98977583536c41a2 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Mon, 24 Apr 2023 23:52:23 +0530 Subject: [PATCH] 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 Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> --- .../accounts/integrations/hooks_controller.rb | 6 +- .../dashboard/api/integrations/openapi.js | 23 +++ .../components/widgets/AIAssistanceButton.vue | 171 ++++++++++++++++++ .../widgets/WootWriter/ReplyBottomPanel.vue | 16 +- .../widgets/conversation/ReplyBox.vue | 2 + .../dashboard/i18n/locale/en/agentMgmt.json | 3 +- .../i18n/locale/en/integrations.json | 17 ++ .../conversation/ConversationAction.vue | 2 +- .../dashboard/store/modules/integrations.js | 4 +- .../FluentIcon/dashboard-icons.json | 1 + app/models/integrations/hook.rb | 11 ++ app/policies/hook_policy.rb | 4 + config/integration/apps.yml | 24 +++ config/locales/en.yml | 3 + config/routes.rb | 6 +- lib/integrations/openai/processor_service.rb | 39 ++++ .../dashboard/images/integrations/openai.png | Bin 0 -> 6630 bytes .../integrations/apps_controller_spec.rb | 51 +++++- .../integrations/hooks_controller_spec.rb | 27 +++ spec/factories/integrations/hooks.rb | 5 + .../openai/processor_service_spec.rb | 51 ++++++ spec/models/integrations/hook_spec.rb | 21 +++ 22 files changed, 480 insertions(+), 7 deletions(-) create mode 100644 app/javascript/dashboard/api/integrations/openapi.js create mode 100644 app/javascript/dashboard/components/widgets/AIAssistanceButton.vue create mode 100644 lib/integrations/openai/processor_service.rb create mode 100644 public/dashboard/images/integrations/openai.png create mode 100644 spec/lib/integrations/openai/processor_service_spec.rb diff --git a/app/controllers/api/v1/accounts/integrations/hooks_controller.rb b/app/controllers/api/v1/accounts/integrations/hooks_controller.rb index dd2af4ef2..d09ea2f00 100644 --- a/app/controllers/api/v1/accounts/integrations/hooks_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/hooks_controller.rb @@ -1,5 +1,5 @@ class Api::V1::Accounts::Integrations::HooksController < Api::V1::Accounts::BaseController - before_action :fetch_hook, only: [:update, :destroy] + before_action :fetch_hook, except: [:create] before_action :check_authorization def create @@ -10,6 +10,10 @@ class Api::V1::Accounts::Integrations::HooksController < Api::V1::Accounts::Base @hook.update!(permitted_params.slice(:status, :settings)) end + def process_event + render json: { message: @hook.process_event(params[:event]) } + end + def destroy @hook.destroy! head :ok diff --git a/app/javascript/dashboard/api/integrations/openapi.js b/app/javascript/dashboard/api/integrations/openapi.js new file mode 100644 index 000000000..88507bf81 --- /dev/null +++ b/app/javascript/dashboard/api/integrations/openapi.js @@ -0,0 +1,23 @@ +/* global axios */ + +import ApiClient from '../ApiClient'; + +class OpenAIAPI extends ApiClient { + constructor() { + super('integrations', { accountScoped: true }); + } + + processEvent({ name = 'rephrase', content, tone, hookId }) { + return axios.post(`${this.url}/hooks/${hookId}/process_event`, { + event: { + name: name, + data: { + tone, + content, + }, + }, + }); + } +} + +export default new OpenAIAPI(); diff --git a/app/javascript/dashboard/components/widgets/AIAssistanceButton.vue b/app/javascript/dashboard/components/widgets/AIAssistanceButton.vue new file mode 100644 index 000000000..7c58f2aeb --- /dev/null +++ b/app/javascript/dashboard/components/widgets/AIAssistanceButton.vue @@ -0,0 +1,171 @@ + + + + diff --git a/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue b/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue index 6922389b0..5f638d6dc 100644 --- a/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue +++ b/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue @@ -91,6 +91,12 @@ v-if="(isAWebWidgetInbox || isAPIInbox) && !isOnPrivateNote" :conversation-id="conversationId" /> +
diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue index accf82565..9278e6b24 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue @@ -120,8 +120,10 @@ :toggle-audio-recorder-play-pause="toggleAudioRecorderPlayPause" :toggle-audio-recorder="toggleAudioRecorder" :toggle-emoji-picker="toggleEmojiPicker" + :message="message" @selectWhatsappTemplate="openWhatsappTemplateModal" @toggle-editor="toggleRichContentEditor" + @replace-text="replaceText" /> diff --git a/app/javascript/dashboard/store/modules/integrations.js b/app/javascript/dashboard/store/modules/integrations.js index c7518547d..7ba6a93af 100644 --- a/app/javascript/dashboard/store/modules/integrations.js +++ b/app/javascript/dashboard/store/modules/integrations.js @@ -17,7 +17,9 @@ const state = { }; const isAValidAppIntegration = integration => { - return ['dialogflow', 'dyte', 'google_translate'].includes(integration.id); + return ['dialogflow', 'dyte', 'google_translate', 'openai'].includes( + integration.id + ); }; export const getters = { getIntegrations($state) { diff --git a/app/javascript/shared/components/FluentIcon/dashboard-icons.json b/app/javascript/shared/components/FluentIcon/dashboard-icons.json index c76fbc33b..d232e586b 100644 --- a/app/javascript/shared/components/FluentIcon/dashboard-icons.json +++ b/app/javascript/shared/components/FluentIcon/dashboard-icons.json @@ -178,6 +178,7 @@ "translate-outline": "M16.953 5.303a1 1 0 0 0-1.906-.606c-.124.389-.236.899-.324 1.344-.565.012-1.12 0-1.652-.038a1 1 0 1 0-.142 1.995c.46.032.934.048 1.416.047a25.649 25.649 0 0 0-.24 1.698c-1.263.716-2.142 1.684-2.636 2.7-.624 1.283-.7 2.857.239 3.883.675.736 1.704.758 2.499.588.322-.068.654-.176.988-.32a1 1 0 0 0 1.746-.93 13.17 13.17 0 0 0-.041-.115 8.404 8.404 0 0 0 2.735-4.06c.286.251.507.55.658.864.284.594.334 1.271.099 1.91-.234.633-.78 1.313-1.84 1.843a1 1 0 0 0 .895 1.789c1.44-.72 2.385-1.758 2.821-2.94a4.436 4.436 0 0 0-.17-3.464 4.752 4.752 0 0 0-2.104-2.165C19.998 9.22 20 9.11 20 9a1 1 0 0 0-1.974-.23 5.984 5.984 0 0 0-1.796.138c.047-.305.102-.626.166-.964a20.142 20.142 0 0 0 2.842-.473 1 1 0 0 0-.476-1.942c-.622.152-1.286.272-1.964.358.048-.208.1-.409.155-.584Zm-3.686 8.015c.166-.34.414-.697.758-1.037.02.348.053.67.098.973.083.56.207 1.048.341 1.477a3.41 3.41 0 0 1-.674.227c-.429.092-.588.019-.614.006l-.004-.001c-.162-.193-.329-.774.095-1.645Zm4.498-2.562a6.362 6.362 0 0 1-1.568 2.73 7.763 7.763 0 0 1-.095-.525 10.294 10.294 0 0 1-.088-1.904c.033-.013.067-.024.1-.036l1.651-.265Zm0 0-1.651.265c.602-.212 1.155-.29 1.651-.265ZM7.536 6.29a6.342 6.342 0 0 0-4.456.331 1 1 0 0 0 .848 1.811 4.342 4.342 0 0 1 3.049-.222c.364.107.568.248.69.37.12.123.203.27.257.454.067.225.087.446.09.69a8.195 8.195 0 0 0-.555-.117c-1.146-.199-2.733-.215-4.262.64-1.271.713-1.796 2.168-1.682 3.448.12 1.326.94 2.679 2.572 3.136 1.48.414 2.913-.045 3.877-.507l.08-.04a1 1 0 0 0 1.96-.281V10.5c0-.053.002-.12.005-.2.012-.417.034-1.16-.168-1.838a3.043 3.043 0 0 0-.755-1.29c-.394-.398-.91-.694-1.547-.881h-.003Zm-.419 5.288c.344.06.647.143.887.222v2.197a7.021 7.021 0 0 1-.905.524c-.792.38-1.682.605-2.473.384-.698-.195-1.06-.742-1.119-1.389-.062-.693.243-1.286.667-1.523.987-.553 2.06-.569 2.943-.415Z", "eye-show-outline": "M12 9.005a4 4 0 1 1 0 8 4 4 0 0 1 0-8Zm0 1.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5ZM12 5.5c4.613 0 8.596 3.15 9.701 7.564a.75.75 0 1 1-1.455.365 8.503 8.503 0 0 0-16.493.004.75.75 0 0 1-1.455-.363A10.003 10.003 0 0 1 12 5.5Z", "eye-hide-outline": "M2.22 2.22a.75.75 0 0 0-.073.976l.073.084 4.034 4.035a9.986 9.986 0 0 0-3.955 5.75.75.75 0 0 0 1.455.364 8.49 8.49 0 0 1 3.58-5.034l1.81 1.81A4 4 0 0 0 14.8 15.86l5.919 5.92a.75.75 0 0 0 1.133-.977l-.073-.084-6.113-6.114.001-.002-1.2-1.198-2.87-2.87h.002L8.719 7.658l.001-.002-1.133-1.13L3.28 2.22a.75.75 0 0 0-1.06 0Zm7.984 9.045 3.535 3.536a2.5 2.5 0 0 1-3.535-3.535ZM12 5.5c-1 0-1.97.148-2.889.425l1.237 1.236a8.503 8.503 0 0 1 9.899 6.272.75.75 0 0 0 1.455-.363A10.003 10.003 0 0 0 12 5.5Zm.195 3.51 3.801 3.8a4.003 4.003 0 0 0-3.801-3.8Z", + "wand-outline": "m13.314 7.565l-.136.126l-10.48 10.488a2.27 2.27 0 0 0 3.211 3.208L16.388 10.9a2.251 2.251 0 0 0-.001-3.182l-.157-.146a2.25 2.25 0 0 0-2.916-.007Zm-.848 2.961l1.088 1.088l-8.706 8.713a.77.77 0 1 1-1.089-1.088l8.707-8.713Zm4.386 4.48L16.75 15a.75.75 0 0 0-.743.648L16 15.75v.75h-.75a.75.75 0 0 0-.743.648l-.007.102c0 .38.282.694.648.743l.102.007H16v.75c0 .38.282.694.648.743l.102.007a.75.75 0 0 0 .743-.648l.007-.102V18h.75a.75.75 0 0 0 .743-.648L19 17.25a.75.75 0 0 0-.648-.743l-.102-.007h-.75v-.75a.75.75 0 0 0-.648-.743L16.75 15l.102.007Zm-1.553-6.254l.027.027a.751.751 0 0 1 0 1.061l-.711.713l-1.089-1.089l.73-.73a.75.75 0 0 1 1.043.018ZM6.852 5.007L6.75 5a.75.75 0 0 0-.743.648L6 5.75v.75h-.75a.75.75 0 0 0-.743.648L4.5 7.25c0 .38.282.693.648.743L5.25 8H6v.75c0 .38.282.693.648.743l.102.007a.75.75 0 0 0 .743-.648L7.5 8.75V8h.75a.75.75 0 0 0 .743-.648L9 7.25a.75.75 0 0 0-.648-.743L8.25 6.5H7.5v-.75a.75.75 0 0 0-.648-.743L6.75 5l.102.007Zm12-2L18.75 3a.75.75 0 0 0-.743.648L18 3.75v.75h-.75a.75.75 0 0 0-.743.648l-.007.102c0 .38.282.693.648.743L17.25 6H18v.75c0 .38.282.693.648.743l.102.007a.75.75 0 0 0 .743-.648l.007-.102V6h.75a.75.75 0 0 0 .743-.648L21 5.25a.75.75 0 0 0-.648-.743L20.25 4.5h-.75v-.75a.75.75 0 0 0-.648-.743L18.75 3l.102.007Z", "grab-handle-outline": [ "M5 4C5 3.44772 5.44772 3 6 3H10C10.5523 3 11 3.44772 11 4V7C11 7.55228 10.5523 8 10 8H6C5.44772 8 5 7.55228 5 7V4Z", "M13 4C13 3.44772 13.4477 3 14 3H18C18.5523 3 19 3.44772 19 4V7C19 7.55228 18.5523 8 18 8H14C13.4477 8 13 7.55228 13 7V4Z", diff --git a/app/models/integrations/hook.rb b/app/models/integrations/hook.rb index 55e64410c..57d052584 100644 --- a/app/models/integrations/hook.rb +++ b/app/models/integrations/hook.rb @@ -25,6 +25,8 @@ class Integrations::Hook < ApplicationRecord validate :validate_settings_json_schema validates :app_id, uniqueness: { scope: [:account_id], unless: -> { app.present? && app.params[:allow_multiple_hooks].present? } } + # TODO: This seems to be only used for slack at the moment + # We can add a validator when storing the integration settings and toggle this in future enum status: { disabled: 0, enabled: 1 } belongs_to :account @@ -45,6 +47,15 @@ class Integrations::Hook < ApplicationRecord update(status: 'disabled') end + def process_event(event) + case app_id + when 'openai' + Integrations::Openai::ProcessorService.new(hook: self, event: event).perform if app_id == 'openai' + else + 'No processor found' + end + end + private def ensure_hook_type diff --git a/app/policies/hook_policy.rb b/app/policies/hook_policy.rb index 3f25cf8e4..c94a71fe4 100644 --- a/app/policies/hook_policy.rb +++ b/app/policies/hook_policy.rb @@ -7,6 +7,10 @@ class HookPolicy < ApplicationPolicy @account_user.administrator? end + def process_event? + true + end + def destroy? @account_user.administrator? end diff --git a/config/integration/apps.yml b/config/integration/apps.yml index d4743a941..5bad57eb6 100644 --- a/config/integration/apps.yml +++ b/config/integration/apps.yml @@ -128,3 +128,27 @@ google_translate: }, ] visible_properties: ['project_id'] +openai: + id: openai + logo: openai.png + i18n_key: openai + action: /openai + hook_type: account + allow_multiple_hooks: false + settings_json_schema: { + "type": "object", + "properties": { + "api_key": { "type": "string" }, + }, + "required": ["api_key"], + "additionalProperties": false, + } + settings_form_schema: [ + { + "label": "API Key", + "type": "text", + "name": "api_key", + "validation": "required", + }, + ] + visible_properties: ['api_key'] diff --git a/config/locales/en.yml b/config/locales/en.yml index 189d04102..0a9026554 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -187,6 +187,9 @@ en: google_translate: name: "Google Translate" description: "Make it easier for agents to translate messages by adding a Google Translate Integration. Google translate helps to identify the language automatically and convert it to the language chosen by the agent/account admin." + openai: + name: "OpenAI" + description: "Integrate powerful AI features into Chatwoot by leveraging the GPT models from OpenAI." public_portal: search: search_placeholder: Search for article by title or body... diff --git a/config/routes.rb b/config/routes.rb index c28dfd41c..590522844 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -177,7 +177,11 @@ Rails.application.routes.draw do resources :webhooks, only: [:index, :create, :update, :destroy] namespace :integrations do resources :apps, only: [:index, :show] - resources :hooks, only: [:create, :update, :destroy] + resources :hooks, only: [:create, :update, :destroy] do + member do + post :process_event + end + end resource :slack, only: [:create, :update, :destroy], controller: 'slack' resource :dyte, controller: 'dyte', only: [] do collection do diff --git a/lib/integrations/openai/processor_service.rb b/lib/integrations/openai/processor_service.rb new file mode 100644 index 000000000..95bb00d84 --- /dev/null +++ b/lib/integrations/openai/processor_service.rb @@ -0,0 +1,39 @@ +class Integrations::Openai::ProcessorService + pattr_initialize [:hook!, :event!] + + def perform + rephrase_message if event['name'] == 'rephrase' + end + + private + + def rephrase_body(tone, message) + { + model: 'gpt-3.5-turbo', + messages: [ + { role: 'system', content: "You are a helpful support agent. Please rephrase the following response to a more #{tone} tone." }, + { role: 'user', content: message } + ] + }.to_json + end + + def rephrase_message + response = make_api_call(rephrase_body(event['data']['tone'], event['data']['content'])) + JSON.parse(response)['choices'].first['message']['content'] + end + + def make_api_call(body) + headers = { + 'Content-Type' => 'application/json', + 'Authorization' => "Bearer #{hook.settings['api_key']}" + } + + response = HTTParty.post( + 'https://api.openai.com/v1/chat/completions', + headers: headers, + body: body + ) + + response.body + end +end diff --git a/public/dashboard/images/integrations/openai.png b/public/dashboard/images/integrations/openai.png new file mode 100644 index 0000000000000000000000000000000000000000..847b476b0c90946d190e224b1e39fa1fd62d07dc GIT binary patch literal 6630 zcmb`MRag|x_wYd)B$RHDl2BqnYU%DSNnz;{SXe^3yE`NV6zOJ3k(5}Ll-Q+9V37u? zKi|9e@^|rG%*@4{XP$Y^nG^FlvD%tSg!nZ0XlQ7JD$4S@PwT(`2JW+`87FDN>1m+` zD;R_I-0i@S51zJYG9Y(rTP79P5B9dYwjV%#9;3FvCz%)(c^Q4*r9*R92ZQ|<$xUTy zem)&gMjB==m?=yZ^h{XH;^jXNJeAy`lIM7{@lx-Bd=DeZ@fOdUdo!Git$xTxJdp5E z`4d!3NAzXvq7M&G! zf9^)^njfFlws;&2T9T2)E%jk{W8#;0%9enQUT2{2Bd5(vw4vN&s*p(8{aWqU2=f1D z^Z%3BkAB5&%$_NIqt*6~pqfYVN0$$79x`=l(-PmeklnV!MQ_BB%RNmEwLOlmrM$x{ zO_4W)k-b2u)Y;bh5eM-Fx;8Xk5n@Adow%wVRQE~w9S1QsvZVjvQ5Q6&Iw7`of zoae3sC&!Tnh~MU!mc0m!9AJbR=uUGZyr|&cQ-VG^$=Y^LOB5?gA?6KCR8`R^8i#8W zors1BELP8DDlH=8{BI_t9^1dwTFidsD|%=%?Y;Z6J-dnHpDYNyEtwrml8o>f4VTZ6 z0*am{Q^GESOyo2$Rk*rm&B-R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Q}jbYEko|Ca{Df8B}yp-=I@i1}Vip<| "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 diff --git a/spec/models/integrations/hook_spec.rb b/spec/models/integrations/hook_spec.rb index 08a08cc4e..874fc4346 100644 --- a/spec/models/integrations/hook_spec.rb +++ b/spec/models/integrations/hook_spec.rb @@ -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