Milvus2 Module¶
pipelines.pipelines.document_stores.milvus2 ¶
Milvus2DocumentStore ¶
you can now run a query using vector similarity and filter for some meta data at the same time! (See https://milvus.io/docs/v2.0.x/comparison.md for more details)
Usage: 1. Start a Milvus service via docker (see https://milvus.io/docs/v2.0.x/install_standalone-docker.md) 2. Run pip install Paddle-Pipelines 3. Init a MilvusDocumentStore() in Pipelines
Overview: Milvus (https://milvus.io/) is a highly reliable, scalable Document Store specialized on storing and processing vectors. Therefore, it is particularly suited for Pipelines users that work with dense retrieval methods (like DPR).
In contrast to FAISS, Milvus ... - runs as a separate service (e.g. a Docker container) and can scale easily in a distributed environment - allows dynamic data management (i.e. you can insert/delete vectors without recreating the whole index) - encapsulates multiple ANN libraries (FAISS, ANNOY ...)
This class uses Milvus for all vector related storage, processing and querying. The meta-data (e.g. for filtering) and the document text are however stored in a separate SQL Database as Milvus does not allow these data types (yet).
Source code in pipelines/pipelines/document_stores/milvus2.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
__init__ ¶
__init__(sql_url: str = 'sqlite:///milvus_document_store.db', host: str = 'localhost', port: str = '19530', connection_pool: str = 'SingletonThread', index: str = 'document', vector_dim: int = None, embedding_dim: int = 768, index_file_size: int = 1024, similarity: str = 'dot_product', index_type: str = 'IVF_FLAT', index_param: Optional[Dict[str, Any]] = None, search_param: Optional[Dict[str, Any]] = None, return_embedding: bool = False, embedding_field: str = 'embedding', id_field: str = 'id', custom_fields: Optional[List[Any]] = None, progress_bar: bool = True, duplicate_documents: str = 'overwrite', isolation_level: str = None, consistency_level: int = 0, recreate_index: bool = False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql_url |
str
|
SQL connection URL for storing document texts and metadata. It defaults to a local, file based SQLite DB. For large scale deployment, Postgres is recommended. If using MySQL then same server can also be used for Milvus metadata. For more details see https://milvus.io/docs/v1.1.0/data_manage.md. |
'sqlite:///milvus_document_store.db'
|
milvus_url |
Milvus server connection URL for storing and processing vectors. Protocol, host and port will automatically be inferred from the URL. See https://milvus.io/docs/v2.0.x/install_standalone-docker.md for instructions to start a Milvus instance. |
required | |
connection_pool |
str
|
Connection pool type to connect with Milvus server. Default: "SingletonThread". |
'SingletonThread'
|
index |
str
|
Index name for text, embedding and metadata (in Milvus terms, this is the "collection name"). |
'document'
|
vector_dim |
int
|
Deprecated. Use embedding_dim instead. |
None
|
embedding_dim |
int
|
The embedding vector size. Default: 768. |
768
|
index_file_size |
int
|
Specifies the size of each segment file that is stored by Milvus and its default value is 1024 MB. When the size of newly inserted vectors reaches the specified volume, Milvus packs these vectors into a new segment. Milvus creates one index file for each segment. When conducting a vector search, Milvus searches all index files one by one. As a rule of thumb, we would see a 30% ~ 50% increase in the search performance after changing the value of index_file_size from 1024 to 2048. Note that an overly large index_file_size value may cause failure to load a segment into the memory or graphics memory. (From https://milvus.io/docs/v2.0.x/performance_faq.md) |
1024
|
similarity |
str
|
The similarity function used to compare document vectors. 'dot_product' is the default and recommended for DPR embeddings. 'cosine' is recommended for Sentence Transformers, but is not directly supported by Milvus. However, you can normalize your embeddings and use |
'dot_product'
|
index_type |
str
|
Type of approximate nearest neighbour (ANN) index used. The choice here determines your tradeoff between speed and accuracy. Some popular options: - FLAT (default): Exact method, slow - IVF_FLAT, inverted file based heuristic, fast - HSNW: Graph based, fast - ANNOY: Tree based, fast See: https://milvus.io/docs/v2.0.x/index.md |
'IVF_FLAT'
|
index_param |
Optional[Dict[str, Any]]
|
Configuration parameters for the chose index_type needed at indexing time. For example: {"nlist": 16384} as the number of cluster units to create for index_type IVF_FLAT. See https://milvus.io/docs/v2.0.x/index.md |
None
|
search_param |
Optional[Dict[str, Any]]
|
Configuration parameters for the chose index_type needed at query time For example: {"nprobe": 10} as the number of cluster units to query for index_type IVF_FLAT. See https://milvus.io/docs/v2.0.x/index.md |
None
|
return_embedding |
bool
|
To return document embedding. |
False
|
embedding_field |
str
|
Name of field containing an embedding vector. |
'embedding'
|
progress_bar |
bool
|
Whether to show a tqdm progress bar or not. Can be helpful to disable in production deployments to keep the logs clean. |
True
|
duplicate_documents |
str
|
Handle duplicates document based on parameter options. Parameter options : ( 'skip','overwrite','fail') skip: Ignore the duplicates documents overwrite: Update any existing documents with the same ID when adding documents. fail: an error is raised if the document ID of the document being added already exists. |
'overwrite'
|
isolation_level |
str
|
see SQLAlchemy's |
None
|
recreate_index |
bool
|
If set to True, an existing Milvus index will be deleted and a new one will be created using the config you are using for initialization. Be aware that all data in the old index will be lost if you choose to recreate the index. Be aware that both the document_index and the label_index will be recreated. |
False
|
Source code in pipelines/pipelines/document_stores/milvus2.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
delete_documents ¶
delete_documents(index: Optional[str] = None, ids: Optional[List[str]] = None, filters: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, str]] = None, batch_size: int = 10000)
Delete all documents (from SQL AND Milvus).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index |
Optional[str]
|
(SQL) index name for storing the docs and metadata |
None
|
filters |
Optional[Dict[str, Any]]
|
Optional filters to narrow down the search space. Example: {"name": ["some", "more"], "category": ["only_one"]} |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in pipelines/pipelines/document_stores/milvus2.py
delete_index ¶
Delete an existing index. The index including all data will be removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index |
str
|
The name of the index to delete. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in pipelines/pipelines/document_stores/milvus2.py
get_all_documents ¶
get_all_documents(index: Optional[str] = None, filters: Optional[Dict[str, Any]] = None, return_embedding: Optional[bool] = None, batch_size: int = 10000, headers: Optional[Dict[str, str]] = None) -> List[Document]
Get documents from the document store (optionally using filter criteria).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index |
Optional[str]
|
Name of the index to get the documents from. If None, the DocumentStore's default index (self.index) will be used. |
None
|
filters |
Optional[Dict[str, Any]]
|
Optional filters to narrow down the documents to return. Example: {"name": ["some", "more"], "category": ["only_one"]} |
None
|
return_embedding |
Optional[bool]
|
Whether to return the document embeddings. |
None
|
batch_size |
int
|
When working with large number of documents, batching can help reduce memory footprint. |
10000
|
Source code in pipelines/pipelines/document_stores/milvus2.py
get_all_documents_generator ¶
get_all_documents_generator(index: Optional[str] = None, filters: Optional[Dict[str, Any]] = None, return_embedding: Optional[bool] = None, batch_size: int = 10000, headers: Optional[Dict[str, str]] = None) -> Generator[Document, None, None]
Get all documents from the document store. Under-the-hood, documents are fetched in batches from the document store and yielded as individual documents. This method can be used to iteratively process a large number of documents without having to load all documents in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index |
Optional[str]
|
Name of the index to get the documents from. If None, the DocumentStore's default index (self.index) will be used. |
None
|
filters |
Optional[Dict[str, Any]]
|
Optional filters to narrow down the documents to return. Example: {"name": ["some", "more"], "category": ["only_one"]} |
None
|
return_embedding |
Optional[bool]
|
Whether to return the document embeddings. |
None
|
batch_size |
int
|
When working with large number of documents, batching can help reduce memory footprint. |
10000
|
Source code in pipelines/pipelines/document_stores/milvus2.py
get_document_by_id ¶
get_document_by_id(id: str, index: Optional[str] = None, headers: Optional[Dict[str, str]] = None) -> Optional[Document]
Fetch a document by specifying its text id string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id |
str
|
ID of the document |
required |
index |
Optional[str]
|
Name of the index to get the documents from. If None, the DocumentStore's default index (self.index) will be used. |
None
|
Source code in pipelines/pipelines/document_stores/milvus2.py
get_documents_by_id ¶
get_documents_by_id(ids: List[str], index: Optional[str] = None, batch_size: int = 10000, headers: Optional[Dict[str, str]] = None) -> List[Document]
Fetch multiple documents by specifying their IDs (strings)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids |
List[str]
|
List of IDs of the documents |
required |
index |
Optional[str]
|
Name of the index to get the documents from. If None, the DocumentStore's default index (self.index) will be used. |
None
|
batch_size |
int
|
When working with large number of documents, batching can help reduce memory footprint. |
10000
|
Source code in pipelines/pipelines/document_stores/milvus2.py
get_embedding_count ¶
get_embedding_count(index: Optional[str] = None, filters: Optional[Dict[str, List[str]]] = None) -> int
Return the count of embeddings in the document store.
Source code in pipelines/pipelines/document_stores/milvus2.py
query_by_embedding ¶
query_by_embedding(query_emb: np.ndarray, filters: Optional[Dict[str, Any]] = None, top_k: int = 10, index: Optional[str] = None, return_embedding: Optional[bool] = None, headers: Optional[Dict[str, str]] = None, scale_score: bool = True) -> List[Document]
Find the document that is most similar to the provided query_emb by using a vector similarity metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_emb |
ndarray
|
Embedding of the query (e.g. gathered from DPR) |
required |
filters |
Optional[Dict[str, Any]]
|
Optional filters to narrow down the search space. Example: {"name": ["some", "more"], "category": ["only_one"]} |
None
|
top_k |
int
|
How many documents to return |
10
|
index |
Optional[str]
|
(SQL) index name for storing the docs and metadata |
None
|
return_embedding |
Optional[bool]
|
To return document embedding |
None
|
scale_score |
bool
|
Whether to scale the similarity score to the unit interval (range of [0,1]). If true (default) similarity scores (e.g. cosine or dot_product) which naturally have a different value range will be scaled to a range of [0,1], where 1 means extremely relevant. Otherwise raw similarity scores (e.g. cosine or dot_product) will be used. |
True
|
Returns:
| Type | Description |
|---|---|
List[Document]
|
|
Source code in pipelines/pipelines/document_stores/milvus2.py
update_embeddings ¶
update_embeddings(retriever: BaseRetriever, index: Optional[str] = None, batch_size: int = 10000, update_existing_embeddings: bool = True, filters: Optional[Dict[str, Any]] = None)
Updates the embeddings in the document store using the encoding model specified in the retriever. This can be useful if want to add or change the embeddings for your documents (e.g. after changing the retriever config).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retriever |
BaseRetriever
|
Retriever to use to get embeddings for text |
required |
index |
Optional[str]
|
(SQL) index name for storing the docs and metadata |
None
|
batch_size |
int
|
When working with large number of documents, batching can help reduce memory footprint. |
10000
|
update_existing_embeddings |
bool
|
Whether to update existing embeddings of the documents. If set to False, only documents without embeddings are processed. This mode can be used for incremental updating of embeddings, wherein, only newly indexed documents get processed. |
True
|
filters |
Optional[Dict[str, Any]]
|
Optional filters to narrow down the documents for which embeddings are to be updated. Example: {"name": ["some", "more"], "category": ["only_one"]} |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in pipelines/pipelines/document_stores/milvus2.py
write_documents ¶
write_documents(documents: Union[List[dict], List[Document]], index: Optional[str] = None, batch_size: int = 10000, duplicate_documents: Optional[str] = None, headers: Optional[Dict[str, str]] = None, index_param: Optional[Dict[str, Any]] = None)
Add new documents to the DocumentStore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents |
Union[List[dict], List[Document]]
|
List of |
required |
index |
Optional[str]
|
(SQL) index name for storing the docs and metadata |
None
|
batch_size |
int
|
When working with large number of documents, batching can help reduce memory footprint. |
10000
|
duplicate_documents |
Optional[str]
|
Handle duplicates document based on parameter options. Parameter options : ( 'skip','overwrite','fail') skip: Ignore the duplicates documents overwrite: Update any existing documents with the same ID when adding documents. fail: an error is raised if the document ID of the document being added already exists. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Raises:
| Type | Description |
|---|---|
DuplicateDocumentError
|
Exception trigger on duplicate document |
Source code in pipelines/pipelines/document_stores/milvus2.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |