2. RAG Basic Eval Langchain
ReadtheDocs Retrieval Augmented Generation (RAG)
In this notebook, we are going to use Milvus documentation pages to create a chatbot about our product. The chatbot is going to follow RAG steps to retrieve chunks of data using Semantic Vector Search, then the Question + Context will be fed as a Prompt to a LLM to generate an answer.
Many RAG demos use OpenAI for the Embedding Model and ChatGPT for the Generative AI model. In this notebook, we will demo a fully open source RAG stack.
Using open-source Q&A with retrieval saves money since we make free calls to our own data almost all the time - retrieval, evaluation, and development iterations.
Let's get started!
Download Data
The data used in this notebook is Milvus documentation web pages.
The code block below downloads all the web pages into a local directory called rtdocs.
I've already uploaded the rtdocs data folder to github, so you should see it if you cloned my repo.
loaded 22 documents
Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus FREE Search Home v2.4.x About Milvus Get StartedPrerequisitesInstall MilvusInstall SDKsQuickstart Concepts User Guide Models Administration Guide Tools Integrations Example Applications FAQs API reference Quickstart This guide explains how to connect to your Milvus cluster and performs CRUD operations in minutes Before you start You have installed Milvus standalone or Milvus cluster. You have installed preferred SDKs. You c
{'source': 'https://milvus.io/docs/quickstart.md'}
22
{'https://milvus.io/docs/architecture_overview.md',
, 'https://milvus.io/docs/aws.md',
, 'https://milvus.io/docs/configure-docker.md',
, 'https://milvus.io/docs/deploy_s3.md',
, 'https://milvus.io/docs/embeddings.md',
, 'https://milvus.io/docs/get-and-scalar-query.md',
, 'https://milvus.io/docs/glossary.md',
, 'https://milvus.io/docs/gpu_index.md',
, 'https://milvus.io/docs/index-vector-fields.md',
, 'https://milvus.io/docs/index.md',
, 'https://milvus.io/docs/insert-update-delete.md',
, 'https://milvus.io/docs/install_standalone-docker.md',
, 'https://milvus.io/docs/manage-collections.md',
, 'https://milvus.io/docs/metric.md',
, 'https://milvus.io/docs/milvus-cdc-overview.md',
, 'https://milvus.io/docs/monitor_overview.md',
, 'https://milvus.io/docs/multi-vector-search.md',
, 'https://milvus.io/docs/quickstart.md',
, 'https://milvus.io/docs/rbac.md',
, 'https://milvus.io/docs/scaleout.md',
, 'https://milvus.io/docs/single-vector-search.md',
, 'https://milvus.io/docs/system_configuration.md'} Load the Embedding Model checkpoint and use it to create vector embeddings
What are Embeddings?
Check out this blog for an introduction to embeddings.
An excellent place to start is by selecting an embedding model from the HuggingFace MTEB Leaderboard, sorted descending by the "Retrieval Average'' column since this task is most relevant to RAG. Then, choose the smallest, highest-ranking embedding model. But, Beware!! some models listed are overfit to the training data, so they won't perform on your data as promised.
Milvus (and Zilliz) only supports tested embedding models that are not overfit!
Use open source Embedding Model from HuggingFace
Use OpenAI Embedding model
MODEL: text-embedding-3-small, EMBEDDING_DIM: 512
Setup LLM for Answer Generation (G-part of RAG)
Length prompt: 406
/opt/miniconda3/envs/py311-unum/lib/python3.11/site-packages/langchain_core/utils/utils.py:161: UserWarning: WARNING! seed is not default parameter.
seed was transferred to model_kwargs.
Please confirm that seed is what you intended.
warnings.warn(
/opt/miniconda3/envs/py311-unum/lib/python3.11/site-packages/langchain_core/utils/utils.py:161: UserWarning: WARNING! frequency_penalty is not default parameter.
frequency_penalty was transferred to model_kwargs.
Please confirm that frequency_penalty is what you intended.
warnings.warn(
Define 4 questions
Search Milvus using PyMilvus API.
💡 By their nature, vector searches are "semantic" searches. For example, if you were to search for "leaky faucet":
Traditional Key-word Search - either or both words "leaky", "faucet" would have to match some text in order to return a web page or link text to the document.
Semantic search - results containing words "drippy" "taps" would be returned as well because these words mean the same thing even though they are different words.
example query length: 75
Small-to-big Chunking
chunk_size: 512, chunk_overlap: 51.0 22 docs split into 355 child documents.
chunk_size: 1586 22 docs split into 124 parent documents.
124
Test with questions
Question: What is the default AUTOINDEX index and vector field distance metric in Milvus?
Result #1, len: 509
chunk: True, and auto_id is enabled for the primary key. Additionally, a vector field is introduced, configured with a dimensionality of 768, along with the inclusion of four scalar fields, each with its respective attributes. Step 2: Set up index parameters Index parameters dictate how Milvus organizes your data within a collection. You can tailor the indexing process for specific fields by adjusting their metric_type and index_type. For the vector field, you have the flexibility to select COSINE, L2, or IP as
("metadata: {'source': 'https://milvus.io/docs/manage-collections.md', "
"'doc_id': '5a89935b-b42b-4844-9de7-281ad89c4a48', 'pk': 451033268878049347}")
Result #2, len: 506
chunk: walks you through the basic operations on creating and managing indexes on vector fields in a collection. Overview Leveraging the metadata stored in an index file, Milvus organizes your data in a specialized structure, facilitating rapid retrieval of requested information during searches or queries. Milvus provides several index types to sort field values for efficient similarity searches. It also offers three metric types: Cosine Similarity (COSINE), Euclidean Distance (L2), and Inner Product (IP) to
("metadata: {'source': 'https://milvus.io/docs/index-vector-fields.md', "
"'doc_id': 'c774f98e-610c-4be2-ad43-b5970ad9b6c3', 'pk': 451033268878049637}")
sources: 2, ['https://milvus.io/docs/manage-collections.md', 'https://milvus.io/docs/index-vector-fields.md']
/opt/miniconda3/envs/py311-unum/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:139: LangChainDeprecationWarning: The method `BaseRetriever.get_relevant_documents` was deprecated in langchain-core 0.1.46 and will be removed in 0.3.0. Use invoke instead. warn_deprecated(
Num parent results: 4
Result #1, len: 1583
chunk: vector in the data to be inserted, are treated as dynamic fields. These additional fields are saved as key-value pairs within a special field named $meta. This feature allows the inclusion of extra fields during data insertion. The automatically indexed and loaded collection from the provided code is ready for immediate data insertions. Customized setup Instead of letting Milvus decide almost everything for your collection, you can determine the schema and index parameters of the collection on your own. Step 1: Set up schema A schema defines the structure of a collection. Within the schema, you have the option to enable or disable enable_dynamic_field, add pre-defined fields, and set attributes for each field. For a detailed explanation of the concept and available data types, refer to Schema Explained. Python Java Node.js In the provided code snippet for Python, the enable_dynamic_field is set to True, and auto_id is enabled for the primary key. Additionally, a vector field is introduced, configured with a dimensionality of 768, along with the inclusion of four scalar fields, each with its respective attributes. Step 2: Set up index parameters Index parameters dictate how Milvus organizes your data within a collection. You can tailor the indexing process for specific fields by adjusting their metric_type and index_type. For the vector field, you have the flexibility to select COSINE, L2, or IP as the metric_type. Python Java Node.js The code snippet above demonstrates how to set up index parameters for the vector field and a scalar field, respectively. For
"metadata: {'source': 'https://milvus.io/docs/manage-collections.md'}"
Result #2, len: 1582
chunk: Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus FREE Search Home v2.4.x About Milvus Get Started Concepts User GuideManage DatabasesManage SchemaManage CollectionsManage IndexesIndex Vector FieldsIndex Scalar FieldsIndex with GPUManage PartitionsInsert, Upsert & DeleteData ImportSearch, Query & GetAdvanced Features Models Administration Guide Tools Integrations Example Applications FAQs API reference Index Vector Fields This guide walks you through the basic operations on creating and managing indexes on vector fields in a collection. Overview Leveraging the metadata stored in an index file, Milvus organizes your data in a specialized structure, facilitating rapid retrieval of requested information during searches or queries. Milvus provides several index types to sort field values for efficient similarity searches. It also offers three metric types: Cosine Similarity (COSINE), Euclidean Distance (L2), and Inner Product (IP) to measure the distances between vector embeddings. It is recommended to create indexes for both the vector field and scalar fields that are frequently accessed. The code snippets on this page use new MilvusClient (Python) to interact with Milvus. New MilvusClient SDKs for other languages will be released in future updates. Preparations As explained in Manage Collections, Milvus automatically generates an index and loads it into memory when creating a collection if any of the following conditions are specified in the collection creation request: The dimensionality of the vector field and the metric type, or The
"metadata: {'source': 'https://milvus.io/docs/index-vector-fields.md'}"
Result #3, len: 1581
chunk: Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus FREE Search Home v2.4.x About Milvus Get Started ConceptsArchitectureBitsetConsistencyMulti-tenancyTimestampSimilarity MetricsTime SynchronizationVector IndexIn-memory IndexOn-disk IndexGPU IndexScalar IndexRerankingTerminology User Guide Models Administration Guide Tools Integrations Example Applications FAQs API reference In-memory Index This topic lists various types of in-memory indexes Milvus supports, scenarios each of them best suits, and parameters users can configure to achieve better search performance. For on-disk indexes, see On-disk Index. Indexing is the process of efficiently organizing data, and it plays a major role in making similarity search useful by dramatically accelerating time-consuming queries on large datasets. To improve query performance, you can specify an index type for each vector field. Currently, a vector field only supports one index type. Milvus automatically deletes the old index when switching the index type. ANNS vector indexes Most of the vector index types supported by Milvus use approximate nearest neighbors search (ANNS) algorithms. Compared with accurate retrieval, which is usually very time-consuming, the core idea of ANNS is no longer limited to returning the most accurate result, but only searching for neighbors of the target. ANNS improves retrieval efficiency by sacrificing accuracy within an acceptable range. According to the implementation methods, the ANNS vector index can be divided into four categories: Tree-based index Graph-based
"metadata: {'source': 'https://milvus.io/docs/index.md'}"
Result #4, len: 1583
chunk: Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus FREE Search Home v2.4.x About Milvus Get Started ConceptsArchitectureBitsetConsistencyMulti-tenancyTimestampSimilarity MetricsTime SynchronizationVector IndexScalar IndexRerankingTerminology User Guide Models Administration Guide Tools Integrations Example Applications FAQs API reference Terminology AutoID AutoID is an attribute of the primary field that determines whether to enable AutoIncrement for the primary field. The value of AutoID is defined based on a timestamp. For more information, refer to create_schema. AutoIndex Milvus automatically decides the most appropriate index type and params for a specific field based on empirical data. This is ideal for situations when you do not need to control the specific index params. For more information, refer to add_index. Attu Attu is an all-in-one administration tool for Milvus that significantly reduces the complexity and cost of managing the system. Birdwatcher Birdwatcher is a debugging tool for Milvus that connects to etcd, allowing you to monitor the status of the Milvus server and make adjustments in real-time. It also supports etcd file backups, aiding developers in troubleshooting. Bulk Writer Bulk Writer is a data processing tool provided by Milvus SDKs (e.g. PyMilvus, Java SDK) , designed to convert raw datasets into a format compatible with Milvus for efficient importing. Bulk Insert Bulk Insert is an API that enhances writing performance by allowing multiple files to be imported in a single request, optimizing operations with
"metadata: {'source': 'https://milvus.io/docs/glossary.md'}"
sources: https://milvus.io/docs/manage-collections.md Length long text to summarize: 1583
Semantic Chunking
This chunker works by determining when to "break" apart sentences. This is done by calculating cosine distances between adjacent sentences. Looking across all these cosine distances, look for outlier distances past some threshold. These outlier distances determine when chunks are split.
There are a few ways to determine what that threshold is, which are controlled by the breakpoint_threshold_type kwarg.
-
percentile(default) — In this method, any distances greater than X percentile is split into a chunk. -
standard_deviation— In this method, any difference greater than X standard deviations is split. -
interquartile— In this method, the interquartile distance is used to split chunks.
Created 87 semantic documents from 22 in 10.94 seconds
('Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus '
'FREE Search Home v2.4.x About Milvus Get StartedPrerequisitesInstall '
'MilvusInstall SDKsQuickstart Concepts User Guide Models Ad')
{}
Created Milvus collection from 87 docs in 1.58 seconds
Test with questions
Question: What does nlist vs nprobe mean in ivf_flat?
Result #1, len: 4834
chunk: By adjusting nprobe, an ideal balance between accuracy and speed can be found for a given scenario. Query time increases sharply as both the number of target input vectors (nq), and the number of clusters to search (nprobe), increase. BIN_IVF_FLAT is the most basic BIN_IVF index, and the encoded data stored in each unit is consistent with the original data. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 SPARSE_INVERTED_INDEX Each dimension maintains a list of vectors that have a non-zero value at that dimension. During search, Milvus iterates through each dimension of the query vector and computes scores for vectors that have non-zero values in those dimensions. Index building parameters Parameter Description Range drop_ratio_build The proportion of small vector values that are excluded during the indexing process. This option allows fine-tuning of the indexing process, making a trade-off between efficiency and accuracy by disregarding small values when building the index. [0, 1] Search parameters Parameter Description Range drop_ratio_search The proportion of small vector values that are excluded during the search process. This option allows fine-tuning of the search process by specifying the ratio of the smallest values in the query vector to ignore. It helps balance search precision and performance. The smaller the value set for drop_ratio_search, the less these small values contribute to the final score. By ignoring some small values, search performance can be improved with minimal impact on accuracy. [0, 1] SPARSE_WAND This index shares similarities with SPARSE_INVERTED_INDEX, while it utilizes the Weak-AND algorithm to further reduce the number of full IP distance evaluations during the search process. Based on our testing, SPARSE_WAND generally outperforms other methods in terms of speed. However, its performance can deteriorate rapidly as the density of the vectors increases. To address this issue, introducing a non-zero drop_ratio_search can significantly enhance performance while only incurring minimal accuracy loss. For more information, refer to Sparse Vector. Index building parameters Parameter Description Range drop_ratio_build The proportion of small vector values that are excluded during the indexing process. This option allows fine-tuning of the indexing process, making a trade-off between efficiency and accuracy by disregarding small values when building the index. [0, 1] Search parameters Parameter Description Range drop_ratio_search The proportion of small vector values that are excluded during the search process. This option allows fine-tuning of the search process by specifying the ratio of the smallest values in the query vector to ignore. It helps balance search precision and performance. The smaller the value set for drop_ratio_search, the less these small values contribute to the final score. By ignoring some small values, search performance can be improved with minimal impact on accuracy. [0, 1] FAQ What is the difference between FLAT index and IVF_FLAT index? IVF_FLAT index divides a vector space into nlist clusters. If you keep the default value of nlist as 16384, Milvus compares the distances between the target vector and the centers of all 16384 clusters to get nprobe nearest clusters. Then Milvus compares the distances between the target vector and the vectors in the selected clusters to get the nearest vectors. Unlike IVF_FLAT, FLAT directly compares the distances between the target vector and each and every vector. Therefore, when the total number of vectors approximately equals nlist, IVF_FLAT and FLAT has little difference in the way of calculation required and search performance. But as the number of vectors grows to two times, three times, or n times of nlist, IVF_FLAT index begins to show increasingly greater advantages. See How to Choose an Index in Milvus for more information. What's next Learn more about the Similarity Metrics supported in Milvus. Edit this page Report a bug Request doc changes On this page In-memory IndexANNS vector indexesIndexes supported in MilvusFAQWhat's next Resources Docs Blog Managed service Tutorials Bootcamp Demo Video Tools Attu Milvus CLI Sizing Tool Milvus backup tool Community Get involved Discord GitHub Milvus. 2024 All rights reserved.
"metadata: {'pk': 451033272369021860}"
Result #2, len: 6794
chunk: Guarantee exact search results. BIN_IVF_FLAT Quantization-based index High-speed query Requires a recall rate as high as possible Supported index Classification Scenario SPARSE_INVERTED_INDEX Inverted index Depends on relatively small datasets. Requires a 100% recall rate. SPARSE_WAND Inverted index Weak-AND algorithm accelerated Can get a significant speed improvement while only sacrificing a small amount of recall. FLAT For vector similarity search applications that require perfect accuracy and depend on relatively small (million-scale) datasets, the FLAT index is a good choice. FLAT does not compress vectors, and is the only index that can guarantee exact search results. Results from FLAT can also be used as a point of comparison for results produced by other indexes that have less than 100% recall. FLAT is accurate because it takes an exhaustive approach to search, which means for each query the target input is compared to every set of vectors in a dataset. This makes FLAT the slowest index on our list, and poorly suited for querying massive vector data. There are no parameters required for the FLAT index in Milvus, and using it does not need data training. Search parameters Parameter Description Range metric_type [Optional] The chosen distance metric. See Supported Metrics. IVF_FLAT IVF_FLAT divides vector data into nlist cluster units, and then compares distances between the target input vector and the center of each cluster. Depending on the number of clusters the system is set to query (nprobe), similarity search results are returned based on comparisons between the target input and the vectors in the most similar cluster(s) only – drastically reducing query time. By adjusting nprobe, an ideal balance between accuracy and speed can be found for a given scenario. Results from the IVF_FLAT performance test demonstrate that query time increases sharply as both the number of target input vectors (nq), and the number of clusters to search (nprobe), increase. IVF_FLAT is the most basic IVF index, and the encoded data stored in each unit is consistent with the original data. Index building parameters Parameter Description Range Default Value nlist Number of cluster units [1, 65536] 128 Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 IVF_SQ8 IVF_FLAT does not perform any compression, so the index files it produces are roughly the same size as the original, raw non-indexed vector data. For example, if the original 1B SIFT dataset is 476 GB, its IVF_FLAT index files will be slightly smaller (~470 GB). Loading all the index files into memory will consume 470 GB of storage. When disk, CPU, or GPU memory resources are limited, IVF_SQ8 is a better option than IVF_FLAT. This index type can convert each FLOAT (4 bytes) to UINT8 (1 byte) by performing Scalar Quantization (SQ). This reduces disk, CPU, and GPU memory consumption by 70—75%. For the 1B SIFT dataset, the IVF_SQ8 index files require just 140 GB of storage. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 IVF_PQ PQ (Product Quantization) uniformly decomposes the original high-dimensional vector space into Cartesian products of m low-dimensional vector spaces, and then quantizes the decomposed low-dimensional vector spaces. Instead of calculating the distances between the target vector and the center of all the units, product quantization enables the calculation of distances between the target vector and the clustering center of each low-dimensional space and greatly reduces the time complexity and space complexity of the algorithm. IVF_PQ performs IVF index clustering before quantizing the product of vectors. Its index file is even smaller than IVF_SQ8, but it also causes a loss of accuracy during searching vectors. Index building parameters and search parameters vary with Milvus distribution. Select your Milvus distribution first. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] m Number of factors of product quantization dim mod m == 0 nbits [Optional] Number of bits in which each low-dimensional vector is stored. [1, 16] (8 by default) Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 SCANN SCANN (Score-aware quantization loss) is similar to IVF_PQ in terms of vector clustering and product quantization. What makes them different lies in the implementation details of product quantization and the use of SIMD (Single-Instruction / Multi-data) for efficient calculation. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] with_raw_data Whether to include the raw data in the index True or False. Defaults to True. Unlike IVF_PQ, default values apply to m and nbits for optimized performance. Search parameters Common search Parameter Description Range Default value nprobe Number of units to query [1, nlist] reorder_k Number of candidate units to query [top_k, ∞] Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 HNSW HNSW (Hierarchical Navigable Small World Graph) is a graph-based indexing algorithm.
"metadata: {'pk': 451033272369021857}"
Length long text to summarize: 11629
"By adjusting nprobe, an ideal balance between accuracy and speed can be found for a given scenario. Query time increases sharply as both the number of target input vectors (nq), and the number of clusters to search (nprobe), increase. BIN_IVF_FLAT is the most basic BIN_IVF index, and the encoded data stored in each unit is consistent with the original data. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 SPARSE_INVERTED_INDEX Each dimension maintains a list of vectors that have a non-zero value at that dimension. During search, Milvus iterates through each dimension of the query vector and computes scores for vectors that have non-zero values in those dimensions. Index building parameters Parameter Description Range drop_ratio_build The proportion of small vector values that are excluded during the indexing process. This option allows fine-tuning of the indexing process, making a trade-off between efficiency and accuracy by disregarding small values when building the index. [0, 1] Search parameters Parameter Description Range drop_ratio_search The proportion of small vector values that are excluded during the search process. This option allows fine-tuning of the search process by specifying the ratio of the smallest values in the query vector to ignore. It helps balance search precision and performance. The smaller the value set for drop_ratio_search, the less these small values contribute to the final score. By ignoring some small values, search performance can be improved with minimal impact on accuracy. [0, 1] SPARSE_WAND This index shares similarities with SPARSE_INVERTED_INDEX, while it utilizes the Weak-AND algorithm to further reduce the number of full IP distance evaluations during the search process. Based on our testing, SPARSE_WAND generally outperforms other methods in terms of speed. However, its performance can deteriorate rapidly as the density of the vectors increases. To address this issue, introducing a non-zero drop_ratio_search can significantly enhance performance while only incurring minimal accuracy loss. For more information, refer to Sparse Vector. Index building parameters Parameter Description Range drop_ratio_build The proportion of small vector values that are excluded during the indexing process. This option allows fine-tuning of the indexing process, making a trade-off between efficiency and accuracy by disregarding small values when building the index. [0, 1] Search parameters Parameter Description Range drop_ratio_search The proportion of small vector values that are excluded during the search process. This option allows fine-tuning of the search process by specifying the ratio of the smallest values in the query vector to ignore. It helps balance search precision and performance. The smaller the value set for drop_ratio_search, the less these small values contribute to the final score. By ignoring some small values, search performance can be improved with minimal impact on accuracy. [0, 1] FAQ What is the difference between FLAT index and IVF_FLAT index? IVF_FLAT index divides a vector space into nlist clusters. If you keep the default value of nlist as 16384, Milvus compares the distances between the target vector and the centers of all 16384 clusters to get nprobe nearest clusters. Then Milvus compares the distances between the target vector and the vectors in the selected clusters to get the nearest vectors. Unlike IVF_FLAT, FLAT directly compares the distances between the target vector and each and every vector. Therefore, when the total number of vectors approximately equals nlist, IVF_FLAT and FLAT has little difference in the way of calculation required and search performance. But as the number of vectors grows to two times, three times, or n times of nlist, IVF_FLAT index begins to show increasingly greater advantages. See How to Choose an Index in Milvus for more information. What's next Learn more about the Similarity Metrics supported in Milvus. Edit this page Report a bug Request doc changes On this page In-memory IndexANNS vector indexesIndexes supported in MilvusFAQWhat's next Resources Docs Blog Managed service Tutorials Bootcamp Demo Video Tools Attu Milvus CLI Sizing Tool Milvus backup tool Community Get involved Discord GitHub Milvus. 2024 All rights reserved. Guarantee exact search results. BIN_IVF_FLAT Quantization-based index High-speed query Requires a recall rate as high as possible Supported index Classification Scenario SPARSE_INVERTED_INDEX Inverted index Depends on relatively small datasets. Requires a 100% recall rate. SPARSE_WAND Inverted index Weak-AND algorithm accelerated Can get a significant speed improvement while only sacrificing a small amount of recall. FLAT For vector similarity search applications that require perfect accuracy and depend on relatively small (million-scale) datasets, the FLAT index is a good choice. FLAT does not compress vectors, and is the only index that can guarantee exact search results. Results from FLAT can also be used as a point of comparison for results produced by other indexes that have less than 100% recall. FLAT is accurate because it takes an exhaustive approach to search, which means for each query the target input is compared to every set of vectors in a dataset. This makes FLAT the slowest index on our list, and poorly suited for querying massive vector data. There are no parameters required for the FLAT index in Milvus, and using it does not need data training. Search parameters Parameter Description Range metric_type [Optional] The chosen distance metric. See Supported Metrics. IVF_FLAT IVF_FLAT divides vector data into nlist cluster units, and then compares distances between the target input vector and the center of each cluster. Depending on the number of clusters the system is set to query (nprobe), similarity search results are returned based on comparisons between the target input and the vectors in the most similar cluster(s) only – drastically reducing query time. By adjusting nprobe, an ideal balance between accuracy and speed can be found for a given scenario. Results from the IVF_FLAT performance test demonstrate that query time increases sharply as both the number of target input vectors (nq), and the number of clusters to search (nprobe), increase. IVF_FLAT is the most basic IVF index, and the encoded data stored in each unit is consistent with the original data. Index building parameters Parameter Description Range Default Value nlist Number of cluster units [1, 65536] 128 Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 IVF_SQ8 IVF_FLAT does not perform any compression, so the index files it produces are roughly the same size as the original, raw non-indexed vector data. For example, if the original 1B SIFT dataset is 476 GB, its IVF_FLAT index files will be slightly smaller (~470 GB). Loading all the index files into memory will consume 470 GB of storage. When disk, CPU, or GPU memory resources are limited, IVF_SQ8 is a better option than IVF_FLAT. This index type can convert each FLOAT (4 bytes) to UINT8 (1 byte) by performing Scalar Quantization (SQ). This reduces disk, CPU, and GPU memory consumption by 70—75%. For the 1B SIFT dataset, the IVF_SQ8 index files require just 140 GB of storage. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 IVF_PQ PQ (Product Quantization) uniformly decomposes the original high-dimensional vector space into Cartesian products of m low-dimensional vector spaces, and then quantizes the decomposed low-dimensional vector spaces. Instead of calculating the distances between the target vector and the center of all the units, product quantization enables the calculation of distances between the target vector and the clustering center of each low-dimensional space and greatly reduces the time complexity and space complexity of the algorithm. IVF_PQ performs IVF index clustering before quantizing the product of vectors. Its index file is even smaller than IVF_SQ8, but it also causes a loss of accuracy during searching vectors. Index building parameters and search parameters vary with Milvus distribution. Select your Milvus distribution first. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] m Number of factors of product quantization dim mod m == 0 nbits [Optional] Number of bits in which each low-dimensional vector is stored. [1, 16] (8 by default) Search parameters Common search Parameter Description Range Default Value nprobe Number of units to query [1, nlist] 8 Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 SCANN SCANN (Score-aware quantization loss) is similar to IVF_PQ in terms of vector clustering and product quantization. What makes them different lies in the implementation details of product quantization and the use of SIMD (Single-Instruction / Multi-data) for efficient calculation. Index building parameters Parameter Description Range nlist Number of cluster units [1, 65536] with_raw_data Whether to include the raw data in the index True or False. Defaults to True. Unlike IVF_PQ, default values apply to m and nbits for optimized performance. Search parameters Common search Parameter Description Range Default value nprobe Number of units to query [1, nlist] reorder_k Number of candidate units to query [top_k, â\x88\x9e] Range search Parameter Description Range Default Value max_empty_result_buckets Maximum number of buckets not returning any search results.This is a range-search parameter and terminates the search process whilst the number of consecutive empty buckets reaches the specified value.Increasing this value can improve recall rate at the cost of increased search time. [1, 65535] 2 HNSW HNSW (Hierarchical Navigable Small World Graph) is a graph-based indexing algorithm."
HTML Chunking using LangChain
Before embedding, it is necessary to decide your chunk strategy, chunk size, and chunk overlap. This section uses:
- Strategy = Use markdown header hierarchies. Keep markdown sections together unless they are too long.
- Chunk size = Use the embedding model's parameter
MAX_SEQ_LENGTH - Overlap = Rule-of-thumb 10-15%
- Function =
- Langchain's
HTMLHeaderTextSplitterto split markdown sections. - Langchain's
RecursiveCharacterTextSplitterto split up long reviews recursively.
- Langchain's
Notice below, each chunk is grounded with the document source page.
In addition, header titles are kept together with the chunk of markdown text.
loaded 22 documents
<class 'list'>
<class 'langchain_core.documents.base.Document'>
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width"/><link rel="icon" href="/favicon-32x32.png"/><meta name="image" property="og:image" content="https://assets.zilliz.com/meta_image_milvus_d6510e10e0.png"/><meta property="og:type" content="WebSite"/><title>Enable RBAC | Milvus Documentation</title><meta name="description" content="Learn how to manage users, roles, and privileges. | v2.4.x"/><meta property="og:title" content="Enable RBAC
{'content_type': 'text/html',
'description': 'Learn how to manage users, roles, and privileges. | v2.4.x',
'language': 'en',
'source': 'https://milvus.io/docs/rbac.md',
'title': 'Enable RBAC | Milvus Documentation'}
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width"/><link rel="icon" href="/favicon-32x32.png"/><meta name="image" property="og:image" content="https://assets.zilliz.com/meta_image_milvus_d6510e10e0.png"/><meta property="og:type" content="WebSite"/><title>Enable RBAC | Milvus Documentation</title><meta name="description" content="Learn how to manage users, roles, and privileges. | v2.4.x"/><meta property="og:title" content="Enable RBAC
{'content_type': 'text/html',
'description': 'Learn how to manage users, roles, and privileges. | v2.4.x',
'language': 'en',
'source': 'https://milvus.io/docs/rbac.md',
'title': 'Enable RBAC | Milvus Documentation'}
22
{'https://milvus.io/docs/architecture_overview.md',
, 'https://milvus.io/docs/aws.md',
, 'https://milvus.io/docs/configure-docker.md',
, 'https://milvus.io/docs/deploy_s3.md',
, 'https://milvus.io/docs/embeddings.md',
, 'https://milvus.io/docs/get-and-scalar-query.md',
, 'https://milvus.io/docs/glossary.md',
, 'https://milvus.io/docs/gpu_index.md',
, 'https://milvus.io/docs/index-vector-fields.md',
, 'https://milvus.io/docs/index.md',
, 'https://milvus.io/docs/insert-update-delete.md',
, 'https://milvus.io/docs/install_standalone-docker.md',
, 'https://milvus.io/docs/manage-collections.md',
, 'https://milvus.io/docs/metric.md',
, 'https://milvus.io/docs/milvus-cdc-overview.md',
, 'https://milvus.io/docs/monitor_overview.md',
, 'https://milvus.io/docs/multi-vector-search.md',
, 'https://milvus.io/docs/quickstart.md',
, 'https://milvus.io/docs/rbac.md',
, 'https://milvus.io/docs/scaleout.md',
, 'https://milvus.io/docs/single-vector-search.md',
, 'https://milvus.io/docs/system_configuration.md'} Pattern for splitting: <h[1-3][^>]*>
docs: 22, split into 66 html chunks.
Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus FREE Search Home v2.4.x About Milvus Get StartedPrerequisitesInstall MilvusInstall SDKsQuickstart Concepts User Guide Models Ad
{'h1': 'Enable RBAC', 'h2': '1. Initiate a Milvus clie', 'h3': '2. Create a user', 'source': 'https://milvus.io/docs/glossary.md', 'doc_index': '3f184da0-d093-4eca-84c9-a20a7f4495b4'}
66 <class 'langchain_core.documents.base.Document'>
Why Milvus Docs Tutorials Tools Blog Community Stars0 Try Managed Milvus FREE Search Home v2.4.x About Milvus Get Started Concepts User Guide Models Administration GuideDeploymentConfigurationManage DependenciesWith Docker or HelmObject StorageMeta StorageMessage StorageWith Milvus OperatorScalingUpgradeMonitoring, Alerts & LogsSecurity Tools Integrations Example Applications FAQs API reference Configure Object Storage with Docker Compose or Helm Milvus uses MinIO for object storage by default, but it also supports using Amazon Simple Storage Service (S3) as persistent object storage for log and index files. This topic describes how to configure S3 for Milvus. You can skip this topic if you are satisfied with MinIO. You can configure S3 with Docker Compose or on K8s. Configure S3 with Docker Compose 1. Configure S3 MinIO is compatible with S3. To configure S3 with Docker Compose, provide your values for the minio section in the milvus.yaml file on the milvus/configs path. See MinIO/S3 Configurations for more information. 2. Refine docker-compose.yaml You'd also remove the MINIO_ADDRESS environment variable for milvus service at docker-compose.yaml. By default milvus will use local minio instead of external S3. 3. Run Milvus Run the following command to start Milvus that uses the S3 configurations. Configurations only take effect after Milvus starts. See Start Milvus for more information. Configure S3 on K8s For Milvus clusters on K8s, you can configure S3 in the same command that starts Milvus. Alternatively, you can configure S3 using the values.yml file on the /charts/milvus path in the milvus-helm repository before you start Milvus. The following table lists the keys for configuring S3 in the YAML file. Key Description Value minio.enabled Enables or disables MinIO. true / false externalS3.enabled Enables or disables S3. true / false externalS3.host The endpoint to access S3. externalS3.port The port to access S3. externalS3.rootPath The root path of the S3 storage. An emtpy string by default. externalS3.accessKey The access key ID for S3. externalS3.secretKey The secret access key for S3. externalS3.bucketName The name of the S3 bucket. externalS3.useSSL Whether to use SSL when connecting The values defaults to false Using the YAML file Configure the minio section in the values.yaml file. Configure the externalS3 section using your values in the values.yaml file. After configuring the preceding sections and saving the values.yaml file, run the following command to install Milvus that uses the S3 configurations. Using a command To install Milvus and configure S3, run the following command using your values. What's next Learn how to configure other Milvus dependencies with Docker Compose or Helm: Configure Meta Storage with Docker Compose or Helm Configure Message Storage with Docker Compose or Helm Edit this page Report a bug Request doc changes On this page Configure Object Storage with Docker Compose or HelmConfigure S3 with Docker ComposeConfigure S3 on K8sWhat's next Resources Docs Blog Managed service Tutorials Bootcamp Demo Video Tools Attu Milvus CLI Sizing Tool Milvus backup tool Community Get involved Discord GitHub Milvus. 2024 All rights reserved.
{'h1': 'Terminology', 'h2': 'AutoID', 'h3': 'Auto Index', 'source': 'https://milvus.io/docs/glossary.md', 'doc_index': '605aa699-ac58-4874-9cc5-5ba3407aa206'}
docs: 66, split into 1047 html sub chunks.
Indexed a total of 1047 child documents in 1.92 seconds
Test with questions
Question: What is the default AUTOINDEX index and vector field distance metric in Milvus?
Result #1, len: 510
chunk: is set to True, and auto_id is enabled for the primary key. Additionally, a vector field is introduced, configured with a dimensionality of 768, along with the inclusion of four scalar fields, each with its respective attributes. Step 2: Set up index parameters Index parameters dictate how Milvus organizes your data within a collection. You can tailor the indexing process for specific fields by adjusting their metric_type and index_type. For the vector field, you have the flexibility to select COSINE, L2,
("metadata: {'source': 'https://milvus.io/docs/manage-collections.md', 'pk': "
'451033276201043058}')
Result #2, len: 506
chunk: walks you through the basic operations on creating and managing indexes on vector fields in a collection. Overview Leveraging the metadata stored in an index file, Milvus organizes your data in a specialized structure, facilitating rapid retrieval of requested information during searches or queries. Milvus provides several index types to sort field values for efficient similarity searches. It also offers three metric types: Cosine Similarity (COSINE), Euclidean Distance (L2), and Inner Product (IP) to
("metadata: {'source': 'https://milvus.io/docs/index-vector-fields.md', 'pk': "
'451033276201043283}')
sources: 2, ['https://milvus.io/docs/manage-collections.md', 'https://milvus.io/docs/index-vector-fields.md']
Length long text to summarize: 1017 sources: https://milvus.io/docs/index-vector-fields.md https://milvus.io/docs/manage-collections.md
Summarize the context using a LLM
Using a small, open source LLM should be good enough for the summarization task.
Length prompt: 1202
MODEL:llama3:latest, FORMAT:gguf, PARAMETER_SIZE:8.0B, QUANTIZATION_LEVEL:Q4_0,
Answer: According to my selected text: The default AUTOINDEX index uses Cosine Similarity (COSINE) as its distance metric. This means that when you create an auto-indexed collection, it will use COSINE by default for the similarity search operations. In summary: The default AUTOINDEX index in Milvus uses Cosine Similarity (COSINE) as its distance metric. ollama_llama3_time: 10.50 seconds
Use an LLM to Generate a chat response to the user's question using the Retrieved Context.
Many different generative LLMs exist these days. Check out the lmsys leaderboard.
In this notebook, we'll try these LLMs:
- The newly released open-source Llama 3 from Meta.
- The cheapest, paid model from Anthropic Claude3 Haiku.
- The standard in its price cateogory, gpt-3.5-turbo, from Openai.
Length prompt: 1487
Try Meta Llama 3 with Ollama to generate a human-like chat response to the user's question
Follow the instructions to install ollama and pull a model.
https://github.com/ollama/ollama
View details about which models are supported by ollama.
https://ollama.com/library/llama3
That page says ollama run llama3 will by default pull the latest "instruct" model, which is fine-tuned for chat/dialogue use cases.
The other kind of llama3 models are "pre-trained" base model.
Example: ollama run llama3:text ollama run llama3:70b-text
Format gguf means the model runs on CPU. gg = "Georgi Gerganov", creator of the C library model format ggml, which was recently changed to gguf.
Quantization (think of it like vector compaction) can lead to higher throughput at the expense of lower accuracy. For the curious, quantization meanings can be found on:
https://huggingface.co/TheBloke/Llama-2-13B-chat-GGML/tree/main.
Below just listing the main quantization types.
- q4_0: Original quant method, 4-bit.
- q4_k_m: Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q4_K
- q5_0: Higher accuracy, higher resource usage and slower inference.
- q5_k_m: Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q5_K
- q 6_k: Uses Q8_K for all tensors
- q8_0: Almost indistinguishable from float16. High resource use and slow. Not recommended for most users.
MODEL:llama3:latest, FORMAT:gguf, PARAMETER_SIZE:8.0B, QUANTIZATION_LEVEL:Q4_0,
According to your provided Context on creating and managing indexes on vector fields in a collection using Milvus, I can answer that: The default AUTOINDEX index uses Cosine Similarity (COSINE) as its distance metric. ollama_llama3_time: 2.66 seconds
Call the LLM using a LangChain rag_chain pipeline.
Answer: Based on the provided Context, the default AUTOINDEX index and vector field distance metric in Milvus are: * Index type: Not specified (since auto_id is enabled), but it will be set to COSINE or L2 based on the metric_type. * Distance metric: Not specified (since no specific metric_type is given). However, considering the provided Context includes a vector field with dimensionality 768 and scalar fields, it's likely that the default distance metric would be COSINE Similarity (COSINE) for the vector field. Sources: ['https://milvus.io/docs/manage-collections.md', 'https://milvus.io/docs/index-vector-fields.md', 'https://milvus.io/docs/index.md'] ollama_llama3_time: 5.76 seconds
Now try Anyscale endpoints
('According to the provided Context, Milvus uses Cosine Similarity (COSINE) as '
'the default distance metric for vector fields.')
llama3_anyscale_endpoints_time: 1.83 seconds
('According to the provided Context, Milvus uses Cosine Similarity (COSINE) as '
'the default distance metric for vector fields.')
llama3_octai_endpoints_time: 0.92 seconds
('The default AUTOINDEX index in Milvus is L2 (Euclidean Distance) and the '
'default distance metric for vector fields is COSINE.')
llama3_groq_endpoints_time: 0.52 seconds
Also try Anthropic Claude3
We've practiced retrieval for free on our own data using open-source LLMs.
Now let's make a call to the paid Claude3. List of models
- Opus - most expensive
- Sonnet
- Haiku - least expensive!
Prompt engineering tutorials
Also try MistralAI's Mixtral 8x7B-Instruct-v0.1
This time ollama's version requires 48GB RAM. If you have big enough compute, run the command:
ollama run mixtral
Since my laptop is a M2 with only 16GB RAM, I decided to run Mixtral using Anyscale Endpoints. Instructions to install.
To get back to Anyscale Endpoints anytime, open the playground.
https://console.anyscale.com/v2/playground
(' The default AUTOINDEX index in Milvus is based on the HNSW (Hierarchical '
'Navigable Small World) algorithm, and the default vector field distance '
'metric is Euclidean Distance (L2).')
mixtral_anyscale_endpoints_time: 2.21 seconds
Also try OpenAI
💡 Note: For use cases that need to always be factually grounded, use very low temperature values while more creative tasks can benefit from higher temperatures.
Length prompt: 1538
Question: What is the default AUTOINDEX index and vector field distance metric in Milvus?
('Answer: The default AUTOINDEX index in Milvus is IVF_SQ8, and the default '
'vector field distance metric is Inner Product (IP).')
chatgpt_3.5_turbo_time: 0.94234
Use Ragas to evaluate RAG pipeline
Ragas is an open source project for evaluating RAG components. Paper, Code, Docs, Intro blog.
Please note that RAGAS can use a large amount of OpenAI api token consumption.
Read through this notebook carefully and pay attention to the number of questions and metrics you want to evaluate.
/opt/miniconda3/envs/py311-unum/lib/python3.11/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`. warnings.warn(
Evaluating CONTEXTS using 4 eval questions:
Evaluating: 0%| | 0/8 [00:00<?, ?it/s]
Evaluate chunking: recursive_context_512_k_2, avg_score: 0.67
Evaluating: 0%| | 0/8 [00:00<?, ?it/s]
Evaluate chunking: html_context_512_k_2, avg_score: 0.77
Evaluating: 0%| | 0/8 [00:00<?, ?it/s]
Evaluate chunking: parent_context_1536_k1, avg_score: 0.7
Evaluating: 0%| | 0/8 [00:00<?, ?it/s]
Failed to parse output. Returning None.
Evaluate chunking: semantic_context_k_1, avg_score: 0.37
Evaluating: 0%| | 0/8 [00:00<?, ?it/s]
Evaluate chunking: semantic_context_k_2_summary, avg_score: 0.5
[{'recursive_context_512_k_2': 0.67},
{'html_context_512_k_2': 0.77},
{'parent_context_1536_k1': 0.7},
{'semantic_context_k_1': 0.37},
{'semantic_context_k_2_summary': 0.5}]
Chunking 108.0% improvement.
Author: Christy Bergman Python implementation: CPython Python version : 3.11.8 IPython version : 8.22.2 unstructured: 0.14.4 lxml : 5.1.0 torch : 2.3.0 pymilvus : 2.4.4 langchain : 0.2.6 ollama : 0.1.8 octoai : 1.0.2 groq : 0.8.0 openai : 1.35.0 ragas : 0.1.9 conda environment: py311-unum