{"id":2833,"date":"2024-06-12T11:19:24","date_gmt":"2024-06-12T18:19:24","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=2833"},"modified":"2024-06-12T11:19:24","modified_gmt":"2024-06-12T18:19:24","slug":"azure-openai-on-your-data-with-semantic-kernel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/azure-openai-on-your-data-with-semantic-kernel\/","title":{"rendered":"Azure OpenAI On Your Data with Semantic Kernel"},"content":{"rendered":"<p>Hello, everyone!<\/p>\n<p>In the fast-paced world of technology, using artificial intelligence to get more out of your data is becoming essential. <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/openai\/concepts\/use-your-data\">Azure OpenAI on Your Data<\/a> is a feature that helps businesses do just that. It combines the advanced AI models from OpenAI with the strong data management tools of Azure, allowing you to use AI with your own data to gain valuable insights and improve your operations.<\/p>\n<p>This article shows how to configure Azure OpenAI On Your Data and use it in the code with Semantic Kernel. What makes it powerful is the ability to combine your data, AI and other Semantic Kernel Plugins to achieve your business scenarios.<\/p>\n<h2><strong>Prerequisites<\/strong><\/h2>\n<p>To execute our code and get information about our data with AI, three services will be required:<\/p>\n<ol>\n<li>Azure OpenAI service &#8211; to be able to talk to AI. Follow this link to create Azure OpenAI service: <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/openai\/chatgpt-quickstart\">Quickstart: Get started using GPT-35-Turbo and GPT-4 with Azure OpenAI Service<\/a>.<\/li>\n<li>Azure AI Search service &#8211; to index our data and make it searchable. Information on how to create Azure AI Search service can be found here: <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/search\/search-create-service-portal\">Create an Azure AI Search service in the portal<\/a>.<\/li>\n<li>Azure Blob storage &#8211; to hold our data files. This service is optional for this functionality, but we will use it as an example how to upload, vectorize and index our files on Azure for testing purposes. This link provides information on how to create Azure Storage account and Azure Blob storage: <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/storage\/blobs\/storage-quickstart-blobs-portal\">Quickstart: Upload, download, and list blobs with the Azure portal<\/a>.<\/li>\n<\/ol>\n<p>Our goal is to combine these services together using following algorithm:<\/p>\n<ol>\n<li>Upload our data files to Azure Blob storage.<\/li>\n<li>Vectorize and index data in Azure AI Search.<\/li>\n<li>Connect Azure OpenAI service with Azure AI Search.<\/li>\n<\/ol>\n<p>Each step can be achieved separately. For simplicity, we will use <em>Add your data<\/em> view in Azure OpenAI Studio, which allows to perform these steps in one place. More information can be found here: <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/openai\/use-your-data-quickstart\">Quickstart: Chat with Azure OpenAI models using your own data<\/a>.<\/p>\n<p>If you already have your data in Azure AI Search, you can proceed to <a href=\"#accessing-your-data-with-sk\">Accessing your data with Semantic Kernel<\/a> section of this article.<\/p>\n<h2><strong>Upload data to Azure AI Search<\/strong><\/h2>\n<p>Open <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/openai\/chatgpt-quickstart#playground\">Azure OpenAI Studio Chat Playground<\/a>, select <em>Add your data<\/em> tab and press <em>Add a data source<\/em> button:\n<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/chat_playground.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2840\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/chat_playground.png\" alt=\"Chat playground in Azure OpenAI Studio\" width=\"1243\" height=\"720\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/chat_playground.png 1243w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/chat_playground-300x174.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/chat_playground-1024x593.png 1024w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/chat_playground-768x445.png 768w\" sizes=\"(max-width: 1243px) 100vw, 1243px\" \/><\/a><\/p>\n<p>In the next window, select Subscription, Azure Blob storage, Azure AI Search service. Specify your index name and make sure that <em>Add vector search to this search resource<\/em> is enabled. After enabling this option, new dropdown will appear to select embedding model. This is required to convert our test files to vectors and store them in Azure AI Search:\n<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/add_data_view.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2843\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/add_data_view.png\" alt=\"Add data view\" width=\"896\" height=\"744\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/add_data_view.png 896w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/add_data_view-300x249.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/add_data_view-768x638.png 768w\" sizes=\"(max-width: 896px) 100vw, 896px\" \/><\/a><\/p>\n<p>After clicking <em>Next<\/em> button, it will redirect us to the next step to upload our files. For testing purposes, we will use <a href=\"https:\/\/github.com\/Azure-Samples\/azure-search-sample-data\/blob\/main\/health-plan\/Benefit_Options.pdf\">PDF file from Azure AI Search Sample Data repository<\/a> with specific content, which AI shouldn&#8217;t know about by default:<\/p>\n<pre class=\"prettyprint\">Welcome to Contoso Electronics! We are excited to offer our employees two comprehensive health \r\ninsurance plans through Northwind Health. \r\n\r\nNorthwind Health Plus \r\nNorthwind Health Plus is a comprehensive plan that provides comprehensive coverage for medical, vision, and dental services. This plan also offers prescription drug coverage, mental health and substance abuse coverage, and coverage for preventive care services.\r\n\r\nNorthwind Standard \r\nNorthwind Standard is a basic plan that provides coverage for medical, vision, and dental services. This plan also offers coverage for preventive care services, as well as prescription drug coverage.<\/pre>\n<p>Let&#8217;s upload this PDF file by clicking <em>Upload files<\/em> button. It will be added to specified Azure Blob storage:\n<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/upload_file-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2871\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/upload_file-1.png\" alt=\"Upload PDF file\" width=\"999\" height=\"824\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/upload_file-1.png 999w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/upload_file-1-300x247.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/upload_file-1-768x633.png 768w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/a><\/p>\n<p>Next window will ask us which Search type to enable. For this example, we are interested in <em>Vector<\/em> search type (there is also Hybrid search type available, which combines vector and full text search):\n<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/data_management.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2846\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/data_management.png\" alt=\"Data management view\" width=\"896\" height=\"743\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/data_management.png 896w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/data_management-300x249.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/data_management-768x637.png 768w\" sizes=\"(max-width: 896px) 100vw, 896px\" \/><\/a><\/p>\n<p>On the next view we will be asked to provide authentication type. You can use <em>System assigned managed identity<\/em> if your Azure AI Search has RBAC authentication enabled. For this example, we will use <em>API key<\/em> authentication type:\n<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/data_connection.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2847\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/data_connection.png\" alt=\"Data connection view\" width=\"896\" height=\"741\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/data_connection.png 896w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/data_connection-300x248.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/data_connection-768x635.png 768w\" sizes=\"(max-width: 896px) 100vw, 896px\" \/><\/a><\/p>\n<p>The last step is to review our configuration and proceed with data upload, which will take a couple of minutes. In order to be sure that Azure AI Search index was created, we can go to Azure AI Search resource on Azure portal, open <em>Search management &gt; Indexes<\/em> page and verify if our index exists. In index, we can check if our data is searchable by using <em>Search explorer<\/em>:\n<a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/azure_ai_search_explorer.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2859\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2024\/06\/azure_ai_search_explorer.png\" alt=\"Azure AI Search explorer\" width=\"913\" height=\"324\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/azure_ai_search_explorer.png 913w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/azure_ai_search_explorer-300x106.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2024\/06\/azure_ai_search_explorer-768x273.png 768w\" sizes=\"(max-width: 913px) 100vw, 913px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"accessing-your-data-with-sk\"><strong>Accessing your data with Semantic Kernel<\/strong><\/h2>\n<p>Now, we want to use AI and ask some questions about our data. Let&#8217;s create new console application and add Semantic Kernel:<\/p>\n<pre class=\"prettyprint\">dotnet new console -n azure-semantic-kernel-quickstart\r\ndotnet add package Microsoft.SemanticKernel<\/pre>\n<p>We will also need following environment variables configured, so we can use it in our application. You can find Azure OpenAI keys and endpoint information on <em>Resource Management &gt; Keys and Endpoint<\/em> page on Azure portal. For Azure AI Search, use following routes on Azure portal:<\/p>\n<ul>\n<li>Endpoint &#8211; can be found in <em>Overview &gt; Url<\/em> property.<\/li>\n<li>API key &#8211; can be found on <em>Settings &gt; Keys<\/em> page.<\/li>\n<li>Index name &#8211; can be found on <em>Search management &gt; Indexes<\/em> page.<\/li>\n<\/ul>\n<p>As soon as all information is collected, let&#8217;s configure environment variables:<\/p>\n<pre class=\"prettyprint\">export AZURE_OPENAI_ENDPOINT=REPLACE_WITH_YOUR_AOAI_ENDPOINT_VALUE_HERE\r\nexport AZURE_OPENAI_API_KEY=REPLACE_WITH_YOUR_AOAI_KEY_VALUE_HERE\r\nexport AZURE_OPENAI_DEPLOYMENT_NAME=REPLACE_WITH_YOUR_AOAI_DEPLOYMENT_VALUE_HERE\r\nexport AZURE_AI_SEARCH_ENDPOINT=REPLACE_WITH_YOUR_AZURE_SEARCH_ENDPOINT_VALUE_HERE\r\nexport AZURE_AI_SEARCH_API_KEY=REPLACE_WITH_YOUR_AZURE_SEARCH_API_KEY_VALUE_HERE\r\nexport AZURE_AI_SEARCH_INDEX=REPLACE_WITH_YOUR_INDEX_NAME_HERE<\/pre>\n<p>In console application, create new <code class=\"language-cs language-csharp\">Kernel<\/code> and add Azure OpenAI chat completion service:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">var kernel = Kernel.CreateBuilder()\r\n    .AddAzureOpenAIChatCompletion(\r\n        deploymentName: Environment.GetEnvironmentVariable(\"AZURE_OPENAI_DEPLOYMENT_NAME\"),\r\n        endpoint: Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\"),\r\n        apiKey: Environment.GetEnvironmentVariable(\"AZURE_OPENAI_API_KEY\"))\r\n    .Build();\r\n<\/code><\/pre>\n<p>As first step, let&#8217;s try to ask a question, which is specific to our data:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">var result = await kernel.InvokePromptAsync(\"What are my available health plans?\");\r\n<\/code><\/pre>\n<p>The result will be similar to this:<\/p>\n<pre class=\"prettyprint\">I'm an AI language model, and I don't have access to your personal information or healthcare options.<\/pre>\n<p>The reason why there is no information about available healthcare options in response is because we didn&#8217;t point our <code class=\"language-cs language-csharp\">Kernel<\/code> to Azure AI Search service yet.<\/p>\n<p>In order to do that, we need to create an instance of chat extension configuration with Azure AI Search Endpoint, API key and index name, and pass this configuration to <code class=\"language-cs language-csharp\">InvokePromptAsync<\/code> method:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">var azureSearchExtensionConfiguration = new AzureSearchChatExtensionConfiguration\r\n{\r\n    SearchEndpoint = new Uri(Environment.GetEnvironmentVariable(\"AZURE_AI_SEARCH_ENDPOINT\")),\r\n    Authentication = new OnYourDataApiKeyAuthenticationOptions(Environment.GetEnvironmentVariable(\"AZURE_AI_SEARCH_API_KEY\")),\r\n    IndexName = Environment.GetEnvironmentVariable(\"AZURE_AI_SEARCH_INDEX\")\r\n};\r\n\r\nvar chatExtensionsOptions = new AzureChatExtensionsOptions { Extensions = { azureSearchExtensionConfiguration } };\r\nvar executionSettings = new OpenAIPromptExecutionSettings { AzureChatExtensionsOptions = chatExtensionsOptions };\r\n\r\nvar result = await kernel.InvokePromptAsync(\"What are my available health plans?\", new(executionSettings));\r\n<\/code><\/pre>\n<p>Now, when we invoke our prompt with execution settings, the result will look like this:<\/p>\n<pre class=\"prettyprint\">You have two available health plans through Contoso Electronics: Northwind Health Plus and Northwind Standard [doc1].\r\n\r\nNorthwind Health Plus is a comprehensive plan that provides coverage for medical, vision, and dental services...\r\n\r\nNorthwind Standard is a basic plan that provides coverage for medical, vision, and dental services, as well as preventive care services and prescription drug coverage...<\/pre>\n<p>Notice this <code class=\"language-cs language-csharp\">[doc1]<\/code> ending. This is a sign that the information was pulled from concrete document from Azure AI Search and our functionality works correctly.<\/p>\n<h2><strong>Summary<\/strong><\/h2>\n<p>Provided example shows how to configure and use Azure OpenAI On Your Data functionality. Together with Semantic Kernel, it is possible to combine it with Plugins and create powerful workflows, which will execute your business logic using your data and AI.<\/p>\n<p>We\u2019re always interested in hearing from you. If you have feedback, questions or want to discuss further, feel free to reach out to us and the community on the<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/discussions\">\u00a0discussion boards<\/a> on GitHub! We would also love your support, if you&#8217;ve enjoyed using Semantic Kernel, give us a star on <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone! In the fast-paced world of technology, using artificial intelligence to get more out of your data is becoming essential. Azure OpenAI on Your Data is a feature that helps businesses do just that. It combines the advanced AI models from OpenAI with the strong data management tools of Azure, allowing you to use [&hellip;]<\/p>\n","protected":false},"author":156732,"featured_media":2365,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[78,46,1],"tags":[48,63,9],"class_list":["post-2833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-learning","category-semantic-kernel","tag-ai","tag-microsoft-semantic-kernel","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>Hello, everyone! In the fast-paced world of technology, using artificial intelligence to get more out of your data is becoming essential. Azure OpenAI on Your Data is a feature that helps businesses do just that. It combines the advanced AI models from OpenAI with the strong data management tools of Azure, allowing you to use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/2833","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\/156732"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=2833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/2833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/2365"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=2833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=2833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=2833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}