-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Response Bot using GPT and Webpage Sources (#7518)
This commit introduces the ability to associate response sources to an inbox, allowing external webpages to be parsed by Chatwoot. The parsed data is converted into embeddings for use with GPT models when managing customer queries. The implementation relies on the `pgvector` extension for PostgreSQL. Database migrations related to this feature are handled separately by `Features::ResponseBotService`. A future update will integrate these migrations into the default rails migrations, once compatibility with Postgres extensions across all self-hosted installation options is confirmed. Additionally, a new GitHub action has been added to the CI pipeline to ensure the execution of specs related to this feature.
- Loading branch information
1 parent
30f3928
commit 480f348
Showing
41 changed files
with
976 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# # | ||
# # This workflow will run specs related to response bot | ||
# # This can only be activated in installations Where vector extension is available. | ||
# # | ||
|
||
name: Run Response Bot spec | ||
on: | ||
push: | ||
branches: | ||
- develop | ||
- master | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-20.04 | ||
services: | ||
postgres: | ||
image: ankane/pgvector | ||
env: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: "" | ||
POSTGRES_DB: postgres | ||
POSTGRES_HOST_AUTH_METHOD: trust | ||
ports: | ||
- 5432:5432 | ||
# needed because the postgres container does not provide a healthcheck | ||
# tmpfs makes DB faster by using RAM | ||
options: >- | ||
--mount type=tmpfs,destination=/var/lib/postgresql/data | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
redis: | ||
image: redis | ||
ports: | ||
- 6379:6379 | ||
options: --entrypoint redis-server | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
|
||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
bundler-cache: true # runs 'bundle install' and caches installed gems automatically | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
|
||
- name: yarn | ||
run: yarn install | ||
|
||
- name: Create database | ||
run: bundle exec rake db:create | ||
|
||
- name: Seed database | ||
run: bundle exec rake db:schema:load | ||
|
||
- name: Enable ResponseBotService in installation | ||
run: RAILS_ENV=test bundle exec rails runner "Features::ResponseBotService.new.enable_in_installation" | ||
|
||
# Run Response Bot specs | ||
- name: Run backend tests | ||
run: | | ||
bundle exec rspec spec/enterprise/controllers/api/v1/accounts/response_sources_controller_spec.rb spec/enterprise/controllers/enterprise/api/v1/accounts/inboxes_controller_spec.rb:47 --profile=10 --format documentation | ||
- name: Upload rails log folder | ||
uses: actions/upload-artifact@v3 | ||
if: always() | ||
with: | ||
name: rails-log-folder | ||
path: log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class ResponseSourcePolicy < ApplicationPolicy | ||
def parse? | ||
@account_user.administrator? | ||
end | ||
|
||
def create? | ||
@account_user.administrator? | ||
end | ||
|
||
def add_document? | ||
@account_user.administrator? | ||
end | ||
|
||
def remove_document? | ||
@account_user.administrator? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/views/api/v1/accounts/inboxes/response_sources.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
json.array! @response_sources do |response_source| | ||
json.partial! 'api/v1/models/response_source', formats: [:json], resource: response_source | ||
end |
1 change: 1 addition & 0 deletions
1
app/views/api/v1/accounts/response_sources/add_document.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! 'api/v1/models/response_source', formats: [:json], resource: @response_source |
1 change: 1 addition & 0 deletions
1
app/views/api/v1/accounts/response_sources/create.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! 'api/v1/models/response_source', formats: [:json], resource: @response_source |
1 change: 1 addition & 0 deletions
1
app/views/api/v1/accounts/response_sources/remove_document.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! 'api/v1/models/response_source', formats: [:json], resource: @response_source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
json.id resource.id | ||
json.name resource.name | ||
json.source_link resource.source_link | ||
json.source_type resource.source_type | ||
json.inbox_id resource.inbox_id | ||
json.account_id resource.account_id | ||
json.created_at resource.created_at.to_i | ||
json.updated_at resource.updated_at.to_i | ||
json.response_documents do | ||
json.array! resource.response_documents do |response_document| | ||
json.id response_document.id | ||
json.document_link response_document.document_link | ||
json.created_at response_document.created_at.to_i | ||
json.updated_at response_document.updated_at.to_i | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,5 @@ | |
enabled: false | ||
- name: audit_logs | ||
enabled: false | ||
- name: response_bot | ||
enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# When working with experimental extensions, which doesn't have support on all providers | ||
# This monkey patch will help us to ignore the extensions when dumping the schema | ||
# Additionally we will also ignore the tables associated with those features and exentions | ||
|
||
# Once the feature stabilizes, we can remove the tables/extension from the ignore list | ||
# Ensure you write appropriate migrations when you do that. | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
module PostgreSQL | ||
class SchemaDumper < ConnectionAdapters::SchemaDumper | ||
cattr_accessor :ignore_extentions, default: [] | ||
|
||
private | ||
|
||
def extensions(stream) | ||
extensions = @connection.extensions | ||
return unless extensions.any? | ||
|
||
stream.puts ' # These are extensions that must be enabled in order to support this database' | ||
extensions.sort.each do |extension| | ||
stream.puts " enable_extension #{extension.inspect}" unless ignore_extentions.include?(extension) | ||
end | ||
stream.puts | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
## Extentions / Tables to be ignored | ||
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.ignore_extentions << 'vector' | ||
ActiveRecord::SchemaDumper.ignore_tables << 'responses' | ||
ActiveRecord::SchemaDumper.ignore_tables << 'response_sources' | ||
ActiveRecord::SchemaDumper.ignore_tables << 'response_documents' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
enterprise/app/controllers/api/v1/accounts/response_sources_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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]).page_links | ||
render json: { links: links } | ||
end | ||
|
||
def create | ||
@response_source = Current.account.response_sources.new(response_source_params) | ||
@response_source.save! | ||
end | ||
|
||
def add_document | ||
@response_source.response_documents.create!(document_link: params[:document_link]) | ||
end | ||
|
||
def remove_document | ||
@response_source.response_documents.find(params[:document_id]).destroy! | ||
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]) | ||
end | ||
end |
4 changes: 4 additions & 0 deletions
4
enterprise/app/controllers/enterprise/api/v1/accounts/inboxes_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ResponseBotJob < ApplicationJob | ||
queue_as :medium | ||
|
||
def perform(conversation) | ||
::Enterprise::MessageTemplates::ResponseBotService.new(conversation: conversation).perform | ||
end | ||
end |
Oops, something went wrong.