From b9ad6dc69d2607caebd999dd66317c78ad4786b1 Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 10:07:47 -0500 Subject: [PATCH 01/14] Refresh AI articles --- articles/cosmos-db/gen-ai/cosmos-ai-graph.md | 49 +++++++++---------- .../cosmos-db/gen-ai/distance-functions.md | 11 +++-- .../cosmos-db/gen-ai/document-ingestion.md | 27 +++++----- articles/cosmos-db/gen-ai/full-text-search.md | 25 ++++++---- articles/cosmos-db/gen-ai/hybrid-search.md | 33 ++++++++----- articles/cosmos-db/gen-ai/integrations.md | 23 ++++----- 6 files changed, 91 insertions(+), 77 deletions(-) diff --git a/articles/cosmos-db/gen-ai/cosmos-ai-graph.md b/articles/cosmos-db/gen-ai/cosmos-ai-graph.md index 006da294b5..df01fec53f 100644 --- a/articles/cosmos-db/gen-ai/cosmos-ai-graph.md +++ b/articles/cosmos-db/gen-ai/cosmos-ai-graph.md @@ -1,41 +1,38 @@ --- - -title: Azure Cosmos DB AI Graph -description: Apply Knowledge Graphs to expand the capability of AI apps. +title: AI knowledge graphs +description: Create AI knowledge graphs using Azure Cosmos DB for NoSQL to allow AI apps to manage and query complex data relationships. author: jcodella ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 9/16/2024 +ms.date: 12/03/2024 ms.author: jacodel ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Retrieval Augmented Generated (RAG) with vector search and knowledge graphs using Azure Cosmos DB -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] - [CosmosAIGraph](https://aka.ms/cosmosaigraph) is an innovative solution that applies the power of Azure Cosmos DB to create AI-powered knowledge graphs. This technology integrates advanced graph database capabilities with AI to provide a robust platform for managing and querying complex data relationships. By utilizing Cosmos DB's scalability and performance in both document and vector form, Cosmos AI Graph enables the creation of sophisticated data models that can answer various data questions and uncover hidden relationships and concepts in semi-structured data. -## What types of questions can knowledge graphs help to answer? +## What types of questions can knowledge graphs help to answer -- **Complex Relationship Queries**: - - *Question*: "What are the direct and indirect connections between Person A and Person B within a social network?" - - *Explanation*: Graph RAG can traverse the graph to find all paths and relationships between two nodes, providing a detailed map of connections, which is difficult for Vector Search as it doesn't have authoritative/curated view of relationships between entities. +- **Complex Relationship Queries**: + - *Question*: "What are the direct and indirect connections between Person A and Person B within a social network?" + - *Explanation*: Graph RAG can traverse the graph to find all paths and relationships between two nodes, providing a detailed map of connections, which is difficult for Vector Search as it doesn't have authoritative/curated view of relationships between entities. - **Hierarchical Data Queries**: - - *Question*: "What is the organizational hierarchy from the CEO down to the entry-level employees in this company?" - - *Explanation*: Graph RAG can efficiently navigate hierarchical structures, identifying parent-child relationships and levels within the hierarchy, whereas Vector Search is more suited for finding similar items rather than understanding hierarchical relationships. + - *Question*: "What is the organizational hierarchy from the CEO down to the entry-level employees in this company?" + - *Explanation*: Graph RAG can efficiently navigate hierarchical structures, identifying parent-child relationships and levels within the hierarchy, whereas Vector Search is more suited for finding similar items rather than understanding hierarchical relationships. - **Contextual Path Queries**: - - *Question*: "What are the steps involved in the supply chain from raw material procurement to the final product delivery?" - - *Explanation*: Graph RAG can follow the specific paths and dependencies within a supply chain graph, providing a step-by-step breakdown. Vector Search, while excellent at finding similar items, lacks the capability to follow and understand the sequence of steps in a process. - + - *Question*: "What are the steps involved in the supply chain from raw material procurement to the final product delivery?" + - *Explanation*: Graph RAG can follow the specific paths and dependencies within a supply chain graph, providing a step-by-step breakdown. Vector Search, while excellent at finding similar items, lacks the capability to follow and understand the sequence of steps in a process. When it comes to [Retrieval Augmented Generation (RAG)](rag.md), combining **Knowledge Graphs** and **Vector Search** can offer powerful capabilities that expand the range of questions that can be answered about the data. Graph RAG enhances the retrieval process by using the structured relationships within a graph, making it ideal for applications that require contextual understanding and complex querying, such as knowledge management systems and personalized content delivery. On the other hand, Vector Search excels in handling unstructured data and finding similarities based on vector embeddings, which is useful for tasks like image and document retrieval. Together, these technologies can provide a comprehensive solution that combines the strengths of both structured and unstructured data processing. - :::image type="content" source="../media/gen-ai/cosmos-ai-graph/cosmos-ai-graph-architecture.png" alt-text="Diagram of the Cosmos AI Graph infrastructure, components, and flow."::: ## OmniRAG @@ -44,22 +41,22 @@ CosmosAIGraph features *OmniRAG*, a versatile approach to data retrieval that dy ### Example user questions and strategy used -| User Questions | Strategy | -|----------------|----------------| -|What is the Python Flask Library | DB RAG | -| What are its dependencies | Graph Rag | -|What is the Python Flask Library | Database RAG | -| What are its dependencies | Graph RAG | -| Who is the author | DB RAG | +| User Questions | Strategy | +| --- | --- | +| What is the Python Flask Library | DB RAG | +| What are its dependencies | Graph Rag | +| What is the Python Flask Library | Database RAG | +| What are its dependencies | Graph RAG | +| Who is the author | DB RAG | | What other libraries did she write | Graph RAG | | Display a graph of all her libraries and their dependencies | Graph RAG | - ## Get started CosmosAIGraph applies Azure Cosmos DB to create AI-powered graphs and knowledge graphs, enabling sophisticated data models for applications like recommendation systems and fraud detection. It combines traditional database, vector database, and graph database capabilities with AI to manage and query complex data relationships efficiently. Get started [here!](https://aka.ms/cosmosaigraph) -## Next steps +## Related content + - [CosmosAIGraph on Azure Cosmos DB TV - YouTube](https://www.youtube.com/watch?v=0alvRmEgIpQ) - [Vector Search with Azure Cosmos DB for NoSQL](vector-search-overview.md) - [Tokens](tokens.md) @@ -68,5 +65,7 @@ CosmosAIGraph applies Azure Cosmos DB to create AI-powered graphs and knowledge - [30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) - [90-day Free Trial and up to $6,000 in throughput credits with Azure AI Advantage](../ai-advantage.md) +## Next step + > [!div class="nextstepaction"] > [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) diff --git a/articles/cosmos-db/gen-ai/distance-functions.md b/articles/cosmos-db/gen-ai/distance-functions.md index b0e632e200..7674451ed5 100644 --- a/articles/cosmos-db/gen-ai/distance-functions.md +++ b/articles/cosmos-db/gen-ai/distance-functions.md @@ -1,13 +1,15 @@ --- -title: Distance functions -description: Distance functions overview. +title: Vector distance functions +description: Overview of Manhattan, Euclidean, cosmine similarity, and dot product vector distance functions in Azure Cosmos DB for NoSQL. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 07/01/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # What are distance functions? @@ -31,6 +33,7 @@ Cosine similarity measures the cosine of the angle between two vectors projected Two vectors are multiplied to return a single number. It combines the two vectors' magnitudes, as well as the cosine of the angle between them, showing how much one vector goes in the direction of another. ## Related content + - [VectorDistance system function](../nosql/query/vectordistance.md) in Azure Cosmos DB NoSQL - [What is a vector database?](../vector-database.md) - [Retrieval Augmented Generation (RAG)](rag.md) diff --git a/articles/cosmos-db/gen-ai/document-ingestion.md b/articles/cosmos-db/gen-ai/document-ingestion.md index 6b6181ce31..6436e26e32 100644 --- a/articles/cosmos-db/gen-ai/document-ingestion.md +++ b/articles/cosmos-db/gen-ai/document-ingestion.md @@ -1,24 +1,23 @@ --- - -title: Ingest and vectorize document files in Azure Cosmos DB -description: Learn how to ingest document files into Azure Cosmos DB for NoSQL +title: Ingest and vectorize document files +titleSuffix: Azure Cosmos DB for NoSQL +description: Ingest document files into Azure Cosmos DB for NoSQL for use in vector indexing and similarity search solutions. author: jcodella ms.service: azure-cosmos-db ms.subservice: nosql ms.custom: - ignite-2024 ms.topic: how-to -ms.date: 9/16/2024 +ms.date: 12/03/2024 ms.author: jacodel ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Load and process document files into Azure Cosmos DB for search -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] - - > [!NOTE] > Document ingestion for Azure Cosmos DB is in private preview. If you're interested to join the preview, we encourage you to join the wait list by signing this form: https://aka.ms/Doc2CDBSignup @@ -28,15 +27,13 @@ We introduce Doc2CDB for Azure Cosmos DB, a powerful accelerator designed to str - Retrieval-Augmented Generation (RAG) over Documents. Personalize your Small and Large Language Models to your data with RAG. By extracting text from document files, chunking and vectorizing the data, then storing it in Azure Cosmos DB, you’re then set up to empower the chatbot to generate more accurate and contextually relevant responses to your scenarios. When you ask a question, the chatbot retrieves the most relevant text chunks through vector search and uses them to generate an answer, grounded in your document data. - :::image type="content" source="../media/gen-ai/document-ingestion/document-ingestion-pipeline.png" alt-text="Diagram of the Cosmos AI Graph infrastructure, components, and flow."::: - ## The end-to-end pipeline Doc2CDB includes several key stages in its pipeline: 1. File Upload to Azure Blob Storage - - The process begins with uploading documents to Azure Blob Storage. This stage ensures that your files are securely stored and easily accessible for further processing. This is compatible with PDFs, Microsoft Office documents (DOCX, XLSX, PPTX, HTML), and Images (JPEG, PNG, BMP, TIFF, HEIF). + - The process begins with uploading documents to Azure Blob Storage. This stage ensures that your files are securely stored and easily accessible for further processing. This is compatible with PDFs, Microsoft Office documents (DOCX, XLSX, PPTX, HTML), and Images (JPEG, PNG, BMP, TIFF, HEIF). 2. Text Extraction - Once the files are uploaded, the next step is text extraction. This involves parsing text data and performing OCR on documents using Azure Document Intelligence, to extract text that can be processed and indexed in Azure Cosmos DB. This stage is crucial for preparing the data for subsequent processing. 3. Text Chunking @@ -47,18 +44,16 @@ Doc2CDB includes several key stages in its pipeline: - Finally, each text chunk, along with its corresponding vector embedding, is stored in an Azure Cosmos DB for NoSQL container as a unique document. This container is configured to perform efficient vector searches and, eventually, full-text searches. By using Azure Cosmos DB’s powerful vector indexing and search capabilities, users can quickly and easily retrieve relevant information from their text data. ## Benefits of the Doc2CDB solution accelerator + - Scalability: Handle large volumes of text data with ease, thanks to the scalable nature of Azure AI services and Azure Cosmos DB - Efficiency: Streamline the text processing pipeline, reducing the time and effort required to manage and search text data. This is preconfigured for you - Advanced Search Capabilities: Utilize ultra-fast and efficient Vector Indexing in Azure Cosmos DB perform vector search to find the most semantically relevant data from your documents -## Get started! +## Get started The Doc2CDB accelerator designed to help you parse, process, and store your document data more easily to take advantage of Azure Cosmos DB’s rich query language and powerful Vector Similarity Search. Visit https://aka.ms/Doc2CDB and give it a try today! - -## Next steps - -Now that your documents are parsed, chunked, vectorized, and stored in Azure Cosmos DB for NoSQL, check out these resources here to get started with vector search and building AI apps: +## Related content - [Vector Search with Azure Cosmos DB for NoSQL](vector-search-overview.md) - [Tokens](tokens.md) @@ -67,5 +62,7 @@ Now that your documents are parsed, chunked, vectorized, and stored in Azure Cos - [30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) - [90-day Free Trial and up to $6,000 in throughput credits with Azure AI Advantage](../ai-advantage.md) +## Next step + > [!div class="nextstepaction"] > [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) diff --git a/articles/cosmos-db/gen-ai/full-text-search.md b/articles/cosmos-db/gen-ai/full-text-search.md index 5ce4d0a45b..a8f2a53357 100644 --- a/articles/cosmos-db/gen-ai/full-text-search.md +++ b/articles/cosmos-db/gen-ai/full-text-search.md @@ -1,16 +1,19 @@ --- -title: Full text search in Azure Cosmos DB for NoSQL -description: Full text search overview +title: Use full-text search (preview) +titleSuffix: Azure Cosmos DB for NoSQL +description: Overview of full text search for querying data using "best matching 25" scoring in Azure Cosmos DB for NoSQL. author: jcodella ms.author: jacodel ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 11/04/2024 +ms.topic: how-to +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- -# Full text search in Azure Cosmos DB for NoSQL (preview) +# Full-text search in Azure Cosmos DB for NoSQL (preview) Azure Cosmos DB for NoSQL now offers a powerful Full Text Search feature in preview, designed to enhance the search capabilities of your applications. @@ -42,7 +45,7 @@ Full Text Search is ideal for a variety of scenarios, including: ## Enable the full text and hybrid search for NoSQL preview feature -Full text search, full text scoring, and hybrid search all require enabling the preview feature on your Azure Cosmos DB for NoSQL account before using. Follow the below steps to register: +Full text search, full text scoring, and hybrid search all require enabling the preview feature on your Azure Cosmos DB for NoSQL account before using. Follow the below steps to register: 1. Navigate to your Azure Cosmos DB for NoSQL resource page. 2. Select the "Features" pane under the "Settings" menu item. @@ -53,11 +56,13 @@ Full text search, full text scoring, and hybrid search all require enabling the :::image type="content" source="../nosql/media/full-text-search/full-text-search-feature.png" alt-text="Screenshot of full text and hybrid search preview feature in the Azure portal."::: ### Configure container policies and indexes for hybrid search + To use full text search capabilities, you'll first need to define two policies: - A container-level full text policy that defines what paths will contain text for the new full text query system functions. - A full text index added to the indexing policy that enables efficient search. ### Full text policy + For every text property you'd like to configure for full text search, you must declare both the `path` of the property with text and the `language` of the text. A simple full text policy can be: ```json @@ -97,6 +102,7 @@ Defining multiple text paths is easily done by adding another element to the `fu > Wild card characters (*, []) are not currently supported in the full text policy or full text index. ### Full text index + Any full text search operations should make use of a [*full text index*](../index-policy.md#full-text-indexes). A full text index can easily be defined in any Azure Cosmos DB for NoSQL index policy per the example below. ```json @@ -121,7 +127,7 @@ Any full text search operations should make use of a [*full text index*](../inde } ``` -Just as with the full text policies, full text indexes can be defined on multiple paths. +Just as with the full text policies, full text indexes can be defined on multiple paths. ```json { @@ -155,9 +161,9 @@ Full text search and scoring operations are performed using the following system - [`FullTextContains`](../nosql/query/fulltextcontains.md): Returns `true` if a given string is contained in the specified property of a document. This is useful in a `WHERE` clause when you want to ensure specific key words are included in the documents returned by your query. - [`FullTextContainsAll`](../nosql/query/fulltextcontainsall.md): Returns `true` if *all* of the given strings are contained in the specified property of a document. This is useful in a `WHERE` clause when you want to ensure that multiple key words are included in the documents returned by your query. - [`FullTextContainsAny`](../nosql/query/fulltextcontainsany.md): Returns `true` if *any* of the given strings are contained in the specified property of a document. This is useful in a `WHERE` clause when you want to ensure that at least one of the key words is included in the documents returned by your query. -- [`FullTextScore`](../nosql/query/fulltextscore.md): Returns a score. This can only be used in an `ORDER BY RANK` clause, where the returned documents are ordered by the rank of the full text score, with most relevant (highest scoring) documents at the top, and least relevant (lowest scoring) documents at the bottom +- [`FullTextScore`](../nosql/query/fulltextscore.md): Returns a score. This can only be used in an `ORDER BY RANK` clause, where the returned documents are ordered by the rank of the full text score, with most relevant (highest scoring) documents at the top, and least relevant (lowest scoring) documents at the bottom. -Here are a few examples of each function in use. +Here are a few examples of each function in use. #### FullTextContains @@ -203,6 +209,7 @@ ORDER BY RANK FullTextScore(c.text, ["bicycle", "mountain"]) > FullTextScore can only be used in the `ORDER BY RANK` clause and not projected in the `SELECT` statement or in a `WHERE` clause. ## Related content + - [``FullTextContains`` system function](../nosql/query/fulltextcontains.md) - [``FullTextContainsAll`` system function](../nosql/query/fulltextcontainsall.md) - [``FullTextContainsAny`` system function](../nosql/query/fulltextcontainsany.md) diff --git a/articles/cosmos-db/gen-ai/hybrid-search.md b/articles/cosmos-db/gen-ai/hybrid-search.md index 77f180e6a6..cf5def30de 100644 --- a/articles/cosmos-db/gen-ai/hybrid-search.md +++ b/articles/cosmos-db/gen-ai/hybrid-search.md @@ -1,16 +1,20 @@ --- -title: Hybrid search in Azure Cosmos DB for NoSQL -description: Hybrid search overview +title: Use hybrid search (preview) +titleSuffix: Azure Cosmos DB for NoSQL +description: Overview of hybrid search that combines vector search with full-text search scoring in Azure Cosmos DB for NoSQL. author: jcodella ms.author: jacodel ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 11/04/2024 +ms.topic: how-to +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Hybrid search in Azure Cosmos DB for NoSQL (preview) + Azure Cosmos DB for NoSQL now supports a powerful hybrid search capability that combines Vector Search with Full Text Search scoring (BM25) using the Reciprocal Rank Fusion (RRF) function. > [!NOTE] @@ -18,13 +22,12 @@ Azure Cosmos DB for NoSQL now supports a powerful hybrid search capability that ## What is hybrid search? - Hybrid search leverages the strengths of both vector-based and traditional keyword-based search methods to deliver more relevant and accurate search results. Hybrid search is easy to do in Azure Cosmos DB for NoSQL due to the ability to store both metadata and vectors within the same document. +Hybrid search leverages the strengths of both vector-based and traditional keyword-based search methods to deliver more relevant and accurate search results. Hybrid search is easy to do in Azure Cosmos DB for NoSQL due to the ability to store both metadata and vectors within the same document. Hybrid search in Azure Cosmos DB for NoSQL integrates two distinct search methodologies: - **Vector search**: Utilizes machine learning models to understand the semantic meaning of queries and documents. This allows for more nuanced and context-aware search results, especially useful for complex queries where traditional keyword search might fall short. - -- **Full text search (BM25)**: A well-established algorithm that scores documents based on the presence and frequency of words and terms. BM25 is particularly effective for straightforward keyword searches, providing a robust baseline for search relevance. +- **Full text search (BM25)**: A well-established algorithm that scores documents based on the presence and frequency of words and terms. BM25 is particularly effective for straightforward keyword searches, providing a robust baseline for search relevance. The results from vector search and full text search are then combined using the Reciprocal Rank Fusion (RRF) function. RRF is a rank aggregation method that merges the rankings from multiple search algorithms to produce a single, unified ranking. This ensures that the final search results benefit from the strengths of both search approaches and offers multiple benefits. @@ -35,10 +38,10 @@ The results from vector search and full text search are then combined using the ## How to use hybrid search 1. Enable the [Vector Search in Azure Cosmos DB for NoSQL feature](../nosql/vector-search.md#enable-the-vector-indexing-and-search-feature). -2. Enable the [Full Text & Hybrid Search for NoSQL preview feature](../gen-ai/full-text-search.md#enable-the-full-text-and-hybrid-search-for-nosql-preview-feature). -3. Create a container with a vector policy, full text policy, vector index, and full text index. -4. Insert your data with text and vector properties. -5. Run hybrid queries against the data. +1. Enable the [Full Text & Hybrid Search for NoSQL preview feature](../gen-ai/full-text-search.md#enable-the-full-text-and-hybrid-search-for-nosql-preview-feature). +1. Create a container with a vector policy, full text policy, vector index, and full text index. +1. Insert your data with text and vector properties. +1. Run hybrid queries against the data. ## Configure policies and indexes for hybrid search @@ -46,6 +49,7 @@ The results from vector search and full text search are then combined using the > Currently, vector policies and vector indexes are immutable after creation. To make changes, please create a new collection. ### A sample vector policy + ```json { "vectorEmbeddings": [ @@ -58,9 +62,10 @@ The results from vector search and full text search are then combined using the } ``` - + ### A sample full text policy - ```json + +```json { "defaultLanguage": "en-US", "fullTextPaths": [ @@ -73,6 +78,7 @@ The results from vector search and full text search are then combined using the ``` ### A sample indexing policy with both full text and vector indexes + ```json { "indexingMode": "consistent", @@ -123,6 +129,7 @@ ORDER BY RANK RRF(VectorDistance(c.vector, [1,2,3]), FullTextScore(c.text, ["tex ``` ## Related content + - [Vector search](../nosql/vector-search.md) - [VectorDistance system function](../nosql/query/vectordistance.md) - [FullTextScore system function](../nosql/query/fulltextscore.md) diff --git a/articles/cosmos-db/gen-ai/integrations.md b/articles/cosmos-db/gen-ai/integrations.md index 3cb07e2b8d..ef1b8fd0c3 100644 --- a/articles/cosmos-db/gen-ai/integrations.md +++ b/articles/cosmos-db/gen-ai/integrations.md @@ -1,32 +1,31 @@ --- -title: Integrations with Azure Cosmos DB for AI apps -description: Learn about integrations with AI/LLM orchestration packages +title: Integrations for AI apps +titleSuffix: Azure Cosmos DB +description: Integrate Azure Cosmos DB with AI and large language model (LLM) orchestration packages like Semantic Kernel and LangChain. author: jcodella ms.service: azure-cosmos-db ms.topic: how-to -ms.date: 9/16/2024 +ms.date: 12/03/2024 ms.author: jacodel ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore --- -# Integrations - -[!INCLUDE[NoSQL, MongoDB](../includes/appliesto-nosql-mongodb.md)] +# Azure Cosmos DB integrations for AI applications Azure Cosmos DB seamlessly integrates with leading large language model (LLM) orchestration packages like [Semantic Kernel](https://github.com/microsoft/semantic-kernel) and [LangChain](https://www.langchain.com/), enabling developers to harness the power of advanced AI capabilities within their applications. These orchestration packages can streamline the management and use of LLMs, embedding models, and databases, making it even easier to develop Generative AI applications. | Integration Tool | Description | Azure Cosmos DB for NoSQL | Azure Cosmos DB for MongoDB (vCore) | -|------------------|-------------|---------------------------|-----------------------------------| +| --- | --- | --- | --- | | **[Semantic Kernel](https://github.com/microsoft/semantic-kernel)** | An open-source framework by Microsoft that combines AI agents with languages like C#, Python, and Java, enabling seamless orchestration of code and AI models. | [Python Connector](/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/azure-cosmosdb-nosql-connector?pivots=programming-language-python)
[.NET Connector](/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/azure-cosmosdb-nosql-connector?pivots=programming-language-csharp) | [Python Connector](/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/azure-cosmosdb-mongodb-connector?pivots=programming-language-python)
[.NET Connector](/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/azure-cosmosdb-mongodb-connector?pivots=programming-language-csharp) | | **[LangChain](https://www.langchain.com/)** | A framework that simplifies the creation of applications powered by large language models (LLMs), offering tools for context-aware reasoning applications in Python, JavaScript, and Java. | [Python](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)
[JavaScript](https://js.langchain.com/docs/integrations/vectorstores/azure_cosmosdb_nosql/)
[Java](https://docs.langchain4j.dev/integrations/embedding-stores/azure-cosmos-nosql/) | [Python](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db/)
[JavaScript](https://js.langchain.com/docs/integrations/vectorstores/azure_cosmosdb_mongodb/)
[Java](https://docs.langchain4j.dev/integrations/embedding-stores/azure-cosmos-mongo-vcore/) | | **[LlamaIndex](https://www.llamaindex.ai/)** | A framework for building context-augmented AI applications that can integrate private or domain-specific data with LLMs for complex workflows. | [Python](https://docs.llamaindex.ai/en/stable/examples/vector_stores/AzureCosmosDBNoSqlDemo/) | [Python](https://docs.llamaindex.ai/en/stable/examples/vector_stores/AzureCosmosDBMongoDBvCoreDemo/) | | **[CosmosAIGraph](https://aka.ms/cosmosaigraph)** | Uses Azure Cosmos DB to create AI-powered knowledge graphs, enabling robust data models and revealing relationships in semi-structured data. | [Quickstart](https://github.com/AzureCosmosDB/CosmosAIGraph/tree/main/impl/docs#quick-start) | [Quickstart](https://github.com/AzureCosmosDB/CosmosAIGraph/tree/main/impl/docs#quick-start) | - -## Next steps - -Check out these resources here to get started with vector search and building AI apps: +## Related content - [Azure Cosmos DB Samples Gallery](https://aka.ms/AzureCosmosDB/Gallery) - [Vector Search with Azure Cosmos DB for NoSQL](vector-search-overview.md) @@ -37,5 +36,7 @@ Check out these resources here to get started with vector search and building AI - [30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) - [90-day Free Trial and up to $6,000 in throughput credits with Azure AI Advantage](../ai-advantage.md) +## Next step + > [!div class="nextstepaction"] > [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) From 1e3a586637d9856356c3d38b77a8936a255d3df3 Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 10:11:48 -0500 Subject: [PATCH 02/14] Refresh AI articles (batch 1) --- articles/cosmos-db/gen-ai/distance-functions.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/articles/cosmos-db/gen-ai/distance-functions.md b/articles/cosmos-db/gen-ai/distance-functions.md index 7674451ed5..b6a27d540c 100644 --- a/articles/cosmos-db/gen-ai/distance-functions.md +++ b/articles/cosmos-db/gen-ai/distance-functions.md @@ -1,6 +1,6 @@ --- title: Vector distance functions -description: Overview of Manhattan, Euclidean, cosmine similarity, and dot product vector distance functions in Azure Cosmos DB for NoSQL. +description: Overview of Manhattan, Euclidean, cosine similarity, and dot product vector distance functions in Azure Cosmos DB for NoSQL. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db @@ -18,11 +18,11 @@ Distance functions are mathematical formulas used to measure the similarity or d ## Manhattan distance -This measures the distance between two points by adding up the absolute differences of their coordinates. Imagine walking in a grid-like city, such as many neighborhoods in Manhattan; it is the total number of blocks you walk north-south and east-west. +This measures the distance between two points by adding up the absolute differences of their coordinates. Imagine walking in a grid-like city, such as many neighborhoods in Manhattan; it's the total number of blocks you walk north-south and east-west. ## Euclidean distance -Euclidean distance measures the straight-line distance between two points. It is named after the ancient Greek mathematician Euclid, who is often referred to as the “father of geometry”. +Euclidean distance measures the straight-line distance between two points. It's named after the ancient mathematician Euclid, who is often referred to as the “father of geometry”. ## Cosine similarity @@ -30,7 +30,7 @@ Cosine similarity measures the cosine of the angle between two vectors projected ## Dot product -Two vectors are multiplied to return a single number. It combines the two vectors' magnitudes, as well as the cosine of the angle between them, showing how much one vector goes in the direction of another. +Two vectors are multiplied to return a single number. It combines the two vectors' magnitudes, and the cosine of the angle between them, showing how much one vector goes in the direction of another. ## Related content From 9eb890a10a447de703ca0979cd5f8367f2d4d05b Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 10:17:32 -0500 Subject: [PATCH 03/14] Refresh AI articles (batch 1) --- articles/cosmos-db/gen-ai/knn-vs-ann.md | 28 ++++---- .../gen-ai/quickstart-rag-chatbot.md | 65 ++++++++++--------- articles/cosmos-db/gen-ai/rag.md | 39 ++++++----- articles/cosmos-db/gen-ai/semantic-cache.md | 18 +++-- 4 files changed, 84 insertions(+), 66 deletions(-) diff --git a/articles/cosmos-db/gen-ai/knn-vs-ann.md b/articles/cosmos-db/gen-ai/knn-vs-ann.md index 9045e1a148..915278e9f1 100644 --- a/articles/cosmos-db/gen-ai/knn-vs-ann.md +++ b/articles/cosmos-db/gen-ai/knn-vs-ann.md @@ -1,13 +1,18 @@ --- -title: kNN vs ANN -description: Explanation and comparison of kNN and ANN algorithms. +title: kNN and ANN algorithm comparison +titleSuffix: Azure Cosmos DB +description: Compare and contrast k-Nearest Neighbors (kNN) and Approximate Nearest Neighbors (ANN) algorithms in relation to Azure Cosmos DB. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 07/01/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- # kNN vs ANN @@ -17,23 +22,24 @@ Two major categories of vector search algorithms are k-Nearest Neighbors (kNN) a ## How kNN works 1. Vectorization: Each data point in the dataset is represented as a vector in a multi-dimensional space. -2. Distance Calculation: To classify a new data point (query point), the algorithm calculates the distance between the query point and all other points in the dataset using a [distance function](distance-functions.md). -3. Finding Neighbors: The algorithm identifies the k closest data points (neighbors) to the query point based on the calculated distances. The value of k (the number of neighbors) is crucial. A small k can be sensitive to noise, while a large k can smooth out details. -4. Making Predictions: +1. Distance Calculation: To classify a new data point (query point), the algorithm calculates the distance between the query point and all other points in the dataset using a [distance function](distance-functions.md). +1. Finding Neighbors: The algorithm identifies the k closest data points (neighbors) to the query point based on the calculated distances. The value of k (the number of neighbors) is crucial. A small k can be sensitive to noise, while a large k can smooth out details. +1. Making Predictions: - Classification: For classification tasks, kNN assigns the class label to the query point that is most common among the k neighbors. Essentially, it performs a "majority vote." - Regression: For regression tasks, kNN predicts the value for the query point as the average (or sometimes weighted average) of the values of the k neighbors. ## How ANN works 1. Vectorization: Each data point in the dataset is represented as a vector in a multi-dimensional space. -2. Indexing and Data Structures: ANN algorithms use advanced data structures (e.g., KD-trees, locality-sensitive hashing, or graph-based methods) to index the data points, allowing for faster searches. -3. Distance Calculation: Instead of calculating the exact distance to every point, ANN algorithms use heuristics to quickly identify regions of the space that are likely to contain the nearest neighbors. -4. Finding Neighbors: The algorithm identifies a set of data points that are likely to be close to the query point. These neighbors are not guaranteed to be the exact closest points but are close enough for practical purposes. -4. Making Predictions: +1. Indexing and Data Structures: ANN algorithms use advanced data structures (e.g., KD-trees, locality-sensitive hashing, or graph-based methods) to index the data points, allowing for faster searches. +1. Distance Calculation: Instead of calculating the exact distance to every point, ANN algorithms use heuristics to quickly identify regions of the space that are likely to contain the nearest neighbors. +1. Finding Neighbors: The algorithm identifies a set of data points that are likely to be close to the query point. These neighbors are not guaranteed to be the exact closest points but are close enough for practical purposes. +1. Making Predictions: - Classification: For classification tasks, ANN assigns the class label to the query point that is most common among the identified neighbors, similar to kNN. - Regression: For regression tasks, ANN predicts the value for the query point as the average (or weighted average) of the values of the identified neighbors. ## Related content + - [What is a vector database?](../vector-database.md) - [Retrieval Augmented Generation (RAG)](rag.md) - [Vector database in Azure Cosmos DB NoSQL](../nosql/vector-search.md) diff --git a/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md b/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md index 4ecec3da96..d6b22f08cd 100644 --- a/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md +++ b/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md @@ -1,35 +1,39 @@ --- - -title: Quickstart - Build a RAG Chatbot -description: Learn how to build a RAG chatbot in Python +title: Build a RAG Chatbot +titleSuffix: Azure Cosmos DB for NoSQL +description: Build a retrieval augmented generation (RAG) chatbot in Python using Azure Cosmos DB for NoSQL's vector search capabilities. author: TheovanKraay ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 06/26/2024 +ms.date: 12/03/2024 ms.author: thvankra ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- -# Quickstart - Build a RAG chatbot with Azure Cosmos DB NoSQL API - -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] +# Build a RAG chatbot with Azure Cosmos DB NoSQL API -In this quickstart, we demonstrate how to build a [RAG Pattern](../gen-ai/rag.md) application using a subset of the Movie Lens dataset. This sample leverages the Python SDK for Azure Cosmos DB for NoSQL to perform vector search for RAG, store and retrieve chat history, and store vectors of the chat history to use as a semantic cache. Azure OpenAI is used to generate embeddings and Large Language Model (LLM) completions. +In this guide, we demonstrate how to build a [RAG Pattern](../gen-ai/rag.md) application using a subset of the Movie Lens dataset. This sample leverages the Python SDK for Azure Cosmos DB for NoSQL to perform vector search for RAG, store and retrieve chat history, and store vectors of the chat history to use as a semantic cache. Azure OpenAI is used to generate embeddings and Large Language Model (LLM) completions. At the end, we create a simple UX using Gradio to allow users to type in questions and display responses generated by Azure OpenAI or served from the cache. The responses will also display an elapsed time to show the impact caching has on performance versus generating a response. -> [!TIP] +> [!TIP] > You can find the full Python notebook sample [here](https://aka.ms/CosmosPythonRAGQuickstart). +> > For more RAG samples, visit: [AzureDataRetrievalAugmentedGenerationSamples](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples) -**Important Note**: This sample requires you to setup accounts for Azure Cosmos DB for NoSQL and Azure OpenAI. To get started, visit: -- [Azure Cosmos DB for NoSQL Python Quickstart](../nosql/quickstart-python.md) -- [Azure Cosmos DB for NoSQL Vector Search](../nosql/vector-search.md) -- [Azure OpenAI](/azure/ai-services/openai) +> [!IMPORTANT] +> This sample requires you to setup accounts for Azure Cosmos DB for NoSQL and Azure OpenAI. To get started, visit: +> +> - [Azure Cosmos DB for NoSQL Python Quickstart](../nosql/quickstart-python.md) +> - [Azure Cosmos DB for NoSQL Vector Search](../nosql/vector-search.md) +> - [Azure OpenAI](/azure/ai-services/openai) +> -### 1. Install Required Packages +## 1. Install Required Packages Install the necessary Python packages to interact with Azure Cosmos DB and other services. @@ -45,7 +49,7 @@ Install the necessary Python packages to interact with Azure Cosmos DB and other ! pip install --user azure-cosmos ``` -### 2. Initialize Your Client Connection +## 2. Initialize Your Client Connection Populate `sample_env_file.env` with the appropriate credentials for Azure Cosmos DB and Azure OpenAI. @@ -116,7 +120,7 @@ storage_file_url = config['storage_file_url'] openai_client = AzureOpenAI(azure_endpoint=openai_endpoint, api_key=openai_key, api_version=openai_api_version) ``` -### 3. Create a Database and Containers with Vector Policies +## 3. Create a Database and Containers with Vector Policies This function takes a database object, a collection name, the name of the document property that stores vectors, and the number of vector dimensions used for the embeddings. @@ -181,7 +185,7 @@ except exceptions.CosmosHttpResponseError: raise ``` -### 4. Generate Embeddings from Azure OpenAI +## 4. Generate Embeddings from Azure OpenAI This function vectorizes the user input for vector search. Ensure the dimensionality and model used match the sample data provided, or else regenerate vectors with your desired model. @@ -204,7 +208,7 @@ def generate_embeddings(text): raise ``` -### 5. Load Data from the JSON File +## 5. Load Data from the JSON File Extract the prevectorized MovieLens dataset from the zip file (see it's location in notebook repo [here](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples/tree/main/DataSet/Movies)). @@ -223,7 +227,7 @@ with open('/Data/MovieLens-4489-256D.json', 'r') as d: len(data) ``` -### 6. Store Data in Azure Cosmos DB +## 6. Store Data in Azure Cosmos DB Upsert data into Azure Cosmos DB for NoSQL. Records are written asynchronously. @@ -305,8 +309,7 @@ async def insert_data(vectorize=False): await insert_data(vectorizeFlag) # or await insert_data() for default ``` - -### 7. Perform Vector Search +## 7. Perform Vector Search This function defines a vector search over the movies data and chat cache collections. @@ -333,7 +336,7 @@ def vector_search(container, vectors, similarity_score=0.02, num_results=5): return formatted_results ``` -### 8. Get Recent Chat History +## 8. Get Recent Chat History This function provides conversational context to the LLM, allowing it to better have a conversation with the user. @@ -354,7 +357,7 @@ def get_chat_history(container, completions=3): return results ``` -### 9. Chat Completion Functions +## 9. Chat Completion Functions Define the functions to handle the chat completion process, including caching responses. @@ -412,7 +415,7 @@ def chat_completion(cache_container, movies_container, user_input): return completions_results['choices'][0]['message']['content'], False ``` -### 10. Cache Generated Responses +## 10. Cache Generated Responses Save the user prompts and generated completions to the cache for faster future responses. @@ -431,7 +434,7 @@ def cache_response(container, user_prompt, prompt_vectors, response): container.create_item(body=chat_document) ``` -### 11. Create a Simple UX in Gradio +## 11. Create a Simple UX in Gradio Build a user interface using Gradio for interacting with the AI application. @@ -466,16 +469,16 @@ demo.launch(debug=True) demo.close() ``` -### Next Steps +## Vector Database Solutions + +[Azure PostgreSQL Server pgvector Extension](../../postgresql/flexible-server/how-to-use-pgvector.md) -This quickstart guide is designed to help you set up and get running with Azure Cosmos DB NoSQL API for vector search applications in a few simple steps. If you have any further questions or need assistance, check out these resources: +## Related content - [30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) - [90-day Free Trial and up to $6,000 in throughput credits with Azure AI Advantage](../ai-advantage.md) +## Next step + > [!div class="nextstepaction"] > [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) - -### More Vector Database Solutions - -- [Azure PostgreSQL Server pgvector Extension](../../postgresql/flexible-server/how-to-use-pgvector.md) diff --git a/articles/cosmos-db/gen-ai/rag.md b/articles/cosmos-db/gen-ai/rag.md index f9f1b5506f..1305c8456c 100644 --- a/articles/cosmos-db/gen-ai/rag.md +++ b/articles/cosmos-db/gen-ai/rag.md @@ -1,62 +1,67 @@ --- - -title: Retrieval Augmented Generation (RAG) in Azure Cosmos DB -description: Learn about Retrieval Augmented Generation (RAG) in Azure Cosmos DB +title: Retrieval augmented generation +titleSuffix: Azure Cosmos DB +description: Learn about retrieval augmented generation (RAG) in the context of Azure Cosmos DB for NoSQL's vector search capabilities. author: TheovanKraay ms.service: azure-cosmos-db ms.subservice: nosql -ms.topic: conceptual -ms.date: 07/09/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.author: thvankra ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- # Retrieval Augmented Generation (RAG) in Azure Cosmos DB -Retrieval Augmented Generation (RAG) combines the power of large language models (LLMs) with robust information retrieval systems to create more accurate and contextually relevant responses. Unlike traditional generative models that rely solely on pre-trained data, RAG architectures enhance an LLM's capabilities by integrating real-time information retrieval. This augmentation ensures responses are not only generative but also grounded in the most relevant, up-to-date data available. +Retrieval Augmented Generation (RAG) combines the power of large language models (LLMs) with robust information retrieval systems to create more accurate and contextually relevant responses. Unlike traditional generative models that rely solely on pre-trained data, RAG architectures enhance an LLM's capabilities by integrating real-time information retrieval. This augmentation ensures responses are not only generative but also grounded in the most relevant, up-to-date data available. Azure Cosmos DB, an operational database that supports vector search, stands out as an excellent platform for implementing RAG. Its ability to handle both operational and analytical workloads in a single database, along with advanced features such as multitenancy and hierarchical partition keys, provides a solid foundation for building sophisticated generative AI applications. ## Key Advantages of Using Azure Cosmos DB ### Unified data storage and retrieval + Azure Cosmos DB enables seamless integration of [vector search](../nosql/vector-search.md) capabilities within a unified database system. This means that your operational data and vectorized data coexist, eliminating the need for separate indexing systems. ### Real-Time data ingestion and querying + Azure Cosmos DB supports real-time ingestion and querying, making it ideal for AI applications. This is crucial for RAG architectures, where the freshness of data can significantly impact the relevance of generated responses. ### Scalability and global distribution + Designed for large-scale applications, Azure Cosmos DB offers global distribution and [instant autoscale](../../cosmos-db/provision-throughput-autoscale.md). This ensures that your RAG-enabled application can handle high query volumes and deliver consistent performance irrespective of user location. ### High availability and reliability + Azure Cosmos DB offers comprehensive SLAs for throughput, latency, and [availability](/azure/reliability/reliability-cosmos-db-nosql). This reliability ensures that your RAG system is always available to generate responses with minimal downtime. ### Multitenancy with hierarchical partition keys + Azure Cosmos DB supports [multitenancy](../nosql/multi-tenancy-vector-search.md) through various performance and security isolation models, making it easier to manage data for different clients or user groups within the same database. This feature is particularly useful for SaaS applications where separation of tenant data is crucial for security and compliance. ### Comprehensive security features -With built-in features such as end-to-end encryption, role-based access control (RBAC), and virtual network (VNet) integration, Azure Cosmos DB ensures that your data remains secure. These security measures are essential for enterprise-grade RAG applications that handle sensitive information. - +With built-in features such as end-to-end encryption, role-based access control (RBAC), and virtual network (VNet) integration, Azure Cosmos DB ensures that your data remains secure. These security measures are essential for enterprise-grade RAG applications that handle sensitive information. ## Implementing RAG with Azure Cosmos DB -> [!TIP] +> [!TIP] > For RAG samples, visit: [AzureDataRetrievalAugmentedGenerationSamples](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples) Here's a streamlined process for building a RAG application with Azure Cosmos DB: 1. **Data Ingestion**: Store your documents, images, and other content types in Azure Cosmos DB. Utilize the database's support for vector search to index and retrieve vectorized content. - -2. **Query Execution**: When a user submits a query, Azure Cosmos DB can quickly retrieve the most relevant data using its vector search capabilities. - -3. **LLM Integration**: Pass the retrieved data to an LLM (e.g., Azure OpenAI) to generate a response. The well-structured data provided by Cosmos DB enhances the quality of the model's output. - -4. **Response Generation**: The LLM processes the data and generates a comprehensive response, which is then delivered to the user. - +1. **Query Execution**: When a user submits a query, Azure Cosmos DB can quickly retrieve the most relevant data using its vector search capabilities. +1. **LLM Integration**: Pass the retrieved data to an LLM (e.g., Azure OpenAI) to generate a response. The well-structured data provided by Cosmos DB enhances the quality of the model's output. +1. **Response Generation**: The LLM processes the data and generates a comprehensive response, which is then delivered to the user. ## Related content + - [What is a vector database?](../vector-database.md) - [Vector database in Azure Cosmos DB NoSQL](../nosql/vector-search.md) - [Vector database in Azure Cosmos DB for MongoDB](../mongodb/vcore/vector-search.md) @@ -64,4 +69,4 @@ Here's a streamlined process for building a RAG application with Azure Cosmos DB - Vector [embeddings](vector-embeddings.md) - [Distance functions](distance-functions.md) - [kNN vs ANN vector search algorithms](knn-vs-ann.md) -- [Multitenancy for Vector Search](../nosql/multi-tenancy-vector-search.md) \ No newline at end of file +- [Multitenancy for Vector Search](../nosql/multi-tenancy-vector-search.md) diff --git a/articles/cosmos-db/gen-ai/semantic-cache.md b/articles/cosmos-db/gen-ai/semantic-cache.md index 8e2be63611..a1b79ce487 100644 --- a/articles/cosmos-db/gen-ai/semantic-cache.md +++ b/articles/cosmos-db/gen-ai/semantic-cache.md @@ -1,15 +1,19 @@ --- -title: Semantic cache +title: Semantic cache for large language models titleSuffix: Azure Cosmos DB description: Learn how semantic cache, in Azure Cosmos DB, provides a way for you to re-use past prompts and completions to address similar prompts using vector similarity. author: markjbrown ms.service: azure-cosmos-db ms.subservice: nosql -ms.topic: conceptual -ms.date: 08/21/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.author: mjbrown ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- # Introduction to semantic cache @@ -72,14 +76,14 @@ There are multiple samples you can use to understand how to build your own seman - [Build a Copilot app using Azure Cosmos DB for NoSQL](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot) -This C# sample demonstrates many of the concepts necessary to build your own Copilot applications in Azure using Azure Cosmos DB for NoSQL. This sample also comes with a [Hands-On-Lab](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot/tree/start?tab=readme-ov-file#hands-on-lab-to-build-a-copilot-app-using-azure-cosmos-db-for-nosql-azure-openai-service-azure-app-service-and-semantic-kernel) that walks users step-by-step through these concepts, including [how to implement a semantic cache](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot/blob/start/lab/lab-guide.md#exercise--implement-a-semantic-cache). +This C# sample demonstrates many of the concepts necessary to build your own Copilot applications in Azure using Azure Cosmos DB for NoSQL. This sample also comes with a [Hands-On-Lab](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot/tree/start?tab=readme-ov-file#hands-on-lab-to-build-a-copilot-app-using-azure-cosmos-db-for-nosql-azure-openai-service-azure-app-service-and-semantic-kernel) that walks users step-by-step through these concepts, including [how to implement a semantic cache](https://github.com/AzureCosmosDB/cosmosdb-nosql-copilot/blob/start/lab/lab-guide.md#exercise--implement-a-semantic-cache). - [Build a Copilot app using Azure Cosmos DB for MongoDB](https://github.com/AzureCosmosDB/cosmosdb-mongo-copilot) -This C# sample demonstrates many of the concepts necessary to build your own Copilot applications in Azure using Azure Cosmos DB for MongoDB. This sample also comes with a [Hands-On-Lab](https://github.com/AzureCosmosDB/cosmosdb-mongo-copilot/tree/start?tab=readme-ov-file#hands-on-lab-to-build-a-copilot-app-with-azure-cosmos-db-for-mongodb-azure-openai-service-and-semantic-kernel) that walks users step-by-step through these concepts, including [how to implement a semantic cache](https://github.com/AzureCosmosDB/cosmosdb-mongo-copilot/blob/start/docs/LABGuide.md#exercise--implement-a-semantic-cache). - +This C# sample demonstrates many of the concepts necessary to build your own Copilot applications in Azure using Azure Cosmos DB for MongoDB. This sample also comes with a [Hands-On-Lab](https://github.com/AzureCosmosDB/cosmosdb-mongo-copilot/tree/start?tab=readme-ov-file#hands-on-lab-to-build-a-copilot-app-with-azure-cosmos-db-for-mongodb-azure-openai-service-and-semantic-kernel) that walks users step-by-step through these concepts, including [how to implement a semantic cache](https://github.com/AzureCosmosDB/cosmosdb-mongo-copilot/blob/start/docs/LABGuide.md#exercise--implement-a-semantic-cache). ## Related content + - [What is a vector database?](../vector-database.md) - [Vector database in Azure Cosmos DB NoSQL](../nosql/vector-search.md) - [Vector database in Azure Cosmos DB for MongoDB](../mongodb/vcore/vector-search.md) @@ -87,4 +91,4 @@ This C# sample demonstrates many of the concepts necessary to build your own Cop - Vector [embeddings](vector-embeddings.md) - [Distance functions](distance-functions.md) - [kNN vs ANN vector search algorithms](knn-vs-ann.md) -- [Multitenancy for Vector Search](../nosql/multi-tenancy-vector-search.md) \ No newline at end of file +- [Multitenancy for Vector Search](../nosql/multi-tenancy-vector-search.md) From 82671314faa1a068c6827f09c4c7a7fbbc64d34b Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:16:21 -0500 Subject: [PATCH 04/14] Refresh AI articles (batch 1) --- .../cosmos-db/.openpublishing.redirection.cosmos-db.json | 5 +++++ articles/cosmos-db/gen-ai/cosmos-ai-graph.md | 4 ++-- .../gen-ai/{quickstart-rag-chatbot.md => rag-chatbot.md} | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) rename articles/cosmos-db/gen-ai/{quickstart-rag-chatbot.md => rag-chatbot.md} (98%) diff --git a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json index 9e2b42bd1b..d667c8ff7b 100644 --- a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json +++ b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json @@ -5974,6 +5974,11 @@ "source_path_from_root": "/articles/cosmos-db/table/samples-dotnet.md", "redirect_url": "/azure/cosmos-db/table/quickstart-dotnet", "redirect_document_id": false + }, + { + "source_path_from_root": "/articlers/cosmos-db/gen-ai/quickstart-rag-chatbot.md", + "redirect_url": "/azure/cosmos-db/gen-ai/rag-chatbot.md", + "redirect_document_id": true } ] } diff --git a/articles/cosmos-db/gen-ai/cosmos-ai-graph.md b/articles/cosmos-db/gen-ai/cosmos-ai-graph.md index df01fec53f..acad9e371e 100644 --- a/articles/cosmos-db/gen-ai/cosmos-ai-graph.md +++ b/articles/cosmos-db/gen-ai/cosmos-ai-graph.md @@ -17,7 +17,7 @@ appliesto: [CosmosAIGraph](https://aka.ms/cosmosaigraph) is an innovative solution that applies the power of Azure Cosmos DB to create AI-powered knowledge graphs. This technology integrates advanced graph database capabilities with AI to provide a robust platform for managing and querying complex data relationships. By utilizing Cosmos DB's scalability and performance in both document and vector form, Cosmos AI Graph enables the creation of sophisticated data models that can answer various data questions and uncover hidden relationships and concepts in semi-structured data. -## What types of questions can knowledge graphs help to answer +## Questions knowledge graphs help to answer - **Complex Relationship Queries**: - *Question*: "What are the direct and indirect connections between Person A and Person B within a social network?" @@ -37,7 +37,7 @@ When it comes to [Retrieval Augmented Generation (RAG)](rag.md), combining **Kno ## OmniRAG -CosmosAIGraph features *OmniRAG*, a versatile approach to data retrieval that dynamically selects the most suitable method — be it database queries, vector matching, or knowledge graph traversal — to answer user queries effectively and with utmost accuracy, as it likely will gather more context and more authoritative conext than any one of these sources could on its own. The key to this dynamic selection is the user intent - determined from the user question using simple utterance analysis and/or AI. This ensures that each query is addressed using the optimal technique, enhancing accuracy and efficiency. For instance, a user query about hierarchical relationships would utilize graph traversal, while a query about similar documents would employ vector search, all within a unified framework provided by CosmosAIGraph. Moreover, with the help of orchestration of within RAG process, more than one source could be used to collect the context for AI, for example the graph could be consulted with first and then for each of the entities found the actual database records could be pulled as well and if no results were found, vector searh would likely return closely matching results. This holistic approach maximizes the strengths of each retrieval method, delivering comprehensive and contextually relevant answers. +CosmosAIGraph features *OmniRAG*, a versatile approach to data retrieval that dynamically selects the most suitable method — be it database queries, vector matching, or knowledge graph traversal — to answer user queries effectively and with utmost accuracy, as it likely will gather more context and more authoritative conext than any one of these sources could on its own. The key to this dynamic selection is the user intent - determined from the user question using simple utterance analysis and/or AI. This ensures that each query is addressed using the optimal technique, enhancing accuracy and efficiency. For instance, a user query about hierarchical relationships would utilize graph traversal, while a query about similar documents would employ vector search, all within a unified framework provided by CosmosAIGraph. Moreover, with the help of orchestration of within RAG process, more than one source could be used to collect the context for AI, for example the graph could be consulted with first and then for each of the entities found the actual database records could be pulled as well and if no results were found, vector search would likely return closely matching results. This holistic approach maximizes the strengths of each retrieval method, delivering comprehensive and contextually relevant answers. ### Example user questions and strategy used diff --git a/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md b/articles/cosmos-db/gen-ai/rag-chatbot.md similarity index 98% rename from articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md rename to articles/cosmos-db/gen-ai/rag-chatbot.md index d6b22f08cd..5831fa35c9 100644 --- a/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md +++ b/articles/cosmos-db/gen-ai/rag-chatbot.md @@ -26,7 +26,7 @@ At the end, we create a simple UX using Gradio to allow users to type in questio > For more RAG samples, visit: [AzureDataRetrievalAugmentedGenerationSamples](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples) > [!IMPORTANT] -> This sample requires you to setup accounts for Azure Cosmos DB for NoSQL and Azure OpenAI. To get started, visit: +> This sample requires you to set up accounts for Azure Cosmos DB for NoSQL and Azure OpenAI. To get started, visit: > > - [Azure Cosmos DB for NoSQL Python Quickstart](../nosql/quickstart-python.md) > - [Azure Cosmos DB for NoSQL Vector Search](../nosql/vector-search.md) @@ -210,7 +210,7 @@ def generate_embeddings(text): ## 5. Load Data from the JSON File -Extract the prevectorized MovieLens dataset from the zip file (see it's location in notebook repo [here](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples/tree/main/DataSet/Movies)). +Extract the prevectorized MovieLens dataset from the zip file (see its location in notebook repo [here](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples/tree/main/DataSet/Movies)). ```python # Unzip the data file From 14b583712928d92a6f82c6b0a1976bf1cde4a0cd Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:18:16 -0500 Subject: [PATCH 05/14] Rework cross-region replica article to how-to guide --- articles/cosmos-db/mongodb/vcore/TOC.yml | 4 +- ... => how-to-cross-region-replica-portal.md} | 177 ++++++++---------- ...luster-networking-adding-firewall-rule.png | Bin .../configure-cluster.png | Bin ...l-distribution-page-on-primary-cluster.png | Bin .../global-distribution-tab.png | Bin .../networking-adding-firewall-rule.png | Bin ...replica-cluster-promotion-confirmation.png | Bin .../replica-cluster-promotion.png | Bin .../select-connection-strings-option.png | Bin .../what-is-my-ip.png | Bin 11 files changed, 80 insertions(+), 101 deletions(-) rename articles/cosmos-db/mongodb/vcore/{quickstart-cross-region-replica-portal.md => how-to-cross-region-replica-portal.md} (66%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/cluster-networking-adding-firewall-rule.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/configure-cluster.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/global-distribution-page-on-primary-cluster.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/global-distribution-tab.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/networking-adding-firewall-rule.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/replica-cluster-promotion-confirmation.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/replica-cluster-promotion.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/select-connection-strings-option.png (100%) rename articles/cosmos-db/mongodb/vcore/media/{quickstart-cross-region-replication => how-to-cross-region-replication-portal}/what-is-my-ip.png (100%) diff --git a/articles/cosmos-db/mongodb/vcore/TOC.yml b/articles/cosmos-db/mongodb/vcore/TOC.yml index 7d2bd9bfc0..27815bb81b 100644 --- a/articles/cosmos-db/mongodb/vcore/TOC.yml +++ b/articles/cosmos-db/mongodb/vcore/TOC.yml @@ -14,8 +14,6 @@ href: quickstart-bicep.md - name: Create resources - Terraform href: quickstart-terraform.md - - name: Create cross-region replica - Azure portal - href: quickstart-cross-region-replica-portal.md - name: Tutorials items: - name: Build web applications @@ -84,6 +82,8 @@ href: /azure-data-studio/extensions/database-migration-for-mongo-extension - name: How to items: + - name: Create cross-region replica using Azure portal + href: cross-region-replica-portal.md - name: Use community tools items: - name: Connect using Studio 3T diff --git a/articles/cosmos-db/mongodb/vcore/quickstart-cross-region-replica-portal.md b/articles/cosmos-db/mongodb/vcore/how-to-cross-region-replica-portal.md similarity index 66% rename from articles/cosmos-db/mongodb/vcore/quickstart-cross-region-replica-portal.md rename to articles/cosmos-db/mongodb/vcore/how-to-cross-region-replica-portal.md index ad4d91c9bc..0c00512ce5 100644 --- a/articles/cosmos-db/mongodb/vcore/quickstart-cross-region-replica-portal.md +++ b/articles/cosmos-db/mongodb/vcore/how-to-cross-region-replica-portal.md @@ -1,8 +1,7 @@ --- -title: | - Quickstart: Create and us a cross-region replica in Azure Cosmos DB for MongoDB vCore +title: Create and use a cross-region replica titleSuffix: Azure Cosmos DB for MongoDB vCore -description: In this quickstart, create a new Azure Cosmos DB for MongoDB vCore cluster replica in another region for disaster recovery (DR) and read scaling purposes in the Azure portal. +description: Create a new Azure Cosmos DB for MongoDB vCore cluster replica in another region for disaster recovery (DR) and read scaling purposes in the Azure portal. author: niklarin ms.author: nlarin ms.service: azure-cosmos-db @@ -10,14 +9,14 @@ ms.subservice: mongodb-vcore ms.custom: - ignite-2024 ms.topic: how-to -ms.date: 11/06/2024 +ms.date: 12/03/2024 +appliesto: + - ✅ MongoDB vCore --- -# Quickstart: Create and use a cross-region cluster replica in Azure Cosmos DB for MongoDB vCore +# Create and use a cross-region cluster replica in Azure Cosmos DB for MongoDB vCore -[!INCLUDE[MongoDB vCore](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)] - -In this quickstart, you create a cluster replica in another region for an Azure Cosmos DB for MongoDB vCore cluster for disaster recovery (DR) purposes. This replica cluster stores a copy of all of your MongoDB resources - databases, collections, and documents - in another Azure region. The replica cluster provides a unique endpoint for various tools and SDKs to connect to and could be promoted to become available for writes if there's a primary region outage. +In this guide, you create a cluster replica in another region for an Azure Cosmos DB for MongoDB vCore cluster for disaster recovery (DR) purposes. This replica cluster stores a copy of all of your MongoDB resources - databases, collections, and documents - in another Azure region. The replica cluster provides a unique endpoint for various tools and SDKs to connect to and could be promoted to become available for writes if there's a primary region outage. ## Prerequisites @@ -58,7 +57,7 @@ Create a MongoDB cluster with a cluster read replica in another region by using | **Storage** | 128 GiB | 1. Unselect the **High availability** option. In the high availability (HA) acknowledgment section, select **I understand**. Finally, select **Save** to persist your changes to the cluster configuration. - + In-region high availability provides an in-region solution where a copy of data from each shard in a cluster is streamed to its standby counterpart located in the same region but in a different availability zone (AZ). High availability uses synchronous replication with zero data loss and automatic failure detection and failover while preserving the connection string intact after failover. High availability might be enabled on the primary cluster for an additional layer of protection from failures. :::image type="content" source="media/quickstart-portal/configure-scale.png" alt-text="Screenshot of cluster tier and scale options for a cluster."::: @@ -75,7 +74,7 @@ Create a MongoDB cluster with a cluster read replica in another region by using | Admin username | Provide a username to access the cluster | This user is created on the cluster as a user administrator. | | Password | Use a unique password to pair with the username | Password must be at least 8 characters and at most 128 characters. | - :::image type="content" source="media/quickstart-cross-region-replication/configure-cluster.png" alt-text="Screenshot of various configuration options for a cluster."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/configure-cluster.png" alt-text="Screenshot of various configuration options for a cluster."::: 1. Select **Next: Global distribution**. @@ -85,17 +84,17 @@ Create a MongoDB cluster with a cluster read replica in another region by using 1. Select a value from the **Read replica region** drop-down list. - :::image type="content" source="media/quickstart-cross-region-replication/global-distribution-tab.png" alt-text="Screenshot of the global distribution tab in cluster provisioning."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/global-distribution-tab.png" alt-text="Screenshot of the global distribution tab in cluster provisioning."::: 1. Select **Next: Networking**. -1. On the **Networking** tab, select **Add current client IP address** to create a firewall rule with the public IP address of your computer, as perceived by the Azure system. +1. On the **Networking** tab, select **Add current client IP address** to create a firewall rule with the public IP address of your computer, as perceived by the Azure system. - :::image type="content" source="media/quickstart-cross-region-replication/networking-adding-firewall-rule.png" alt-text="Screenshot of networking settings."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/networking-adding-firewall-rule.png" alt-text="Screenshot of the networking settings for a cluster."::: Verify your IP address before saving this configuration. In some situations, the IP address observed by Azure portal differs from the IP address used when accessing the Internet and Azure services. Thus, you may need to change the start IP and end IP to make the rule function as expected. Use a search engine or other online tool to check your own IP address. For example, search for *what is my IP*. - :::image type="content" source="media/quickstart-cross-region-replication/what-is-my-ip.png" alt-text="Screenshot of a web search result for the current host's public IP address."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/what-is-my-ip.png" alt-text="Screenshot of a web search result for the current host's public IP address."::: You can also select add 0.0.0.0 - 255.255.255.255 firewall rule to allow not just your IP, but the whole Internet to access the cluster. In this situation, clients still must log in with the correct username and password to use the cluster. Nevertheless, it's best to allow worldwide access for only short periods of time and for only non-production databases. @@ -105,7 +104,7 @@ You can also select add 0.0.0.0 - 255.255.255.255 firewall rule to allow not jus 1. Select **Go to resource** to go to the Azure Cosmos DB for MongoDB cluster page. - :::image type="content" source="media/quickstart-portal/deployment-complete.png" alt-text="Screenshot of the deployment page for a cluster."::: + :::image type="content" source="media/quickstart-portal/deployment-complete.png" alt-text="Screenshot of the deployment page for a cluster."::: ## Connect to primary cluster and ingest data @@ -113,59 +112,59 @@ Get the connection string you need to connect to the primary (read-write) cluste 1. From the Azure Cosmos DB for MongoDB vCore primary cluster page, select the **Connection strings** navigation menu option under **Settings**. - :::image type="content" source="media/quickstart-cross-region-replication/select-connection-strings-option.png" alt-text="Screenshot of the connection strings page in the cluster properties."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/select-connection-strings-option.png" alt-text="Screenshot of the connection strings page in the cluster properties."::: 1. Copy the value from the **Self (always this cluster)** field. - + 1. In command line, use the MongoDB shell to connect to the primary cluster using the connection string. -```cmd +```shell mongosh "mongodb+srv://@.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" ``` ### Ingest data -Create a *my_script.js* script file to run it from the MongoDB shell. - -```JavaScript - let dogDocs = [ - { - name: "pooch", - breed: "poodle", - weight: "6 lbs" - }, - { - name: "mutt", - breed: "bulldog", - weight: "10 lbs" - } - ]; - - let catDocs = [ - { - name: "minni", - breed: "persian", - color: "white" - }, - { - name: "tinkle", - breed: "bombay", - color: "black" - } - ]; - - let dogIndex = { name : 1 }; - let catIndex = { name : 1 }; - - let collInfoObjs = [ - { coll: "dogs", data: dogDocs, index: dogIndex }, - { coll: "cats", data: catDocs, index: catIndex } - ]; - - for (obj of collInfoObjs) { - db[obj.coll].insertMany(obj.data); - db[obj.coll].createIndex(obj.index); - } +Create a *my_script.js* script file to run it from the MongoDB shell. + +```javascript +let dogDocs = [ + { + name: "pooch", + breed: "poodle", + weight: "6 lbs" + }, + { + name: "mutt", + breed: "bulldog", + weight: "10 lbs" + } +]; + +let catDocs = [ + { + name: "minni", + breed: "persian", + color: "white" + }, + { + name: "tinkle", + breed: "bombay", + color: "black" + } +]; + +let dogIndex = { name : 1 }; +let catIndex = { name : 1 }; + +let collInfoObjs = [ + { coll: "dogs", data: dogDocs, index: dogIndex }, + { coll: "cats", data: catDocs, index: catIndex } +]; + +for (obj of collInfoObjs) { + db[obj.coll].insertMany(obj.data); + db[obj.coll].createIndex(obj.index); +} ``` This script file creates two collections and inserts documents with data into those collections. @@ -173,17 +172,17 @@ Save my_script.js file in a folder accessible to the MongoDB shell session. Run the script from the MongoDB shell connected to the primary MongoDB cluster. -```MongoDB Shell +```mongodb load(my_script.js); ``` In the MongoDB shell connected to the primary MongoDB cluster, read data from the database. -```MongoDB Shell +```mongodb db.dogs.find(); db.cats.find(); ``` - + ## Enable access to replica cluster > [!IMPORTANT] @@ -191,16 +190,16 @@ db.cats.find(); 1. From the Azure Cosmos DB for MongoDB vCore *primary* cluster page, select the **Global distribution** page under **Settings**. - :::image type="content" source="media/quickstart-cross-region-replication/global-distribution-page-on-primary-cluster.png" alt-text="Screenshot of the global distribution page in the primary cluster properties."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/global-distribution-page-on-primary-cluster.png" alt-text="Screenshot of the global distribution page in the primary cluster properties."::: 1. Select *cluster replica name* in the **Read replica** field to open the read cluster replica properties in the Azure portal. - -1. On the MongoDB vCore replica cluster page, under **Settings**, select **Networking**. -1. On the **Networking** page, select **Add current client IP address** to create a firewall rule with the public IP address of your computer, as perceived by the Azure system. +1. On the MongoDB vCore replica cluster page, under **Settings**, select **Networking**. - :::image type="content" source="media/quickstart-cross-region-replication/cluster-networking-adding-firewall-rule.png" alt-text="Screenshot of the networking page on read replica cluster."::: +1. On the **Networking** page, select **Add current client IP address** to create a firewall rule with the public IP address of your computer, as perceived by the Azure system. + :::image type="content" source="media/how-to-cross-region-replication-portal/cluster-networking-adding-firewall-rule.png" alt-text="Screenshot of the networking page on read replica cluster."::: + Verify your IP address before saving this configuration. In some situations, the IP address observed by Azure portal differs from the IP address used when accessing the Internet and Azure services. You can also select add 0.0.0.0 - 255.255.255.255 firewall rule to allow not just your IP, but the whole Internet to access the cluster. In this situation, clients still must log in with the correct username and password to use the cluster. 1. Select **Save** on the toolbar to save the settings. It might take a few minutes for the updated networking settings to become effective. @@ -212,21 +211,21 @@ Get the connection string for the read cluster replica in another region. 1. On the replica cluster sidebar, under **Cluster management**, select **Connection strings**. 1. Copy the value from the **Connection string** field. - + > [!IMPORTANT] - > The connection string of the read replica cluster contains unique *replica cluster name* that you selected during replica creation. The username and password values for the read replica cluster are always the same as the ones on its primary cluster. + > The connection string of the read replica cluster contains unique *replica cluster name* that you selected during replica creation. The username and password values for the read replica cluster are always the same as the ones on its primary cluster. 1. In command line, use the MongDB shell to connect to the read replica cluster using its connection string. -```cmd -mongosh "mongodb+srv://@.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" -``` + ```shell + mongosh "mongodb+srv://@.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" + ``` ### Read data from replica cluster In the MongoDB shell connected to the replica cluster, read data from the database. -```MongoDB Shell +```mongodb db.dogs.find(); db.cats.find(); ``` @@ -241,11 +240,11 @@ To promote a cluster read replica to a read-write cluster, follow these steps: 1. On the **Global distribution** page, select **Promote** on the toolbar to initiate read replica promotion to read-write cluster. - :::image type="content" source="media/quickstart-cross-region-replication/replica-cluster-promotion.png" alt-text="Screenshot of the read replica cluster global distribution page with the promote button."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/replica-cluster-promotion.png" alt-text="Screenshot of the read replica cluster global distribution page with the promote button."::: 1. In the **Promote cluster** pop-up window, confirm that you understand how replica promotion works, and select **Promote**. Replica promotion might take a few minutes to complete. - :::image type="content" source="media/quickstart-cross-region-replication/replica-cluster-promotion-confirmation.png" alt-text="Screenshot of the read replica cluster global distribution page with the promote confirmation pop-up window."::: + :::image type="content" source="media/how-to-cross-region-replication-portal/replica-cluster-promotion-confirmation.png" alt-text="Screenshot of the read replica cluster global distribution page with the promote confirmation pop-up window."::: ### Write to promoted cluster replica @@ -253,49 +252,29 @@ Once replica promotion is completed, the promoted replica becomes available for Use the MongDB shell in command line to connect to *the promoted replica cluster* using its connection string. -```cmd +```shell mongosh "mongodb+srv://@.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" ``` In the MongoDB shell session, perform a write operation. -```MongoDB Shell +```mongodb db.createCollection('foxes') ``` Use the MongDB shell in command line to connect to *the new replica cluster* (former primary cluster) using its connection string. You can use self connection string or the global read-write connection string. -```cmd +```shell mongosh "mongodb+srv://@.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000" ``` In the MongoDB shell, confirm that writes are now disabled on the new replica (former primary cluster). -```MongoDB Shell +```mongodb db.createCollection('bears') ``` -## Clean up resources - -When you're done with Azure Cosmos DB for MongoDB vCore cluster, you can delete the Azure resources you created so you don't incur more charges. - -1. In the Azure portal search bar, search for and select **Resource groups**. - -1. In the list, select the resource group you used for this quickstart. - - :::image type="content" source="media/quickstart-portal/locate-resource-group.png" alt-text="Screenshot of a list of resource groups filtered down to a specific prefix."::: - -1. On the resource group page, select **Delete resource group**. - - :::image type="content" source="media/quickstart-portal/select-delete-resource-group-option.png" alt-text="Screenshot of the delete resource group option in the menu for a specific resource group."::: - -1. In the deletion confirmation dialog, enter the name of the resource group to confirm that you intend to delete it. Finally, select **Delete** to permanently delete the resource group. - - :::image type="content" source="media/quickstart-portal/delete-resource-group-dialog.png" alt-text="Screenshot of the delete resource group confirmation dialog with the name of the group filled out."::: - ## Related content -In this guide, you learned how to create a cluster read replica for an Azure Cosmos DB for MongoDB vCore cluster and use it for disaster recovery and cross-region read scalability purposes. - - [Learn about cross-region replication in Azure Cosmos DB for MongoDB vCore](./cross-region-replication.md) - [Migrate data to Azure Cosmos DB for MongoDB vCore](./migration-options.md) diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/cluster-networking-adding-firewall-rule.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/cluster-networking-adding-firewall-rule.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/cluster-networking-adding-firewall-rule.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/cluster-networking-adding-firewall-rule.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/configure-cluster.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/configure-cluster.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/configure-cluster.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/configure-cluster.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/global-distribution-page-on-primary-cluster.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/global-distribution-page-on-primary-cluster.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/global-distribution-page-on-primary-cluster.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/global-distribution-page-on-primary-cluster.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/global-distribution-tab.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/global-distribution-tab.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/global-distribution-tab.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/global-distribution-tab.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/networking-adding-firewall-rule.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/networking-adding-firewall-rule.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/networking-adding-firewall-rule.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/networking-adding-firewall-rule.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/replica-cluster-promotion-confirmation.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/replica-cluster-promotion-confirmation.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/replica-cluster-promotion-confirmation.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/replica-cluster-promotion-confirmation.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/replica-cluster-promotion.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/replica-cluster-promotion.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/replica-cluster-promotion.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/replica-cluster-promotion.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/select-connection-strings-option.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/select-connection-strings-option.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/select-connection-strings-option.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/select-connection-strings-option.png diff --git a/articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/what-is-my-ip.png b/articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/what-is-my-ip.png similarity index 100% rename from articles/cosmos-db/mongodb/vcore/media/quickstart-cross-region-replication/what-is-my-ip.png rename to articles/cosmos-db/mongodb/vcore/media/how-to-cross-region-replication-portal/what-is-my-ip.png From 745bfba88843efcb9388567b63a8751aefae407e Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:25:21 -0500 Subject: [PATCH 06/14] Refresh AI articles (batch 1) --- articles/cosmos-db/.openpublishing.redirection.cosmos-db.json | 2 +- articles/cosmos-db/TOC.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json index d667c8ff7b..04ce3904ac 100644 --- a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json +++ b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json @@ -5976,7 +5976,7 @@ "redirect_document_id": false }, { - "source_path_from_root": "/articlers/cosmos-db/gen-ai/quickstart-rag-chatbot.md", + "source_path_from_root": "/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md", "redirect_url": "/azure/cosmos-db/gen-ai/rag-chatbot.md", "redirect_document_id": true } diff --git a/articles/cosmos-db/TOC.yml b/articles/cosmos-db/TOC.yml index 96e66fd607..495f7f8cb1 100644 --- a/articles/cosmos-db/TOC.yml +++ b/articles/cosmos-db/TOC.yml @@ -49,8 +49,8 @@ expanded: true - name: AI Applications items: - - name: Quickstart - build a RAG chatbot - href: gen-ai/quickstart-rag-chatbot.md + - name: Build a RAG chatbot + href: gen-ai/rag-chatbot.md - name: Ingest and vectorize document files href: gen-ai/document-ingestion.md - name: Cosmos AI Graph - Graph RAG From 530f1f8c0008727b8f769034f222daca013448dc Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:27:24 -0500 Subject: [PATCH 07/14] Fix redirects --- .../cosmos-db/.openpublishing.redirection.cosmos-db.json | 5 +++++ articles/cosmos-db/mongodb/vcore/TOC.yml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json index 9e2b42bd1b..607a6a48cb 100644 --- a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json +++ b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json @@ -5970,6 +5970,11 @@ "redirect_url": "/azure/cosmos-db/table/quickstart-nodejs", "redirect_document_id": false }, + { + "source_path_from_root": "/articles/cosmos-db/mongodb/vcore/quickstart-cross-region-replica-portal.md", + "redirect_url": "/azure/cosmos-db/mongodb/vcore/how-to-cross-region-replica-portal", + "redirect_document_id": false + }, { "source_path_from_root": "/articles/cosmos-db/table/samples-dotnet.md", "redirect_url": "/azure/cosmos-db/table/quickstart-dotnet", diff --git a/articles/cosmos-db/mongodb/vcore/TOC.yml b/articles/cosmos-db/mongodb/vcore/TOC.yml index 27815bb81b..0fc60fde73 100644 --- a/articles/cosmos-db/mongodb/vcore/TOC.yml +++ b/articles/cosmos-db/mongodb/vcore/TOC.yml @@ -83,7 +83,7 @@ - name: How to items: - name: Create cross-region replica using Azure portal - href: cross-region-replica-portal.md + href: how-to-cross-region-replica-portal.md - name: Use community tools items: - name: Connect using Studio 3T From b9585a97c8fcee88902cfa9c4cbbfd0c92e7ea3f Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:29:11 -0500 Subject: [PATCH 08/14] Refresh AI articles (batch 1) --- articles/cosmos-db/.openpublishing.redirection.cosmos-db.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json index 04ce3904ac..3c33401694 100644 --- a/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json +++ b/articles/cosmos-db/.openpublishing.redirection.cosmos-db.json @@ -5977,7 +5977,7 @@ }, { "source_path_from_root": "/articles/cosmos-db/gen-ai/quickstart-rag-chatbot.md", - "redirect_url": "/azure/cosmos-db/gen-ai/rag-chatbot.md", + "redirect_url": "/azure/cosmos-db/gen-ai/rag-chatbot", "redirect_document_id": true } ] From d234f4cfccc65f3a79469e2a00698c727e3e493c Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:51:54 -0500 Subject: [PATCH 09/14] Refresh AI articles (#2) --- articles/cosmos-db/gen-ai/tokens.md | 16 +- .../cosmos-db/gen-ai/vector-embeddings.md | 32 +- .../gen-ai/vector-search-overview.md | 20 +- .../vcore/ai-advertisement-generation.md | 55 ++- articles/cosmos-db/mongodb/vcore/rag.md | 316 +++++++++--------- .../mongodb/vcore/vector-search-ai.md | 15 +- .../cosmos-db/mongodb/vcore/vector-search.md | 83 ++--- 7 files changed, 306 insertions(+), 231 deletions(-) diff --git a/articles/cosmos-db/gen-ai/tokens.md b/articles/cosmos-db/gen-ai/tokens.md index 35e3d85190..3b7bd1ae1e 100644 --- a/articles/cosmos-db/gen-ai/tokens.md +++ b/articles/cosmos-db/gen-ai/tokens.md @@ -1,20 +1,26 @@ --- -title: LLM tokens -description: Overview of tokens in large language models. +title: Large language model tokens +titleSuffix: Azure Cosmos DB +description: An explanation and conceptual overview of tokens in large language models (LLMs) for Azure Cosmos DB. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 07/01/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- # What are tokens? -Tokens are small chunks of text generated by splitting the input text into smaller segments. These segments can either be words or groups of characters, varying in length from a single character to an entire word. For instance, the word hamburger would be divided into tokens such as ham, bur, and ger while a short and common word like pear would be considered a single token. LLMs like GPT-3.5 or GPT-4 break words into tokens for processing. +Tokens are small chunks of text generated by splitting the input text into smaller segments. These segments can either be words or groups of characters, varying in length from a single character to an entire word. For instance, the word hamburger would be divided into tokens such as ham, bur, and ger while a short and common word like pear would be considered a single token. Large language models (LLMs) like GPT-3.5 or GPT-4 break words into tokens for processing. ## Related content + - [What is a vector database?](../vector-database.md) - [Retrieval Augmented Generation (RAG)](rag.md) - [Vector database in Azure Cosmos DB NoSQL](../nosql/vector-search.md) diff --git a/articles/cosmos-db/gen-ai/vector-embeddings.md b/articles/cosmos-db/gen-ai/vector-embeddings.md index b187001031..b50e0879ab 100644 --- a/articles/cosmos-db/gen-ai/vector-embeddings.md +++ b/articles/cosmos-db/gen-ai/vector-embeddings.md @@ -1,25 +1,35 @@ --- -title: Vector embeddings -description: Vector embeddings overview. +title: High-dimensional vector embeddings +titleSuffix: Azure Cosmos DB +description: Overview of high-dimensional vector embeddings, or mathematical representations of data in Azure Cosmos DB. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 07/01/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- -# What are vector embeddings? +# Vector embeddings in Azure Cosmos DB Vectors, also known as embeddings or vector embeddings, are mathematical representations of data in a high-dimensional space. They represent various types of information — text, images, audio — a format that machine learning models can process. When an AI model receives text input, it first tokenizes the text into tokens. Each token is then converted into its corresponding embedding. This conversion process can be done using an embedding generation model, such as [Azure OpenAI Embeddings](/azure/ai-services/openai/how-to/embeddings) or [Hugging Face on Azure](https://azure.microsoft.com/solutions/hugging-face-on-azure). The model processes these embeddings through multiple layers, capturing complex patterns and relationships within the text. The output embeddings can then be converted back into tokens if needed, generating readable text. +## Vectors + Each embedding is a vector of floating-point numbers, such that the distance between two embeddings in the vector space is correlated with semantic similarity between two inputs in the original format. For example, if two texts are similar, then their vector representations should also be similar. These high-dimensional representations capture semantic meaning, making it easier to perform tasks like searching, clustering, and classifying. Here are two examples of texts represented as vectors: -:::image type="content" source="../media/gen-ai/concepts/vector-examples.png" lightbox="../media/gen-ai/concepts/vector-examples.png" alt-text="Screenshot of vector examples."::: -Image source: [OpenAI](https://openai.com/index/introducing-text-and-code-embeddings/) +:::image type="content" source="../media/gen-ai/concepts/vector-examples.png" lightbox="../media/gen-ai/concepts/vector-examples.png" alt-text="Screenshot of vector examples in Azure Cosmos DB."::: + +> Image source: [OpenAI](https://openai.com/index/introducing-text-and-code-embeddings/) + +## Dimensions and closeness Each box containing floating-point numbers corresponds to a dimension, and each dimension corresponds to a feature or attribute that may or may not be comprehensible to humans. Large language model text embeddings typically have a few thousand dimensions, while more complex data models may have tens of thousands of dimensions. @@ -27,12 +37,16 @@ Between the two vectors in the above example, some dimensions are similar while This image shows the spatial closeness of vectors that are similar, contrasting vectors that are drastically different: -:::image type="content" source="../media/gen-ai/concepts/vector-closeness.png" lightbox="../media/gen-ai/concepts/vector-closeness.png" alt-text="Screenshot of vector closeness."::: -Image source: [OpenAI](https://openai.com/index/introducing-text-and-code-embeddings/) +:::image type="content" source="../media/gen-ai/concepts/vector-closeness.png" lightbox="../media/gen-ai/concepts/vector-closeness.png" alt-text="Screenshot of vector closeness in Azure Cosmos DB."::: + +> Image source: [OpenAI](https://openai.com/index/introducing-text-and-code-embeddings/) + +## Examples You can see more examples in this [interactive visualization](https://openai.com/index/introducing-text-and-code-embeddings/#_1Vr7cWWEATucFxVXbW465e) that transforms data into a three-dimensional space. ## Related content + - [What is a vector database?](../vector-database.md) - [Retrieval Augmented Generation (RAG)](rag.md) - [Vector database in Azure Cosmos DB NoSQL](../nosql/vector-search.md) diff --git a/articles/cosmos-db/gen-ai/vector-search-overview.md b/articles/cosmos-db/gen-ai/vector-search-overview.md index d04cc09d7c..2423ec4912 100644 --- a/articles/cosmos-db/gen-ai/vector-search-overview.md +++ b/articles/cosmos-db/gen-ai/vector-search-overview.md @@ -1,26 +1,36 @@ --- -title: Vector search concept overview -description: Vector search concept overview +title: Vector similarity search +titleSuffix: Azure Cosmos DB +description: Overview of the vector similarity search functionality in Azure Cosmos DB's various vector search features. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db -ms.topic: conceptual -ms.date: 07/01/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- -# What is vector search? +# Vector search in Azure Cosmos DB Vector search is a method that helps you find similar items based on their data characteristics rather than by exact matches on a property field. This technique is useful in applications such as searching for similar text, finding related images, making recommendations, or even detecting anomalies. It works by taking the [vector embeddings](vector-embeddings.md) of your data and query, and then measuring the [distance](distance-functions.md) between the data vectors and your query vector. The data vectors that are closest to your query vector are the ones that are found to be most similar semantically. +## Examples + This [interactive visualization](https://openai.com/index/introducing-text-and-code-embeddings/#_1Vr7cWWEATucFxVXbW465e) shows some examples of closeness and distance between vectors. +## Algorithms + Two major types of vector search algorithms are k-nearest neighbors (kNN) and approximate nearest neighbor (ANN). Between [kNN and ANN](knn-vs-ann.md), the latter offers a balance between accuracy and efficiency, making it better suited for large-scale applications. Some well-known ANN algorithms include Inverted File (IVF), Hierarchical Navigable Small World (HNSW), and the state-of-the-art DiskANN. Using an integrated vector search feature in a fully featured database ([as opposed to a pure vector database](../vector-database.md#integrated-vector-database-vs-pure-vector-database)) offers an efficient way to store, index, and search high-dimensional vector data directly alongside other application data. This approach removes the necessity of migrating your data to costlier alternative vector databases and provides a seamless integration of your AI-driven applications. ## Related content + - [What is a vector database?](../vector-database.md) - [Retrieval Augmented Generation (RAG)](rag.md) - [Vector database in Azure Cosmos DB NoSQL](../nosql/vector-search.md) diff --git a/articles/cosmos-db/mongodb/vcore/ai-advertisement-generation.md b/articles/cosmos-db/mongodb/vcore/ai-advertisement-generation.md index 93136b98df..70e21d5713 100644 --- a/articles/cosmos-db/mongodb/vcore/ai-advertisement-generation.md +++ b/articles/cosmos-db/mongodb/vcore/ai-advertisement-generation.md @@ -1,41 +1,44 @@ --- -title: AI-Enhanced Advertisement Generation using Azure Cosmos DB for MongoDB vCore -titleSuffix: Azure Cosmos DB +title: AI-enhanced advertisement generation +titleSuffix: Azure Cosmos DB for MongoDB vCore description: Demonstrates the use of Azure Cosmos DB for MongoDB vCore's vector similarity search and OpenAI embeddings to generate advertising content. author: khelanmodi ms.author: khelanmodi ms.service: azure-cosmos-db ms.subservice: mongodb-vcore ms.topic: concept-article -ms.date: 03/12/2024 +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ MongoDB vCore --- -# AI-Enhanced Advertisement Generation using Azure Cosmos DB for MongoDB vCore +# AI-Enhanced advertisement generation using Azure Cosmos DB for MongoDB vCore + In this guide, we demonstrate how to create dynamic advertising content that resonates with your audience, using our personalized AI assistant, Heelie. Utilizing Azure Cosmos DB for MongoDB vCore, we harness the [vector similarity search](./vector-search.md) functionality to semantically analyze and match inventory descriptions with advertisement topics. The process is made possible by generating vectors for inventory descriptions using OpenAI embeddings, which significantly enhance their semantic depth. These vectors are then stored and indexed within the Cosmos DB for MongoDB vCore resource. When generating content for advertisements, we vectorize the advertisement topic to find the best-matching inventory items. This is followed by a retrieval augmented generation (RAG) process, where the top matches are sent to OpenAI to craft a compelling advertisement. The entire codebase for the application is available in a [GitHub repository](https://aka.ms/adgen) for your reference. ## Features + - **Vector Similarity Search**: Uses Azure Cosmos DB for MongoDB vCore's powerful vector similarity search to improve semantic search capabilities, making it easier to find relevant inventory items based on the content of advertisements. - **OpenAI Embeddings**: Utilizes the cutting-edge embeddings from OpenAI to generate vectors for inventory descriptions. This approach allows for more nuanced and semantically rich matches between the inventory and the advertisement content. - **Content Generation**: Employs OpenAI's advanced language models to generate engaging, trend-focused advertisements. This method ensures that the content is not only relevant but also captivating to the target audience. ## Prerequisites -- Azure OpenAI: Let's setup the Azure OpenAI resource. Access to this service is currently available by application only. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Once you have access, complete the following steps: - - Create an Azure OpenAI resource following this [quickstart](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal). - - Deploy a `completions` and an `embeddings` model - - For more information on `completions`, go [here](/azure/ai-services/openai/how-to/completions). - - For more information on `embeddings`, go [here](/azure/ai-services/openai/how-to/embeddings). - - Note down your endpoint, key, and deployment names. +- Azure OpenAI: Let's setup the Azure OpenAI resource. Access to this service is currently available by application only. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Once you have access, complete the following steps: + - Create an Azure OpenAI resource following this [quickstart](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal). + - Deploy a `completions` and an `embeddings` model. + - For more information on `completions`, go [here](/azure/ai-services/openai/how-to/completions). + - For more information on `embeddings`, go [here](/azure/ai-services/openai/how-to/embeddings). + - Note down your endpoint, key, and deployment names. - Cosmos DB for MongoDB vCore resource: Let's start by creating an Azure Cosmos DB for MongoDB vCore resource for free following this [quick start](./quickstart-portal.md) guide. - - Note down the connection details (connection string). - + - Note down the connection details. - Python environment (>= 3.9 version) with packages such as `numpy`, `openai`, `pymongo`, `python-dotenv`, `azure-core`, `azure-cosmos`, `tenacity`, and `gradio`. - - Download the [data file](https://github.com/jayanta-mondal/ignite-demo/blob/main/data/shoes_with_vectors.json) and save it in a designated data folder. ## Running the Script + Before we dive into the exciting part of generating AI-enhanced advertisements, we need to set up our environment. This setup involves installing the necessary packages to ensure our script runs smoothly. Here’s a step-by-step guide to get everything ready. ### 1.1 Install Necessary Packages @@ -55,7 +58,8 @@ Firstly, we need to install a few Python packages. Open your terminal and run th ``` ### 1.2 Setting Up the OpenAI and Azure Client -After installing the necessary packages, the next step involves setting up our OpenAI and Azure clients for the script, which is crucial for authenticating our requests to the OpenAI API and Azure services. + +After installing the necessary packages, the next step involves setting up our OpenAI and Azure clients for the script, which is crucial for authenticating our requests to the OpenAI API and Azure services. ```python import json @@ -80,6 +84,7 @@ client = AzureOpenAI( ``` ## Solution architecture + ![solution architecture](./media/tutorial-adgen/architecture.png) ## 2. Creating Embeddings and Setting up Cosmos DB @@ -112,10 +117,13 @@ if embeddings is not None: The function takes a text input — like a product description — and uses the `client.embeddings.create` method from the OpenAI API to generate a vector embedding for that text. We're using the `text-embedding-ada-002` model here, but you can choose other models based on your requirements. If the process is successful, it prints the generated embeddings; otherwise, it handles exceptions by printing an error message. ## 3. Connect and set up Cosmos DB for MongoDB vCore + With our embeddings ready, the next step is to store and index them in a database that supports vector similarity search. Azure Cosmos DB for MongoDB vCore is a perfect fit for this task because it's purpose built to store your transactional data and perform vector search all in one place. ### 3.1 Set up the connection + To connect to Cosmos DB, we use the pymongo library, which allows us to interact with MongoDB easily. The following code snippet establishes a connection with our Cosmos DB for MongoDB vCore instance: + ```python import pymongo @@ -133,6 +141,7 @@ Once you've established a connection to Azure Cosmos DB, the next steps involve ### 4.1 Set Up the Database and Collection First, we create a database and a collection within our Cosmos DB instance. Here’s how: + ```python DATABASE_NAME = "AdgenDatabase" COLLECTION_NAME = "AdgenCollection" @@ -150,9 +159,11 @@ else: ``` ### 4.2 Create the vector index + To perform efficient vector similarity searches within our collection, we need to create a vector index. Cosmos DB supports different types of [vector indexes](./vector-search.md), and here we discuss two: IVF and HNSW. ### IVF + IVF stands for Inverted File Index, is the default vector indexing algorithm, which works on all cluster tiers. It's an approximate nearest neighbors (ANN) approach that uses clustering to speeding up the search for similar vectors in a dataset. To create an IVF index, use the following command: ```javascript @@ -175,10 +186,10 @@ db.command({ }); ``` -> [!IMPORTANT] +> [!IMPORTANT] > **You can only create one index per vector property.** That is, you cannot create more than one index that points to the same vector property. If you want to change the index type (e.g., from IVF to HNSW) you must drop the index first before creating a new index. -### HNSW +### HNSW HNSW stands for Hierarchical Navigable Small World, a graph-based data structure that partitions vectors into clusters and subclusters. With HNSW, you can perform fast approximate nearest neighbor search at higher speeds with greater accuracy. HNSW is an approximate (ANN) method. Here's how to set it up: @@ -204,10 +215,12 @@ db.command( } ) ``` + > [!NOTE] -> HNSW indexing is only available on M40 cluster tiers and higher. +> HNSW indexing is only available on M40 cluster tiers and higher. ## 5. Insert data to the collection + Now insert the inventory data, which includes descriptions and their corresponding vector embeddings, into the newly created collection. To insert data into our collection, we use the `insert_many()` method provided by the `pymongo` library. The method allows us to insert multiple documents into the collection at once. Our data is stored in a JSON file, which we'll load and then insert into the database. Download the [shoes_with_vectors.json](https://github.com/jayanta-mondal/ignite-demo/blob/main/data/shoes_with_vectors.json) file from the GitHub repository and store it in a `data` directory within your project folder. @@ -223,9 +236,11 @@ print(f"Number of data points added: {len(result.inserted_ids)}") ``` ## 6. Vector Search in Cosmos DB for MongoDB vCore + With our data successfully uploaded, we can now apply the power of vector search to find the most relevant items based on a query. The vector index we created earlier enables us to perform semantic searches within our dataset. ### 6.1 Conducting a Vector Search + To perform a vector search, we define a function `vector_search` that takes a query and the number of results to return. The function generates a vector for the query using the `generate_embeddings` function we defined earlier, then uses Cosmos DB's `$search` functionality to find the closest matching items based on their vector embeddings. ```python @@ -250,7 +265,9 @@ def vector_search(query, num_results=3): results = collection.aggregate(pipeline) return results ``` + ## 6.2 Perform vector search query + Finally, we execute our vector search function with a specific query and process the results to display them: ```python @@ -358,6 +375,7 @@ def render_html_page(ad_topic): ``` ## 8. Putting it all together + To make our advertisement generation interactive, we employ Gradio, a Python library for creating simple web UIs. We define a UI that allows users to input ad topics and then dynamically generates and displays the resulting advertisement. ```python @@ -382,7 +400,8 @@ if __name__ == "__main__": demo.launch() ``` -## Output +## Output + ![Output screen](./media/tutorial-adgen/result.png) ## Next step diff --git a/articles/cosmos-db/mongodb/vcore/rag.md b/articles/cosmos-db/mongodb/vcore/rag.md index 01ccf05fd7..8f3454fe6d 100644 --- a/articles/cosmos-db/mongodb/vcore/rag.md +++ b/articles/cosmos-db/mongodb/vcore/rag.md @@ -1,19 +1,22 @@ --- -title: Optimize Retrieval-Augmented Generation (RAG) with Azure Cosmos DB for MongoDB (vCore), LangChain, and OpenAI -titleSuffix: Azure Cosmos DB -description: Learn how to enhance AI-based applications using Retrieval-Augmented Generation (RAG) with Azure Cosmos DB for MongoDB (vCore), LangChain, and OpenAI. Discover key concepts, architecture, and real-world applications. +title: Retrieval-augmented generation with LangChain and OpenAI +titleSuffix: Azure Cosmos DB for MongoDB vCore +description: Enhance AI-based applications using Retrieval-Augmented Generation (RAG) with Azure Cosmos DB for MongoDB (vCore), LangChain, and OpenAI. author: khelanmodi ms.author: khelanmodi ms.service: azure-cosmos-db ms.subservice: mongodb-vcore -ms.topic: conceptual -ms.date: 07/08/2024 +ms.topic: how-to +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ MongoDB vCore --- -# RAG with vCore-based Azure Cosmos DB for MongoDB -In the fast-evolving realm of generative AI, Large Language Models (LLMs) like GPT-3.5 have transformed natural language processing. However, an emerging trend in AI is the use of vector stores, which play a pivotal role in enhancing AI applications. +# Retrieval-augmented generation (RAG) with vCore-based Azure Cosmos DB for MongoDB + +In the fast-evolving realm of generative AI, Large Language Models (LLMs) like GPT-3.5 have transformed natural language processing. However, an emerging trend in AI is the use of vector stores, which play a pivotal role in enhancing AI applications. This tutorial explores how to use Azure Cosmos DB for MongoDB (vCore), LangChain, and OpenAI to implement Retrieval-Augmented Generation (RAG) for superior AI performance alongside discussing LLMs and their limitations. We explore the rapidly adopted paradigm of "retrieval-augmented generation" (RAG), and briefly discuss the LangChain framework, Azure OpenAI models. Finally, we integrate these concepts into a real-world application. By the end, readers will have a solid understanding of these concepts. @@ -43,7 +46,7 @@ The architecture diagram below illustrates the key components of our RAG impleme ![Architecture Diagram](./media/vector/architecture-diagram.png) -## Key components and frameworks +## Key components and frameworks We'll now discuss the various frameworks, models, and components used in this tutorial, emphasizing their roles and nuances. @@ -72,19 +75,18 @@ OpenAI is a leader in AI research, providing various models for language generat ### Embedding models vs. Language generation models -| **Category** | **Text Embedding Model** | **Language Model** | -|---------------------------|----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------| -| **Purpose** | Converting text into vector embeddings. | Understanding and generating natural language. | -| **Function** | Transforms textual data into high-dimensional arrays of numbers, capturing the semantic meaning of the text. | Comprehends and produces human-like text based on given input. | -| **Output** | Array of numbers (vector embeddings). | Text, answers, translations, code, etc. | -| **Example Output** | Each embedding represents the semantic meaning of the text in numerical form, with a dimensionality determined by the model. For example, `text-embedding-ada-002` generates vectors with 1536 dimensions. | Contextually relevant and coherent text generated based on the input provided. For example, `gpt-3.5-turbo` can generate responses to questions, translate text, write code, and more. | -| **Typical Use Cases** | - Semantic search | - Chatbots | -| | - Recommendation systems | - Automated content creation | -| | - Clustering and classification of text data | - Language translation | -| | - Information retrieval | - Summarization | -| **Data Representation** | Numerical representation (embeddings) | Natural language text | -| **Dimensionality** | The length of the array corresponds to the number of dimensions in the embedding space, for example, 1536 dimensions. | Typically represented as a sequence of tokens, with the context determining the length. | - +| **Category** | **Text Embedding Model** | **Language Model** | +| --- | --- | --- | +| **Purpose** | Converting text into vector embeddings. | Understanding and generating natural language. | +| **Function** | Transforms textual data into high-dimensional arrays of numbers, capturing the semantic meaning of the text. | Comprehends and produces human-like text based on given input. | +| **Output** | Array of numbers (vector embeddings). | Text, answers, translations, code, etc. | +| **Example Output** | Each embedding represents the semantic meaning of the text in numerical form, with a dimensionality determined by the model. For example, `text-embedding-ada-002` generates vectors with 1536 dimensions. | Contextually relevant and coherent text generated based on the input provided. For example, `gpt-3.5-turbo` can generate responses to questions, translate text, write code, and more. | +| **Typical Use Cases** | - Semantic search | - Chatbots | +| | - Recommendation systems | - Automated content creation | +| | - Clustering and classification of text data | - Language translation | +| | - Information retrieval | - Summarization | +| **Data Representation** | Numerical representation (embeddings) | Natural language text | +| **Dimensionality** | The length of the array corresponds to the number of dimensions in the embedding space, for example, 1536 dimensions. | Typically represented as a sequence of tokens, with the context determining the length. | ### Main components of the application @@ -103,14 +105,18 @@ OpenAI is a leader in AI research, providing various models for language generat To get started with optimizing retrieval-augmented generation (RAG) using Azure Cosmos DB for MongoDB (vCore), follow these steps: - **Create the following resources on Microsoft Azure:** - - **Azure Cosmos DB for MongoDB vCore cluster**: See the [Quick Start guide here](https://aka.ms/tryvcore). - - **Azure OpenAI resource with:** - - **Embedding model deployment** (for example, `text-embedding-ada-002`). - - **Chat model deployment** (for example, `gpt-35-turbo`). - -### Sample documents -In this tutorial, we will be loading a single text file using [Document](https://python.langchain.com/v0.1/docs/modules/data_connection/document_loaders/). These files should be saved in a directory named **data** in the **src** folder. The contents of the are as follows: -```food_items.json + - **Azure Cosmos DB for MongoDB vCore cluster**: See the [Quick Start guide here](https://aka.ms/tryvcore). + - **Azure OpenAI resource with:** + - **Embedding model deployment** (for example, `text-embedding-ada-002`). + - **Chat model deployment** (for example, `gpt-35-turbo`). + +### Sample documents + +In this tutorial, we will be loading a single text file using [Document](https://python.langchain.com/v0.1/docs/modules/data_connection/document_loaders/). These files should be saved in a directory named **data** in the **src** folder. The contents of the are as follows: + +#### `food_items.json` + +```json { "category": "Cold Dishes", "name": "Hamachi Fig", @@ -119,142 +125,154 @@ In this tutorial, we will be loading a single text file using [Document](https:/ }, ``` -### Load documents -1. Set the Cosmos DB for MongoDB (vCore) connection string, Database Name, Collection Name, and Index: -```python -mongo_client = MongoClient(mongo_connection_string) -database_name = "Contoso" -db = mongo_client[database_name] -collection_name = "ContosoCollection" -index_name = "ContosoIndex" -collection = db[collection_name] -``` - -2. Initialize the Embedding Client. -```python -from langchain_openai import AzureOpenAIEmbeddings - -openai_embeddings_model = os.getenv("AZURE_OPENAI_EMBEDDINGS_MODEL_NAME", "text-embedding-ada-002") -openai_embeddings_deployment = os.getenv("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME", "text-embedding") - -azure_openai_embeddings: AzureOpenAIEmbeddings = AzureOpenAIEmbeddings( - model=openai_embeddings_model, - azure_deployment=openai_embeddings_deployment, -) -``` +### Load documents -3. Create embeddings from the data, save to the database and return a connection to your vector store, Cosmos DB for MongoDB (vCore). -```python -vector_store: AzureCosmosDBVectorSearch = AzureCosmosDBVectorSearch.from_documents( - json_data, - azure_openai_embeddings, - collection=collection, - index_name=index_name, -) -``` +1. Set the Cosmos DB for MongoDB (vCore) connection string, Database Name, Collection Name, and Index: -4. Create the following [HNSW vector Index](./vector-search.md) on the collection (Note the name of the index is same as above). -```python -num_lists = 100 -dimensions = 1536 -similarity_algorithm = CosmosDBSimilarityType.COS -kind = CosmosDBVectorSearchType.VECTOR_HNSW -m = 16 -ef_construction = 64 - -vector_store.create_index( - num_lists, dimensions, similarity_algorithm, kind, m, ef_construction -) -``` + ```python + mongo_client = MongoClient(mongo_connection_string) + database_name = "Contoso" + db = mongo_client[database_name] + collection_name = "ContosoCollection" + index_name = "ContosoIndex" + collection = db[collection_name] + ``` + +1. Initialize the Embedding Client. + + ```python + from langchain_openai import AzureOpenAIEmbeddings + + openai_embeddings_model = os.getenv("AZURE_OPENAI_EMBEDDINGS_MODEL_NAME", "text-embedding-ada-002") + openai_embeddings_deployment = os.getenv("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME", "text-embedding") + + azure_openai_embeddings: AzureOpenAIEmbeddings = AzureOpenAIEmbeddings( + model=openai_embeddings_model, + azure_deployment=openai_embeddings_deployment, + ) + ``` + +1. Create embeddings from the data, save to the database and return a connection to your vector store, Cosmos DB for MongoDB (vCore). + + ```python + vector_store: AzureCosmosDBVectorSearch = AzureCosmosDBVectorSearch.from_documents( + json_data, + azure_openai_embeddings, + collection=collection, + index_name=index_name, + ) + ``` + +1. Create the following [HNSW vector Index](./vector-search.md) on the collection (Note the name of the index is same as above). + + ```python + num_lists = 100 + dimensions = 1536 + similarity_algorithm = CosmosDBSimilarityType.COS + kind = CosmosDBVectorSearchType.VECTOR_HNSW + m = 16 + ef_construction = 64 + + vector_store.create_index( + num_lists, dimensions, similarity_algorithm, kind, m, ef_construction + ) + ``` ### Perform Vector search using Cosmos DB for MongoDB (vCore) 1. Connect to your vector store. -```python -vector_store: AzureCosmosDBVectorSearch = AzureCosmosDBVectorSearch.from_connection_string( - connection_string=mongo_connection_string, - namespace=f"{database_name}.{collection_name}", - embedding=azure_openai_embeddings, -) -``` -2. Define a function that performs semantic similarity search using Cosmos DB Vector Search on a query (note this code snippet is just a test function). -```python -query = "beef dishes" -docs = vector_store.similarity_search(query) -print(docs[0].page_content) -``` - -3. Initialize the Chat Client to implement a RAG function. -```python -azure_openai_chat: AzureChatOpenAI = AzureChatOpenAI( - model=openai_chat_model, - azure_deployment=openai_chat_deployment, -) -``` - -4. Create a RAG function. -```python -history_prompt = ChatPromptTemplate.from_messages( - [ - MessagesPlaceholder(variable_name="chat_history"), - ("user", "{input}"), - ( - "user", - """Given the above conversation, - generate a search query to look up to get information relevant to the conversation""", - ), - ] -) - -context_prompt = ChatPromptTemplate.from_messages( - [ - ("system", "Answer the user's questions based on the below context:\n\n{context}"), - MessagesPlaceholder(variable_name="chat_history"), - ("user", "{input}"), - ] -) -``` - -5. Converts the vector store into a retriever, which can search for relevant documents based on specified parameters. -```python -vector_store_retriever = vector_store.as_retriever( - search_type=search_type, search_kwargs={"k": limit, "score_threshold": score_threshold} -) -``` - -6. Create a retriever chain that is aware of the conversation history, ensuring contextually relevant document retrieval using the **azure_openai_chat** model and **vector_store_retriever**. -```python -retriever_chain = create_history_aware_retriever(azure_openai_chat, vector_store_retriever, history_prompt) -``` - -7. Create a chain that combines retrieved documents into a coherent response using the language model (**azure_openai_chat**) and a specified prompt (**context_prompt**). -```python -context_chain = create_stuff_documents_chain(llm=azure_openai_chat, prompt=context_prompt) -``` - -8. Create a chain that handles the entire retrieval process, integrating the history-aware retriever chain and the document combination chain. This RAG chain can be executed to retrieve and generate contextually accurate responses. -```python -rag_chain: Runnable = create_retrieval_chain( - retriever=retriever_chain, - combine_docs_chain=context_chain, -) -``` + ```python + vector_store: AzureCosmosDBVectorSearch = AzureCosmosDBVectorSearch.from_connection_string( + connection_string=mongo_connection_string, + namespace=f"{database_name}.{collection_name}", + embedding=azure_openai_embeddings, + ) + ``` + +1. Define a function that performs semantic similarity search using Cosmos DB Vector Search on a query (note this code snippet is just a test function). + + ```python + query = "beef dishes" + docs = vector_store.similarity_search(query) + print(docs[0].page_content) + ``` + +1. Initialize the Chat Client to implement a RAG function. + + ```python + azure_openai_chat: AzureChatOpenAI = AzureChatOpenAI( + model=openai_chat_model, + azure_deployment=openai_chat_deployment, + ) + ``` + +1. Create a RAG function. + + ```python + history_prompt = ChatPromptTemplate.from_messages( + [ + MessagesPlaceholder(variable_name="chat_history"), + ("user", "{input}"), + ( + "user", + """Given the above conversation, + generate a search query to look up to get information relevant to the conversation""", + ), + ] + ) + + context_prompt = ChatPromptTemplate.from_messages( + [ + ("system", "Answer the user's questions based on the below context:\n\n{context}"), + MessagesPlaceholder(variable_name="chat_history"), + ("user", "{input}"), + ] + ) + ``` + +1. Converts the vector store into a retriever, which can search for relevant documents based on specified parameters. + + ```python + vector_store_retriever = vector_store.as_retriever( + search_type=search_type, search_kwargs={"k": limit, "score_threshold": score_threshold} + ) + ``` + +1. Create a retriever chain that is aware of the conversation history, ensuring contextually relevant document retrieval using the **azure_openai_chat** model and **vector_store_retriever**. + + ```python + retriever_chain = create_history_aware_retriever(azure_openai_chat, vector_store_retriever, history_prompt) + ``` + +1. Create a chain that combines retrieved documents into a coherent response using the language model (**azure_openai_chat**) and a specified prompt (**context_prompt**). + + ```python + context_chain = create_stuff_documents_chain(llm=azure_openai_chat, prompt=context_prompt) + ``` + +1. Create a chain that handles the entire retrieval process, integrating the history-aware retriever chain and the document combination chain. This RAG chain can be executed to retrieve and generate contextually accurate responses. + + ```python + rag_chain: Runnable = create_retrieval_chain( + retriever=retriever_chain, + combine_docs_chain=context_chain, + ) + ``` + +### Sample outputs -### Sample outputs The screenshot below illustrates the outputs for various questions. A purely semantic-similarity search returns the raw text from the source documents, while the question-answering app using the RAG architecture generates precise and personalized answers by combining retrieved document contents with the language model. ![Rag Comic App](./media/vector/rag-cosmic-screenshot.png) ### Conclusion -In this tutorial, we explored how to build a question-answering app that interacts with your private data using Cosmos DB as a vector store. By leveraging the retrieval-augmented generation (RAG) architecture with LangChain and Azure OpenAI, we demonstrated how vector stores are essential for LLM applications. -RAG is a significant advancement in AI, particularly in natural language processing, and combining these technologies allows for the creation of powerful AI-driven applications for various use cases. +In this tutorial, we explored how to build a question-answering app that interacts with your private data using Cosmos DB as a vector store. By leveraging the retrieval-augmented generation (RAG) architecture with LangChain and Azure OpenAI, we demonstrated how vector stores are essential for LLM applications. -## Next steps +RAG is a significant advancement in AI, particularly in natural language processing, and combining these technologies allows for the creation of powerful AI-driven applications for various use cases. -For a detailed, hands-on experience and to see how RAG can be implemented using Azure Cosmos DB for MongoDB (vCore), LangChain, and OpenAI models, visit our GitHub repository. +## Next step > [!div class="nextstepaction"] > [Check out RAG sample on GitHub](https://github.com/Azure-Samples/Cosmic-Food-RAG-app) - diff --git a/articles/cosmos-db/mongodb/vcore/vector-search-ai.md b/articles/cosmos-db/mongodb/vcore/vector-search-ai.md index f349301a34..d65bc4b66a 100644 --- a/articles/cosmos-db/mongodb/vcore/vector-search-ai.md +++ b/articles/cosmos-db/mongodb/vcore/vector-search-ai.md @@ -1,21 +1,21 @@ --- title: Open-source vector databases -titleSuffix: -description: Open-source vector database functionalities, examples, challenges, and solutions. +titleSuffix: Azure Cosmos DB for MongoDB vCore +description: Review open-source vector database functionalities, examples, challenges, and solutions in relation to Azure Cosmos DB for MongoDB vCore. author: gahl-levy ms.author: gahllevy ms.service: azure-cosmos-db ms.subservice: mongodb-vcore -ms.topic: conceptual -ms.date: 04/02/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ MongoDB vCore --- # Open-source vector databases -[!INCLUDE[MongoDB vCore](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)] - When developers select vector databases, the open-source options provide numerous benefits. "Open source" means that the software's source code is available freely, enabling users to customize the database according to their specific needs. This flexibility is beneficial for organizations that are subject to unique regulatory requirements on data, such as companies in the financial services industry. Another advantage of open-source vector databases is the strong community support they enjoy. Active user communities often contribute to the development of these databases, provide support, and share best practices, promoting innovation. @@ -78,6 +78,7 @@ A fully managed vector database that is integrated in a highly performant NoSQL An example is the Integrated Vector Database in Azure Cosmos DB for MongoDB. It allows developers to enjoy the same financial benefit associated with open-source vector databases, while the service provider handles maintenance, updates, and scalability. When it’s time to scale up operations, upgrading is quick and easy while keeping a low [total cost of ownership (TCO)](introduction.md#low-total-cost-of-ownership-tco). This service can also be used to conveniently [scale MongoDB](../reimagined.md) applications that are already in production. -## Next steps +## Next step + > [!div class="nextstepaction"] > [Use lifetime free tier of Integrated Vector Database in Azure Cosmos DB for MongoDB](free-tier.md) diff --git a/articles/cosmos-db/mongodb/vcore/vector-search.md b/articles/cosmos-db/mongodb/vcore/vector-search.md index 78ce50bb17..35502f1584 100644 --- a/articles/cosmos-db/mongodb/vcore/vector-search.md +++ b/articles/cosmos-db/mongodb/vcore/vector-search.md @@ -1,7 +1,7 @@ --- -title: Vector store +title: Integrated vector store titleSuffix: Azure Cosmos DB for MongoDB vCore -description: Use vector store in Azure Cosmos DB for MongoDB vCore to enhance AI-based applications. +description: Use the integrated vector store in Azure Cosmos DB for MongoDB vCore to enhance AI-based applications. author: gahl-levy ms.author: gahllevy ms.service: azure-cosmos-db @@ -9,15 +9,15 @@ ms.subservice: mongodb-vcore ms.custom: - ignite-2023 - ignite-2024 -ms.topic: conceptual -ms.date: 11/1/2023 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ MongoDB vCore --- -# Vector Store in vCore-based Azure Cosmos DB for MongoDB - -[!INCLUDE[MongoDB vCore](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)] +# Vector store in Azure Cosmos DB for MongoDB vCore Use the Integrated Vector Database in Azure Cosmos DB for MongoDB (vCore) to seamlessly connect your AI-based applications with your data that's stored in Azure Cosmos DB. This integration can include apps that you built by using [Azure OpenAI embeddings](/azure/ai-services/openai/tutorials/embeddings). The natively integrated vector database enables you to efficiently store, index, and query high-dimensional vector data that's stored directly in Azure Cosmos DB for MongoDB (vCore), along with the original data from which the vector data is created. It eliminates the need to transfer your data to alternative vector stores and incur additional costs. @@ -31,8 +31,8 @@ In a vector store, vector search algorithms are used to index and query embeddin In the Integrated Vector Database in Azure Cosmos DB for MongoDB (vCore), embeddings can be stored, indexed, and queried alongside the original data. This approach eliminates the extra cost of replicating data in a separate pure vector database. Moreover, this architecture keeps the vector embeddings and original data together, which better facilitates multi-modal data operations, and enables greater data consistency, scale, and performance. - ## Perform Vector Similarity search + Azure Cosmos DB for MongoDB (vCore) provides robust vector search capabilities, allowing you to perform high-speed similarity searches across complex datasets. To perform vector search in Azure Cosmos DB for MongoDB, you first need to create a vector index. Cosmos DB currently supports three types of vector indexes: - **DiskANN (Recommended)**: Ideal for large-scale datasets, leveraging SSDs for efficient memory usage while maintaining high recall in approximate nearest-neighbor (ANN) searches. @@ -64,20 +64,20 @@ DiskANN indexes are available on M40 tiers and above. To create the DiskANN inde } ``` -|Field |Type |Description | -|--------------------|---------|---------| -| `index_name` | string | Unique name of the index. | -| `path_to_property` | string | Path to the property that contains the vector. This path can be a top-level property or a dot notation path to the property. Vectors must be a `number[]` to be indexed and used in vector search results. Using another type, such as `double[]`, prevents the document from being indexed. Non-indexed documents won't be returned in the result of a vector search. | -| `kind` | string | Type of vector index to create. The options are `vector-ivf`, `vector-hnsw`, and `vector-diskann`. | -| `dimensions` | integer | Number of dimensions for vector similarity. DiskANN supports up to 2000 dimensions, with future support planned for 40,000+. | -| `similarity` | string | Similarity metric to use with the index. Possible options are `COS` (cosine distance), `L2` (Euclidean distance), and `IP` (inner product). | -| `maxDegree` | integer | Maximum number of edges per node in the graph. This parameter ranges from 20 to 2048 (default is 32). Higher `maxDegree` is suitable for datasets with high dimensionality and/or high accuracy requirements. | -| `lBuild` | integer | Sets the number of candidate neighbors evaluated during DiskANN index construction. This parameter, which ranges from 10 to 500 (default is 50), balances accuracy and computational overhead: higher values improve index quality and accuracy but increase build time | - -> [!Note] +| Field | Type | Description | +| --- | --- | --- | +| `index_name` | string | Unique name of the index. | +| `path_to_property` | string | Path to the property that contains the vector. This path can be a top-level property or a dot notation path to the property. Vectors must be a `number[]` to be indexed and used in vector search results. Using another type, such as `double[]`, prevents the document from being indexed. Non-indexed documents won't be returned in the result of a vector search. | +| `kind` | string | Type of vector index to create. The options are `vector-ivf`, `vector-hnsw`, and `vector-diskann`. | +| `dimensions` | integer | Number of dimensions for vector similarity. DiskANN supports up to 2000 dimensions, with future support planned for 40,000+. | +| `similarity` | string | Similarity metric to use with the index. Possible options are `COS` (cosine distance), `L2` (Euclidean distance), and `IP` (inner product). | +| `maxDegree` | integer | Maximum number of edges per node in the graph. This parameter ranges from 20 to 2048 (default is 32). Higher `maxDegree` is suitable for datasets with high dimensionality and/or high accuracy requirements. | +| `lBuild` | integer | Sets the number of candidate neighbors evaluated during DiskANN index construction. This parameter, which ranges from 10 to 500 (default is 50), balances accuracy and computational overhead: higher values improve index quality and accuracy but increase build time | + +> [!NOTE] > Enable the "DiskANN Vector Index for vCore-based Azure Cosmos DB for MongoDB" feature in the "Preview Features" tab of your Azure Subscription. Learn more about preview features [here](/azure/azure-resource-manager/management/preview-features). -### **Perform a vector search with DiskANN** +### Perform a vector search with DiskANN To perform a vector search, use the `$search` aggregation pipeline stage, and query with the `cosmosSearch` operator. DiskANN allows high-performance searches across massive datasets with **optional** filtering such as geospatial or text-based filters. @@ -97,8 +97,8 @@ To perform a vector search, use the `$search` aggregation pipeline stage, and qu }, ``` -|Field |Type |Description | -|---------|---------|---------| +| Field | Type | Description | +| --- | --- | --- | | `lSearch` | integer | Specifies the size of the dynamic candidate list for search. The default value is `40`, with a configurable range from `10` to `1000`. Increasing the value enhances recall but may reduce search speed. | | `k` | integer | Defines the number of search results to return. The `k` value must be less than or equal to `lSearch`. | @@ -168,7 +168,7 @@ This command creates a DiskANN vector index on the `contentVector` field in `exa ### Perform a Vector Search -To find documents with similar vectors within a specific geographic radius, specify the `queryVector` for similarity search and include a geospatial filter. +To find documents with similar vectors within a specific geographic radius, specify the `queryVector` for similarity search and include a geospatial filter. ```python query_vector = [0.52, 0.28, 0.12] @@ -251,13 +251,15 @@ You can create HNSW (Hierarchical Navigable Small World) indexes on M40 cluster } ``` -|Field |Type |Description | -|---------|---------|---------| -|`m` |integer |The max number of connections per layer (`16` by default, minimum value is `2`, maximum value is `100`). Higher m is suitable for datasets with high dimensionality and/or high accuracy requirements. | -|`efConstruction` |integer |the size of the dynamic candidate list for constructing the graph (`64` by default, minimum value is `4`, maximum value is `1000`). Higher `efConstruction` will result in better index quality and higher accuracy, but it will also increase the time required to build the index. `efConstruction` has to be at least `2 * m` | +| Field | Type | Description | +| --- | --- | --- | +| `m` | integer | The max number of connections per layer (`16` by default, minimum value is `2`, maximum value is `100`). Higher m is suitable for datasets with high dimensionality and/or high accuracy requirements. | +| `efConstruction` | integer | the size of the dynamic candidate list for constructing the graph (`64` by default, minimum value is `4`, maximum value is `1000`). Higher `efConstruction` will result in better index quality and higher accuracy, but it will also increase the time required to build the index. `efConstruction` has to be at least `2 * m` | ### Perform a vector search with HNSW + To perform a vector search, use the `$search` aggregation pipeline stage the query with the `cosmosSearch` operator. + ```javascript { "$search": { @@ -270,14 +272,15 @@ To perform a vector search, use the `$search` aggregation pipeline stage the que } } ``` -|Field |Type |Description | -|---------|---------|---------| -|`efSearch` |integer |The size of the dynamic candidate list for search (`40` by default). A higher value provides better recall at the cost of speed. | + +| Field | Type | Description | +| --- | --- | --- | +| `efSearch` | integer | The size of the dynamic candidate list for search (`40` by default). A higher value provides better recall at the cost of speed. | > [!NOTE] > Creating an HSNW index with large datasets can result in your Azure Cosmos DB for MongoDB vCore resource running out of memory, or can limit the performance of other operations running on your database. If you encounter such issues, these can be mitigated by scaling your resource to a higher cluster tier, or creating a new DiskANN vector index. -## Example using an HNSW index. +## Example using an HNSW index The following examples show you how to index vectors, add documents that have vector properties, perform a vector search, and retrieve the index configuration. @@ -305,6 +308,7 @@ db.runCommand({ ] }); ``` + This command creates an HNSW index against the `contentVector` property in the documents that are stored in the specified collection, `exampleCollection`. The `cosmosSearchOptions` property specifies the parameters for the HNSW vector index. If your document has the vector stored in a nested property, you can set this property by using a dot notation path. For example, you might use `text.contentVector` if `contentVector` is a subproperty of `text`. ### Add vectors to your database @@ -366,7 +370,6 @@ In this example, a vector search is performed by using `queryVector` as an input ] ``` - ### [IVF](#tab/IVF) To create a vector index using the IVF (Inverted File) algorithm, use the following `createIndexes` template and set the `"kind"` parameter to `"vector-ivf"`: @@ -424,13 +427,12 @@ To perform a vector search, use the `$search` aggregation pipeline stage in a Mo } } ``` -To retrieve the similarity score (`searchScore`) along with the documents found by the vector search, use the `$project` operator to include `searchScore` and rename it as `` in the results. Then the document is also projected as nested object. Note that the similarity score is calculated using the metric defined in the vector index. +To retrieve the similarity score (`searchScore`) along with the documents found by the vector search, use the `$project` operator to include `searchScore` and rename it as `` in the results. Then the document is also projected as nested object. Note that the similarity score is calculated using the metric defined in the vector index. > [!IMPORTANT] > Vectors must be a `number[]` to be indexed. Using another type, such as `double[]`, prevents the document from being indexed. Non-indexed documents won't be returned in the result of a vector search. - ## Example using an IVF Index Inverted File (IVF) Indexing is a method that organizes vectors into clusters. During a vector search, the query vector is first compared against the centers of these clusters. The search is then conducted within the cluster whose center is closest to the query vector. @@ -506,12 +508,12 @@ To perform a vector search, use the `$search` aggregation pipeline stage in a Mo } } ``` + To retrieve the similarity score (`searchScore`) along with the documents found by the vector search, use the `$project` operator to include `searchScore` and rename it as `` in the results. Then the document is also projected as nested object. Note that the similarity score is calculated using the metric defined in the vector index. ### Query vectors and vector distances (aka similarity scores) using $search" -Continuing with the last example, create another vector, `queryVector`. Vector search measures the distance between `queryVector` and the vectors in the `vectorContent` path of your documents. You can set the number of results that the search returns by setting the parameter `k`, which is set to `2` here. You can also set `nProbes`, which is an integer that controls the number of nearby clusters that are inspected in each search. A higher value may improve accuracy, however the search will be slower as a result. This is an optional parameter with a default value of 1 and cannot be larger than the `numLists` value specified in the vector index. - +Continuing with the last example, create another vector, `queryVector`. Vector search measures the distance between `queryVector` and the vectors in the `vectorContent` path of your documents. You can set the number of results that the search returns by setting the parameter `k`, which is set to `2` here. You can also set `nProbes`, which is an integer that controls the number of nearby clusters that are inspected in each search. A higher value may improve accuracy, however the search will be slower as a result. This is an optional parameter with a default value of 1 and cannot be larger than the `numLists` value specified in the vector index. ```javascript const queryVector = [0.52, 0.28, 0.12]; @@ -557,6 +559,7 @@ In this example, a vector search is performed by using `queryVector` as an input } ] ``` + --- ### Get vector index definitions @@ -587,8 +590,8 @@ In this example, `vectorIndex` is returned with all the `cosmosSearch` parameter ] ``` - ## Filtered vector search (preview) + You can now execute vector searches with any supported query filter such as `$lt`, `$lte`, `$eq`, `$neq`, `$gte`, `$gt`, `$in`, `$nin`, and `$regex`. Enable the "filtering vector search" feature in the "Preview Features" tab of your Azure Subscription. Learn more about preview features [here](/azure/azure-resource-manager/management/preview-features). First, you'll need to define an index for your filter in addition to a vector index. For example, you can define the filter index on a property @@ -624,20 +627,24 @@ db.exampleCollection.aggregate([ } ]); ``` + > [!IMPORTANT] -> While in preview, filtered vector search may require you to adjust your vector index parameters to achieve higher accuracy. For example, increasing `m`, `efConstruction`, or `efSearch` when using HNSW, or `numLists`, or `nProbes` when using IVF, may lead to better results. You should test your configuration before use to ensure that the results are satisfactory. +> While in preview, filtered vector search may require you to adjust your vector index parameters to achieve higher accuracy. For example, increasing `m`, `efConstruction`, or `efSearch` when using HNSW, or `numLists`, or `nProbes` when using IVF, may lead to better results. You should test your configuration before use to ensure that the results are satisfactory. ## Use LLM Orchestration tools ### Use as a vector database with Semantic Kernel + Use Semantic Kernel to orchestrate your information retrieval from Azure Cosmos DB for MongoDB vCore and your LLM. Learn more [here](https://github.com/microsoft/semantic-kernel/tree/main/python/semantic_kernel/connectors/memory/azure_cosmosdb). https://github.com/microsoft/semantic-kernel/tree/main/python/semantic_kernel/connectors/memory/azure_cosmosdb ### Use as a vector database with LangChain + Use LangChain to orchestrate your information retrieval from Azure Cosmos DB for MongoDB vCore and your LLM. Learn more [here](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db). ### Use as a semantic cache with LangChain + Use LangChain and Azure Cosmos DB for MongoDB (vCore) to orchestrate Semantic Caching, using previously recorded LLM responses that can save you LLM API costs and reduce latency for responses. Learn more [here](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db/) ## Features and limitations From 07f291d8f02ee67310ab44da5b50f759be9d08b1 Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 12:56:57 -0500 Subject: [PATCH 10/14] Refresh AI articles (#3) --- .../nosql/how-to-dotnet-vector-index-query.md | 77 ++++++------ .../nosql/how-to-java-vector-index-query.md | 83 +++++++------ .../how-to-javascript-vector-index-query.md | 53 ++++---- .../nosql/how-to-manage-indexing-policy.md | 46 +++---- .../nosql/how-to-python-vector-index-query.md | 115 +++++++++--------- .../nosql/multi-tenancy-vector-search.md | 47 +++---- .../nosql/query/how-to-enable-use-copilot.md | 12 +- articles/cosmos-db/nosql/vector-search.md | 100 +++++++-------- 8 files changed, 271 insertions(+), 262 deletions(-) diff --git a/articles/cosmos-db/nosql/how-to-dotnet-vector-index-query.md b/articles/cosmos-db/nosql/how-to-dotnet-vector-index-query.md index bc2faaeced..93e1b5ec17 100644 --- a/articles/cosmos-db/nosql/how-to-dotnet-vector-index-query.md +++ b/articles/cosmos-db/nosql/how-to-dotnet-vector-index-query.md @@ -7,27 +7,29 @@ ms.author: jacodel ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 08/01/2023 +ms.date: 12/03/2024 ms.custom: query-reference, devx-track-dotnet, build-2024, ignite-2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Index and query vectors in Azure Cosmos DB for NoSQL in .NET -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] Before you use vector indexing and search, you must first enable the feature. This article covers the following steps: 1. Enabling the Vector Search in Azure Cosmos DB for NoSQL feature. -2. Setting up the Azure Cosmos DB container for vector search -3. Authoring vector embedding policy -4. Adding vector indexes to the container indexing policy -5. Creating a container with vector indexes and vector embedding policy -6. Performing a vector search on the stored data +1. Setting up the Azure Cosmos DB container for vector search +1. Authoring vector embedding policy +1. Adding vector indexes to the container indexing policy +1. Creating a container with vector indexes and vector embedding policy +1. Performing a vector search on the stored data This guide walks through the process of creating vector data, indexing the data, and then querying the data in a container. ## Prerequisites + - An existing Azure Cosmos DB for NoSQL account. - If you don't have an Azure subscription, [Try Azure Cosmos DB for NoSQL free](https://cosmos.azure.com/try/). - If you have an existing Azure subscription, [create a new Azure Cosmos DB for NoSQL account](how-to-create-account.md). @@ -38,10 +40,10 @@ This guide walks through the process of creating vector data, indexing the data, Vector search for Azure Cosmos DB for NoSQL requires enabling the feature by completing the following steps: 1. Navigate to your Azure Cosmos DB for NoSQL resource page. -2. Select the "Features" pane under the "Settings" menu item. -3. Select for "Vector Search in Azure Cosmos DB for NoSQL." -4. Read the description of the feature to confirm you want to enable it. -5. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. +1. Select the "Features" pane under the "Settings" menu item. +1. Select for "Vector Search in Azure Cosmos DB for NoSQL." +1. Read the description of the feature to confirm you want to enable it. +1. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. > [!TIP] > Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. @@ -53,20 +55,18 @@ Vector search for Azure Cosmos DB for NoSQL requires enabling the feature by com > --capabilities EnableNoSQLVectorSearch > ``` - > [!NOTE] > The registration request will be autoapproved; however, it may take 15 minutes to take effect. -## Understanding the steps involved in vector search - +## Understanding the steps involved in vector search -Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). +Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). -1. Create and store vector embeddings for the fields on which you want to perform vector search. -2. Specify the vector embedding paths in the vector embedding policy. -3. Include any desired vector indexes in the indexing policy for the container. +1. Create and store vector embeddings for the fields on which you want to perform vector search. +1. Specify the vector embedding paths in the vector embedding policy. +1. Include any desired vector indexes in the indexing policy for the container. -For subsequent sections of this article, we consider the below structure for the items stored in our container: +For subsequent sections of this article, we consider the below structure for the items stored in our container: ```json { @@ -79,18 +79,19 @@ For subsequent sections of this article, we consider the below structure for the } ``` -## Creating a vector embedding policy for your container. +## Creating a vector embedding policy for your container + Next, you need to define a container vector policy. This policy provides information that is used to inform the Azure Cosmos DB query engine how to handle vector properties in the VectorDistance system functions. This policy also informs the vector indexing policy of necessary information, should you choose to specify one. The following information is included in the contained vector policy: - * “path”: The property path that contains vectors  - * “datatype”: The type of the elements of the vector (default Float32)  - * “dimensions”: The length of each vector in the path (default 1536)  - * “distanceFunction”: The metric used to compute distance/similarity (default Cosine)  +- “path”: The property path that contains vectors  +- “datatype”: The type of the elements of the vector (default Float32)  +- “dimensions”: The length of each vector in the path (default 1536)  +- “distanceFunction”: The metric used to compute distance/similarity (default Cosine)  -For our example with book details, the vector policy can look like the example JSON below: +For our example with book details, the vector policy can look like the example JSON below: -```csharp +```csharp Database db = await client.CreateDatabaseIfNotExistsAsync("vector-benchmarking"); List embeddings = new List() { @@ -109,13 +110,13 @@ For our example with book details, the vector policy can look like the example J Dimensions = 10, } }; -``` +``` +## Creating a vector index in the indexing policy -## Creating a vector index in the indexing policy -Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. Currently, the vector search feature for Azure Cosmos DB for NoSQL is supported only on new containers so you need to apply the vector policy during the time of container creation and it can’t be modified later. For this example, the indexing policy would look something like this: +Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. Currently, the vector search feature for Azure Cosmos DB for NoSQL is supported only on new containers so you need to apply the vector policy during the time of container creation and it can’t be modified later. For this example, the indexing policy would look something like this: -```csharp +```csharp Collection collection = new Collection(embeddings); ContainerProperties properties = new ContainerProperties(id: "vector-container", partitionKeyPath: "/id") { @@ -134,13 +135,14 @@ Once the vector embedding paths are decided, vector indexes need to be added to }; properties.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); properties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/vector/*" }); -``` ->[!IMPORTANT] +``` + +> [!IMPORTANT] > The vector path added to the "excludedPaths" section of the indexing policy to ensure optimized performance for insertion. Not adding the vector path to "excludedPaths" will result in higher RU charge and latency for vector insertions. -## Running vector similarity search query +## Running vector similarity search query -Once you create a container with the desired vector policy, and insert vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here: +Once you create a container with the desired vector policy, and insert vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here: ```sql SELECT TOP 10 c.title, VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10]) AS SimilarityScore   @@ -150,7 +152,7 @@ ORDER BY VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10])   This query retrieves the book titles along with similarity scores with respect to your query. Here's an example in .NET: -```csharp +```csharp float[] embedding = {1f,2f,3f,4f,5f,6f,7f,8f,9f,10f}; var queryDef = new QueryDefinition( query: $"SELECT c.title, VectorDistance(c.contentVector,@embedding) AS SimilarityScore FROM c ORDER BY VectorDistance(c.contentVector,@embedding)" @@ -166,9 +168,10 @@ This query retrieves the book titles along with similarity scores with respect t Console.WriteLine($"Found item:\t{item}"); } } -``` +``` + +## Related content -## Next steps - [VectorDistance system function](query/vectordistance.md) - [Vector indexing](../index-policy.md) - [Setup Azure Cosmos DB for NoSQL for vector search](../vector-search.md). diff --git a/articles/cosmos-db/nosql/how-to-java-vector-index-query.md b/articles/cosmos-db/nosql/how-to-java-vector-index-query.md index 79d83c7e5d..de951a5973 100644 --- a/articles/cosmos-db/nosql/how-to-java-vector-index-query.md +++ b/articles/cosmos-db/nosql/how-to-java-vector-index-query.md @@ -7,39 +7,43 @@ ms.author: jacodel ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 08/01/2023 +ms.date: 12/03/2024 ms.custom: query-reference, devx-track-java, build-2024, devx-track-extended-java, ignite-2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- -# Index and query vectors in Azure Cosmos DB for NoSQL in Java +# Index and query vectors in Azure Cosmos DB for NoSQL in Java Before you use vector indexing and search, you must first enable the feature. This article covers the following steps: 1. Enabling the Vector Search in Azure Cosmos DB for NoSQL feature -2. Setting up the Azure Cosmos DB container for vector search -3. Authoring vector embedding policy -4. Adding vector indexes to the container indexing policy -5. Creating a container with vector indexes and vector embedding policy -6. Performing a vector search on the stored data +1. Setting up the Azure Cosmos DB container for vector search +1. Authoring vector embedding policy +1. Adding vector indexes to the container indexing policy +1. Creating a container with vector indexes and vector embedding policy +1. Performing a vector search on the stored data This guide walks through the process of creating vector data, indexing the data, and then querying the data in a container. ## Prerequisites + - An existing Azure Cosmos DB for NoSQL account. - If you don't have an Azure subscription, [Try Azure Cosmos DB for NoSQL free](https://cosmos.azure.com/try/). - If you have an existing Azure subscription, [create a new Azure Cosmos DB for NoSQL account](how-to-create-account.md). - Latest version of the Azure Cosmos DB [Java](sdk-java-v4.md) SDK. ## Enable the feature + Vector search for Azure Cosmos DB for NoSQL requires feature enablement. Follow the below steps to register: 1. Navigate to your Azure Cosmos DB for NoSQL resource page. -2. Select the "Features" pane under the "Settings" menu item. -3. Select for "Vector Search in Azure Cosmos DB for NoSQL." -4. Read the description of the feature to confirm you want to enable it. -5. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. +1. Select the "Features" pane under the "Settings" menu item. +1. Select for "Vector Search in Azure Cosmos DB for NoSQL." +1. Read the description of the feature to confirm you want to enable it. +1. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. > [!TIP] > Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. @@ -54,42 +58,44 @@ Vector search for Azure Cosmos DB for NoSQL requires feature enablement. Follow > [!NOTE] > The registration request will be autoapproved; however, it may take 15 minutes to take effect. -## Understanding the steps involved in vector search +## Understanding the steps involved in vector search -The following steps assume that you know how to [setup a Cosmos DB NoSQL account and create a database](quickstart-portal.md). The vector search feature is currently not supported on the existing containers, so you need to create a new container and specify the container-level vector embedding policy, and the vector indexing policy at the time of container creation. +The following steps assume that you know how to [setup a Cosmos DB NoSQL account and create a database](quickstart-portal.md). The vector search feature is currently not supported on the existing containers, so you need to create a new container and specify the container-level vector embedding policy, and the vector indexing policy at the time of container creation. -Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). +Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). -1. Create and store vector embeddings for the fields on which you want to perform vector search. -2. Specify the vector embedding paths in the vector embedding policy. -3. Include any desired vector indexes in the indexing policy for the container. +1. Create and store vector embeddings for the fields on which you want to perform vector search. +2. Specify the vector embedding paths in the vector embedding policy. +3. Include any desired vector indexes in the indexing policy for the container. -For subsequent sections of this article, we consider the below structure for the items stored in our container: +For subsequent sections of this article, we consider the below structure for the items stored in our container: ```json { -"title": "book-title", -"author": "book-author", -"isbn": "book-isbn", -"description": "book-description", -"contentVector": [2, -1, 4, 3, 5, -2, 5, -7, 3, 1], -"coverImageVector": [0.33, -0.52, 0.45, -0.67, 0.89, -0.34, 0.86, -0.78] + "title": "book-title", + "author": "book-author", + "isbn": "book-isbn", + "description": "book-description", + "contentVector": [2, -1, 4, 3, 5, -2, 5, -7, 3, 1], + "coverImageVector": [0.33, -0.52, 0.45, -0.67, 0.89, -0.34, 0.86, -0.78] } ``` First, create the `CosmosContainerProperties` object. + ```java CosmosContainerProperties collectionDefinition = new CosmosContainerProperties(UUID.randomUUID().toString(), "Partition_Key_Def"); ``` ## Creating a vector embedding policy for your container + Next, you need to define a container vector policy. This policy provides information that is used to inform the Azure Cosmos DB query engine how to handle vector properties in the VectorDistance system functions. This also informs the vector indexing policy of necessary information, should you choose to specify one. The following information is included in the contained vector policy: - * “path”: The property path that contains vectors  - * “datatype”: The type of the elements of the vector (default Float32)  - * “dimensions”: The length of each vector in the path (default 1536)  - * “distanceFunction”: The metric used to compute distance/similarity (default Cosine)  +- “path”: The property path that contains vectors  +- “datatype”: The type of the elements of the vector (default Float32)  +- “dimensions”: The length of each vector in the path (default 1536)  +- “distanceFunction”: The metric used to compute distance/similarity (default Cosine)  For our example with book details, the vector policy may look like the example JSON: @@ -114,11 +120,11 @@ cosmosVectorEmbeddingPolicy.setCosmosVectorEmbeddings(Arrays.asList(embedding1, collectionDefinition.setVectorEmbeddingPolicy(cosmosVectorEmbeddingPolicy); ``` +## Creating a vector index in the indexing policy -## Creating a vector index in the indexing policy -Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. Currently, the vector search feature for Azure Cosmos DB for NoSQL is supported only on new containers so you need to apply the vector policy during the time of container creation and it can’t be modified later. For this example, the indexing policy would look something like below: +Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. Currently, the vector search feature for Azure Cosmos DB for NoSQL is supported only on new containers so you need to apply the vector policy during the time of container creation and it can’t be modified later. For this example, the indexing policy would look something like below: -```java +```java IndexingPolicy indexingPolicy = new IndexingPolicy(); indexingPolicy.setIndexingMode(IndexingMode.CONSISTENT); ExcludedPath excludedPath1 = new ExcludedPath("/coverImageVector/*"); @@ -140,22 +146,20 @@ cosmosVectorIndexSpec2.setType(CosmosVectorIndexType.DISK_ANN.toString()); indexingPolicy.setVectorIndexes(Arrays.asList(cosmosVectorIndexSpec1, cosmosVectorIndexSpec2, cosmosVectorIndexSpec3)); collectionDefinition.setIndexingPolicy(indexingPolicy); -``` +``` Finally, create the container with the container index policy and the vector index policy. -```java +```java database.createContainer(collectionDefinition).block(); ``` - - ->[!IMPORTANT] +> [!IMPORTANT] > The vector path added to the "excludedPaths" section of the indexing policy to ensure optimized performance for insertion. Not adding the vector path to "excludedPaths" will result in higher RU charge and latency for vector insertions. -## Running vector similarity search query +## Running vector similarity search query -Once you have created a container with the desired vector policy, and inserted vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here: +Once you have created a container with the desired vector policy, and inserted vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here: ```sql SELECT TOP 10 c.title, VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10]) AS SimilarityScore   @@ -181,7 +185,8 @@ ArrayList paramList = new ArrayList(); } ``` -## Next steps +## Related content + - [VectorDistance system function](query/vectordistance.md) - [Vector indexing](../index-policy.md) - [Setup Azure Cosmos DB for NoSQL for vector search](../vector-search.md). diff --git a/articles/cosmos-db/nosql/how-to-javascript-vector-index-query.md b/articles/cosmos-db/nosql/how-to-javascript-vector-index-query.md index c5d9731e4e..9d5c37bfff 100644 --- a/articles/cosmos-db/nosql/how-to-javascript-vector-index-query.md +++ b/articles/cosmos-db/nosql/how-to-javascript-vector-index-query.md @@ -7,29 +7,24 @@ ms.author: jacodel ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 08/08/2024 +ms.date: 12/03/2024 ms.custom: query-reference, build-2024, devx-track-js, ignite-2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Index and query vectors in Azure Cosmos DB for NoSQL in JavaScript -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] - Before you use Vector Indexing and Search, you must first enable the feature. This article covers the following steps: 1. Enabling the Vector Search in Azure Cosmos DB for NoSQL feature - -2. Setting up the Azure Cosmos DB container for vector search - -3. Authoring vector embedding policy - -4. Adding vector indexes to the container indexing policy - -5. Creating a container with vector indexes and vector embedding policy - -6. Performing a vector search on the stored data +1. Setting up the Azure Cosmos DB container for vector search +1. Authoring vector embedding policy +1. Adding vector indexes to the container indexing policy +1. Creating a container with vector indexes and vector embedding policy +1. Performing a vector search on the stored data This guide walks through the process of creating vector data, indexing the data, and then querying the data in a container. @@ -45,24 +40,20 @@ This guide walks through the process of creating vector data, indexing the data, Vector search for Azure Cosmos DB for NoSQL requires enabling the feature by completing the following steps: 1. Navigate to your Azure Cosmos DB for NoSQL resource page. - 1. Select the "Features" pane under the "Settings" menu item. - 1. Select for "Vector Search in Azure Cosmos DB for NoSQL." - -2. Read the description of the feature to confirm you want to enable it. - -3. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. - -> [!TIP] -> Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. -> -> ```azurecli -> az cosmosdb update \ -> --resource-group \ -> --name \ -> --capabilities EnableNoSQLVectorSearch -> ``` +1. Read the description of the feature to confirm you want to enable it. +1. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. + + > [!TIP] + > Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. + > + > ```azurecli + > az cosmosdb update \ + > --resource-group \ + > --name \ + > --capabilities EnableNoSQLVectorSearch + > ``` > [!NOTE] > The registration request will be autoapproved; however, it may take 15 minutes to take effect. @@ -74,8 +65,8 @@ The following steps assume that you know how to [setup a Cosmos DB NoSQL account Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the "contentVector" property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the "title" "author" "isbn" and "description" properties before creating the embedding). The second is "coverImageVector," which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). 1. Create and store vector embeddings for the fields on which you want to perform vector search. -2. Specify the vector embedding paths in the vector embedding policy. -3. Include any desired vector indexes in the indexing policy for the container. +1. Specify the vector embedding paths in the vector embedding policy. +1. Include any desired vector indexes in the indexing policy for the container. For subsequent sections of this article, we consider this structure for the items stored in our container: diff --git a/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md b/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md index a7cee6c9f4..329e13b9e8 100644 --- a/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md +++ b/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md @@ -5,15 +5,16 @@ author: markjbrown ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 03/08/2023 +ms.date: 12/03/2024 ms.author: mjbrown ms.custom: devx-track-csharp, build-2024, ignite-2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Manage indexing policies in Azure Cosmos DB -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] In Azure Cosmos DB, data is indexed following [indexing policies](../index-policy.md) that are defined for each container. The default indexing policy for newly created containers enforces range indexes for any string or number. You can override this policy with your own custom indexing policy. @@ -24,7 +25,7 @@ In Azure Cosmos DB, data is indexed following [indexing policies](../index-polic Here are some examples of indexing policies shown in [their JSON format](../index-policy.md). They're exposed on the Azure portal in JSON format. The same parameters can be set through the Azure CLI or any SDK. -### Opt-out policy to selectively exclude some property paths +### Opt-out policy to selectively exclude some property paths ```json { @@ -69,7 +70,7 @@ Here are some examples of indexing policies shown in [their JSON format](../inde > [!NOTE] > We generally recommend that you use an *opt-out* indexing policy. Azure Cosmos DB proactively indexes any new property that might be added to your data model. -### Using a spatial index on a specific property path only +### Using a spatial index on a specific property path only ```json { @@ -100,10 +101,11 @@ Here are some examples of indexing policies shown in [their JSON format](../inde ``` ## Vector indexing policy examples -In addition to including or excluding paths for individual properties, you can also specify a [vector index](../index-policy.md#vector-indexes). In general, vector indexes should be specified whenever the `VectorDistance` system function is used to measure similarity between a query vector and a vector property. + +In addition to including or excluding paths for individual properties, you can also specify a [vector index](../index-policy.md#vector-indexes). In general, vector indexes should be specified whenever the `VectorDistance` system function is used to measure similarity between a query vector and a vector property. > [!NOTE] -> Before proceeding, you must enable the [Azure Cosmos DB NoSQL Vector Indexing and Search](vector-search.md#enable-the-vector-indexing-and-search-feature). +> Before proceeding, you must enable the [Azure Cosmos DB NoSQL Vector Indexing and Search](vector-search.md#enable-the-vector-indexing-and-search-feature). >[!IMPORTANT] > A vector indexing policy must be on the same path defined in the container's vector policy. [Learn more about container vector policies](vector-search.md#container-vector-policies). @@ -134,7 +136,7 @@ In addition to including or excluding paths for individual properties, you can a } ``` ->[!IMPORTANT] +> [!IMPORTANT] > The vector path added to the "excludedPaths" section of the indexing policy to ensure optimized performance for insertion. Not adding the vector path to "excludedPaths" will result in higher RU charge and latency for vector insertions. > [!IMPORTANT] @@ -155,8 +157,9 @@ The `quantizedFlat` index stores quantized or compressed vectors on the index. V The `diskANN` index is a separate index defined specifically for vectors leveraging [DiskANN](https://www.microsoft.com/research/publication/diskann-fast-accurate-billion-point-nearest-neighbor-search-on-a-single-node/), a suite of highly performant vector indexing algorithms developed by Microsoft Research. DiskANN indexes can offer some of the lowest latency, highest query-per-second (QPS), and lowest RU cost queries at high accuracy. However, since DiskANN is an approximate nearest neighbors (ANN) index, the accuracy may be lower than `quantizedFlat` or `flat`. The `diskANN` and `quantizedFlat` indexes can take optional index build parameters that can be used to tune the accuracy vs latency trade off that applies to every Approximate Nearest Neighbors vector index. -* `quantizationByteSize`: Sets the size (in bytes) for product quantization. Min=1, Default=dynamic (system decides), Max=512. Setting this larger may result in higher accuracy vector searches at expense of higher RU cost and higher latency. This applies to both `quantizedFlat` and `DiskANN` index types. -* `indexingSearchListSize`: Sets how many vectors to search over during index build construction. Min=10, Default=100, Max=500. Setting this larger may result in higher accuracy vector searches at the expense of longer index build times and higher vector ingest latencies. This applies to `DiskANN` indexes only. + +- `quantizationByteSize`: Sets the size (in bytes) for product quantization. Min=1, Default=dynamic (system decides), Max=512. Setting this larger may result in higher accuracy vector searches at expense of higher RU cost and higher latency. This applies to both `quantizedFlat` and `DiskANN` index types. +- `indexingSearchListSize`: Sets how many vectors to search over during index build construction. Min=10, Default=100, Max=500. Setting this larger may result in higher accuracy vector searches at the expense of longer index build times and higher vector ingest latencies. This applies to `DiskANN` indexes only. ### Tuple indexing policy examples @@ -185,8 +188,7 @@ WHERE WHERE ev.name = ‘M&M’ AND ev.category = ‘Candy’) ``` - -## Composite indexing policy examples +## Composite indexing policy examples In addition to including or excluding paths for individual properties, you can also specify a composite index. To perform a query that has an `ORDER BY` clause for multiple properties, a [composite index](../index-policy.md#composite-indexes) is required on those properties. If the query includes filters along with sorting on multiple properties, you may need more than one composite index. @@ -361,11 +363,10 @@ In Azure Cosmos DB, the indexing policy can be updated using any of the followin An [indexing policy update](../index-policy.md#modifying-the-indexing-policy) triggers an index transformation. The progress of this transformation can also be tracked from the SDKs. > [!NOTE] -> When you update indexing policy, writes to Azure Cosmos DB are uninterrupted. Learn more about [indexing transformations](../index-policy.md#modifying-the-indexing-policy) - -> [!IMPORTANT] -> Removing an index takes effect immediately, whereas adding a new index takes some time as it requires an indexing transformation. When replacing one index with another (for example, replacing a single property index with a composite-index) make sure to add the new index first and then wait for the index transformation to complete **before** you remove the previous index from the indexing policy. Otherwise this will negatively affect your ability to query the previous index and may break any active workloads that reference the previous index. +> When you update indexing policy, writes to Azure Cosmos DB are uninterrupted. Learn more about [indexing transformations](../index-policy.md#modifying-the-indexing-policy). +> [!IMPORTANT] +> Removing an index takes effect immediately, whereas adding a new index takes some time as it requires an indexing transformation. When replacing one index with another (for example, replacing a single property index with a composite-index) make sure to add the new index first and then wait for the index transformation to complete **before** you remove the previous index from the indexing policy. Otherwise this will negatively affect your ability to query the previous index and may break any active workloads that reference the previous index. ### Use the Azure portal @@ -393,9 +394,9 @@ To create a container with a custom indexing policy, see [Create a container wit To create a container with a custom indexing policy, see [Create a container with a custom index policy using PowerShell](manage-with-powershell.md#create-container-custom-index). -### Use the .NET SDK +### Use the .NET SDK -# [.NET SDK V3](#tab/dotnetv3) +#### [.NET SDK V3](#tab/dotnetv3) The `ContainerProperties` object from the [.NET SDK v3](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/) exposes an `IndexingPolicy` property that lets you change the `IndexingMode` and add or remove `IncludedPaths` and `ExcludedPaths`. For more information, see [Quickstart: Azure Cosmos DB for NoSQL client library for .NET](quickstart-dotnet.md). @@ -452,7 +453,7 @@ await client.GetDatabase("database").DefineContainer(name: "container", partitio .CreateIfNotExistsAsync(); ``` -# [.NET SDK V2](#tab/dotnetv2) +#### [.NET SDK V2](#tab/dotnetv2) The `DocumentCollection` object from the [.NET SDK v2](https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/) exposes an `IndexingPolicy` property that lets you change the `IndexingMode` and add or remove `IncludedPaths` and `ExcludedPaths`. @@ -635,6 +636,7 @@ const containerResponse = await client.database('database').container('container // retrieve the index transformation progress from the response headers const indexTransformationProgress = replaceResponse.headers['x-ms-documentdb-collection-index-transformation-progress']; ``` + Add a composite index: ```javascript @@ -669,7 +671,7 @@ Add a composite index: ### Use the Python SDK -# [Python SDK V3](#tab/pythonv3) +#### [Python SDK V3](#tab/pythonv3) When you use the [Python SDK V3](https://pypi.org/project/azure-cosmos/), the container configuration is managed as a dictionary. From this dictionary, you can access the indexing policy and all its attributes. For more information, see [Quickstart: Azure Cosmos DB for NoSQL client library for Python](quickstart-python.md). @@ -733,7 +735,7 @@ Update the container with changes: response = client.ReplaceContainer(containerPath, container) ``` -# [Python SDK V4](#tab/pythonv4) +#### [Python SDK V4](#tab/pythonv4) When you use the [Python SDK V4](https://pypi.org/project/azure-cosmos/), the container configuration is managed as a dictionary. From this dictionary, you can access the indexing policy and all its attributes. @@ -808,9 +810,7 @@ container_client.read(populate_quota_info = True, --- -## Next steps - -Read more about the indexing in the following articles: +## Related content - [Indexing overview](../index-overview.md) - [Indexing policy](../index-policy.md) diff --git a/articles/cosmos-db/nosql/how-to-python-vector-index-query.md b/articles/cosmos-db/nosql/how-to-python-vector-index-query.md index 430ba92e6c..d7c158e43c 100644 --- a/articles/cosmos-db/nosql/how-to-python-vector-index-query.md +++ b/articles/cosmos-db/nosql/how-to-python-vector-index-query.md @@ -7,90 +7,93 @@ ms.author: jacodel ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: how-to -ms.date: 08/01/2023 +ms.date: 12/03/2024 ms.custom: query-reference, devx-track-python, build-2024, ignite-2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Index and query vectors in Azure Cosmos DB for NoSQL in Python -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] Before you use vector indexing and search, you must first enable the feature. This article covers the following steps: 1. Enabling the Vector Search in Azure Cosmos DB for NoSQL feature -2. Setting up the Azure Cosmos DB container for vector search -3. Authoring vector embedding policy -4. Adding vector indexes to the container indexing policy -5. Creating a container with vector indexes and vector embedding policy -6. Performing a vector search on the stored data +1. Setting up the Azure Cosmos DB container for vector search +1. Authoring vector embedding policy +1. Adding vector indexes to the container indexing policy +1. Creating a container with vector indexes and vector embedding policy +1. Performing a vector search on the stored data This guide walks through the process of creating vector data, indexing the data, and then querying the data in a container. ## Prerequisites + - An existing Azure Cosmos DB for NoSQL account. - If you don't have an Azure subscription, [Try Azure Cosmos DB for NoSQL free](https://cosmos.azure.com/try/). - If you have an existing Azure subscription, [create a new Azure Cosmos DB for NoSQL account](how-to-create-account.md). - Latest version of the Azure Cosmos DB [Python](sdk-python.md) SDK. ## Enable the feature + Vector search for Azure Cosmos DB for NoSQL requires enabling the feature by completing the following steps: 1. Navigate to your Azure Cosmos DB for NoSQL resource page. -2. Select the "Features" pane under the "Settings" menu item. -3. Select for "Vector Search in Azure Cosmos DB for NoSQL." -4. Read the description of the feature to confirm you want to enable it. -5. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. - -> [!TIP] -> Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. -> -> ```azurecli -> az cosmosdb update \ -> --resource-group \ -> --name \ -> --capabilities EnableNoSQLVectorSearch -> ``` - +1. Select the "Features" pane under the "Settings" menu item. +1. Select for "Vector Search in Azure Cosmos DB for NoSQL." +1. Read the description of the feature to confirm you want to enable it. +1. Select "Enable" to turn on vector search in Azure Cosmos DB for NoSQL. + + > [!TIP] + > Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. + > + > ```azurecli + > az cosmosdb update \ + > --resource-group \ + > --name \ + > --capabilities EnableNoSQLVectorSearch + > ``` > [!NOTE] > The registration request will be autoapproved; however, it may take 15 minutes to take effect. -## Understanding the steps involved in vector search +## Understanding the steps involved in vector search -The following steps assume that you know how to [setup a Cosmos DB NoSQL account and create a database](quickstart-portal.md). The vector search feature is currently not supported on the existing containers, so you need to create a new container and specify the container-level vector embedding policy, and the vector indexing policy at the time of container creation. +The following steps assume that you know how to [setup a Cosmos DB NoSQL account and create a database](quickstart-portal.md). The vector search feature is currently not supported on the existing containers, so you need to create a new container and specify the container-level vector embedding policy, and the vector indexing policy at the time of container creation. -Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We’ll also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). +Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We’ll also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](/azure/ai-services/openai/concepts/models#embeddings ) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](/azure/ai-services/computer-vision/concept-image-retrieval). -1. Create and store vector embeddings for the fields on which you want to perform vector search. -2. Specify the vector embedding paths in the vector embedding policy. -3. Include any desired vector indexes in the indexing policy for the container. +1. Create and store vector embeddings for the fields on which you want to perform vector search. +1. Specify the vector embedding paths in the vector embedding policy. +1. Include any desired vector indexes in the indexing policy for the container. -For subsequent sections of this article, we consider the below structure for the items stored in our container: +For subsequent sections of this article, we consider the below structure for the items stored in our container: ```json { -"title": "book-title", -"author": "book-author", -"isbn": "book-isbn", -"description": "book-description", -"contentVector": [2, -1, 4, 3, 5, -2, 5, -7, 3, 1], -"coverImageVector": [0.33, -0.52, 0.45, -0.67, 0.89, -0.34, 0.86, -0.78] -} + "title": "book-title", + "author": "book-author", + "isbn": "book-isbn", + "description": "book-description", + "contentVector": [2, -1, 4, 3, 5, -2, 5, -7, 3, 1], + "coverImageVector": [0.33, -0.52, 0.45, -0.67, 0.89, -0.34, 0.86, -0.78] +} ``` ## Creating a vector embedding policy for your container + Next, you need to define a container vector policy. This policy provides information that is used to inform the Azure Cosmos DB query engine how to handle vector properties in the VectorDistance system functions. This also informs the vector indexing policy of necessary information, should you choose to specify one. The following information is included in the contained vector policy: - * “path”: The property path that contains vectors  - * “datatype”: The type of the elements of the vector (default Float32)  - * “dimensions”: The length of each vector in the path (default 1536)  - * “distanceFunction”: The metric used to compute distance/similarity (default Cosine)  +- “path”: The property path that contains vectors  +- “datatype”: The type of the elements of the vector (default Float32)  +- “dimensions”: The length of each vector in the path (default 1536)  +- “distanceFunction”: The metric used to compute distance/similarity (default Cosine)  For our example with book details, the vector policy may look like the example JSON: -```python +```python vector_embedding_policy = {     "vectorEmbeddings": [         { @@ -107,14 +110,13 @@ vector_embedding_policy = {         } ] } -``` - +``` +## Creating a vector index in the indexing policy -## Creating a vector index in the indexing policy -Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. For this example, the indexing policy would look something like this: +Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. For this example, the indexing policy would look something like this: -```python +```python indexing_policy = {     "includedPaths": [         { @@ -138,21 +140,19 @@ indexing_policy = {         }     ] } -``` +``` ->[!IMPORTANT] +> [!IMPORTANT] > The vector path added to the "excludedPaths" section of the indexing policy to ensure optimized performance for insertion. Not adding the vector path to "excludedPaths" will result in higher RU charge and latency for vector insertions. - > [!IMPORTANT] > Currently vector search in Azure Cosmos DB for NoSQL is supported on new containers only. You need to set both the container vector policy and any vector indexing policy during the time of container creation as it can’t be modified later. Both policies will be modifiable in a future improvement to the preview feature. - -## Create container with vector policy +## Create container with vector policy Currently the vector search feature for Azure Cosmos DB for NoSQL is supported only on new containers so you need to apply the vector policy during the time of container creation and it can’t be modified later. -```python +```python try:         container = db.create_container_if_not_exists(                     id=CONTAINER_NAME, @@ -163,11 +163,11 @@ try:     except exceptions.CosmosHttpResponseError: raise -``` +``` -## Running vector similarity search query +## Running vector similarity search query -Once you create a container with the desired vector policy, and insert vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here: +Once you create a container with the desired vector policy, and insert vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here: ```sql SELECT TOP 10 c.title, VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10]) AS SimilarityScore   @@ -177,7 +177,7 @@ ORDER BY VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10])   This query retrieves the book titles along with similarity scores with respect to your query. Here's an example in Python: -```python +```python query_embedding = [1,2,3,4,5,6,7,8,9,10] # Query for items for item in container.query_items( @@ -187,7 +187,8 @@ for item in container.query_items(             ],             enable_cross_partition_query=True):     print(json.dumps(item, indent=True)) -``` +``` + ## Related content - [VectorDistance system function](query/vectordistance.md) diff --git a/articles/cosmos-db/nosql/multi-tenancy-vector-search.md b/articles/cosmos-db/nosql/multi-tenancy-vector-search.md index d9c922286d..982553e6a8 100644 --- a/articles/cosmos-db/nosql/multi-tenancy-vector-search.md +++ b/articles/cosmos-db/nosql/multi-tenancy-vector-search.md @@ -1,27 +1,30 @@ --- - title: Multitenancy in Azure Cosmos DB -description: Learn concepts for building multitenant gen-ai apps in Azure Cosmos DB +description: Review critical concepts required to build multi-tenant generative AI applications in Azure Cosmos DB. author: TheovanKraay ms.service: azure-cosmos-db ms.subservice: nosql -ms.topic: conceptual -ms.date: 06/26/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.author: thvankra ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- # Multitenancy for vector search in Azure Cosmos DB -> "OpenAI relies on Cosmos DB to dynamically scale their ChatGPT service – one of the fastest-growing consumer apps ever – enabling high reliability and low maintenance." +> "OpenAI relies on Cosmos DB to dynamically scale their ChatGPT service – one of the fastest-growing consumer apps ever – enabling high reliability and low maintenance." > — Satya Nadella -Azure Cosmos DB stands out as the world's first full-featured serverless operational database with vector search, offering unparalleled scalability and performance. By using Azure Cosmos DB, users can enhance their vector search capabilities, ensuring high reliability and low maintenance for multitenant applications. +Azure Cosmos DB stands out as the world's first full-featured serverless operational database with vector search, offering unparalleled scalability and performance. By using Azure Cosmos DB, users can enhance their vector search capabilities, ensuring high reliability and low maintenance for multitenant applications. Multitenancy enables a single instance of a database to serve multiple customers, or tenants, simultaneously. This approach efficiently shares infrastructure and operational overhead, resulting in cost savings and simplified management. It's a crucial design consideration for SaaS applications and some internal enterprise solutions. -Multitenancy introduces complexity. Your system must scale efficiently to maintain high performance across all tenants, who may have unique workloads, requirements, and service-level agreements (SLAs). +Multitenancy introduces complexity. Your system must scale efficiently to maintain high performance across all tenants, who may have unique workloads, requirements, and service-level agreements (SLAs). Imagine a fictional AI-assisted research platform called ResearchHub. Serving thousands of companies and individual researchers, ResearchHub manages varying user bases, data scales, and SLAs. Ensuring low query latency and high performance is vital for sustaining an excellent user experience. @@ -157,16 +160,16 @@ When designing a multitenant system with Cosmos DB, consider these factors: Azure Cosmos DB's support for DiskANN vector index capability makes it an excellent choice for applications that require fast, high-dimensional searches, such as AI-assisted research platforms like ResearchHub. Here’s how you can leverage these capabilities: **Efficient Storage and Retrieval:** - - **Vector Indexing:** Use the DiskANN vector index to efficiently store and retrieve high-dimensional vectors. This is useful for applications that involve similarity searches in large datasets, such as image recognition or document similarity. - - **Performance Optimization:** DiskANN’s vector search capabilities enable quick, accurate searches, ensuring low latency and high performance, which is critical for maintaining a good user experience. +- **Vector Indexing:** Use the DiskANN vector index to efficiently store and retrieve high-dimensional vectors. This is useful for applications that involve similarity searches in large datasets, such as image recognition or document similarity. +- **Performance Optimization:** DiskANN’s vector search capabilities enable quick, accurate searches, ensuring low latency and high performance, which is critical for maintaining a good user experience. **Scaling Across Tenants:** - - **Partition Key-Per-Tenant:** Utilize partition keys to logically isolate tenant data while benefiting from Cosmos DB’s scalable infrastructure. - - **Hierarchical Partitioning:** Implement hierarchical partitioning to further segment data within each tenant’s partition, improving query performance and resource distribution. +- **Partition Key-Per-Tenant:** Utilize partition keys to logically isolate tenant data while benefiting from Cosmos DB’s scalable infrastructure. +- **Hierarchical Partitioning:** Implement hierarchical partitioning to further segment data within each tenant’s partition, improving query performance and resource distribution. **Security and Compliance:** - - **Customer-Managed Keys:** Implement customer-managed keys for data encryption at rest, ensuring each tenant’s data is securely isolated. - - **Regular Key Rotation:** Enhance security by regularly rotating encryption keys stored in Azure Key Vault. +- **Customer-Managed Keys:** Implement customer-managed keys for data encryption at rest, ensuring each tenant’s data is securely isolated. +- **Regular Key Rotation:** Enhance security by regularly rotating encryption keys stored in Azure Key Vault. ### Real-world example: implementing ResearchHub @@ -190,16 +193,18 @@ Multi-tenancy in Azure Cosmos DB, especially with its DiskANN vector index capab Azure Cosmos DB provides the tools necessary to build a robust, secure, and scalable multitenant environment. With the power of DiskANN vector indexing, you can deliver fast, high-dimensional searches that drive your AI applications. -### Next steps +## Vector database solutions -[30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) +[Azure PostgreSQL Server pgvector Extension](../../postgresql/flexible-server/how-to-use-pgvector.md) -[Multitenancy and Azure Cosmos DB](https://aka.ms/CosmosMultitenancy) +:::image type="content" source="../media/vector-search/azure-databases-and-ai-search.png" lightbox="../media/vector-search/azure-databases-and-ai-search.png" alt-text="Diagram of Vector indexing services for Azure PostgreSQL."::: -> [!div class="nextstepaction"] -> [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) +## Related content -## More vector database solutions -- [Azure PostgreSQL Server pgvector Extension](../../postgresql/flexible-server/how-to-use-pgvector.md) +- [30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) +- [Multitenancy and Azure Cosmos DB](https://aka.ms/CosmosMultitenancy) -:::image type="content" source="../media/vector-search/azure-databases-and-ai-search.png" lightbox="../media/vector-search/azure-databases-and-ai-search.png" alt-text="Diagram of Vector indexing services."::: \ No newline at end of file +## Next step + +> [!div class="nextstepaction"] +> [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) diff --git a/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md b/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md index ac1a0aefc2..4ee977d6e7 100644 --- a/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md +++ b/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md @@ -1,5 +1,5 @@ --- -title: Query NoSQL with Microsoft Copilot for Azure (preview) +title: Queries with Microsoft Copilot for Azure (preview) titleSuffix: Azure Cosmos DB for NoSQL description: Generate suggestions from natural language prompts to write NoSQL queries using Microsoft Copilot for Azure in Cosmos DB (preview). author: seesharprun @@ -10,16 +10,16 @@ ms.custom: - ignite-2023 ms.topic: how-to ms.devlang: nosql -ms.date: 08/22/2024 +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot # CustomerIntent: As a developer, I want to use Copilot so that I can write queries faster and easier. +appliesto: + - ✅ NoSQL --- # Generate NoSQL queries with Microsoft Copilot for Azure in Cosmos DB (preview) -[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)] - Microsoft Copilot for Azure in Cosmos DB (preview) can assist with authoring Azure Cosmos DB for NoSQL queries by generating queries based on your natural English-language prompts. Copilot is available to use in the API for NoSQL's query editor within the Data Explorer. With Copilot in the API for NoSQL, you can: - Ask questions about your data as you would in text or conversation to generate a NoSQL query. @@ -28,7 +28,6 @@ Microsoft Copilot for Azure in Cosmos DB (preview) can assist with authoring Azu > [!NOTE] > You may see the database `CopilotSampleDb` appear in Data Explorer. This is a completely separate database managed by Microsoft and access is provided to you (at no cost) as a testbed to become familiar with Microsoft Copilot for Azure in Cosmos DB. This database consists of 100% synthetic data created by Microsoft and has has no interaction or relationships to any data you may have in Azure Cosmos DB. - > [!WARNING] > Copilot is a preview feature that is powered by large language models (LLMs). Output produced by Copilot may contain inaccuracies, biases, or other unintended content. This occurs because the model powering Copilot was trained on information from the internet and other sources. As with any generative AI model, humans should review the output produced by Copilot before use. @@ -38,11 +37,12 @@ Microsoft Copilot for Azure in Cosmos DB (preview) can assist with authoring Azu - If you don't have an Azure subscription, [create an account for free](https://azure.microsoft.com/free). - Once you have an existing Azure subscription, [create a new Azure Cosmos DB for NoSQL account](../quickstart-portal.md). - Enroll your Azure subscription, in the Microsoft Copilot for Azure in Cosmos DB [preview feature](/azure/azure-resource-manager/management/preview-features). - + > [!IMPORTANT] > Review these [preview terms](https://azure.microsoft.com/support/legal/preview-supplemental-terms/#AzureOpenAI-PoweredPreviews) before using query Copilot for NoSQL. ## Access the feature + As a preview feature, you'll have to add Microsoft Copilot for Azure in Cosmos DB preview to your Azure subscription. Once enrolled, you can find Microsoft Copilot for Azure integrated with the Data Explorer’s query editor. 1. Navigate to any Azure Cosmos DB NoSQL resource. diff --git a/articles/cosmos-db/nosql/vector-search.md b/articles/cosmos-db/nosql/vector-search.md index 050268964a..639ecb0f9e 100644 --- a/articles/cosmos-db/nosql/vector-search.md +++ b/articles/cosmos-db/nosql/vector-search.md @@ -1,26 +1,25 @@ --- -title: Vector store +title: Integrated vector store titleSuffix: Azure Cosmos DB for NoSQL -description: Use vector store in Azure Cosmos DB for NoSQL to enhance AI-based applications. +description: Enhance AI-based applications using the integrated vector store functionality in Azure Cosmos DB for NoSQL author: jcodella ms.author: jacodel ms.service: azure-cosmos-db ms.subservice: nosql ms.custom: - - Build 2024 - build-2024 - ignite-2024 -ms.topic: conceptual -ms.date: 5/7/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL --- # Vector Search in Azure Cosmos DB for NoSQL -[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] - -Azure Cosmos DB for NoSQL now offers efficient vector indexing and search. This feature is designed to handle high-dimensional vectors, enabling efficient and accurate vector search at any scale. You can now store vectors directly in the documents alongside your data. Each document in your database can contain not only traditional schema-free data, but also high-dimensional vectors as other properties of the documents. This colocation of data and vectors allows for efficient indexing and searching, as the vectors are stored in the same logical unit as the data they represent. Keeping vectors and data together simplifies data management, AI application architectures, and the efficiency of vector-based operations. +Azure Cosmos DB for NoSQL now offers efficient vector indexing and search. This feature is designed to handle high-dimensional vectors, enabling efficient and accurate vector search at any scale. You can now store vectors directly in the documents alongside your data. Each document in your database can contain not only traditional schema-free data, but also high-dimensional vectors as other properties of the documents. This colocation of data and vectors allows for efficient indexing and searching, as the vectors are stored in the same logical unit as the data they represent. Keeping vectors and data together simplifies data management, AI application architectures, and the efficiency of vector-based operations. Azure Cosmos DB for NoSQL offers the flexibility it offers in choosing the vector indexing method: - A "flat" or k-nearest neighbors exact search (sometimes called brute-force) can provide 100% retrieval recall for smaller, focused vector searches. especially when combined with query filters and partition-keys. @@ -29,14 +28,13 @@ Azure Cosmos DB for NoSQL offers the flexibility it offers in choosing the vecto [Learn more about vector indexing here](../index-policy.md#vector-indexes) - Vector search in Azure Cosmos DB can be combined with all other supported Azure Cosmos DB NoSQL query filters and indexes using `WHERE` clauses. This enables your vector searches to be the most relevant data to your applications. This feature enhances the core capabilities of Azure Cosmos DB, making it more versatile for handling vector data and search requirements in AI applications. ## What is a vector store? -A vector store or [vector database](../vector-database.md) is a database designed to store and manage vector embeddings, which are mathematical representations of data in a high-dimensional space. In this space, each dimension corresponds to a feature of the data, and tens of thousands of dimensions might be used to represent sophisticated data. A vector's position in this space represents its characteristics. Words, phrases, or entire documents, and images, audio, and other types of data can all be vectorized. +A vector store or [vector database](../vector-database.md) is a database designed to store and manage vector embeddings, which are mathematical representations of data in a high-dimensional space. In this space, each dimension corresponds to a feature of the data, and tens of thousands of dimensions might be used to represent sophisticated data. A vector's position in this space represents its characteristics. Words, phrases, or entire documents, and images, audio, and other types of data can all be vectorized. ## How does a vector store work? @@ -45,50 +43,48 @@ In a vector store, vector search algorithms are used to index and query embeddin In the Integrated Vector Database in Azure Cosmos DB for NoSQL, embeddings can be stored, indexed, and queried alongside the original data. This approach eliminates the extra cost of replicating data in a separate pure vector database. Moreover, this architecture keeps the vector embeddings and original data together, which better facilitates multi-modal data operations, and enables greater data consistency, scale, and performance. ## Enable the vector indexing and search feature -Vector indexing and search in Azure Cosmos DB for NoSQL requires enabling on the Features page of your Azure Cosmos DB. Follow the below steps to register: + +Vector indexing and search in Azure Cosmos DB for NoSQL requires enabling on the Features page of your Azure Cosmos DB. Follow the below steps to register: 1. Navigate to your Azure Cosmos DB for NoSQL resource page. -2. Select the "Features" pane under the "Settings" menu item. -3. Select the “Vector Search in Azure Cosmos DB for NoSQL” feature. -4. Read the description of the feature to confirm you want to enable it. -5. Select "Enable" to turn on the vector indexing and search capability. +1. Select the "Features" pane under the "Settings" menu item. +1. Select the “Vector Search in Azure Cosmos DB for NoSQL” feature. +1. Read the description of the feature to confirm you want to enable it. +1. Select "Enable" to turn on the vector indexing and search capability. + + > [!TIP] + > Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. + > + > ```azurecli + > az cosmosdb update \ + > --resource-group \ + > --name \ + > --capabilities EnableNoSQLVectorSearch + > ``` + > > [!NOTE] > The registration request will be autoapproved; however, it may take 15 minutes to fully activate on the account. -> [!TIP] -> Alternatively, use the Azure CLI to update the capabilities of your account to support NoSQL vector search. -> -> ```azurecli -> az cosmosdb update \ -> --resource-group \ -> --name \ -> --capabilities EnableNoSQLVectorSearch -> ``` -> - -> [!div class="nextstepaction"] -> [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) - ## Container Vector Policies -Performing vector search with Azure Cosmos DB for NoSQL requires you to define a vector policy for the container. This provides essential information for the database engine to conduct efficient similarity search for vectors found in the container's documents. This also informs the vector indexing policy of necessary information, should you choose to specify one. The following information is included in the contained vector policy: - * “path”: the property containing the vector (required). - * “datatype”: the data type of the vector property (default Float32).  - * “dimensions”: The dimensionality or length of each vector in the path. All vectors in a path should have the same number of dimensions. (default 1536). - * “distanceFunction”: The metric used to compute distance/similarity. Supported metrics are: - * [cosine](https://en.wikipedia.org/wiki/Cosine_similarity), which has values from -1 (least similar) to +1 (most similar). - * [dot product](https://en.wikipedia.org/wiki/Dot_product), which has values from -inf (least similar) to +inf (most similar). - * [euclidean](https://en.wikipedia.org/wiki/Euclidean_distance), which has values from 0 (most similar) to +inf) (least similar). +Performing vector search with Azure Cosmos DB for NoSQL requires you to define a vector policy for the container. This provides essential information for the database engine to conduct efficient similarity search for vectors found in the container's documents. This also informs the vector indexing policy of necessary information, should you choose to specify one. The following information is included in the contained vector policy: +- “path”: the property containing the vector (required). +- “datatype”: the data type of the vector property (default Float32).  +- “dimensions”: The dimensionality or length of each vector in the path. All vectors in a path should have the same number of dimensions. (default 1536). +- “distanceFunction”: The metric used to compute distance/similarity. Supported metrics are: + - [cosine](https://en.wikipedia.org/wiki/Cosine_similarity), which has values from -1 (least similar) to +1 (most similar). + - [dot product](https://en.wikipedia.org/wiki/Dot_product), which has values from -inf (least similar) to +inf (most similar). + - [euclidean](https://en.wikipedia.org/wiki/Euclidean_distance), which has values from 0 (most similar) to +inf) (least similar). - > [!NOTE] > Each unique path can have at most one policy. However, multiple policies can be specified provided that they all target a different path. - + The container vector policy can be described as JSON objects. Here are two examples of valid container vector policies: -**A policy with a single vector path** +### A policy with a single vector path + ```json { "vectorEmbeddings": [ @@ -102,7 +98,8 @@ The container vector policy can be described as JSON objects. Here are two examp } ``` -**A policy with two vector paths** +### A policy with two vector paths + ```json { "vectorEmbeddings": [ @@ -136,11 +133,12 @@ The container vector policy can be described as JSON objects. Here are two examp > The `quantizedFlat` and `diskANN` indexes requires that at least 1,000 vectors to be inserted. This is to ensure accuracy of the quantization process. If there are fewer than 1,000 vectors, a full scan is executed instead and will lead to higher RU charges for a vector search query. A few points to note: - - The `flat` and `quantizedFlat` index types uses Azure Cosmos DB's index to store and read each vector when performing a vector search. Vector searches with a `flat` index are brute-force searches and produce 100% accuracy or recall. That is, it's guaranteed to find the most similar vectors in the dataset. However, there's a limitation of `505` dimensions for vectors on a flat index. - - The `quantizedFlat` index stores quantized (compressed) vectors on the index. Vector searches with `quantizedFlat` index are also brute-force searches, however their accuracy might be slightly less than 100% since the vectors are quantized before adding to the index. However, vector searches with `quantized flat` should have lower latency, higher throughput, and lower RU cost than vector searches on a `flat` index. This is a good option for smaller scenarios, or scenarios where you're using query filters to narrow down the vector search to a relatively small set of vectors. `quantizedFlat` is recommended when the number of vectors to be indexed is somewhere around 50,000 or fewer per physical partition. However, this is just a general guideline and actual performance should be tested as each scenario can be different. +- The `flat` and `quantizedFlat` index types uses Azure Cosmos DB's index to store and read each vector when performing a vector search. Vector searches with a `flat` index are brute-force searches and produce 100% accuracy or recall. That is, it's guaranteed to find the most similar vectors in the dataset. However, there's a limitation of `505` dimensions for vectors on a flat index. + +- The `quantizedFlat` index stores quantized (compressed) vectors on the index. Vector searches with `quantizedFlat` index are also brute-force searches, however their accuracy might be slightly less than 100% since the vectors are quantized before adding to the index. However, vector searches with `quantized flat` should have lower latency, higher throughput, and lower RU cost than vector searches on a `flat` index. This is a good option for smaller scenarios, or scenarios where you're using query filters to narrow down the vector search to a relatively small set of vectors. `quantizedFlat` is recommended when the number of vectors to be indexed is somewhere around 50,000 or fewer per physical partition. However, this is just a general guideline and actual performance should be tested as each scenario can be different. - - The `diskANN` index is a separate index defined specifically for vectors using [DiskANN](https://www.microsoft.com/research/publication/diskann-fast-accurate-billion-point-nearest-neighbor-search-on-a-single-node/), a suite of high performance vector indexing algorithms developed by Microsoft Research. DiskANN indexes can offer some of the lowest latency, highest throughput, and lowest RU cost queries, while still maintaining high accuracy. In general, DiskANN is the most performant of all index types if there are more than 50,000 vectors per physical partition. +- The `diskANN` index is a separate index defined specifically for vectors using [DiskANN](https://www.microsoft.com/research/publication/diskann-fast-accurate-billion-point-nearest-neighbor-search-on-a-single-node/), a suite of high performance vector indexing algorithms developed by Microsoft Research. DiskANN indexes can offer some of the lowest latency, highest throughput, and lowest RU cost queries, while still maintaining high accuracy. In general, DiskANN is the most performant of all index types if there are more than 50,000 vectors per physical partition. Here are examples of valid vector index policies: @@ -203,7 +201,7 @@ Here are examples of valid vector index policies: } ``` ->[!IMPORTANT] +> [!IMPORTANT] > The vector path added to the "excludedPaths" section of the indexing policy to ensure optimized performance for insertion. Not adding the vector path to "excludedPaths" will result in higher RU charge and latency for vector insertions. > [!IMPORTANT] @@ -219,11 +217,11 @@ FROM c  ORDER BY VectorDistance(c.contentVector, [1,2,3])   ``` ->[!IMPORTANT] +> [!IMPORTANT] > Always use a `TOP N` clause in the `SELECT` statement of a query. Otherwise the vector search will try to return many more results and the query will cost more RUs and have higher latency than necessary. - ## Current limitations + Vector indexing and search in Azure Cosmos DB for NoSQL has some limitations. - `quantizedFlat` and `diskANN` indexes require at least 1,000 vectors to be indexed to ensure that the quantization is accurate. If fewer than 1,000 vectors are indexed, then a full-scan is used instead and RU charges may be higher. - Vectors indexed with the `flat` index type can be at most 505 dimensions. Vectors indexed with the `quantizedFlat` or `DiskANN` index type can be at most 4,096 dimensions. @@ -233,7 +231,8 @@ Vector indexing and search in Azure Cosmos DB for NoSQL has some limitations. - At this time, vector indexing and search is not supported on accounts with Analytical Store (and Synapse Link) and Shared Throughput. - Once vector indexing and search is enabled on a container, it cannot be disabled. -## Next step +## Related content + - [DiskANN + Azure Cosmos DB - Microsoft Mechanics Video](https://www.youtube.com/watch?v=MlMPIYONvfQ) - [.NET - How-to Index and query vector data](how-to-dotnet-vector-index-query.md) - [Python - How-to Index and query vector data](how-to-python-vector-index-query.md) @@ -246,3 +245,8 @@ Vector indexing and search in Azure Cosmos DB for NoSQL has some limitations. - [LangChain, Python](https://python.langchain.com/v0.2/docs/integrations/vectorstores/azure_cosmos_db_no_sql/) - [Semantic Kernel, .NET](https://github.com/microsoft/semantic-kernel/tree/main/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL) - [Semantic Kernel, Python](https://github.com/microsoft/semantic-kernel/tree/main/python/semantic_kernel/connectors/memory/azure_cosmosdb_no_sql) + +## Next step + +> [!div class="nextstepaction"] +> [Use the Azure Cosmos DB lifetime free tier](../free-tier.md) From fe3f7926c96ca3b2616151f4ef9c5ddd1fedb20a Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 13:01:37 -0500 Subject: [PATCH 11/14] Refresh AI articles (#4) --- articles/cosmos-db/ai-advantage.md | 12 +++++-- articles/cosmos-db/ai-agents.md | 15 ++++---- articles/cosmos-db/copilot-faq.yml | 10 ++++-- articles/cosmos-db/introduction.md | 40 ++++++++++++--------- articles/cosmos-db/solutions.md | 10 +++++- articles/cosmos-db/vector-database.md | 52 +++++++++++++++------------ 6 files changed, 86 insertions(+), 53 deletions(-) diff --git a/articles/cosmos-db/ai-advantage.md b/articles/cosmos-db/ai-advantage.md index b8e2e99478..616a5de9c4 100644 --- a/articles/cosmos-db/ai-advantage.md +++ b/articles/cosmos-db/ai-advantage.md @@ -8,15 +8,21 @@ ms.service: azure-cosmos-db ms.custom: - ignite-2023 ms.topic: overview -ms.date: 11/08/2023 +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB + - ✅ MongoDB vCore + - ✅ Cassanda + - ✅ Gremlin + - ✅ Table + - ✅ PostgreSQL --- # Try Azure Cosmos DB free with Azure AI Advantage -[!INCLUDE[NoSQL, MongoDB, Cassandra, Gremlin, Table](includes/appliesto-nosql-mongodb-cassandra-gremlin-table.md)] - Azure offers a full, integrated stack purpose-built for AI-powered applications. If you build your AI application stack on Azure using Azure Cosmos DB, your design can lead to solutions that get to market faster, experience lower latency, and have comprehensive built-in security. There are many benefits when using Azure Cosmos DB and Azure AI together: diff --git a/articles/cosmos-db/ai-agents.md b/articles/cosmos-db/ai-agents.md index bb57969eb3..7c6d423aec 100644 --- a/articles/cosmos-db/ai-agents.md +++ b/articles/cosmos-db/ai-agents.md @@ -1,18 +1,21 @@ --- -title: AI agent +title: AI agents and solutions +titleSuffix: Azure Cosmos DB description: Learn about key concepts for agents and step through the implementation of an AI agent memory system. author: wmwxwa ms.author: wangwilliam ms.service: azure-cosmos-db ms.custom: - ignite-2024 -ms.topic: conceptual -ms.date: 06/26/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ MongoDB vCore --- -# AI agent +# AI agents in Azure Cosmos DB AI agents are designed to perform specific tasks, answer questions, and automate processes for users. These agents vary widely in complexity. They range from simple chatbots, to copilots, to advanced AI assistants in the form of digital or robotic systems that can run complex workflows autonomously. @@ -843,11 +846,11 @@ export default function ChatLayout(messages) { User prompts are on the right side and colored blue. Responses from the AI travel agent are on the left side and colored green. As the following image shows, the HTML-formatted responses are accounted for in the conversation. -:::image type="content" source="media/gen-ai/ai-agent/chat-screenshot.png" lightbox="media/gen-ai/ai-agent/chat-screenshot.png" alt-text="Screenshot of a chat."::: +:::image type="content" source="media/gen-ai/ai-agent/chat-screenshot.png" lightbox="media/gen-ai/ai-agent/chat-screenshot.png" alt-text="Screenshot of a chat with an AI agent."::: When your AI agent is ready to go into production, you can use semantic caching to improve query performance by 80% and to reduce LLM inference and API call costs. To implement semantic caching, see [this post on the Stochastic Coder blog](https://stochasticcoder.com/2024/03/22/improve-llm-performance-using-semantic-cache-with-cosmos-db/). -:::image type="content" source="media/gen-ai/ai-agent/semantic-caching.png" lightbox="media/gen-ai/ai-agent/semantic-caching.png" alt-text="Diagram of semantic caching."::: +:::image type="content" source="media/gen-ai/ai-agent/semantic-caching.png" lightbox="media/gen-ai/ai-agent/semantic-caching.png" alt-text="Diagram of semantic caching for AI agents."::: ## Related content diff --git a/articles/cosmos-db/copilot-faq.yml b/articles/cosmos-db/copilot-faq.yml index d1fc13da51..d2bce07655 100644 --- a/articles/cosmos-db/copilot-faq.yml +++ b/articles/cosmos-db/copilot-faq.yml @@ -1,7 +1,7 @@ ### YamlMime:FAQ metadata: - title: | - Frequently asked questions about Microsoft Copilot for Azure in Cosmos DB (preview). + title: Frequently asked questions about Microsoft Copilot for Azure (preview). + titleSuffix: Azure Cosmos DB description: | Get answers to frequently asked questions about Microsoft Copilot for Azure in Cosmos DB (preview). Questions include general functionality, responsible AI, and trust categories. author: jcodella @@ -11,9 +11,13 @@ metadata: ms.custom: - ignite-2023 ms.topic: faq - ms.date: 11/10/2023 + ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot + appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL title: Frequently asked questions about Microsoft Copilot for Azure in Cosmos DB (preview). summary: | This article provides information on general and transparency frequently asked questions about Microsoft Copilot for Azure in Cosmos DB. diff --git a/articles/cosmos-db/introduction.md b/articles/cosmos-db/introduction.md index ba2269ad84..30fc0684a5 100644 --- a/articles/cosmos-db/introduction.md +++ b/articles/cosmos-db/introduction.md @@ -1,21 +1,27 @@ --- title: Unified AI Database titleSuffix: Azure Cosmos DB -description: Database for AI Era - Azure Cosmos DB is a NoSQL, relational, and vector database that provides unmatched reliability and flexibility for your operational data needs. +description: Review Azure Cosmos DB as a NoSQL, relational, and vector database for the AI era that has unmatched reliability and flexibility for operational data needs. author: markjbrown ms.author: mjbrown ms.service: azure-cosmos-db ms.topic: overview -ms.date: 04/03/2024 +ms.date: 12/03/2024 adobe-target: true ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB + - ✅ MongoDB vCore + - ✅ Cassanda + - ✅ Gremlin + - ✅ Table + - ✅ PostgreSQL --- # Azure Cosmos DB - Database for the AI Era -[!INCLUDE[NoSQL, MongoDB, Cassandra, Gremlin, Table, PostgreSQL](includes/appliesto-nosql-mongodb-cassandra-gremlin-table-postgresql.md)] - > "OpenAI relies on Cosmos DB to dynamically scale their ChatGPT service – one of the fastest-growing consumer apps ever – enabling high reliability and low maintenance." > – Satya Nadella, Microsoft chairman and chief executive officer @@ -27,7 +33,7 @@ Azure Cosmos DB simplifies and expedites your application development by being t ## An AI database providing industry-leading capabilities... -## ...for free +### ...for free Azure Cosmos DB is a fully managed NoSQL, relational, and vector database. It offers single-digit millisecond response times, automatic and instant scalability, along with guaranteed speed at any scale. Business continuity is assured with [SLA-backed](https://azure.microsoft.com/support/legal/sla/cosmos-db) availability and enterprise-grade security. @@ -43,9 +49,9 @@ As a fully managed service, Azure Cosmos DB takes database administration off yo The following free options are available: -* [Azure Cosmos DB lifetime free tier](free-tier.md) provides 1000 [RU/s](request-units.md) of throughput and 25 GB of storage free. -* [Azure AI Advantage](ai-advantage.md) offers 40,000 [RU/s](request-units.md) of throughput for 90 days (equivalent of up to $6,000) to Azure AI or GitHub Copilot customers. -* [Try Azure Cosmos DB free](https://azure.microsoft.com/try/cosmosdb/) for 30 days without creating an Azure account; no commitment follows when the trial period ends. +- [Azure Cosmos DB lifetime free tier](free-tier.md) provides 1000 [RU/s](request-units.md) of throughput and 25 GB of storage free. +- [Azure AI Advantage](ai-advantage.md) offers 40,000 [RU/s](request-units.md) of throughput for 90 days (equivalent of up to $6,000) to Azure AI or GitHub Copilot customers. +- [Try Azure Cosmos DB free](https://azure.microsoft.com/try/cosmosdb/) for 30 days without creating an Azure account; no commitment follows when the trial period ends. When you decide that Azure Cosmos DB is right for you, you can receive up to 63% discount on [Azure Cosmos DB prices through Reserved Capacity](reserved-capacity.md). @@ -55,11 +61,11 @@ When you decide that Azure Cosmos DB is right for you, you can receive up to 63% > [!TIP] > To learn more about Azure Cosmos DB, join us every Thursday at 1PM Pacific on Azure Cosmos DB Live TV. See the [Upcoming session schedule and past episodes](https://www.youtube.com/@AzureCosmosDB/streams). -## ...for more than just AI apps +### ...for more than just AI apps Besides AI, Azure Cosmos DB should also be your goto database for a variety of use cases, including [retail and marketing](use-cases.md#retail-and-marketing), [IoT and telematics](use-cases.md#iot-and-telematics), [gaming](use-cases.md#gaming), [social](social-media-apps.md), and [personalization](use-cases.md#personalization), among others. Azure Cosmos DB is well positioned for solutions that handle massive amounts of data, reads, and writes at a global scale with near-real response times. Azure Cosmos DB's guaranteed high availability, high throughput, low latency, and tunable consistency are huge advantages when building these types of applications. -##### For what kinds of apps is Azure Cosmos DB a good fit? +#### For what kinds of apps is Azure Cosmos DB a good fit - **Flexible Schema for Iterative Development.** For example, apps wanting to adopt flexible modern DevOps practices and accelerate feature deployment timelines. - **Latency sensitive workloads.** For example, real-time Personalization. @@ -67,14 +73,14 @@ Besides AI, Azure Cosmos DB should also be your goto database for a variety of u - **High throughput workloads.** For example, IoT device state/telemetry. - **Highly available mission critical workloads.** For example, customer-facing Web Apps. -##### For what kinds of apps is Azure Cosmos DB a poor fit? +#### For what kinds of apps is Azure Cosmos DB a poor fit - **Analytical workloads (OLAP).** For example, interactive, streaming, and batch analytics to enable Data Scientist / Data Analyst scenarios. Consider Microsoft Fabric instead. - **Highly relational apps.** For example, white-label CRM applications. Consider Azure SQL, Azure Database for MySQL, or Azure Database for PostgreSQL instead. -## ...with unmatched reliability and flexibility +### ...with unmatched reliability and flexibility -### Guaranteed speed at any scale +#### Guaranteed speed at any scale Gain unparalleled [SLA-backed](https://azure.microsoft.com/support/legal/sla/cosmos-db) speed and throughput, fast global access, and instant elasticity. @@ -82,7 +88,7 @@ Gain unparalleled [SLA-backed](https://azure.microsoft.com/support/legal/sla/cos - Multi-region writes and data distribution to any Azure region with just a button. - Independently and elastically scale storage and throughput across any Azure region – even during unpredictable traffic bursts – for unlimited scale worldwide. -### Simplified application development +#### Simplified application development Build fast with open-source APIs, multiple SDKs, schemaless data, and no-ETL analytics over operational data. @@ -93,7 +99,7 @@ Build fast with open-source APIs, multiple SDKs, schemaless data, and no-ETL ana - Change feed makes it easy to track and manage changes to database containers and create triggered events with Azure Functions. - Azure Cosmos DB's schema-less service automatically indexes all your data, regardless of the data model, to deliver blazing fast queries. -### Mission-critical ready +#### Mission-critical ready Guarantee business continuity, 99.999% availability, and enterprise-level security for every application. @@ -102,7 +108,7 @@ Guarantee business continuity, 99.999% availability, and enterprise-level securi - Enjoy enterprise-grade encryption-at-rest with self-managed keys. - Azure role-based access control keeps your data safe and offers fine-tuned control. -### Fully managed and cost-effective +#### Fully managed and cost-effective End-to-end database management, with serverless and automatic scaling matching your application and total cost of ownership (TCO) needs. @@ -111,7 +117,7 @@ End-to-end database management, with serverless and automatic scaling matching y - Serverless model offers spiky workloads automatic and responsive service to manage traffic bursts on demand. - Autoscale provisioned throughput automatically and instantly scales capacity for unpredictable workloads, while maintaining [SLAs](https://azure.microsoft.com/support/legal/sla/cosmos-db). -### Azure Synapse Link for Azure Cosmos DB +#### Azure Synapse Link for Azure Cosmos DB [Azure Synapse Link for Azure Cosmos DB](synapse-link.md) is a cloud-native hybrid transactional and analytical processing (HTAP) capability that enables analytics at near real-time over operational data in Azure Cosmos DB. Azure Synapse Link creates a tight seamless integration between Azure Cosmos DB and Azure Synapse Analytics. diff --git a/articles/cosmos-db/solutions.md b/articles/cosmos-db/solutions.md index 1f8948f9c7..664b43bb2f 100644 --- a/articles/cosmos-db/solutions.md +++ b/articles/cosmos-db/solutions.md @@ -7,10 +7,18 @@ ms.author: wangwilliam ms.service: azure-cosmos-db ms.subservice: nosql ms.topic: best-practice -ms.date: 02/27/2024 +ms.date: 12/03/2024 zone_pivot_groups: azure-cosmos-db-apis-nosql-mongodb ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB + - ✅ MongoDB vCore + - ✅ Cassanda + - ✅ Gremlin + - ✅ Table + - ✅ PostgreSQL --- # Best practices and solutions using AI and Azure Cosmos DB diff --git a/articles/cosmos-db/vector-database.md b/articles/cosmos-db/vector-database.md index 7f1d12db1a..181aa23289 100644 --- a/articles/cosmos-db/vector-database.md +++ b/articles/cosmos-db/vector-database.md @@ -1,24 +1,27 @@ --- -title: Vector database -description: Vector database functionalities, implementation, and comparison. +title: Integrated vector database +titleSuffix: Azure Cosmos DB +description: Review how to use Azure Cosmos DB as a vectror database in numerous domains and situations across analytical and generative AI. author: markjbrown ms.author: mjbrown ms.service: azure-cosmos-db ms.custom: - build-2024 -ms.topic: conceptual -ms.date: 10/09/2024 +ms.topic: concept-article +ms.date: 12/03/2024 ms.collection: - ce-skilling-ai-copilot +appliesto: + - ✅ NoSQL + - ✅ MongoDB vCore + - ✅ PostgreSQL --- # Vector database -> [!Tip] +> [!TIP] > Visit our new **[Samples Gallery](https://aka.ms/AzureCosmosDB/Gallery/AI)** for the latest Vector Database and RAG Pattern App Samples -[!INCLUDE[NoSQL, MongoDB vCore, PostgreSQL](includes/appliesto-nosql-mongodbvcore-postgresql.md)] - Vector databases are used in numerous domains and situations across analytical and generative AI, including natural language processing, video and image recognition, recommendation system, and search, among others. In 2023, a notable trend in software was the integration of AI enhancements, often achieved by incorporating specialized standalone vector databases into existing tech stacks. This article explains what vector databases are and presents an alternative architecture that you might want to consider: using an integrated vector database in the NoSQL or relational database you already use, especially when working with multi-modal data. This approach not only allows you to reduce cost but also achieve greater data consistency, scalability, and performance. @@ -26,7 +29,7 @@ In 2023, a notable trend in software was the integration of AI enhancements, oft > [!TIP] > Data consistency, scalability, and performance are critical for data-intensive applications, which is why OpenAI chose to build the ChatGPT service on top of Azure Cosmos DB. You, too, can take advantage of its integrated vector database, as well as its single-digit millisecond response times, automatic and instant scalability, and guaranteed speed at any scale. See [implementation samples](#how-to-implement-integrated-vector-database-functionalities) and [try](#next-step) it for free. -## What is a vector database? +## What is a vector database A vector database is a database designed to store and manage [vector embeddings](#embeddings), which are mathematical representations of data in a high-dimensional space. In this space, each dimension corresponds to a feature of the data, and tens of thousands of dimensions might be used to represent sophisticated data. A vector's position in this space represents its characteristics. Words, phrases, or entire documents, and images, audio, and other types of data can all be vectorized. These vector embeddings are used in similarity search, multi-modal search, recommendations engines, large languages models (LLMs), etc. @@ -96,13 +99,13 @@ Retrieval-augmentated generation (RAG) is an architecture that augments the capa A simple RAG pattern using Azure Cosmos DB for NoSQL could be: 1. Enroll in the [Azure Cosmos DB NoSQL Vector Index preview](nosql/vector-search.md) -2. Setup a database and container with a container vector policy and vector index. -3. Insert data into an Azure Cosmos DB for NoSQL database and container -4. Create embeddings from a data property using Azure OpenAI Embeddings -5. Link the Azure Cosmos DB for NoSQL. -6. Create a vector index over the embeddings properties -7. Create a function to perform vector similarity search based on a user prompt -8. Perform question answering over the data using an Azure OpenAI Completions model +1. Setup a database and container with a container vector policy and vector index. +1. Insert data into an Azure Cosmos DB for NoSQL database and container +1. Create embeddings from a data property using Azure OpenAI Embeddings +1. Link the Azure Cosmos DB for NoSQL. +1. Create a vector index over the embeddings properties +1. Create a function to perform vector similarity search based on a user prompt +1. Perform question answering over the data using an Azure OpenAI Completions model The RAG pattern, with prompt engineering, serves the purpose of enhancing response quality by offering more contextual information to the model. RAG enables the model to apply a broader knowledge base by incorporating relevant external sources into the generation process, resulting in more comprehensive and informed responses. For more information on "grounding" LLMs, see [grounding LLMs](https://techcommunity.microsoft.com/t5/fasttrack-for-azure/grounding-llms/ba-p/3843857). [[Go back](#vector-database-use-cases)] @@ -153,18 +156,21 @@ Use the natively [integrated vector database in Azure Cosmos DB for MongoDB](mon Use the natively integrated vector database in [Azure Cosmos DB for PostgreSQL](postgresql/howto-use-pgvector.md), which offers an efficient way to store, index, and search high-dimensional vector data directly alongside other application data. This approach removes the necessity of migrating your data to costlier alternative vector databases and provides a seamless integration of your AI-driven applications. #### Code sample + - Python: [Python notebook tutorial - food review chatbot](https://github.com/microsoft/AzureDataRetrievalAugmentedGenerationSamples/tree/main/Python/CosmosDB-PostgreSQL_CognitiveSearch) -### Next step +## Vector database solutions -[30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) +- [Azure PostgreSQL Server pgvector Extension](../postgresql/flexible-server/how-to-use-pgvector.md) -[90-day Free Trial and up to $6,000 in throughput credits with Azure AI Advantage](ai-advantage.md) +:::image type="content" source="media/vector-search/azure-databases-and-ai-search.png" lightbox="media/vector-search/azure-databases-and-ai-search.png" alt-text="Diagram of Vector indexing services for Azure Cosmos DB."::: -> [!div class="nextstepaction"] -> [Use the Azure Cosmos DB lifetime free tier](free-tier.md) +## Related content -## More vector database solutions -- [Azure PostgreSQL Server pgvector Extension](../postgresql/flexible-server/how-to-use-pgvector.md) +- [30-day Free Trial without Azure subscription](https://azure.microsoft.com/try/cosmosdb/) +- [90-day Free Trial and up to $6,000 in throughput credits with Azure AI Advantage](ai-advantage.md) + +## Next step -:::image type="content" source="media/vector-search/azure-databases-and-ai-search.png" lightbox="media/vector-search/azure-databases-and-ai-search.png" alt-text="Diagram of Vector indexing services."::: +> [!div class="nextstepaction"] +> [Use the Azure Cosmos DB lifetime free tier](free-tier.md) From 1fb4aa96bd3ec854b9a4a0623fe94128aeb0a435 Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 13:04:15 -0500 Subject: [PATCH 12/14] Fix hyperlink --- articles/cosmos-db/nosql/troubleshoot-query-performance.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/articles/cosmos-db/nosql/troubleshoot-query-performance.md b/articles/cosmos-db/nosql/troubleshoot-query-performance.md index 5b0324cab4..949e4d6c69 100644 --- a/articles/cosmos-db/nosql/troubleshoot-query-performance.md +++ b/articles/cosmos-db/nosql/troubleshoot-query-performance.md @@ -179,7 +179,7 @@ Updated indexing policy: **RU charge:** 2.98 RUs -You can add properties to the indexing policy at any time, with no effect on write or read availability. You can [track index transformation progress](./how-to-manage-indexing-policy.md#dotnet-sdk). +You can add properties to the indexing policy at any time, with no effect on write or read availability. You can [track index transformation progress](./how-to-manage-indexing-policy.md). ### Understand which system functions use the index From 8c85097c32fcbbb38d83d0afa0c1e54d44e05980 Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 14:15:23 -0500 Subject: [PATCH 13/14] Implement changes --- articles/cosmos-db/ai-agents.md | 68 +++++++++++++-------------- articles/cosmos-db/introduction.md | 2 +- articles/cosmos-db/vector-database.md | 2 +- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/articles/cosmos-db/ai-agents.md b/articles/cosmos-db/ai-agents.md index 7c6d423aec..b2cde79c4c 100644 --- a/articles/cosmos-db/ai-agents.md +++ b/articles/cosmos-db/ai-agents.md @@ -244,22 +244,22 @@ The GitHub repository contains a Python project in the *loader* directory. It's Set up your Python virtual environment in the *loader* directory by running the following command: -```python - python -m venv venv +```shell +python -m venv venv ``` Activate your environment and install dependencies in the *loader* directory: -```python - venv\Scripts\activate - python -m pip install -r requirements.txt +```shell +venv\Scripts\activate +python -m pip install -r requirements.txt ``` Create a file named *.env* in the *loader* directory, to store the following environment variables: -```python - OPENAI_API_KEY="" - MONGO_CONNECTION_STRING="mongodb+srv:" +```env +OPENAI_API_KEY="" +MONGO_CONNECTION_STRING="mongodb+srv:" ``` #### Load documents and vectors @@ -273,7 +273,6 @@ from cosmosdbloader import CosmosDBLoader from itinerarybuilder import ItineraryBuilder import json - cosmosdb_loader = CosmosDBLoader(DB_Name='travel') #read in ship data @@ -304,13 +303,13 @@ collection.create_index([('name', 'text')]) Load the documents, load the vectors, and create indexes by running the following command from the *loader* directory: -```python - python main.py +```shell +python main.py ``` Here's the output of *main.py*: -```markdown +```output --build itinerary-- --load itinerary-- --load destinations-- @@ -336,28 +335,28 @@ We used Python version 3.11.4 for the development and testing of the API. Set up your Python virtual environment in the *api* directory: -```python - python -m venv venv +```shell +python -m venv venv ``` Activate your environment and install dependencies by using the *requirements* file in the *api* directory: -```python - venv\Scripts\activate - python -m pip install -r requirements.txt +```shell +venv\Scripts\activate +python -m pip install -r requirements.txt ``` Create a file named *.env* in the *api* directory, to store your environment variables: -```python - OPENAI_API_KEY="" - MONGO_CONNECTION_STRING="mongodb+srv:" +```env +OPENAI_API_KEY="" +MONGO_CONNECTION_STRING="mongodb+srv:" ``` Now that you've configured the environment and set up variables, run the following command from the *api* directory to initiate the server: -```python - python app.py +```shell +python app.py ``` The FastAPI server starts on the localhost loopback 127.0.0.1 port 8000 by default. You can access the Swagger documents by using the following localhost address: `http://127.0.0.1:8000/docs`. @@ -372,7 +371,7 @@ Select **Try it out** for `/session/`. :::image type="content" source="media/gen-ai/ai-agent/fastapi-get-session.png" lightbox="media/gen-ai/ai-agent/fastapi-get-session.png" alt-text="Screenshot of the use of the Get Session method in Python FastAPI, with the button for trying it out."::: -```python +```json { "session_id": "0505a645526f4d68a3603ef01efaab19" } @@ -383,9 +382,9 @@ For the AI agent, you only need to simulate a session. The stubbed-out method me Here are the contents of *web/session.py*: ```python - @router.get("/") - def get_session(): - return {'session_id':str(uuid.uuid4().hex)} +@router.get("/") +def get_session(): + return {'session_id':str(uuid.uuid4().hex)} ``` ### Start a conversation with the AI travel agent @@ -398,7 +397,7 @@ Select **Try it out** for `/agent/agent_chat`. Use this example parameter: -```python +```json { "input": "I want to take a relaxing vacation.", "session_id": "0505a645526f4d68a3603ef01efaab19" @@ -409,7 +408,7 @@ The initial execution results in a recommendation for the Tranquil Breeze Cruise The similarity search scores appear as output from the API for debugging purposes. Here's the output after a call to `data.mongodb.travel.similarity_search()`: -```markdown +```output 0.8394561085977978 0.8086545112328692 2 @@ -447,7 +446,6 @@ from service import TravelAgentTools as agent_tools load_dotenv(override=False) - chat : ChatOpenAI | None=None agent_with_chat_history : RunnableWithMessageHistory | None=None @@ -598,8 +596,8 @@ Install Node.js and the dependencies before testing the React interface. Run the following command from the *web* directory to perform a clean installation of project dependencies. The installation might take some time. -```javascript - npm ci +```shell +npm ci ``` Next, create a file named *.env* within the *web* directory to facilitate the storage of environment variables. Include the following details in the newly created *.env* file: @@ -608,8 +606,8 @@ Next, create a file named *.env* within the *web* directory to facilitate the st Now, run the following command from the *web* directory to initiate the React web user interface: -```javascript - npm start +```shell +npm start ``` Running the previous command opens the React web application. @@ -627,7 +625,7 @@ The main component serves as the central manager of the application. It acts as Here are the contents of *main.js*: ```javascript - import React, { Component } from 'react' +import React, { Component } from 'react' import { Stack, Link, Paper } from '@mui/material' import TravelAgent from './TripPlanning/TravelAgent' @@ -817,7 +815,7 @@ Select **Effortlessly plan your voyage** to open the travel assistant. The chat layout component oversees the arrangement of the chat. It systematically processes the chat messages and implements the formatting specified in the `message` JSON object. -Here are the contents of *TripPlanning/ChatLayout.py*: +Here are the contents of *TripPlanning/ChatLayout.js*: ```javascript import React from 'react' diff --git a/articles/cosmos-db/introduction.md b/articles/cosmos-db/introduction.md index 30fc0684a5..082ab046a0 100644 --- a/articles/cosmos-db/introduction.md +++ b/articles/cosmos-db/introduction.md @@ -29,7 +29,7 @@ Today's applications are required to be highly responsive and always online. The The surge of AI-powered applications created another layer of complexity, because many of these applications integrate a multitude of data stores. For example, some organizations built applications that simultaneously connect to MongoDB, Postgres, Redis, and Gremlin. These databases differ in implementation workflow and operational performances, posing extra complexity for scaling applications. -Azure Cosmos DB simplifies and expedites your application development by being the single database for your operational data needs, from [geo-replicated distributed caching](https://medium.com/@marcodesanctis2/using-azure-cosmos-db-as-your-persistent-geo-replicated-distributed-cache-b381ad80f8a0) to backup to [vector indexing and search](vector-database.md). It provides the data infrastructure for modern applications like [AI agent](ai-agents.md), digital commerce, Internet of Things, and booking management. It can accommodate all your operational data models, including relational, document, vector, key-value, graph, and table. +Azure Cosmos DB simplifies and expedites your application development by being the single database for your operational data needs, from [geo-replicated distributed caching](https://medium.com/@marcodesanctis2/using-azure-cosmos-db-as-your-persistent-geo-replicated-distributed-cache-b381ad80f8a0) to backup storage, to [vector indexing and search](vector-database.md). It provides the data infrastructure for modern applications like [AI agent](ai-agents.md), digital commerce, Internet of Things, and booking management. It can accommodate all your operational data models, including relational, document, vector, key-value, graph, and table. ## An AI database providing industry-leading capabilities... diff --git a/articles/cosmos-db/vector-database.md b/articles/cosmos-db/vector-database.md index 181aa23289..b99706b033 100644 --- a/articles/cosmos-db/vector-database.md +++ b/articles/cosmos-db/vector-database.md @@ -1,7 +1,7 @@ --- title: Integrated vector database titleSuffix: Azure Cosmos DB -description: Review how to use Azure Cosmos DB as a vectror database in numerous domains and situations across analytical and generative AI. +description: Review how to use Azure Cosmos DB as a vector database in numerous domains and situations across analytical and generative AI. author: markjbrown ms.author: mjbrown ms.service: azure-cosmos-db From d7c4d0a201912d271fc4072435e17c6f40adcebb Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Tue, 3 Dec 2024 15:13:38 -0500 Subject: [PATCH 14/14] PR review changes --- articles/cosmos-db/nosql/how-to-manage-indexing-policy.md | 2 +- articles/cosmos-db/nosql/how-to-python-vector-index-query.md | 2 +- articles/cosmos-db/nosql/multi-tenancy-vector-search.md | 4 ++-- articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md b/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md index 329e13b9e8..8fa26eb959 100644 --- a/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md +++ b/articles/cosmos-db/nosql/how-to-manage-indexing-policy.md @@ -156,7 +156,7 @@ The `quantizedFlat` index stores quantized or compressed vectors on the index. V The `diskANN` index is a separate index defined specifically for vectors leveraging [DiskANN](https://www.microsoft.com/research/publication/diskann-fast-accurate-billion-point-nearest-neighbor-search-on-a-single-node/), a suite of highly performant vector indexing algorithms developed by Microsoft Research. DiskANN indexes can offer some of the lowest latency, highest query-per-second (QPS), and lowest RU cost queries at high accuracy. However, since DiskANN is an approximate nearest neighbors (ANN) index, the accuracy may be lower than `quantizedFlat` or `flat`. -The `diskANN` and `quantizedFlat` indexes can take optional index build parameters that can be used to tune the accuracy vs latency trade off that applies to every Approximate Nearest Neighbors vector index. +The `diskANN` and `quantizedFlat` indexes can take optional index build parameters that can be used to tune the accuracy vs latency trade-off that applies to every Approximate Nearest Neighbors vector index. - `quantizationByteSize`: Sets the size (in bytes) for product quantization. Min=1, Default=dynamic (system decides), Max=512. Setting this larger may result in higher accuracy vector searches at expense of higher RU cost and higher latency. This applies to both `quantizedFlat` and `DiskANN` index types. - `indexingSearchListSize`: Sets how many vectors to search over during index build construction. Min=10, Default=100, Max=500. Setting this larger may result in higher accuracy vector searches at the expense of longer index build times and higher vector ingest latencies. This applies to `DiskANN` indexes only. diff --git a/articles/cosmos-db/nosql/how-to-python-vector-index-query.md b/articles/cosmos-db/nosql/how-to-python-vector-index-query.md index d7c158e43c..3bb037026f 100644 --- a/articles/cosmos-db/nosql/how-to-python-vector-index-query.md +++ b/articles/cosmos-db/nosql/how-to-python-vector-index-query.md @@ -146,7 +146,7 @@ indexing_policy = { > The vector path added to the "excludedPaths" section of the indexing policy to ensure optimized performance for insertion. Not adding the vector path to "excludedPaths" will result in higher RU charge and latency for vector insertions. > [!IMPORTANT] -> Currently vector search in Azure Cosmos DB for NoSQL is supported on new containers only. You need to set both the container vector policy and any vector indexing policy during the time of container creation as it can’t be modified later. Both policies will be modifiable in a future improvement to the preview feature. +> Currently vector search in Azure Cosmos DB for NoSQL is supported on new containers only. You need to set both the container vector policy and any vector indexing policy during the time of container creation as it can’t be modified later. ## Create container with vector policy diff --git a/articles/cosmos-db/nosql/multi-tenancy-vector-search.md b/articles/cosmos-db/nosql/multi-tenancy-vector-search.md index 982553e6a8..d40cba437c 100644 --- a/articles/cosmos-db/nosql/multi-tenancy-vector-search.md +++ b/articles/cosmos-db/nosql/multi-tenancy-vector-search.md @@ -1,6 +1,6 @@ --- title: Multitenancy in Azure Cosmos DB -description: Review critical concepts required to build multi-tenant generative AI applications in Azure Cosmos DB. +description: Review critical concepts required to build multitenant generative AI applications in Azure Cosmos DB. author: TheovanKraay ms.service: azure-cosmos-db ms.subservice: nosql @@ -42,7 +42,7 @@ For a higher density of tenants and lower isolation, the partition key-per-tenan - **Cost Efficiency:** Sharing a single Cosmos DB account across multiple tenants reduces overhead. - **Scalability:** Can manage a large number of tenants, each isolated within their partition key. - **Simplified Management:** Fewer Cosmos DB accounts to manage. -- **Hierarchical Partition Keys (HPK):** Optimizes data organization and query performance in multi-tenant apps with a high number of tenants. +- **Hierarchical Partition Keys (HPK):** Optimizes data organization and query performance in multitenant apps with a high number of tenants. **Drawbacks:** - **Resource Contention:** Shared resources can lead to contention during peak usage. diff --git a/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md b/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md index 4ee977d6e7..389d28b55f 100644 --- a/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md +++ b/articles/cosmos-db/nosql/query/how-to-enable-use-copilot.md @@ -53,7 +53,7 @@ As a preview feature, you'll have to add Microsoft Copilot for Azure in Cosmos D 1. Next, open the query editor experience from one of two ways: - - Select the **Query faster with Copilot** card on the Data Explorer's welcome screen. This option will take you to the a query editor targeting the `CopilotSampleDb` database and `SampleContainer` container, which contains sample data for you to use with Copilot. This database is managed by Microsoft and does not interact or connect to your other databases. `CopoilotSampleDb` is free for all Azure Cosmos DB NoSQL customers. + - Select the **Query faster with Copilot** card on the Data Explorer's welcome screen. This option will take you to the query editor targeting the `CopilotSampleDb` database and `SampleContainer` container, which contains sample data for you to use with Copilot. This database is managed by Microsoft and does not interact or connect to your other databases. `CopoilotSampleDb` is free for all Azure Cosmos DB NoSQL customers. - Select an existing API for NoSQL database and container. Then, select **New SQL Query** from the menu bar.