Milvus¶
The Milvus
class is a vector database implementation that allows you to store, query, and manage vector embeddings. It’s designed to work with various embedding models and provides an efficient way to perform similarity searches.
Configuration¶
To use the Milvus vector database, you need to configure it in your YAML configuration file. Here’s an example configuration:
- name: openai_embed_3_large
db_type: milvus
embedding_model: openai_embed_3_large
collection_name: openai_embed_3_large
uri: ${MILVUS_URI}
token: ${MILVUS_TOKEN}
embedding_batch: 50
similarity_metric: cosine
embedding_model: str
Purpose: Specifies the name or identifier of the embedding model to be used.
Example: “openai_embed_3_large”
Note: This should correspond to a valid embedding model that your system can use to generate vector embeddings. For more information see custom your embedding model documentation.
collection_name: str
Purpose: Sets the name of the Milvus collection where the vectors will be stored.
Example: “my_vector_collection”
Note: If the collection doesn’t exist, it will be created. If it exists, it will be loaded.
embedding_batch: int = 100
Purpose: Determines the number of embeddings to process in a single batch.
Default: 100
Note: Adjust this based on your system’s memory and processing capabilities. Larger batches may be faster but require more memory.
similarity_metric: str = "cosine"
Purpose: Specifies the metric used to calculate similarity between vectors.
Default: “cosine”
Options: “cosine”, “l2” (Euclidean distance), “ip” (Inner Product)
Note: Choose the metric that best suits your use case and data characteristics.
uri: str = "http://localhost:19530"
Purpose: The URI of the Milvus server.
Default: “http://localhost:19530”
Example: “http://milvus-server.com:19530”
Note: Use the appropriate URI for your Milvus server deployment.
db_name: str = ""
Purpose: Specifies the name of the database to use on the Milvus server.
Default: “” (empty string, uses the default database)
Note: Only set this if you’re using multiple databases on your Milvus server.
token: str = ""
Purpose: Authentication token for the Milvus server.
Default: “” (empty string, no token)
Note: Set this if your Milvus server requires token-based authentication.
user: str = ""
Purpose: Username for authentication with the Milvus server.
Default: “” (empty string, no username)
Note: Set this if your Milvus server requires username/password authentication.
password: str = ""
Purpose: Password for authentication with the Milvus server.
Default: “” (empty string, no password)
Note: Set this along with the username if required for authentication.
timeout: Optional[float] = None
Purpose: Specifies the timeout duration (in seconds) for Milvus operations.
Default: None
Example: 30.0 (30 seconds timeout)
Note: Set this to control how long the client should wait for server responses before timing out.
Usage¶
Here’s a brief overview of how to use the main functions of the Milvus vector database:
Adding Vectors:
await milvus_db.add(ids, texts)
This method adds new vectors to the database. It takes a list of IDs and corresponding texts, generates embeddings, and inserts them into the Milvus collection.
Querying:
ids, distances = await milvus_db.query(queries, top_k)
Performs a similarity search on the stored vectors. It returns the IDs of the most similar vectors and their distances.
Fetching Vectors:
vectors = await milvus_db.fetch(ids)
Retrieves the vectors associated with the given IDs.
Checking Existence:
exists = await milvus_db.is_exist(ids)
Checks if the given IDs exist in the database.
Deleting Vectors:
await milvus_db.delete(ids)
Deletes the vectors associated with the given IDs from the database.
Deleting the Collection:
milvus_db.delete_collection()
Deletes the collection from the Milvus server.