Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f22e66b869 | ||
|
|
3857ff67b4 |
+38
@@ -0,0 +1,38 @@
|
|||||||
|
class ReplaceCaptainAssistantResponseEmbeddingIndexWithCosine < ActiveRecord::Migration[7.1]
|
||||||
|
disable_ddl_transaction!
|
||||||
|
|
||||||
|
OLD_INDEX_NAME = 'vector_idx_knowledge_entries_embedding'.freeze
|
||||||
|
NEW_INDEX_NAME = 'vector_idx_captain_assistant_responses_embedding_cosine'.freeze
|
||||||
|
MINIMUM_PGVECTOR_VERSION = Gem::Version.new('0.8.0')
|
||||||
|
|
||||||
|
def up
|
||||||
|
ensure_iterative_scans_supported!
|
||||||
|
|
||||||
|
add_index :captain_assistant_responses,
|
||||||
|
:embedding,
|
||||||
|
using: :ivfflat,
|
||||||
|
opclass: :vector_cosine_ops,
|
||||||
|
name: NEW_INDEX_NAME,
|
||||||
|
algorithm: :concurrently
|
||||||
|
remove_index :captain_assistant_responses, name: OLD_INDEX_NAME, algorithm: :concurrently
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
add_index :captain_assistant_responses,
|
||||||
|
:embedding,
|
||||||
|
using: :ivfflat,
|
||||||
|
opclass: :vector_l2_ops,
|
||||||
|
name: OLD_INDEX_NAME,
|
||||||
|
algorithm: :concurrently
|
||||||
|
remove_index :captain_assistant_responses, name: NEW_INDEX_NAME, algorithm: :concurrently
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def ensure_iterative_scans_supported!
|
||||||
|
installed_version = Gem::Version.new(select_value("SELECT extversion FROM pg_extension WHERE extname = 'vector'"))
|
||||||
|
return if installed_version >= MINIMUM_PGVECTOR_VERSION
|
||||||
|
|
||||||
|
raise StandardError, "pgvector #{MINIMUM_PGVECTOR_VERSION} or newer is required for filtered FAQ search"
|
||||||
|
end
|
||||||
|
end
|
||||||
+2
-2
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.1].define(version: 2026_07_13_184351) do
|
ActiveRecord::Schema[7.1].define(version: 2026_07_14_230000) do
|
||||||
# These extensions should be enabled to support this database
|
# These extensions should be enabled to support this database
|
||||||
enable_extension "pg_stat_statements"
|
enable_extension "pg_stat_statements"
|
||||||
enable_extension "pg_trgm"
|
enable_extension "pg_trgm"
|
||||||
@@ -361,7 +361,7 @@ ActiveRecord::Schema[7.1].define(version: 2026_07_13_184351) do
|
|||||||
t.index ["account_id"], name: "index_captain_assistant_responses_on_account_id"
|
t.index ["account_id"], name: "index_captain_assistant_responses_on_account_id"
|
||||||
t.index ["assistant_id"], name: "index_captain_assistant_responses_on_assistant_id"
|
t.index ["assistant_id"], name: "index_captain_assistant_responses_on_assistant_id"
|
||||||
t.index ["documentable_id", "documentable_type"], name: "idx_cap_asst_resp_on_documentable"
|
t.index ["documentable_id", "documentable_type"], name: "idx_cap_asst_resp_on_documentable"
|
||||||
t.index ["embedding"], name: "vector_idx_knowledge_entries_embedding", using: :ivfflat
|
t.index ["embedding"], name: "vector_idx_captain_assistant_responses_embedding_cosine", opclass: :vector_cosine_ops, using: :ivfflat
|
||||||
t.index ["status"], name: "index_captain_assistant_responses_on_status"
|
t.index ["status"], name: "index_captain_assistant_responses_on_status"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -17,11 +17,11 @@
|
|||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# idx_cap_asst_resp_on_documentable (documentable_id,documentable_type)
|
# idx_cap_asst_resp_on_documentable (documentable_id,documentable_type)
|
||||||
# index_captain_assistant_responses_on_account_id (account_id)
|
# index_captain_assistant_responses_on_account_id (account_id)
|
||||||
# index_captain_assistant_responses_on_assistant_id (assistant_id)
|
# index_captain_assistant_responses_on_assistant_id (assistant_id)
|
||||||
# index_captain_assistant_responses_on_status (status)
|
# index_captain_assistant_responses_on_status (status)
|
||||||
# vector_idx_knowledge_entries_embedding (embedding) USING ivfflat
|
# vector_idx_captain_assistant_responses_embedding_cosine (embedding) USING ivfflat
|
||||||
#
|
#
|
||||||
class Captain::AssistantResponse < ApplicationRecord
|
class Captain::AssistantResponse < ApplicationRecord
|
||||||
self.table_name = 'captain_assistant_responses'
|
self.table_name = 'captain_assistant_responses'
|
||||||
@@ -48,7 +48,11 @@ class Captain::AssistantResponse < ApplicationRecord
|
|||||||
|
|
||||||
def self.search(query, account_id: nil)
|
def self.search(query, account_id: nil)
|
||||||
embedding = Captain::Llm::EmbeddingService.new(account_id: account_id).get_embedding(query)
|
embedding = Captain::Llm::EmbeddingService.new(account_id: account_id).get_embedding(query)
|
||||||
nearest_neighbors(:embedding, embedding, distance: 'cosine').limit(5)
|
|
||||||
|
transaction do
|
||||||
|
connection.execute("SET LOCAL ivfflat.iterative_scan = 'relaxed_order'")
|
||||||
|
nearest_neighbors(:embedding, embedding, distance: 'cosine').limit(5).load
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
Reference in New Issue
Block a user