Skip to content

Commit

Permalink
Added remove_texts on base, elasticsearch, pgvector vectorsearch (#525)
Browse files Browse the repository at this point in the history
* Added remove_texts on base vectorsearch

* Added remove_texts on elasticsearch vectorsearch

* Added remove_texts on pgvector vectorsearch

* Update tests

* Update pgvector_spec.rb

---------

Co-authored-by: Andrei Bondarev <[email protected]>
  • Loading branch information
gregogalante and andreibondarev authored Mar 25, 2024
1 parent e2da91e commit 86b7faf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/langchain/vectorsearch/pgvector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def update_texts(texts:, ids:)
upsert_texts(texts: texts, ids: ids)
end

# Remove a list of texts from the index
# @param ids [Array<Integer>] The ids of the texts to remove from the index
# @return [Integer] The number of texts removed from the index
def remove_texts(ids:)
@db[table_name.to_sym].where(id: ids).delete
end

# Create default schema
def create_default_schema
db.run "CREATE EXTENSION IF NOT EXISTS vector"
Expand Down
30 changes: 30 additions & 0 deletions spec/langchain/vectorsearch/pgvector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@
end
end

describe "#remove_texts" do
before do
allow_any_instance_of(
OpenAI::Client
).to receive(:embeddings)
.with(
parameters: {
dimensions: 1536,
model: "text-embedding-3-small",
input: "Hello World"
}
)
.and_return({
"object" => "list",
"data" => [
{"embedding" => 1536.times.map { rand }}
]
})
end

it "removes texts" do
values = subject.add_texts(texts: ["Hello World", "Hello World"])
ids = values.flatten
expect(ids.length).to eq(2)

result = subject.remove_texts(ids: ids)
expect(result).to eq(2)
end
end

describe "#similarity_search" do
before do
allow_any_instance_of(
Expand Down

0 comments on commit 86b7faf

Please sign in to comment.