{"id":1484,"date":"2023-11-13T05:00:57","date_gmt":"2023-11-13T13:00:57","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=1484"},"modified":"2023-11-13T08:29:55","modified_gmt":"2023-11-13T16:29:55","slug":"building-ai-applications-with-microsoft-semantic-kernel-and-mongodb-atlas-vector-search","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/building-ai-applications-with-microsoft-semantic-kernel-and-mongodb-atlas-vector-search\/","title":{"rendered":"Building AI Applications with Microsoft Semantic Kernel and MongoDB Atlas Vector Search"},"content":{"rendered":"<p class=\"code-line\" dir=\"auto\" data-line=\"0\">As part of this guest blogpost by the MongoDB Atlas Vector Search team, we are excited to announce native support for <a href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\" data-href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\">MongoDB Atlas Vector Search<\/a>\u00a0in\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/overview\/\" data-href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/overview\/\">Microsoft Semantic Kernel<\/a>. With this integration, users can bring the power of LLMs (large language models) to their proprietary data securely, and build generative AI applications using RAG (retrieval-augmented generation) with programming languages like Python and C#. The accompanying tutorial will walk you through an example.<\/p>\n<h2 id=\"what-is-mongodb-atlas-vector-search\" class=\"code-line\" dir=\"auto\" data-line=\"11\">What is MongoDB Atlas Vector Search?<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"12\">MongoDB Atlas Vector Search is a fully managed service that simplifies the process of effectively indexing high-dimensional vector embedding data within MongoDB and being able to perform fast vector similarity searches.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"14\">Embedding refers to the representation of words, phrases, or other entities as dense vectors in a continuous vector space. It&#8217;s designed to ensure that words with similar meanings are grouped closer together. This method helps computer models better understand and process language by recognizing patterns and relationships between words and is what allows us to search by semantic meaning.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"16\">When data is converted into numeric vector embeddings using encoding models, these embeddings can be stored directly alongside their respective source data within the MongoDB database. This co-location of vector embeddings and the original data not only enhances the efficiency of queries but also eliminates potential synchronization issues. By avoiding the need to maintain separate databases or synchronization processes for the source data and its embeddings, MongoDB provides a seamless and integrated data retrieval experience.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"18\">This consolidated approach streamlines database management and allows for intuitive and sophisticated semantic searches, making the integration of AI-powered experiences easier.<\/p>\n<h2 id=\"microsoft-semantic-kernel-and-mongodb\" class=\"code-line\" dir=\"auto\" data-line=\"20\">Microsoft Semantic Kernel and MongoDB<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"21\">This combination enables developers to build AI-powered intelligent applications using MongoDB Atlas Vector Search and large language models from providers like OpenAI, Azure OpenAI, and Hugging Face.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"23\">Despite all their incredible capabilities, LLMs have a knowledge cutoff date and often need to be augmented with proprietary, up-to-date information for the particular business that an application is being built for. This \u201clong-term memory for LLM\u201d capability for AI-powered intelligent applications is typically powered by leveraging\u00a0<a href=\"https:\/\/www.mongodb.com\/developer\/products\/atlas\/building-generative-ai-applications-vector-search-open-source-models\/\" data-href=\"https:\/\/www.mongodb.com\/developer\/products\/atlas\/building-generative-ai-applications-vector-search-open-source-models\/\">vector embeddings<\/a>. Semantic Kernel allows for storing and retrieving this vector context for AI apps using the memory plugin (which now has support for MongoDB Atlas Vector Search).<\/p>\n<h2 id=\"tutorial\" class=\"code-line\" dir=\"auto\" data-line=\"26\">Tutorial<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"27\"><a href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\" data-href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\">Atlas Vector Search<\/a>\u00a0is integrated in this tutorial to provide a way to interact with our memory store that was created through our MongoDB and Semantic Kernel\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/python\/semantic_kernel\/connectors\/memory\/mongodb_atlas\" data-href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/python\/semantic_kernel\/connectors\/memory\/mongodb_atlas\">connector<\/a>.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"29\">This tutorial takes you through how to use Microsoft Semantic Kernel to properly upload and embed documents into your MongoDB Atlas cluster, and then conduct queries using Microsoft Semantic Kernel as well, all in Python!<\/p>\n<h2 id=\"pre-requisites\" class=\"code-line\" dir=\"auto\" data-line=\"31\">Pre-requisites<\/h2>\n<ul class=\"code-line\" dir=\"auto\" data-line=\"32\">\n<li class=\"code-line\" dir=\"auto\" data-line=\"32\"><a href=\"https:\/\/www.mongodb.com\/docs\/atlas\/getting-started\/\" data-href=\"https:\/\/www.mongodb.com\/docs\/atlas\/getting-started\/\">MongoDB Atlas cluster<\/a><\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"33\">IDE of your choice (this tutorial uses\u00a0<a href=\"https:\/\/colab.research.google.com\/drive\/1Qlgor8-nFEkugMdTADYVKIYZejSAWQUV#scrollTo=U8u9vfQYnWvt\" data-href=\"https:\/\/colab.research.google.com\/drive\/1Qlgor8-nFEkugMdTADYVKIYZejSAWQUV#scrollTo=U8u9vfQYnWvt\">Google Colab<\/a>\u00a0\u2014 please refer to it if you\u2019d like to run the commands directly)<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"34\"><a href=\"https:\/\/platform.openai.com\/docs\/introduction\" data-href=\"https:\/\/platform.openai.com\/docs\/introduction\">OpenAI API key<\/a><\/li>\n<\/ul>\n<p class=\"code-line\" dir=\"auto\" data-line=\"36\">Let\u2019s get started!<\/p>\n<h2 id=\"setting-up-our-atlas-cluster\" class=\"code-line\" dir=\"auto\" data-line=\"38\">Setting up our Atlas cluster<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"39\">Visit the\u00a0<a href=\"https:\/\/account.mongodb.com\/account\/login\" data-href=\"https:\/\/account.mongodb.com\/account\/login\">MongoDB Atlas<\/a>\u00a0dashboard and set up your cluster. In order to take advantage of the\u00a0<code>$vectorSearch<\/code>\u00a0operator in an aggregation pipeline, you need to run MongoDB Atlas 6.0.11 or higher. This tutorial can be built using a free cluster.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"41\">When you\u2019re setting up your deployment, you\u2019ll be prompted to set up a database user and rules for your network connection. Please ensure you save your username and password somewhere safe and have the correct IP address rules in place so your cluster can connect properly.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"43\">If you need more help getting started, check out our\u00a0<a href=\"https:\/\/www.mongodb.com\/basics\/mongodb-atlas-tutorial\" data-href=\"https:\/\/www.mongodb.com\/basics\/mongodb-atlas-tutorial\">tutorial on MongoDB Atlas<\/a>.<\/p>\n<h2 id=\"installing-the-latest-version-of-semantic-kernel\" class=\"code-line\" dir=\"auto\" data-line=\"45\">Installing the latest version of Semantic Kernel<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"46\">In order to be successful with our tutorial, let\u2019s ensure we have the most up-to-date version of Semantic Kernel installed in our IDE. As of the creation of this tutorial, the latest version is 0.3.14. Please run this\u00a0<code>pip<\/code>\u00a0command in your IDE to get started:<\/p>\n<pre><code class=\"code-line\" dir=\"auto\" data-line=\"47\"><code>!python -m pip install semantic-kernel==0.3.14.dev<\/code><\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"50\">Once it has been successfully run, you will see various packages being downloaded. Please ensure\u00a0<code>pymongo<\/code>\u00a0is downloaded in this list.<\/p>\n<h2 id=\"setting-up-our-imports\" class=\"code-line\" dir=\"auto\" data-line=\"52\">Setting up our imports<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"53\">Here, include the information about our OpenAI API key and our connection string.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"55\">Let\u2019s set up the necessary imports:<\/p>\n<div>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">import openai\r\nimport semantic_kernel as sk\r\nfrom semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAITextEmbedding\r\nfrom semantic_kernel.connectors.memory.mongodb_atlas import MongoDBAtlasMemoryStore\r\n\r\nkernel = sk.Kernel()\r\nopenai.api_key = '&lt;your-openai-key&gt;'\r\nMONGODB_SEARCH_INDEX=\"defaultRandomFacts\"\r\nMONGODB_DATABASE=\"semantic-kernel\"\r\nMONGODB_COLLECTION=\"randomFacts\"\r\n\r\nkernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(\"gpt-3.5-turbo\", openai.api_key))\r\nkernel.add_text_embedding_generation_service(\"ada\", OpenAITextEmbedding(\"text-embedding-ada-002\", openai.api_key))\r\n\r\nkernel.register_memory_store(memory_store=MongoDBAtlasMemoryStore(index_name=MONGODB_SEARCH_INDEX, database_name=MONGODB_DATABASE, connection_string=\"&lt;mongodb-uri&gt;\"))\r\nkernel.import_skill(sk.core_skills.TextMemorySkill())\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"78\">Importing in OpenAI is crucial because we are using their data model to embed not only our documents but also our queries. We also want to import their Text Embedding library for this same reason. For this tutorial, we are using the embedding model\u00a0<code>ada-002<\/code>, but please double check that you\u2019re using a model that is compatible with your OpenAI API key.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"80\">Our\u00a0<code>MongoDBAtlasMemoryStore<\/code>\u00a0class is very important as it\u2019s the part that enables us to use MongoDB as our memory store. This means we can connect to the Semantic Kernel and have our documents properly saved and formatted in our cluster. For more information on this class, please refer to the\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/python\/semantic_kernel\/connectors\/memory\/mongodb_atlas\" data-href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/python\/semantic_kernel\/connectors\/memory\/mongodb_atlas\">repository<\/a>.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"82\">This is also where you will need to incorporate your OpenAI API key along with your\u00a0<a href=\"https:\/\/www.mongodb.com\/docs\/atlas\/\" data-href=\"https:\/\/www.mongodb.com\/docs\/atlas\/\">MongoDB connection string<\/a>, and other important variables that we will use. The ones above are just a suggestion, but if they are changed while attempting the tutorial, please ensure they are consistent throughout. For help on accessing your OpenAI key, please read the section below.<\/p>\n<h3 id=\"generate-your-openai-key\" class=\"code-line\" dir=\"auto\" data-line=\"85\">Generate your OpenAI key<\/h3>\n<p class=\"code-line\" dir=\"auto\" data-line=\"86\">In order to generate our embeddings, we will use the OpenAI API. First, we\u2019ll need a secret key. To create your OpenAI key, you&#8217;ll need to create an account. Once you have that, visit the\u00a0<a href=\"https:\/\/platform.openai.com\/\" data-href=\"https:\/\/platform.openai.com\/\">OpenAI API<\/a>\u00a0and you should be greeted with a screen like the one below. Click on your profile icon in the top right of the screen to get the dropdown menu and select \u201cView API keys\u201d.<\/p>\n<p dir=\"auto\" data-line=\"86\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture1.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1491\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture1.png\" alt=\"Where to find your API key on OpenAI\" width=\"468\" height=\"208\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture1.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture1-300x133.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"91\">Here, you can generate your own API key by clicking the \u201cCreate new secret key\u201d button. Give it a name and store it somewhere safe. This is all you need from OpenAI to use their API to generate your embeddings.<\/p>\n<p dir=\"auto\" data-line=\"91\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture10.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1492\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture10.png\" alt=\"how to create a new API key\" width=\"468\" height=\"185\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture10.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture10-300x119.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<h2 id=\"the-need-for-retrieval-augmented-generation-rag\" class=\"code-line\" dir=\"auto\" data-line=\"96\">The need for retrieval-augmented generation (RAG)<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"97\">Retrieval-augmented regeneration, also known as RAG, is an NLP technique that can help improve the quality of large language models (LLMs). It\u2019s an artificial intelligence framework for getting data from an external knowledge source. The memory store we are creating using Microsoft Semantic Kernel is an example of this. But why is RAG necessary? Let\u2019s take a look at an example.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"99\">LLMs like OpenAI GPT-3.5 exhibit an impressive and wide range of skills. They are trained on the data available on the internet about a wide range of topics and can answer queries accurately. Using Semantic Kernel, let\u2019s ask OpenAI\u2019s LLM if Albert Einstein likes coffee:<\/p>\n<div>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\"># Wrap your prompt in a function\r\nprompt = kernel.create_semantic_function(\"\"\"\r\nAs a friendly AI Copilot, answer the question: Did Albert Einstein like coffee?\r\n\"\"\")\r\n\r\nprint(prompt())<\/code><\/pre>\n<div><span style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\">The output received is:<\/span><\/div>\n<div><\/div>\n<pre class=\"prettyprint language-xml\"><code class=\"language-xml\"><span style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\">Yes, Albert Einstein was known to enjoy coffee. He was often seen with a cup of coffee in his hand and would frequently visit cafes to discuss scientific ideas with his colleagues over a cup of coffee.<\/span><\/code><\/pre>\n<\/div>\n<p class=\"code-line\" dir=\"auto\" data-line=\"112\">Since this information was available on the public internet, the LLM was able to provide the correct answer.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"114\">But LLMs have their limitations: They have a knowledge cutoff (September 2021, in the case of OpenAI) and do not know about proprietary and personal data. They also have a tendency to hallucinate \u2014 that is, they may confidently make up facts and provide answers that may seem to be accurate but are actually incorrect. Here is an example to demonstrate this knowledge gap:<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">prompt = kernel.create_semantic_function(\"\"\"\r\nAs a friendly AI Copilot, answer the question: Did I like coffee?\r\n\"\"\")\r\n\r\nprint(prompt())<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"123\">The output received is:<\/p>\n<pre class=\"prettyprint language-xml\"><code class=\"language-xml\">As an AI, I don't have personal preferences or experiences, so I can't say whether \"I\" liked coffee or not. However, coffee is a popular beverage enjoyed by many people around the world. It has a distinct taste and aroma that some people find appealing, while others may not enjoy it as much. Ultimately, whether someone likes coffee or not is a subjective matter and varies from person to person.<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"128\">As you can see, there is a knowledge gap here because we don\u2019t have our personal data loaded in OpenAI that our query can access. So let\u2019s change that. Continue on through the tutorial to learn how to augment the knowledge base of the LLM with proprietary data.<\/p>\n<h2 id=\"add-some-documents-into-our-mongodb-cluster\" class=\"code-line\" dir=\"auto\" data-line=\"130\">Add some documents into our MongoDB cluster<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"131\">Once we have incorporated our MongoDB connection string and our OpenAI API key, we are ready to add some documents into our MongoDB cluster.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"133\">Please ensure you\u2019re specifying the proper collection variable below that we set up above.<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">async def populate_memory(kernel: sk.Kernel) -&gt; None:\r\n# Add some documents to the semantic memory\r\nawait kernel.memory.save_information_async(\r\ncollection=MONGODB_COLLECTION, id=\"1\", text=\"We enjoy coffee and Starbucks\"\r\n)\r\nawait kernel.memory.save_information_async(\r\ncollection=MONGODB_COLLECTION, id=\"2\", text=\"We are Associate Developer Advocates at MongoDB\"\r\n)\r\nawait kernel.memory.save_information_async(\r\ncollection=MONGODB_COLLECTION, id=\"3\", text=\"We have great coworkers and we love our teams!\"\r\n)\r\nawait kernel.memory.save_information_async(\r\ncollection=MONGODB_COLLECTION, id=\"4\", text=\"Our names are Anaiya and Tim\"\r\n)\r\nawait kernel.memory.save_information_async(\r\ncollection=MONGODB_COLLECTION, id=\"5\", text=\"We have been to New York City and Dublin\"\r\n)<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"156\">Here, we are using the\u00a0<code>populate_memory<\/code>\u00a0function to define five documents with various facts about Anaiya and Tim. As you can see, the name of our collection is called \u201crandomFacts\u201d, we have specified the ID for each document (please ensure each ID is unique, otherwise you will get an error), and then we have included a text phrase we want to embed.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"158\">Once you have successfully filled in your information and have run this command, let\u2019s add them to our cluster \u2014 aka let\u2019s populate our memory! To do this, please run the command:<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">print(\"Populating memory...aka adding in documents\")\r\nawait populate_memory(kernel)<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"164\">Once this command has been successfully run, you should see the database, collection, documents, and their embeddings populate in your Atlas cluster. The screenshot below shows how the first document looks after running these commands.<\/p>\n<p dir=\"auto\" data-line=\"164\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/semantickernel3.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1527\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/semantickernel3.jpg\" alt=\"screenshot of our first document with the text embedding\" width=\"512\" height=\"460\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/semantickernel3.jpg 512w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/semantickernel3-300x270.jpg 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"169\">Once the documents added to our memory have their embeddings, let\u2019s set up our search index and ensure we can generate embeddings for our queries.<\/p>\n<h2 id=\"create-a-vector-search-index-in-mongodb\" class=\"code-line\" dir=\"auto\" data-line=\"172\">Create a vector search index in MongoDB<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"173\">In order to use the\u00a0<code>$vectorSearch<\/code>\u00a0operator on our data, we need to set up an appropriate search index. We\u2019ll do this in the\u00a0<a href=\"https:\/\/account.mongodb.com\/account\/login\" data-href=\"https:\/\/account.mongodb.com\/account\/login\">Atlas UI<\/a>. Select the \u201cSearch&#8221; tab on your cluster and click \u201cCreate Search Index\u201d.<\/p>\n<p dir=\"auto\" data-line=\"173\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture4.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1528\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture4.png\" alt=\"where to create a vector search index\" width=\"468\" height=\"177\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture4.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture4-300x113.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"178\">We want to choose the &#8220;JSON Editor Option&#8221; and click &#8220;Next&#8221;.<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture5.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1529\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture5.png\" alt=\"specifics for creating a search index\" width=\"468\" height=\"235\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture5.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture5-300x151.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"183\">On this page, we&#8217;re going to select our target database,\u00a0<code>semantic-kernel<\/code>, and collection,\u00a0<code>randomFacts<\/code>.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"185\">For this tutorial, we are naming our index\u00a0<code>defaultRandomFacts<\/code>. The index will look like this:<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n    \"mappings\": {\r\n        \"dynamic\": true,\r\n        \"fields\": {\r\n            \"embedding\": {\r\n                \"dimensions\": 1536,\r\n                \"similarity\": \"dotProduct\",\r\n                \"type\": \"knnVector\"\r\n            }\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"203\">The fields specify the embedding field name in our documents,\u00a0<code>embedding<\/code>, the dimensions of the model used to embed,\u00a0<code>1536<\/code>, and the similarity function to use to find K-nearest neighbors,\u00a0<code>dotProduct<\/code>. It&#8217;s very important that the dimensions in the index match that of the model used for embedding. This data has been embedded using the same model as the one we&#8217;ll be using, but other models are available and may use different dimensions.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"205\">Check out our\u00a0<a href=\"https:\/\/www.mongodb.com\/docs\/atlas\/atlas-search\/vector-search\/\" data-href=\"https:\/\/www.mongodb.com\/docs\/atlas\/atlas-search\/vector-search\/\">Vector Search documentation<\/a>\u00a0for more information on the index configuration settings.<\/p>\n<h2 id=\"query-documents-using-microsoft-semantic-kernel\" class=\"code-line\" dir=\"auto\" data-line=\"209\">Query documents using Microsoft Semantic Kernel<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"210\">In order to query your new documents hosted in your MongoDB cluster \u201cmemory\u201d store, we can use the\u00a0<code>memory.search_async<\/code>\u00a0function. Run the following commands and watch the magic happen:<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">result = await kernel.memory.search_async(MONGODB_COLLECTION, 'What is my job title?')\r\n\r\nprint(f\"Retrieved document: {result[0].text}, {result[0].relevance}\")<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\" data-line=\"218\">Now you can ask any question and get an accurate response!<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"220\">Examples of questions asked and the results:<\/p>\n<p dir=\"auto\" data-line=\"220\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture6.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1530\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture6.png\" alt=\"the result of the question: What is my job title?\" width=\"468\" height=\"66\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture6.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture6-300x42.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"227\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture7.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1531\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture7.png\" alt=\"the result of the question: Do I like caffeine?\" width=\"468\" height=\"67\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture7.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture7-300x43.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"227\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture8.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1532\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture8.png\" alt=\"Where in the world have I visited?\" width=\"468\" height=\"57\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture8.png 468w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/11\/Picture8-300x37.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<h2 id=\"conclusion\" class=\"code-line\" dir=\"auto\" data-line=\"230\">Conclusion<\/h2>\n<p class=\"code-line\" dir=\"auto\" data-line=\"231\">In this tutorial, you have learned a lot of very useful concepts:<\/p>\n<ul class=\"code-line\" dir=\"auto\" data-line=\"232\">\n<li class=\"code-line\" dir=\"auto\" data-line=\"232\">What Microsoft Semantic Kernel is and why it\u2019s important.<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"233\">How to connect Microsoft Semantic Kernel to a MongoDB Atlas cluster.<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"234\">How to add in documents to your MongoDB memory store (and embed them, in the process, through Microsoft Semantic Kernel).<\/li>\n<li class=\"code-line\" dir=\"auto\" data-line=\"235\">How to query your new documents in your memory store using Microsoft Semantic Kernel.<\/li>\n<\/ul>\n<p class=\"code-line\" dir=\"auto\" data-line=\"237\">For more information on MongoDB Vector Search, please visit the\u00a0<a href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\" data-href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\">documentation<\/a>, and for more information on Microsoft Semantic Kernel, please visit their\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\" data-href=\"https:\/\/github.com\/microsoft\/semantic-kernel\">repository<\/a>\u00a0and\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/overview\/\" data-href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/overview\/\">resources<\/a>.<\/p>\n<p class=\"code-line\" dir=\"auto\" data-line=\"239\">If you have any questions, please visit our\u00a0<a href=\"https:\/\/www.mongodb.com\/community\/forums\/\" data-href=\"https:\/\/www.mongodb.com\/community\/forums\/\">MongoDB Developer Community Forum<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>As part of this guest blogpost by the MongoDB Atlas Vector Search team, we are excited to announce native support for MongoDB Atlas Vector Search\u00a0in\u00a0Microsoft Semantic Kernel. With this integration, users can bring the power of LLMs (large language models) to their proprietary data securely, and build generative AI applications using RAG (retrieval-augmented generation) with [&hellip;]<\/p>\n","protected":false},"author":135264,"featured_media":5137,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1484","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-semantic-kernel"],"acf":[],"blog_post_summary":"<p>As part of this guest blogpost by the MongoDB Atlas Vector Search team, we are excited to announce native support for MongoDB Atlas Vector Search\u00a0in\u00a0Microsoft Semantic Kernel. With this integration, users can bring the power of LLMs (large language models) to their proprietary data securely, and build generative AI applications using RAG (retrieval-augmented generation) with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/135264"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=1484"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1484\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/5137"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=1484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=1484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=1484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}