Skip to content

Commit

Permalink
fix: batch insertions into Milvus vector store (run-llama#12837)
Browse files Browse the repository at this point in the history
  • Loading branch information
gblazq authored and chrisalexiuk-nvidia committed Apr 25, 2024
1 parent 04ae551 commit 9ac8ad5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pymilvus # noqa
from llama_index.core.bridge.pydantic import Field, PrivateAttr
from llama_index.core.schema import BaseNode, TextNode
from llama_index.core.utils import iter_batch
from llama_index.core.vector_stores.types import (
BasePydanticVectorStore,
MetadataFilters,
Expand All @@ -27,6 +28,7 @@

logger = logging.getLogger(__name__)

DEFAULT_BATCH_SIZE = 100
MILVUS_ID_FIELD = "id"


Expand Down Expand Up @@ -118,6 +120,7 @@ class MilvusVectorStore(BasePydanticVectorStore):
output_fields: List[str] = Field(default_factory=list)
index_config: Optional[dict]
search_config: Optional[dict]
batch_size: int = DEFAULT_BATCH_SIZE

_milvusclient: MilvusClient = PrivateAttr()
_collection: Any = PrivateAttr()
Expand All @@ -137,6 +140,7 @@ def __init__(
output_fields: Optional[List[str]] = None,
index_config: Optional[dict] = None,
search_config: Optional[dict] = None,
batch_size: int = DEFAULT_BATCH_SIZE,
**kwargs: Any,
) -> None:
"""Init params."""
Expand All @@ -151,6 +155,7 @@ def __init__(
output_fields=output_fields or [],
index_config=index_config if index_config else {},
search_config=search_config if search_config else {},
batch_size=batch_size,
)

# Select the similarity metric
Expand Down Expand Up @@ -225,7 +230,8 @@ def add(self, nodes: List[BaseNode], **add_kwargs: Any) -> List[str]:
insert_list.append(entry)

# Insert the data into milvus
self._collection.insert(insert_list)
for insert_batch in iter_batch(insert_list, self.batch_size):
self._collection.insert(insert_batch)
if add_kwargs.get("force_flush", False):
self._collection.flush()
self._create_index_if_required()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-vector-stores-milvus"
readme = "README.md"
version = "0.1.9"
version = "0.1.10"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
Expand Down

0 comments on commit 9ac8ad5

Please sign in to comment.