Skip to content

Commit

Permalink
Add Milvus#remove_texts() method (#689)
Browse files Browse the repository at this point in the history
* Add Milvus#remove_texts() method

* Bump milvus gem
  • Loading branch information
andreibondarev authored Jul 1, 2024
1 parent a6a13a7 commit 73c1172
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ GEM
net-smtp
matrix (0.4.2)
method_source (1.1.0)
milvus (0.9.2)
milvus (0.9.3)
faraday (>= 2.0.1, < 3)
mini_mime (1.1.5)
mini_portile2 (2.8.6)
Expand Down Expand Up @@ -479,7 +479,7 @@ DEPENDENCIES
langchainrb!
llama_cpp (~> 0.9.4)
mail (~> 2.8)
milvus (~> 0.9.2)
milvus (~> 0.9.3)
mistral-ai
nokogiri (~> 1.13)
open-weather-ruby-client (~> 0.4.0)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ You can instantiate any other supported vector search database:
client = Langchain::Vectorsearch::Chroma.new(...) # `gem "chroma-db", "~> 0.6.0"`
client = Langchain::Vectorsearch::Epsilla.new(...) # `gem "epsilla-ruby", "~> 0.0.3"`
client = Langchain::Vectorsearch::Hnswlib.new(...) # `gem "hnswlib", "~> 0.8.1"`
client = Langchain::Vectorsearch::Milvus.new(...) # `gem "milvus", "~> 0.9.2"`
client = Langchain::Vectorsearch::Milvus.new(...) # `gem "milvus", "~> 0.9.3"`
client = Langchain::Vectorsearch::Pinecone.new(...) # `gem "pinecone", "~> 0.1.6"`
client = Langchain::Vectorsearch::Pgvector.new(...) # `gem "pgvector", "~> 0.2"`
client = Langchain::Vectorsearch::Qdrant.new(...) # `gem "qdrant-ruby", "~> 0.9.3"`
Expand Down
2 changes: 1 addition & 1 deletion langchain.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "google_search_results", "~> 2.0.0"
spec.add_development_dependency "hnswlib", "~> 0.8.1"
spec.add_development_dependency "hugging-face", "~> 0.3.4"
spec.add_development_dependency "milvus", "~> 0.9.2"
spec.add_development_dependency "milvus", "~> 0.9.3"
spec.add_development_dependency "llama_cpp", "~> 0.9.4"
spec.add_development_dependency "nokogiri", "~> 1.13"
spec.add_development_dependency "mail", "~> 2.8"
Expand Down
19 changes: 17 additions & 2 deletions lib/langchain/vectorsearch/milvus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Milvus < Base
# Wrapper around Milvus REST APIs.
#
# Gem requirements:
# gem "milvus", "~> 0.9.2"
# gem "milvus", "~> 0.9.3"
#
# Usage:
# milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, api_key:)
Expand Down Expand Up @@ -39,6 +39,21 @@ def add_texts(texts:)
)
end

# Deletes a list of texts in the index
#
# @param ids [Array<Integer>] The ids of texts to delete
# @return [Boolean] The response from the server
def remove_texts(ids:)
raise ArgumentError, "ids must be an array" unless ids.is_a?(Array)
# Convert ids to integers if strings are passed
ids = ids.map(&:to_i)

client.entities.delete(
collection_name: index_name,
expression: "id in #{ids}"
)
end

# TODO: Add update_texts method

# Create default schema
Expand Down Expand Up @@ -83,7 +98,7 @@ def create_default_schema
# @return [Boolean] The response from the server
def create_default_index
client.indices.create(
collection_name: "Documents",
collection_name: index_name,
field_name: "vectors",
extra_params: [
{key: "metric_type", value: "L2"},
Expand Down
12 changes: 12 additions & 0 deletions spec/langchain/vectorsearch/milvus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@
end
end

describe "remove_texts" do
let(:response) { {"status" => {}, "IDs" => {"IdField" => nil}, "delete_cnt" => 1} }

before do
allow(subject.client).to receive_message_chain(:entities, :delete).and_return(response)
end

it "adds texts" do
expect(subject.remove_texts(ids: [450847466900986695])).to eq(response)
end
end

describe "#similarity_search_by_vector" do
before do
allow(subject.client.collections).to receive(:load).and_return(true)
Expand Down

0 comments on commit 73c1172

Please sign in to comment.