{"id":7010,"date":"2023-11-15T08:00:38","date_gmt":"2023-11-15T16:00:38","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=7010"},"modified":"2024-06-13T14:02:14","modified_gmt":"2024-06-13T21:02:14","slug":"introducing-vector-search-in-azure-managed-instance-for-apache-cassandra","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/introducing-vector-search-in-azure-managed-instance-for-apache-cassandra\/","title":{"rendered":"Introducing Vector Database in Azure Managed Instance for Apache Cassandra!"},"content":{"rendered":"<p>We are delighted to announce <a href=\"https:\/\/cassandra.apache.org\/doc\/latest\/cassandra\/new\/index.html#new-features-in-apache-cassandra-5-0\" target=\"_blank\" rel=\"noopener\">Cassandra 5.0<\/a> in <strong>public preview<\/strong> for <a href=\"https:\/\/azure.microsoft.com\/products\/managed-instance-apache-cassandra\" target=\"_blank\" rel=\"noopener\">Azure Managed Instance for Apache Cassandra<\/a>, with <a href=\"https:\/\/cassandra.apache.org\/doc\/latest\/cassandra\/vector-search\/overview.html\" target=\"_blank\" rel=\"noopener\">vector search<\/a> capabilities enabled!<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-size: 24pt;\">Vector Database in Cassandra 5.0<\/span><\/h2>\n<p>In Cassandra 5.0, the integrated v<span data-contrast=\"auto\">ector database is backed by storage-attached indexes that represent data as vectors in a multi-dimensional space. We use <a href=\"https:\/\/www.microsoft.com\/research\/publication\/diskann-fast-accurate-billion-point-nearest-neighbor-search-on-a-single-node\/\" target=\"_blank\" rel=\"noopener\">DiskANN<\/a>, <span class=\"ui-provider ef axt axu axv axw axx axy axz aya ayb ayc ayd aye ayf ayg ayh ayi ayj ayk ayl aym ayn ayo ayp ayq ayr ays ayt ayu ayv ayw ayx ayy ayz aza\" dir=\"ltr\">a Microsoft Research-born algorithm that allows for adding new vectors to an index without the need for reindexing, which causes downtime in other vector stores. <\/span>DiskANN enables fast and accurate similarity-based searches, which can be used to build next-generation AI apps that use <a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/overview\" target=\"_blank\" rel=\"noopener\">Azure OpenAI<\/a> and other LLMs.<\/span><\/p>\n<p><span data-ccp-props=\"{}\">With the integrated vector database, your applications find similar items based on their data characteristics rather than exact matches on a property field. They<\/span> work by taking the vector representations (lists of numbers) of your data that you have created using an ML model, or an embeddings API such as <a href=\"https:\/\/learn.microsoft.com\/azure\/cognitive-services\/openai\/concepts\/understand-embeddings\" target=\"_blank\" rel=\"noopener\">Azure OpenAI Service Embeddings<\/a>\u00a0or\u00a0<a href=\"https:\/\/azure.microsoft.com\/en-us\/solutions\/hugging-face-on-azure\/\" target=\"_blank\" rel=\"noopener\">Hugging Face on Azure<\/a>. They then measure the distance 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.<\/p>\n<h2><\/h2>\n<h4 id=\"create-a-vector-index\"><span style=\"font-size: 18pt;\">Create a Vector Index<\/span><\/h4>\n<p>In Cassandra, first create a keyspace and table which has the new vector data type. The second parameter represents the number of dimensions that the vector can hold. In this case, we&#8217;ll only add 2 dimensions. However, the dimensionality of your vectors may be different. For example, if you were using <a href=\"https:\/\/learn.microsoft.com\/azure\/cognitive-services\/openai\/how-to\/embeddings\" target=\"_blank\" rel=\"noopener\">OpenAI Embeddings<\/a>, this would be set to <em>1536.<\/em><\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\"><span class=\"cm-line\">CREATE KEYSPACE test <\/span><\/code>WITH replication = {'class': 'NetworkTopologyStrategy', 'dc1': '3'}<code class=\"language-sql\"><span class=\"cm-line\">;\r\n<\/span><span class=\"cm-line\">use test;\r\n<\/span><span class=\"cm-line\">CREATE TABLE s (k int PRIMARY KEY, v vector&lt;float, 2&gt;);<\/span><\/code><\/pre>\n<p>Next, create an index on the vector field:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">CREATE CUSTOM INDEX ON s(v) USING 'StorageAttachedIndex';<\/code><\/pre>\n<h2><\/h2>\n<h4 id=\"add-vectors-to-your-database\"><span style=\"font-size: 18pt;\">Add vectors to your table<\/span><\/h4>\n<p>To add vectors to your table, you can use the OpenAI Embeddings model to <a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/how-to\/embeddings?tabs=console\" target=\"_blank\" rel=\"noopener\">generate embeddings from the data<\/a>. In this example, we\u2019ll insert a few rows that contain sample embeddings:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">INSERT INTO s (k, v) VALUES (1, [1, 1.8]);\r\nINSERT INTO s (k, v) VALUES (1, [1, 2.1]);<\/code><\/pre>\n<h2><\/h2>\n<h4 id=\"perform-a-vector-search\"><span style=\"font-size: 18pt;\">Perform a vector search<\/span><\/h4>\n<p class=\"prettyprint language-sql\">Finally, you can order by the vector column, with an Approximate Nearest Neighbour (ANN) search:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">SELECT k, v FROM s WHERE k = 1 ORDER BY v ANN OF [2, 2] LIMIT 4 ALLOW FILTERING;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2 id=\"create-a-vector-index\"><span style=\"font-size: 24pt;\">Generative AI Apps<\/span><\/h2>\n<p>Lets put this all together in an app that uses vector search in Azure Managed Instance for Apache Cassandra! To get you started, we&#8217;ve created a Java-based ChatGPT-like <a href=\"https:\/\/github.com\/Azure-Samples\/cassandra-mi-chatgpt-sample\" target=\"_blank\" rel=\"noopener\">sample app<\/a> that integrates with Cassandra 5.0 using vector search. The sample allows you to load a file containing your own private contextual data, and uses Azure OpenAI features to create a chat experience.<\/p>\n<p>Included in the sample are:<\/p>\n<ol>\n<li>Azure OpenAI embedding with vector dimensionality of 1536. Azure OpenAI will automatically create embeddings from the data loaded into the vector store.<\/li>\n<li>Loading and searching the data from a vector store table in Cassandra 5.0, and performing a similarity search using Approximate Nearest Neighbour (ANN).<\/li>\n<li>Generating prompts and marshalling chat history (in memory).<\/li>\n<li>Chat completion using Azure OpenAI.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4>How it works<\/h4>\n<p><img decoding=\"async\" class=\"\" src=\"https:\/\/github.com\/Azure-Samples\/cassandra-mi-chatgpt-sample\/blob\/main\/docs\/workflow.png?raw=true\" alt=\"workflow.png\" width=\"1059\" height=\"719\" \/><\/p>\n<p>Follow the instructions in the repo to create a generative AI chat experience using vector search in Azure Managed Instance for Apache Cassandra! Once you&#8217;ve loaded your data, you can ask the app questions and it will answer based on the private contextual data you provided.<\/p>\n<p>Be careful not to use data that&#8217;s too biased! \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p><div style=\"width: 1724px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-7010-1\" width=\"1724\" height=\"848\" loop autoplay preload=\"auto\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/chatgpt-2.mp4?_=1\" \/><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/chatgpt-2.mp4\">https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/chatgpt-2.mp4<\/a><\/video><\/div><\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"perform-a-vector-search\"><span style=\"font-size: 24pt;\">Vector search at scale<\/span><\/h2>\n<p>Apache Cassandra is a massively scalable database with transparent <a href=\"https:\/\/cassandra.apache.org\/doc\/stable\/cassandra\/data_modeling\/intro.html#partitions\" target=\"_blank\" rel=\"noopener\">partitioning<\/a> that stores data across many independent, fault-tolerant nodes in a cluster. Vector searches in Cassandra 5.0 will perform much better if searches are directed towards vectors stored in a single partition.<\/p>\n<p>In the app sample above, by default, each data file you load into the vector store will generate 1 partition of vectors in the table. If you load multiple files, this will result in cross-partition key queries when the app attempts to perform a vector search against your contextual data.<\/p>\n<p>For large-scale apps that leverage vector search, you should design your approach so that, as much as possible, similarity searches are scoped to a single partition or a small number of partitions.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/logs.png\"><img decoding=\"async\" class=\"alignnone wp-image-7037\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/logs-300x44.png\" alt=\"Image logs\" width=\"1057\" height=\"155\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/logs-300x44.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2023\/11\/logs-768x114.png 768w\" sizes=\"(max-width: 1057px) 100vw, 1057px\" \/><\/a><\/p>\n<h2 id=\"getting-started\" class=\"\"><span style=\"font-size: 18pt;\">Getting started<\/span><\/h2>\n<ul>\n<li>Find Azure Managed Instance for Cassandra in <a href=\"https:\/\/ms.portal.azure.com\/#home\" target=\"_blank\" rel=\"noopener\">Azure Portal<\/a><\/li>\n<li>Get started easily using <a href=\"https:\/\/docs.microsoft.com\/azure\/managed-instance-apache-cassandra\/create-cluster-portal\" target=\"_blank\" rel=\"noopener\">Quickstart documentation<\/a><\/li>\n<li>Check out the ChatGPT sample for Azure Managed Instance for Apache Cassandra <a href=\"https:\/\/github.com\/Azure-Samples\/cassandra-mi-chatgpt-sample\" target=\"_blank\" rel=\"noopener\">here<\/a>!<\/li>\n<li>Find out about pricing <a href=\"https:\/\/azure.microsoft.com\/pricing\/details\/managed-instance-apache-cassandra\/?cdn=disable\" target=\"_blank\" rel=\"noopener\">here<\/a>!<\/li>\n<li>Read all our technical documentation <a href=\"https:\/\/docs.microsoft.com\/azure\/managed-instance-apache-cassandra\/\">here<\/a><\/li>\n<\/ul>\n<h3>About Azure Cosmos DB<\/h3>\n<p><a href=\"https:\/\/azure.microsoft.com\/en-us\/products\/cosmos-db\/\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">Azure Cosmos DB<\/span><\/a> is <span data-contrast=\"none\">a fully managed NoSQL, relational, and vector database service for modern app development with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB, and Apache Cassandra. <\/span><a href=\"https:\/\/aka.ms\/trycosmosdb\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">Try Azure Cosmos DB for free here<\/span><\/a><span data-contrast=\"none\">. To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<\/span><a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">Twitter<\/span><\/a><span data-contrast=\"none\">,\u00a0<\/span><a href=\"https:\/\/www.youtube.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">YouTube<\/span><\/a><span data-contrast=\"none\">, and\u00a0<\/span><a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">LinkedIn<\/span><\/a><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p class=\"prettyprint language-sql\"><code class=\"language-sql\"><\/code><\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We are delighted to announce Cassandra 5.0 in public preview for Azure Managed Instance for Apache Cassandra, with vector search capabilities enabled! &nbsp; Vector Database in Cassandra 5.0 In Cassandra 5.0, the integrated vector database is backed by storage-attached indexes that represent data as vectors in a multi-dimensional space. We use DiskANN, a Microsoft Research-born [&hellip;]<\/p>\n","protected":false},"author":9387,"featured_media":4366,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"video","meta":{"_acf_changed":false,"footnotes":""},"categories":[1610,1862,1821],"tags":[1888,1893,1891,1860,1892,1868],"class_list":["post-7010","post","type-post","status-publish","format-video","has-post-thumbnail","hentry","category-ai","category-azure-openai","category-managed-instance-apache-cassandra","tag-azure-managed-instance-for-apache-cassandra","tag-azure-openai","tag-cassandra-5-0","tag-chatgpt","tag-generative-ai","tag-vector-search","post_format-post-format-video"],"acf":[],"blog_post_summary":"<p>We are delighted to announce Cassandra 5.0 in public preview for Azure Managed Instance for Apache Cassandra, with vector search capabilities enabled! &nbsp; Vector Database in Cassandra 5.0 In Cassandra 5.0, the integrated vector database is backed by storage-attached indexes that represent data as vectors in a multi-dimensional space. We use DiskANN, a Microsoft Research-born [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/7010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/9387"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=7010"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/7010\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/4366"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=7010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=7010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=7010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}