File tree Expand file tree Collapse file tree 3 files changed +24
-0
lines changed
langchainrb_overrides/vectorsearch
langchainrb_rails/active_record
spec/langchainrb_rails/active_record Expand file tree Collapse file tree 3 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,16 @@ def update_texts(texts:, ids:)
5555 add_texts ( texts : texts , ids : ids )
5656 end
5757
58+ # Remove vectors from the index
59+ #
60+ # @param ids [Array<String>] The ids of the vectors to remove
61+ # @return [Boolean] true
62+ def remove_texts ( ids :)
63+ # Since the record is being destroyed and the `embedding` is a column on the record,
64+ # we don't need to do anything here.
65+ true
66+ end
67+
5868 # Invoke a rake task that will create an initializer (`config/initializers/langchain.rb`) file
5969 # and db/migrations/* files
6070 def create_default_schema
Original file line number Diff line number Diff line change @@ -6,12 +6,14 @@ module ActiveRecord
66 # * `vectorsearch` class method to set the vector search provider
77 # * `similarity_search` class method to search for similar texts
88 # * `upsert_to_vectorsearch` instance method to upsert the record to the vector search provider
9+ # * `destroy_from_vectorsearch` instance method to remove the record from the vector search provider
910 #
1011 # Usage:
1112 # class Recipe < ActiveRecord::Base
1213 # vectorsearch
1314 #
1415 # after_save :upsert_to_vectorsearch
16+ # after_destroy :destroy_from_vectorsearch
1517 #
1618 # # Overwriting how the model is serialized before it's indexed
1719 # def as_vector
@@ -56,6 +58,17 @@ def upsert_to_vectorsearch
5658 end
5759 end
5860
61+ # Remove the record from the vector search provider
62+ # This method should be called in an ActiveRecord `after_destroy` callback
63+ #
64+ # @return [Boolean] true
65+ # @raise [Error] Removing from vector search DB failed
66+ def destroy_from_vectorsearch
67+ self . class . class_variable_get ( :@@provider ) . remove_texts (
68+ ids : [ id ]
69+ )
70+ end
71+
5972 # Used to serialize the DB record to an indexable vector text
6073 # Overwrite this method in your model to customize
6174 #
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ class Dummy
77RSpec . describe LangchainrbRails ::ActiveRecord ::Hooks do
88 it "responds to instance methods" do
99 expect ( ::Dummy . new ) . to respond_to ( :upsert_to_vectorsearch )
10+ expect ( ::Dummy . new ) . to respond_to ( :destroy_from_vectorsearch )
1011 expect ( ::Dummy . new ) . to respond_to ( :as_vector )
1112 end
1213
You can’t perform that action at this time.
0 commit comments