Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Milvus#remove_texts() method #689

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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