PyMilvus Model Components
PyMilvus Model is a Python library that provides model components, primarily for generating dense and sparse embeddings, intended for use with Milvus. It leverages popular deep learning frameworks like Hugging Face Transformers and Sentence-Transformers to offer a unified interface for various pre-trained models. The current version is 0.3.2, and it typically releases updates as new features or model integrations become available, often in sync with PyMilvus SDK developments.
Common errors
-
OSError: BAAI/bge-small-en-v1.5 does not appear to have a file named pytorch_model.bin or tf_model.h5 or flax_model.msgpack. Make sure 'BAAI/bge-small-en-v1.5' is a valid model identifier listed on 'https://huggingface.co/models'.
cause The specified model name is incorrect, inaccessible, or the network connection to Hugging Face Model Hub is unstable.fixDouble-check the model name for typos. Verify network connectivity. Ensure the model exists and is publicly accessible on Hugging Face. Try a simpler, well-known model first like 'sentence-transformers/all-MiniLM-L6-v2' to rule out network/access issues. -
ModuleNotFoundError: No module named 'torch'
cause The underlying `torch` library, a dependency of `sentence-transformers` (which `pymilvus-model` uses), was not installed correctly or is missing from your environment.fixEnsure `torch` is installed. Run `pip install torch` or, for GPU support, follow specific instructions on `pytorch.org` (e.g., `pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118` for CUDA 11.8). -
RuntimeError: 'cuda' is not available. Make sure you have a CUDA enabled GPU and install the CUDA version of PyTorch.
cause Attempting to use `device='cuda'` without a CUDA-enabled GPU or without PyTorch installed with CUDA support (you might have installed the CPU-only version).fixIf no GPU is available, explicitly use `device='cpu'` when initializing the encoder. If a GPU is available, ensure PyTorch is installed with CUDA support by following the instructions on `pytorch.org/get-started/locally/` carefully for your specific OS and CUDA version.
Warnings
- gotcha Model names provided to `DenseEncoder` or `SparseEncoder` must correspond to models supported by the underlying `sentence-transformers` or `transformers` library, and be accessible on the Hugging Face Model Hub. Misspelled or unsupported model names will lead to loading errors.
- gotcha Using large embedding models or encoding many texts can be computationally intensive. By default, `torch` might use the CPU, leading to slow performance. GPU acceleration (CUDA) is highly recommended for production use cases.
- deprecated The `pymilvus_model` library is relatively new (0.x.x) and its API might evolve in future minor versions. While no specific breaking changes are noted yet, expect potential adjustments in argument names or class structures as it matures towards a 1.0 release.
Install
-
pip install pymilvus-model
Imports
- DenseEncoder
from pymilvus_model.dense.encoder import DenseEncoder
- SparseEncoder
from pymilvus_model.sparse.encoder import SparseEncoder
- MultiModalEncoder
from pymilvus_model.multi_modal.encoder import MultiModalEncoder
Quickstart
from pymilvus_model.dense.encoder import DenseEncoder
# Initialize a DenseEncoder with a common embedding model
# Ensure the model name is valid and accessible (e.g., from Hugging Face Model Hub)
encoder = DenseEncoder(model_name='BAAI/bge-small-en-v1.5', device='cpu') # Change to 'cuda' if GPU available and configured
texts = [
'The quick brown fox jumps over the lazy dog.',
'Artificial intelligence is rapidly advancing.',
'Milvus is an open-source vector database.'
]
# Encode documents to get dense embeddings
embeddings = encoder.encode_documents(texts)
print(f"Encoded {len(texts)} texts.")
print(f"Shape of embeddings: {embeddings.shape}")
print(f"First embedding (truncated): {embeddings[0][:5]}...")