{"id":40612,"date":"2025-03-13T12:21:55","date_gmt":"2025-03-13T06:51:55","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=40612"},"modified":"2025-05-06T14:06:42","modified_gmt":"2025-05-06T08:36:42","slug":"integrating-azure-openai-models-in-your-projects-a-comprehensive-guide","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2025\/03\/integrating-azure-openai-models-in-your-projects-a-comprehensive-guide\/","title":{"rendered":"Integrating Azure OpenAI models in your Projects: A Comprehensive Guide"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignnone size-full wp-image-40633\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models.png\" alt=\"Integrating Azure OpenAI models\" width=\"1925\" height=\"1100\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models.png 1925w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models-300x171.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models-1024x585.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models-768x439.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models-1536x878.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/Integrating-Azure-OpenAI-models-660x377.png 660w\" sizes=\"(max-width: 1925px) 100vw, 1925px\" \/><\/p>\n<p>In the previous <a href=\"https:\/\/www.inogic.com\/blog\/2024\/12\/empower-your-projects-with-ai-a-comprehensive-guide-to-azure-openai-service\/\">blog<\/a>, we have explored how to install and configure Azure OpenAI Service, now we will be unlocking its potential further by integrating the appropriate model in your projects by programmatically in Microsoft Dynamics 365 CRM.<\/p>\n<p>Imagine a world where customer service agents don\u2019t have to spend time searching through endless records to find the information they need. Thanks to Azure AI and its integration with Microsoft Dynamics CRM, this is now a reality. Instead of manually digging through customer data, agents can simply ask questions to AI assistant, like \u201cWhat\u2019s the status of this customer\u2019s last order?\u201d or \u201cHas this customer raised any previous support tickets?\u201d and get instant, accurate answers.<\/p>\n<p>This seamless, AI-powered interaction transforms the customer service experience, enabling agents to respond quickly and provide personalized support without the hassle of searching for details. With natural language processing and real-time insights, Azure AI makes it easier than ever for agents to focus on what really matters\u2014solving problems and delighting customers.<\/p>\n<h2><strong>Prerequisites<\/strong><\/h2>\n<p>Before getting started, ensure you have:<\/p>\n<ul>\n<li>An active <a href=\"https:\/\/azure.microsoft.com\/en-us\/free\/\" target=\"_blank\" rel=\"noopener\">Azure subscription<\/a>.<\/li>\n<\/ul>\n<h2><strong>Configuration<\/strong><\/h2>\n<h3><strong>1. Creating a Service &amp; Deploying a model:<\/strong><\/h3>\n<p>You can follow the steps in the following article, if you are new to this concept and want to learn how to <a href=\"https:\/\/www.inogic.com\/blog\/2024\/12\/empower-your-projects-with-ai-a-comprehensive-guide-to-azure-openai-service\/\">create an Azure OpenAI service &amp; deploy an OpenAI model<\/a>.<\/p>\n<h3><strong>2. Access the API Key &amp; Url :<\/strong><\/h3>\n<ul>\n<li>Once you are done with deploying the appropriate Model(as per your requirement), navigate to the Azure Portal<\/li>\n<li>Navigate to the resource section and click on Azure AI Foundry.<\/li>\n<li>Then go to Deployments tab which is located in the left side pane and select the deployed model.<\/li>\n<li>After clicking on the deployed model, you will find your Key and Target Uri present in the Endpoint tab<\/li>\n<li>Retrieve this Key and Target Uri as they are the contact points which will be further used in your code to access the Azure OpenAI service.<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-40613\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/1Azure-OpenAI-models.png\" alt=\"Azure OpenAI models\" width=\"1376\" height=\"685\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/1Azure-OpenAI-models.png 1376w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/1Azure-OpenAI-models-300x149.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/1Azure-OpenAI-models-1024x510.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/1Azure-OpenAI-models-768x382.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/1Azure-OpenAI-models-660x329.png 660w\" sizes=\"(max-width: 1376px) 100vw, 1376px\" \/><\/p>\n<h3><strong>3. JavaScript Code for GPT-4o mini Integration: <\/strong><\/h3>\n<p>Here\u2019s the JavaScript code that will interact with the Azure OpenAI API using jQuery for AJAX requests. The code allows you to retrieve account data dynamically from the CRM, send it along with user queries to GPT-4o Mini, and display the response in a chatbot UI.<\/p>\n<pre class=\"lang:css gutter:true start:1\">\/\/ Define the Azure OpenAI API endpoint and API key\r\n\r\nvar apiKey = \"YOUR_API_KEY\"; \/\/ Replace with your Azure OpenAI API key\r\n\r\nvar endpoint = \"YOUR_ENDPOINT_URL\"; \/\/ Replace with your Azure OpenAI API \u00a0endpoint\r\n\r\nvar model = \"gpt-4o-mini\"; \/\/Replace with the appropriate model\r\n\r\n\/\/ System message to guide the assistant's behaviour\r\n\r\nvar systemMessage = {\r\n\r\nrole: 'system',\r\n\r\ncontent: \"The intelligent assistant is designed to interpret user prompts and deliver \r\ntailored responses for general queries. For general inquiries, such as informational or exploratory questions, the assistant utilizes its internal knowledge base to provide accurate, fact-based answers derived from historical data.\"\r\n\r\n};\r\n\r\n\/\/ Function to get the current Account ID dynamically from the CRM form\r\n\r\nfunction getAccountId() {\r\n\r\nvar formContext = parent.Xrm.Page || window.formContext;\r\n\r\nvar accountId = formContext.data.entity.getId();\r\n\r\nreturn accountId ? accountId.replace(\/[{}]\/g, '') : null;\r\n\r\n}\r\n\r\n\/\/ Function to retrieve account data using jQuery AJAX\r\n\r\nfunction retrieveAccountData(accountId) {\r\n\r\nvar serverUrl = parent.Xrm.Utility.getGlobalContext().getClientUrl();\r\n\r\nvar query = `${serverUrl}\/api\/data\/v9.2\/accounts?$filter=accountid eq '${accountId}'`;\r\n\r\nreturn $.ajax({\r\n\r\nurl: query,\r\n\r\nmethod: \"GET\",\r\n\r\nheaders: { \"Accept\": \"application\/json\" },\r\n\r\ndataType: \"json\"\r\n\r\n}).then(response =&gt; response, error =&gt; {\r\n\r\nconsole.error('Error retrieving account data:', error);\r\n\r\nreturn null;\r\n\r\n});\r\n\r\n}\r\n\r\n\/\/ Function to send request to OpenAI API using jQuery AJAX\r\n\r\nfunction getGPTResponse(query, accountData) {\r\n\r\nvar requestBody = {\r\n\r\nmodel: model,\r\n\r\nmessages: [\r\n\r\nsystemMessage,\r\n\r\n{ role: \"user\", content: `Here is some information about the account: ${JSON.stringify(accountData)}. ${query}` }\r\n\r\n]\r\n\r\n};\r\n\r\nreturn $.ajax({\r\n\r\nurl: endpoint,\r\n\r\nmethod: \"POST\",\r\n\r\nheaders: {\r\n\r\n\"Content-Type\": \"application\/json\",\r\n\r\n\"api-key\": apiKey\r\n\r\n},\r\n\r\ndata: JSON.stringify(requestBody),\r\n\r\ndataType: \"json\"\r\n\r\n}).then(response =&gt; response.choices[0].message.content, error =&gt; {\r\n\r\nconsole.log(\"Error calling OpenAI API:\", error);\r\n\r\nreturn \"Sorry, I couldn't process your request.\";\r\n\r\n});\r\n\r\n}\r\n\r\n\/\/ Function to send the user query and get a response\r\n\r\nfunction sendQuery() {\r\n\r\nvar userQuery = $('#user-input').val();\r\n\r\nif (!userQuery) return;\r\n\r\n\/\/ Display user query in chat box\r\n\r\nvar chatBox = $('#chat-box');\r\n\r\nchatBox.append(`&lt;div&gt;&lt;b&gt;You:&lt;\/b&gt; ${userQuery}&lt;\/div&gt;`);\r\n\r\n\/\/ Get Account ID dynamically\r\n\r\nvar accountId = getAccountId();\r\n\r\nif (!accountId) {\r\n\r\nalert(\"No account ID found.\");\r\n\r\nreturn;\r\n\r\n}\r\n\r\n\/\/ Retrieve account data, then send it to OpenAI API\r\n\r\nretrieveAccountData(accountId).then(accountData =&gt; {\r\n\r\nreturn getGPTResponse(userQuery, accountData);\r\n\r\n}).then(response =&gt; {\r\n\r\nchatBox.append(`&lt;div&gt;&lt;b&gt;Bot:&lt;\/b&gt; ${response}&lt;\/div&gt;`);\r\n\r\n$('#user-input').val('');\r\n\r\nchatBox.scrollTop(chatBox[0].scrollHeight);\r\n\r\n});\r\n\r\n}\r\n\r\n\/\/ Bind the send button click event\r\n\r\n$(document).ready(() =&gt; {\r\n\r\n$('#send-btn').on('click', sendQuery);\r\n\r\n});<\/pre>\n<p>In this code:<\/p>\n<ol>\n<li>Replace YOUR_API_KEY and YOUR_ENDPOINT_URL with your actual API key and endpoint.<\/li>\n<li>The getAccountId function retrieves the account Id dynamically from the CRM form.<\/li>\n<li>The retrieveAccountData function uses jQuery&#8217;s ajax to fetch account data from Dynamics 365 CRM via the Web API.<\/li>\n<li>The getGPTResponse function sends the user\u2019s query and account data to Azure OpenAI and displays the response in the chat UI.<\/li>\n<\/ol>\n<h3><strong>4. Integrate the agent in CRM dynamics 365.<\/strong><\/h3>\n<p>To upload and integrate a web resource in Dynamics 365 CRM, go to <strong>Settings &gt; Solutions<\/strong>, create or select a solution, paste the above code under <strong>Web Resources<\/strong>, save and publish it, then navigate to the form, insert the web resource, position it, and publish the form.<\/p>\n<h3><strong>5. Test the Chatbot (<\/strong>Once everything is set up, test the integration):<\/h3>\n<ol>\n<li>Go to Dynamics 365 CRM and redirect to form where the web resource is added.<\/li>\n<li>Type a query in the input field and click Send as shown below in screenshot.<\/li>\n<li>The chatbot should respond with information related to the current account, retrieved dynamically from Dynamics 365 CRM, enhanced with GPT-4o Mini\u2019s response.<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-40614\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models.png\" alt=\"Azure OpenAI models\" width=\"1622\" height=\"861\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models.png 1622w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models-300x159.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models-1024x544.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models-768x408.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models-1536x815.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/03\/2Azure-OpenAI-models-660x350.png 660w\" sizes=\"(max-width: 1622px) 100vw, 1622px\" \/><\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>Integrating Azure AI Services, specifically the GPT-4o Mini model, into your Dynamics 365 CRM using JavaScript and jQuery can provide a highly interactive and intelligent chatbot feature. By following the steps outlined in this blog, you can easily set up an AI-driven chatbot that leverages real-time data from Dynamics 365 CRM, helping improve user engagement and automate responses to common queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous blog, we have explored how to install and configure Azure OpenAI Service, now we will be unlocking its potential further by integrating the appropriate model in your projects by programmatically in Microsoft Dynamics 365 CRM. Imagine a world where customer service agents don\u2019t have to spend time searching through endless records to\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2025\/03\/integrating-azure-openai-models-in-your-projects-a-comprehensive-guide\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2843,2361],"tags":[3127],"class_list":["post-40612","post","type-post","status-publish","format-standard","hentry","category-azure-openai","category-technical","tag-azure-openai-models"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/40612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=40612"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/40612\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=40612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=40612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=40612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}