-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from merefield/semantic_search
FEATURE: Semantic Search
- Loading branch information
Showing
23 changed files
with
548 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Job is triggered on a Post destruction. | ||
class ::Jobs::ChatbotPostEmbeddingDeleteJob < Jobs::Base | ||
sidekiq_options retry: false | ||
|
||
def execute(opts) | ||
begin | ||
post_id = opts[:id] | ||
|
||
::DiscourseChatbot.progress_debug_message("101. Deleting a Post Embedding for Post id: #{post_id}") | ||
|
||
::DiscourseChatbot::PostEmbedding.find_by(post_id: post_id).destroy! | ||
rescue => e | ||
Rails.logger.error ("OpenAIBot Post Embedding: There was a problem, but will retry til limit: #{e}") | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# Job is triggered on an update to a Post. | ||
class ::Jobs::ChatbotPostEmbeddingJob < Jobs::Base | ||
sidekiq_options retry: 5, dead: false | ||
|
||
def execute(opts) | ||
begin | ||
post_id = opts[:id] | ||
|
||
::DiscourseChatbot.progress_debug_message("100. Creating/updating a Post Embedding for Post id: #{post_id}") | ||
|
||
post_embedding = ::DiscourseChatbot::PostEmbeddingProcess.new | ||
|
||
post_embedding.upsert_embedding(post_id) | ||
rescue => e | ||
Rails.logger.error ("OpenAIBot Post Embedding: There was a problem, but will retry til limit: #{e}") | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ::DiscourseChatbot::PostEmbedding < ActiveRecord::Base | ||
self.table_name = 'chatbot_post_embeddings' | ||
|
||
validates :post_id, presence: true, uniqueness: true | ||
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
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class EnableEmbeddingExtension < ActiveRecord::Migration[7.0] | ||
def change | ||
begin | ||
enable_extension :embedding | ||
rescue Exception => e | ||
if DB.query_single("SELECT 1 FROM pg_available_extensions WHERE name = 'embedding';").empty? | ||
STDERR.puts "----------------------------DISCOURSE CHATBOT ERROR----------------------------------" | ||
STDERR.puts " Discourse Chatbot now requires the embedding extension on the PostgreSQL database." | ||
STDERR.puts " Run a `./launcher rebuild app` to fix it on a standard install." | ||
STDERR.puts " Alternatively, you can remove Discourse Chatbot to rebuild." | ||
STDERR.puts "----------------------------DISCOURSE CHATBOT ERROR----------------------------------" | ||
end | ||
raise e | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20230820010103_create_chatbot_embeddings_table.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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateChatbotEmbeddingsTable < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :chatbot_embeddings do |t| | ||
t.integer :post_id, null: false, index: { unique: true }, foreign_key: true | ||
t.column :embedding, "real[]", null: false | ||
t.timestamps | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
db/migrate/20230820010105_create_chatbot_embeddings_index.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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateChatbotEmbeddingsIndex < ActiveRecord::Migration[7.0] | ||
def up | ||
execute <<-SQL | ||
CREATE INDEX hnsw_index_on_chatbot_embeddings ON chatbot_embeddings USING hnsw(embedding) | ||
WITH (dims=1536, m=64, efconstruction=64, efsearch=64); | ||
SQL | ||
end | ||
|
||
def down | ||
execute <<-SQL | ||
DROP INDEX hnsw_index_on_chatbot_embeddings; | ||
SQL | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
db/migrate/20230826010101_rename_chatbot_embeddings_table.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,13 @@ | ||
|
||
# frozen_string_literal: true | ||
|
||
class RenameChatbotEmbeddingsTable < ActiveRecord::Migration[7.0] | ||
def change | ||
begin | ||
Migration::SafeMigrate.disable! | ||
rename_table :chatbot_embeddings, :chatbot_post_embeddings | ||
ensure | ||
Migration::SafeMigrate.enable! | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RenameChatbotEmbeddingsIndex < ActiveRecord::Migration[7.0] | ||
def change | ||
rename_index :chatbot_post_embeddings, 'hnsw_index_on_chatbot_embeddings', 'hnsw_index_on_chatbot_post_embeddings' | ||
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
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,31 @@ | ||
# frozen_string_literal: true | ||
require "openai" | ||
|
||
module ::DiscourseChatbot | ||
|
||
class OpenAIBotBase < Bot | ||
def initialize | ||
::OpenAI.configure do |config| | ||
config.access_token = SiteSetting.chatbot_open_ai_token | ||
end | ||
if !SiteSetting.chatbot_open_ai_model_custom_url.blank? | ||
::OpenAI.configure do |config| | ||
config.uri_base = SiteSetting.chatbot_open_ai_model_custom_url | ||
end | ||
end | ||
if SiteSetting.chatbot_open_ai_model_custom_api_type == "azure" | ||
::OpenAI.configure do |config| | ||
config.api_type = :azure | ||
config.api_version = SiteSetting.chatbot_open_ai_model_custom_api_version | ||
end | ||
end | ||
@client = ::OpenAI::Client.new | ||
@model_name = SiteSetting.chatbot_open_ai_model_custom ? SiteSetting.chatbot_open_ai_model_custom_name : SiteSetting.chatbot_open_ai_model | ||
end | ||
|
||
def get_response(prompt) | ||
raise "Overwrite me!" | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.