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: APIs for add and remove documents
  • Loading branch information
sojan-official committed Jul 14, 2023
commit b7c7b0978b35fb3c78675cb6d88421e6aa213385
22 changes: 22 additions & 0 deletions app/controllers/api/v1/accounts/response_documents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Api::V1::Accounts::ResponseSourcesController < Api::V1::Accounts::BaseController
before_action :current_account
before_action :check_authorization

def parse
links = PageCrawlerService.new(params[:link]).get_links
render json: { links: links }
end

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
15 changes: 15 additions & 0 deletions app/controllers/api/v1/accounts/response_sources_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Api::V1::Accounts::ResponseSourcesController < Api::V1::Accounts::BaseController
before_action :current_account
before_action :check_authorization
before_action :find_response_source, only: [:add_document, :remove_document]

def parse
links = PageCrawlerService.new(params[:link]).get_links
Expand All @@ -13,8 +14,22 @@ def create
@response_source
end

def add_document
@response_source.response_documents.create!(document_link: params[:document_link])
@response_source
end

def remove_document
@response_source.response_documents.find(params[:document_id]).destroy!
@response_source
end

private

def find_response_source
@response_source = Current.account.response_sources.find(params[:id])
end

def response_source_params
params.require(:response_source).permit(:name, :source_link, :inbox_id,
response_documents_attributes: [:document_link])
Expand Down
1 change: 1 addition & 0 deletions app/models/response_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class ResponseSource < ApplicationRecord
enum source_type: { external: 0, kbase: 1, inbox: 2 }
belongs_to :account
belongs_to :inbox
has_many :response_documents, dependent: :destroy
has_many :responses, through: :response_documents

Expand Down
8 changes: 8 additions & 0 deletions app/policies/response_source_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ def parse?
def create?
@account_user.administrator?
end

def add_document?
@account_user.administrator?
end

def remove_document?
@account_user.administrator?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.id @label.id
json.title @label.title
json.description @label.description
json.color @label.color
json.show_on_sidebar @label.show_on_sidebar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.id @label.id
json.title @label.title
json.description @label.description
json.color @label.color
json.show_on_sidebar @label.show_on_sidebar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.id @label.id
json.title @label.title
json.description @label.description
json.color @label.color
json.show_on_sidebar @label.show_on_sidebar
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
collection do
post :parse
end
member do
post :add_document
post :remove_document
end
end

resources :notifications, only: [:index, :update] do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

describe 'POST /api/v1/accounts/{account.id}/response_sources/parse' do
Expand All @@ -13,15 +14,13 @@

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

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 'returns links in the webpage' do
crawler = double
allow(PageCrawlerService).to receive(:new).and_return(crawler)
Expand Down Expand Up @@ -52,15 +51,13 @@

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 { post "/api/v1/accounts/#{account.id}/response_sources", params: valid_params }.not_to change(ResponseSource, :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 response sources and documents' do
expect do
post "/api/v1/accounts/#{account.id}/response_sources", headers: admin.create_new_auth_token,
Expand All @@ -72,4 +69,63 @@
end
end
end

describe 'POST /api/v1/accounts/{account.id}/response_sources/{response_source.id}/add_document' do
let!(:response_source) { create(:response_source, account: account, inbox: inbox) }
let(:valid_params) do
{ document_link: 'http://test.test' }
end

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

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

context 'when it is an authenticated user' do
it 'creates the response sources and documents' do
expect do
post "/api/v1/accounts/#{account.id}/response_sources/#{response_source.id}/add_document", headers: admin.create_new_auth_token,
params: valid_params
end.to change(ResponseDocument, :count).by(1)
expect(response).to have_http_status(:success)
end
end
end

describe 'POST /api/v1/accounts/{account.id}/response_sources/{response_source.id}/remove_document' do
let!(:response_source) { create(:response_source, account: account, inbox: inbox) }
let!(:response_document) { response_source.response_documents.create!(document_link: 'http://test.test') }
let(:valid_params) do
{ document_id: response_document.id }
end

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

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

context 'when it is an authenticated user' do
it 'creates the response sources and documents' do
expect do
post "/api/v1/accounts/#{account.id}/response_sources/#{response_source.id}/remove_document", headers: admin.create_new_auth_token,
params: valid_params
end.to change(ResponseDocument, :count).by(-1)
expect(response).to have_http_status(:success)

expect { response_document.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/factories/response_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :response_source do
name { Faker::Name.name }
source_link { Faker::Internet.url }
account
end
end