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: endpoint to create sources
  • Loading branch information
sojan-official committed Jul 13, 2023
commit dc01127857d7fc7348b9e8a80bfc31ae16c1f570
17 changes: 17 additions & 0 deletions app/controllers/api/v1/accounts/response_sources_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Api::V1::Accounts::ResponseSourcesController < Api::V1::Accounts::BaseController
before_action :current_account
before_action :check_authorization

def create
@response_source = Current.account.response_sources.new(response_source_params)
@response_source.save!
@response_source
end

private

def response_source_params
params.require(:response_source).permit(:name, :source_link, :inbox_id,
response_documents_attributes: [:document_link])
end
end
8 changes: 0 additions & 8 deletions app/jobs/response_source_job.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ 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
1 change: 1 addition & 0 deletions app/models/inbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ 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

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

Expand Down
10 changes: 7 additions & 3 deletions app/models/response_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@
#
# Indexes
#
# index_response_documents_on_account_id_and_document_link (account_id,document_link) UNIQUE
# index_response_documents_on_document (document_type,document_id)
# index_response_documents_on_response_source_id (response_source_id)
# index_response_documents_on_document (document_type,document_id)
# index_response_documents_on_response_source_id (response_source_id)
#
class ResponseDocument < ApplicationRecord
has_many :responses, dependent: :destroy
belongs_to :account
belongs_to :response_source

before_validation :set_account
after_update :handle_content_change

private

def set_account
self.account = response_source.account
end

def handle_content_change
return unless saved_change_to_content? && content_was.nil? && !content.nil?

Expand Down
12 changes: 3 additions & 9 deletions app/models/response_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# inbox_id :bigint not null
# source_model_id :bigint
#
# Indexes
#
# index_response_sources_on_account_id_and_source_link (account_id,source_link) UNIQUE
# index_response_sources_on_source_model (source_model_type,source_model_id)
# index_response_sources_on_source_model (source_model_type,source_model_id)
#
class ResponseSource < ApplicationRecord
enum source_type: { external: 0, kbase: 1, inbox: 2 }
belongs_to :account
has_many :response_documents, dependent: :destroy
has_many :responses, through: :response_documents

after_save :process_response_source

private

def process_response_source
ResponseSourceJob.perform_later(id)
end
accepts_nested_attributes_for :response_documents
end
5 changes: 5 additions & 0 deletions app/policies/response_source_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ResponseSourcePolicy < ApplicationPolicy
def create?
@account_user.administrator?
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
end
end
resources :labels, only: [:index, :show, :create, :update, :destroy]
resources :response_sources, only: [:create]

resources :notifications, only: [:index, :update] do
collection do
Expand Down
6 changes: 3 additions & 3 deletions db/migrate/20230709113830_create_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ def create_response_sources
t.string :source_link
t.references :source_model, polymorphic: true
t.bigint :account_id, null: false
t.bigint :inbox_id, null: false
t.timestamps
end

add_index :response_sources, [:account_id, :source_link], unique: true
end

def create_response_documents
Expand All @@ -30,7 +29,6 @@ def create_response_documents
t.timestamps
end

add_index :response_documents, [:account_id, :document_link], unique: true
add_index :response_documents, :response_source_id
end

Expand All @@ -40,9 +38,11 @@ def create_responses
t.string :question, null: false
t.text :answer, null: false
t.bigint :account_id, null: false
t.vector :embedding, limit: 1536
t.timestamps
end

add_index :responses, :response_document_id
add_index :responses, :embedding, using: :ivfflat, opclass: :vector_l2_ops
end
end
6 changes: 0 additions & 6 deletions db/migrate/20230710155231_add_embeddding_to_responses.rb

This file was deleted.

7 changes: 3 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_07_10_155231) do
ActiveRecord::Schema[7.0].define(version: 2023_07_09_113830) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -823,7 +823,6 @@
t.bigint "account_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id", "document_link"], name: "index_response_documents_on_account_id_and_document_link", unique: true
t.index ["document_type", "document_id"], name: "index_response_documents_on_document"
t.index ["response_source_id"], name: "index_response_documents_on_response_source_id"
end
Expand All @@ -835,9 +834,9 @@
t.string "source_model_type"
t.bigint "source_model_id"
t.bigint "account_id", null: false
t.bigint "inbox_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id", "source_link"], name: "index_response_sources_on_account_id_and_source_link", unique: true
t.index ["source_model_type", "source_model_id"], name: "index_response_sources_on_source_model"
end

Expand All @@ -846,9 +845,9 @@
t.string "question", null: false
t.text "answer", null: false
t.bigint "account_id", null: false
t.vector "embedding", limit: 1536
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.vector "embedding", limit: 1536
t.index ["embedding"], name: "index_responses_on_embedding", using: :ivfflat
t.index ["response_document_id"], name: "index_responses_on_response_document_id"
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'rails_helper'

RSpec.describe 'Response Sources API', type: :request do
let!(:account) { create(:account) }
let!(:inbox) { create(:inbox, account: account) }

describe 'POST /api/v1/accounts/{account.id}/response_sources' do
let(:valid_params) do
{
response_source: {
name: 'Test',
source_link: 'http://test.test',
inbox_id: inbox.id,
response_documents_attributes: [
{ document_link: 'http://test1.test' },
{ document_link: 'http://test2.test' }
]
}
}
end

context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
expect { post "/api/v1/accounts/#{account.id}/response_sources", params: valid_params }.not_to change(Label, :count)

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

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

it 'creates the contact' do
expect do
post "/api/v1/accounts/#{account.id}/response_sources", headers: admin.create_new_auth_token,
params: valid_params
end.to change(ResponseSource, :count).by(1)

expect(ResponseDocument.count).to eq(2)
expect(response).to have_http_status(:success)
end
end
end
end