Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Response Bot using GPT and Webpage Sources #7518

Merged
merged 41 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3cbbc3a
poc: Search support articles via OpenAI embeddings
sojan-official May 22, 2023
fe86e78
Merge branch 'develop' into spike/CW-1690-embeddings-for-articles
sojan-official May 29, 2023
3e0a71d
Chore: basic prototype
sojan-official Mar 20, 2023
8fbfe44
chore: GPT bot with article recommendations
sojan-official May 29, 2023
44a24bb
Merge branch 'develop' into spike/CW-1690-embeddings-for-articles
sojan-official May 29, 2023
cdaa40b
chore: minor formatting
sojan-official May 29, 2023
a728bb2
Merge branch 'develop' into spike/CW-1690-embeddings-for-articles
sojan-official Jun 21, 2023
f26649a
Merge branch 'develop' into spike/CW-1690-embeddings-for-articles
sojan-official Jul 5, 2023
c921aec
chore: Response Sources
sojan-official Jul 10, 2023
ce872de
Merge branch 'spike/CW-1690-embeddings-for-articles' into chore/respo…
sojan-official Jul 10, 2023
870d2e6
chore: fi
sojan-official Jul 13, 2023
41a7a25
chore: clean up
sojan-official Jul 13, 2023
a732ec1
chore: clean up
sojan-official Jul 13, 2023
78814eb
Merge branch 'develop' into chore/response-sources-with-embeddings
sojan-official Jul 13, 2023
bf6d69a
chore: fix
sojan-official Jul 13, 2023
dc01127
chore: endpoint to create sources
sojan-official Jul 13, 2023
590cff4
chor: fixes
sojan-official Jul 13, 2023
11a16a8
chore: The bot
sojan-official Jul 13, 2023
6510886
chore: reoder gemfile
sojan-official Jul 13, 2023
cc61674
chore: fix
sojan-official Jul 13, 2023
b7c7b09
chore: APIs for add and remove documents
sojan-official Jul 14, 2023
ada6c9c
Chore: clean up
sojan-official Jul 14, 2023
3729a9f
Merge branch 'develop' into chore/response-sources-with-embeddings
sojan-official Jul 14, 2023
dfa9b27
chore: fixes
sojan-official Jul 17, 2023
fce44b7
Merge branch 'develop' into chore/response-sources-with-embeddings
sojan-official Jul 17, 2023
c459f42
chore: clean up
sojan-official Jul 17, 2023
98e3ae0
chore: clean up
sojan-official Jul 17, 2023
f63feff
chore: refactor
sojan-official Jul 17, 2023
fec1e8a
Merge branch 'develop' into chore/response-sources-with-embeddings
pranavrajs Jul 19, 2023
30a2009
Merge branch 'develop' into chore/response-sources-with-embeddings
pranavrajs Jul 19, 2023
3ca3f9d
chore: add feature lock
sojan-official Jul 20, 2023
4538d35
chore: clean up
sojan-official Jul 20, 2023
1db6b2a
chore: fix specs
sojan-official Jul 20, 2023
e99a8ee
Merge branch 'develop' into chore/response-sources-with-embeddings
sojan-official Jul 21, 2023
4b05257
chore: fix code climate
sojan-official Jul 21, 2023
45a2754
chore: fix specs
sojan-official Jul 21, 2023
455df91
chore: test latest postgres
sojan-official Jul 21, 2023
ede19fe
chore: tests
sojan-official Jul 21, 2023
441da57
chore: update
sojan-official Jul 21, 2023
fe86348
Empty-Commit
sojan-official Jul 21, 2023
acf5b66
chore: clean up
sojan-official Jul 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: clean up
  • Loading branch information
sojan-official committed Jul 17, 2023
commit c459f420163977289837bf01558807a65493c2df
4 changes: 0 additions & 4 deletions app/controllers/api/v1/accounts/inboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ def campaigns
@campaigns = @inbox.campaigns
end

def response_sources
@response_sources = @inbox.response_sources
end

def avatar
@inbox.avatar.attachment.destroy! if @inbox.avatar.attached?
head :ok
Expand Down
5 changes: 1 addition & 4 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ class Account < ApplicationRecord
has_many :notification_settings, dependent: :destroy_async
has_many :notifications, dependent: :destroy_async
has_many :portals, dependent: :destroy_async, class_name: '::Portal'
has_many :response_sources, dependent: :destroy_async
has_many :response_documents, dependent: :destroy_async
has_many :responses, dependent: :destroy_async
has_many :sms_channels, dependent: :destroy_async, class_name: '::Channel::Sms'
has_many :teams, dependent: :destroy_async
has_many :telegram_bots, dependent: :destroy_async
Expand Down Expand Up @@ -154,5 +151,5 @@ def remove_account_sequences
end

Account.prepend_mod_with('Account')
Account.include_mod_with('EnterpriseAccountConcern')
Account.include_mod_with('Concerns::Account')
Account.include_mod_with('Audit::Account')
4 changes: 1 addition & 3 deletions app/models/inbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ class Inbox < ApplicationRecord
has_one :agent_bot, through: :agent_bot_inbox
has_many :webhooks, dependent: :destroy_async
has_many :hooks, dependent: :destroy_async, class_name: 'Integrations::Hook'
has_many :response_sources, dependent: :destroy_async
has_many :response_documents, through: :response_sources
has_many :responses, through: :response_documents

enum sender_name_type: { friendly: 0, professional: 1 }

Expand Down Expand Up @@ -180,3 +177,4 @@ def check_channel_type?

Inbox.prepend_mod_with('Inbox')
Inbox.include_mod_with('Audit::Inbox')
Inbox.include_mod_with('Concerns::Inbox')
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Enterprise::Api::V1::Accounts::InboxesController
def response_sources
@response_sources = @inbox.response_sources
end

def inbox_attributes
super + ee_inbox_attributes
end
Expand Down
10 changes: 10 additions & 0 deletions enterprise/app/models/enterprise/concerns/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Enterprise::Concerns::Account
extend ActiveSupport::Concern

included do
has_many :sla_policies, dependent: :destroy_async
has_many :response_sources, dependent: :destroy_async
has_many :response_documents, dependent: :destroy_async
has_many :responses, dependent: :destroy_async
end
end
9 changes: 9 additions & 0 deletions enterprise/app/models/enterprise/concerns/inbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Enterprise::Concerns::Inbox
extend ActiveSupport::Concern

included do
has_many :response_sources, dependent: :destroy_async
has_many :response_documents, through: :response_sources
has_many :responses, through: :response_documents
end
end

This file was deleted.

File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions enterprise/app/services/features/response_bot_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def drop_tables
private

def create_response_sources_table
return if MIGRATION_VERSION.table_exists?(:response_sources)

MIGRATION_VERSION.create_table :response_sources do |t|
t.integer :source_type, null: false, default: 0
t.string :name, null: false
Expand All @@ -48,6 +50,8 @@ def create_response_sources_table
end

def create_response_documents_table
return if MIGRATION_VERSION.table_exists?(:response_documents)

MIGRATION_VERSION.create_table :response_documents do |t|
t.bigint :response_source_id, null: false
t.string :document_link
Expand All @@ -61,6 +65,8 @@ def create_response_documents_table
end

def create_responses_table
return if MIGRATION_VERSION.table_exists?(:responses)

MIGRATION_VERSION.create_table :responses do |t|
t.bigint :response_document_id
t.string :question, null: false
Expand Down
37 changes: 0 additions & 37 deletions spec/controllers/api/v1/accounts/inboxes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,43 +234,6 @@
end
end

describe 'GET /api/v1/accounts/{account.id}/inboxes/{inbox.id}/response_sources' do
let(:inbox) { create(:inbox, account: account) }
let!(:response_source) { create(:response_source, account: account, inbox: inbox) }

context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources"

expect(response).to have_http_status(:unauthorized)
end
end

context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:administrator) { create(:user, account: account, role: :administrator) }

it 'returns unauthorized for agents' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources",
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:unauthorized)
end

it 'returns all response_sources belonging to the inbox to administrators' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources",
headers: administrator.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
body = JSON.parse(response.body, symbolize_names: true)
expect(body.first[:id]).to eq(response_source.id)
expect(body.length).to eq(1)
end
end
end

describe 'DELETE /api/v1/accounts/{account.id}/inboxes/{inbox.id}/avatar' do
let(:inbox) { create(:inbox, account: account) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,41 @@
end
end
end

describe 'GET /api/v1/accounts/{account.id}/inboxes/{inbox.id}/response_sources' do
let(:inbox) { create(:inbox, account: account) }
let!(:response_source) { create(:response_source, account: account, inbox: inbox) }

context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources"

expect(response).to have_http_status(:unauthorized)
end
end

context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:administrator) { create(:user, account: account, role: :administrator) }

it 'returns unauthorized for agents' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources",
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:unauthorized)
end

it 'returns all response_sources belonging to the inbox to administrators' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources",
headers: administrator.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
body = JSON.parse(response.body, symbolize_names: true)
expect(body.first[:id]).to eq(response_source.id)
expect(body.length).to eq(1)
end
end
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
WebMock.disable_net_connect!(allow_localhost: true)

RSpec.configure do |config|
config.before(:suite) do
if ChatwootApp.enterprise?
Features::ResponseBotService.new.enable_in_installation
end
end

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
Expand Down