fix(captain): align FAQ search index with cosine distance
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
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
|
||||
|
||||
def up
|
||||
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
|
||||
end
|
||||
+2
-2
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# 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
|
||||
enable_extension "pg_stat_statements"
|
||||
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 ["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 ["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"
|
||||
end
|
||||
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# idx_cap_asst_resp_on_documentable (documentable_id,documentable_type)
|
||||
# index_captain_assistant_responses_on_account_id (account_id)
|
||||
# index_captain_assistant_responses_on_assistant_id (assistant_id)
|
||||
# index_captain_assistant_responses_on_status (status)
|
||||
# vector_idx_knowledge_entries_embedding (embedding) USING ivfflat
|
||||
# idx_cap_asst_resp_on_documentable (documentable_id,documentable_type)
|
||||
# index_captain_assistant_responses_on_account_id (account_id)
|
||||
# index_captain_assistant_responses_on_assistant_id (assistant_id)
|
||||
# index_captain_assistant_responses_on_status (status)
|
||||
# vector_idx_captain_assistant_responses_embedding_cosine (embedding) USING ivfflat
|
||||
#
|
||||
class Captain::AssistantResponse < ApplicationRecord
|
||||
self.table_name = 'captain_assistant_responses'
|
||||
@@ -48,7 +48,11 @@ class Captain::AssistantResponse < ApplicationRecord
|
||||
|
||||
def self.search(query, account_id: nil)
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user