Skip to content

Commit 3baee0e

Browse files
979100: conversation of open ai to azure open ai
1 parent 668bef4 commit 3baee0e

File tree

12 files changed

+154
-135
lines changed

12 files changed

+154
-135
lines changed

ej2-asp-core-mvc/ai-assistview/EJ2_ASP.MVC/ai-integrations/gemini-integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The Syncfusion AI AssistView supports integration with [Gemini](https://ai.googl
1414

1515
## Getting Started With the AI AssistView control
1616

17-
Before integrating Gemini AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your Vue application:
17+
Before integrating Gemini AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your MVC application:
1818

1919
[ MVC Getting Started Guide](../getting-started)
2020

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
---
22
layout: post
3-
title: Open AI in ##Platform_Name## AI AssistView Control | Syncfusion
4-
description: Checkout and learn about Integration of Open AI in Syncfusion ##Platform_Name## AI AssistView control of Syncfusion Essential JS 2 and more.
3+
title: Azure Open AI in ##Platform_Name## AI AssistView Control | Syncfusion
4+
description: Checkout and learn about Integration of Azure Open AI in Syncfusion ##Platform_Name## AI AssistView control of Syncfusion Essential JS 2 and more.
55
platform: ej2-asp-core-mvc
6-
control: Open AI
6+
control: Azure Open AI
77
publishingplatform: ##Platform_Name##
88
documentation: ug
99
---
1010

11-
# Integration of Open AI With AI AssistView control
11+
# Integration of Azure Open AI With AI AssistView control
1212

13-
The Syncfusion AI AssistView supports integration with [OpenAI](https://platform.openai.com/docs/overview), enabling advanced conversational AI features in your MVC applications.
13+
The Syncfusion AI AssistView supports integration with [Azure Open AI](https://microsoft.github.io/PartnerResources/skilling/ai-ml-academy/resources/openai), enabling advanced conversational AI features in your Angular applications.
1414

1515
## Getting Started With the AI AssistView control
1616

17-
Before integrating Open AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your Vue application:
17+
Before integrating Azure Open AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your MVC application:
1818

1919
[ MVC Getting Started Guide](../getting-started)
2020

2121
## Prerequisites
2222

23-
* OpenAI account to generate an API key for accessing the `OpenAI` API
23+
* An Azure account with access to `Azure Open AI` services and a generated API key.
2424
* [System requirements for ASP.NET MVC controls](https://ej2.syncfusion.com/aspnetmvc/documentation/system-requirements) to create MVC application
2525

2626
## Install Packages
@@ -33,35 +33,39 @@ NuGet\Install-Package Syncfusion.EJ2.MVC5
3333

3434
```
3535

36-
Install the Open AI package in the application using Package Manager Console.
36+
Install the Open AI and Azure Open AI package in the application using Package Manager Console.
3737

3838
```bash
3939

4040
NuGet\Install-Package OpenAI
41-
41+
NuGet\Install-Package Azure.AI.OpenAI
42+
NuGet\Install-Package Azure.Core
43+
4244
```
4345

44-
## Generate API Key
46+
## Configure Azure Open AI
4547

46-
1. Go to [Open AI](https://platform.openai.com/docs/overview) and sign in with your Google account. If you don’t have one, create a new account.
48+
1. Log in to the [Azure Portal](https://portal.azure.com/#home) and navigate to your Azure Open AI resource.
4749

48-
2. Once logged in, click on your profile icon in the top-right corner and select `API Keys` from the dropdown menu.
50+
2. Under Resource Management, select Keys and Endpoint to retrieve your API key and endpoint URL.
4951

50-
3. Click the `+ Create new secret key` button. You’ll be prompted to name the key (optional). Confirm to generate the key.
52+
3. Copy the API key, endpoint, and deployment name (e.g., gpt-4o-mini). Ensure the API version (e.g., 2024-07-01-preview) matches your resource configuration.
5153

52-
4. Your API key will be displayed once. Copy it and store it securely, as it won’t be shown again.
54+
4. Store these values securely, as they will be used in your application.
5355

54-
> `Security Note`: Never commit the API key to version control. Use environment variables or a secret manager for production.
56+
> `Security Note`: expose your API key in client-side code for production applications. Use a server-side proxy or environment variables to manage sensitive information securely.
5557
56-
## Integration Open AI with AI AssistView
58+
## Integration Azure Open AI with AI AssistView
5759

5860
You can add the below respective files in your application:
5961

60-
* Add your generated `API Key` at the line in .cs file
62+
* Update the following configuration values with your Azure Open AI details:
6163

6264
```bash
6365

64-
string apiKey = 'Place your API key here';
66+
string endpoint = "Your_Azure_OpenAI_Endpoint";
67+
string apiKey = "Your_Azure_OpenAI_API_Key";
68+
string deploymentName = "Your_Deployment_Name";
6569

6670
```
6771

@@ -74,12 +78,12 @@ string apiKey = 'Place your API key here';
7478
{% endhighlight %}
7579
{% endtabs %}
7680

77-
![Open AI](../../images/open-ai.png)
81+
![Azure Open AI](../../images/open-ai.png)
7882

7983
## Run and Test
8084

8185
Run the application in the browser using the following command.
8286

8387
Build and run the app (Ctrl + F5).
8488

85-
Open `https://localhost:44321` to interact with your Open AI for dynamic response.
89+
Open `https://localhost:44321` to interact with your Azure Open AI for dynamic response.

ej2-asp-core-mvc/ai-assistview/EJ2_ASP.NETCORE/ai-integrations/gemini-integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The Syncfusion AI AssistView supports integration with [Gemini](https://ai.googl
1414

1515
## Getting Started With the AI AssistView control
1616

17-
Before integrating Gemini AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your Vue application:
17+
Before integrating Gemini AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your Core application:
1818

1919
[ ASP.NET CORE Getting Started Guide](../getting-started)
2020

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
---
22
layout: post
3-
title: Open AI in ##Platform_Name## AI AssistView Control | Syncfusion
4-
description: Checkout and learn about Integration of Open AI in Syncfusion ##Platform_Name## AI AssistView control of Syncfusion Essential JS 2 and more.
3+
title: Azure Open AI in ##Platform_Name## AI AssistView Control | Syncfusion
4+
description: Checkout and learn about Integration of Azure Open AI in Syncfusion ##Platform_Name## AI AssistView control of Syncfusion Essential JS 2 and more.
55
platform: ej2-asp-core-mvc
6-
control: Open AI
6+
control: Azure Open AI
77
publishingplatform: ##Platform_Name##
88
documentation: ug
99
---
1010

11-
# Integration of Open AI With AI AssistView control
11+
# Integration of Azure Open AI With AI AssistView control
1212

13-
The Syncfusion AI AssistView supports integration with [OpenAI](https://platform.openai.com/docs/overview), enabling advanced conversational AI features in your Core applications.
13+
The Syncfusion AI AssistView supports integration with [Azure Open AI](https://microsoft.github.io/PartnerResources/skilling/ai-ml-academy/resources/openai), enabling advanced conversational AI features in your Angular applications.
1414

1515
## Getting Started With the AI AssistView control
1616

17-
Before integrating Open AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your Vue application:
17+
Before integrating Azure Open AI, ensure that the Syncfusion AI AssistView control is correctly rendered in your Core app:
1818

1919
[ CORE Getting Started Guide](../getting-started)
2020

2121
## Prerequisites
2222

23-
* OpenAI account to generate an API key for accessing the `OpenAI` API
23+
* An Azure account with access to `Azure Open AI` services and a generated API key.
2424
* [System requirements for ASP.NET MVC controls](https://ej2.syncfusion.com/aspnetmvc/documentation/system-requirements) to create Core application
2525

2626
## Install Packages
@@ -33,35 +33,39 @@ NuGet\Install-Package Syncfusion.EJ2.AspNet.Core
3333

3434
```
3535

36-
Install the Open AI package in the application using Package Manager Console.
36+
Install the Open AI and Azure Open AI package in the application using Package Manager Console.
3737

3838
```bash
3939

40-
NuGet\Install-Package
41-
40+
NuGet\Install-Package OpenAI
41+
NuGet\Install-Package Azure.AI.OpenAI
42+
NuGet\Install-Package Azure.Core
43+
4244
```
4345

44-
## Generate API Key
46+
## Configure Azure Open AI
4547

46-
1. Go to [Open AI](https://platform.openai.com/docs/overview) and sign in with your Google account. If you don’t have one, create a new account.
48+
1. Log in to the [Azure Portal](https://portal.azure.com/#home) and navigate to your Azure Open AI resource.
4749

48-
2. Once logged in, click on your profile icon in the top-right corner and select `API Keys` from the dropdown menu.
50+
2. Under Resource Management, select Keys and Endpoint to retrieve your API key and endpoint URL.
4951

50-
3. Click the `+ Create new secret key` button. You’ll be prompted to name the key (optional). Confirm to generate the key.
52+
3. Copy the API key, endpoint, and deployment name (e.g., gpt-4o-mini). Ensure the API version (e.g., 2024-07-01-preview) matches your resource configuration.
5153

52-
4. Your API key will be displayed once. Copy it and store it securely, as it won’t be shown again.
54+
4. Store these values securely, as they will be used in your application.
5355

54-
> `Security Note`: Never commit the API key to version control. Use environment variables or a secret manager for production.
56+
> `Security Note`: Never expose your API key in client-side code for production applications. Use a server-side proxy or environment variables to manage sensitive information securely.
5557
56-
## Integration Open AI with AI AssistView
58+
## Integration Azure Open AI with AI AssistView
5759

5860
You can add the below respective files in your application:
5961

60-
* Add your generated `API Key` at the line in .cs file
62+
* Update the following configuration values with your Azure Open AI details:
6163

6264
```bash
6365

64-
string apiKey = 'Place your API key here';
66+
string endpoint = "Your_Azure_OpenAI_Endpoint";
67+
string apiKey = "Your_Azure_OpenAI_API_Key";
68+
string deploymentName = "Your_Deployment_Name";
6569

6670
```
6771

@@ -74,12 +78,12 @@ string apiKey = 'Place your API key here';
7478
{% endhighlight %}
7579
{% endtabs %}
7680

77-
![Open AI](../../images/open-ai.png)
81+
![Azure Open AI](../../images/open-ai.png)
7882

7983
## Run and Test
8084

8185
Run the application in the browser using the following command.
8286

8387
Build and run the app (Ctrl + F5).
8488

85-
Open `https://localhost:44321` to interact with your Open AI for dynamic response.
89+
Open `https://localhost:44321` to interact with your Azure Open AI for dynamic response.

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/gemini-ai/geminicore.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,4 @@ public class ToolbarItemModel
7676
public string align { get; set; }
7777
public string iconCss { get; set; }
7878
}
79-
}
80-
}
79+
}

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/gemini-ai/geminimvc.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public async Task<IActionResult> GetAIResponse([FromBody] PromptRequest request)
4848

4949
if (string.IsNullOrEmpty(responseText?.Text))
5050
{
51-
_logger.LogError("OpenAI API returned no text.");
52-
return BadRequest("No response from OpenAI.");
51+
_logger.LogError("Gemini API returned no text.");
52+
return BadRequest("No response from Gemini.");
5353
}
5454

55-
_logger.LogInformation("OpenAI response received: {Response}", responseText?.Text);
55+
_logger.LogInformation("Gemini response received: {Response}", responseText?.Text);
5656
return Json(responseText?.Text);
5757
}
5858
catch (Exception ex)
5959
{
60-
_logger.LogError("Exception in OpenAI call: {Message}", ex.Message);
60+
_logger.LogError("Exception in Gemini call: {Message}", ex.Message);
6161
return BadRequest($"Error generating response: {ex.Message}");
6262
}
6363
}

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/open-ai/openaicore.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using OpenAI;
2-
2+
using Azure;
3+
using Azure.AI.OpenAI;
34
namespace WebApplication4.Pages
45
{
56
public class IndexModel : PageModel
@@ -38,25 +39,32 @@ public async Task<IActionResult> OnPostGetAIResponse([FromBody] PromptRequest re
3839
return BadRequest("Prompt cannot be empty.");
3940
}
4041

41-
string apiKey = ""; // Replace with your OpenAI API key
42-
var openAiClient = new OpenAIClient(apiKey);
43-
var chatClient = openAiClient.GetChatClient("gpt-4o-mini"); // Use your preferred model, e.g., "gpt-4o-mini" or "gpt-4o"
42+
string endpoint = ""; // Replace with your Azure OpenAI endpoint
43+
string apiKey = ""; // Replace with your Azure OpenAI API key
44+
string deploymentName = ""; // Replace with your Azure OpenAI deployment name (e.g., gpt-4o-mini)
4445

45-
ChatCompletion completion = await chatClient.CompleteChatAsync(request.Prompt);
46-
string responseText = completion.Content[0].Text;
46+
var credential = new AzureKeyCredential(apiKey);
47+
var client = new AzureOpenAIClient(new Uri(endpoint), credential);
48+
var chatClient = client.GetChatClient(deploymentName);
4749

50+
var chatCompletionOptions = new ChatCompletionOptions();
51+
var completion = await chatClient.CompleteChatAsync(
52+
new[] { new UserChatMessage(request.Prompt) },
53+
chatCompletionOptions
54+
);
55+
string responseText = completion.Value.Content[0].Text;
4856
if (string.IsNullOrEmpty(responseText))
4957
{
50-
_logger.LogError("OpenAI API returned no text.");
51-
return BadRequest("No response from OpenAI.");
58+
_logger.LogError("Azure OpenAI API returned no text.");
59+
return BadRequest("No response from Azure OpenAI.");
5260
}
5361

54-
_logger.LogInformation("OpenAI response received: {Response}", responseText);
62+
_logger.LogInformation("Azure OpenAI response received: {Response}", responseText);
5563
return new JsonResult(responseText);
5664
}
5765
catch (Exception ex)
5866
{
59-
_logger.LogError("Exception in Gemini call: {Message}", ex.Message);
67+
_logger.LogError("Exception in Azure OpenAI call: {Message}", ex.Message);
6068
return BadRequest($"Error generating response: {ex.Message}");
6169
}
6270
}
@@ -78,5 +86,4 @@ public class ToolbarItemModel
7886
public string align { get; set; }
7987
public string iconCss { get; set; }
8088
}
81-
}
82-
}
89+
}

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/open-ai/openaimvc.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using OpenAI;
2+
using OpenAI.Chat;
3+
using Azure;
4+
using Azure.AI.OpenAI;
25

36
namespace AssistViewDemo.Controllers
47
{
@@ -37,25 +40,34 @@ public async Task<IActionResult> GetAIResponse([FromBody] PromptRequest request)
3740
return BadRequest("Prompt cannot be empty.");
3841
}
3942

40-
string apiKey = ""; // Replace with your OpenAI API key
41-
var openAiClient = new OpenAIClient(apiKey);
42-
var chatClient = openAiClient.GetChatClient("gpt-4o-mini"); // Use your preferred model, e.g., "gpt-4o-mini" or "gpt-4o"
43+
// Azure OpenAI configuration
44+
string endpoint = ""; // Replace with your Azure OpenAI endpoint
45+
string apiKey = ""; // Replace with your Azure OpenAI API key
46+
string deploymentName = ""; // Replace with your Azure OpenAI deployment name (e.g., gpt-4o-mini)
4347

44-
OpenAI.Chat.ChatCompletion completion = await chatClient.CompleteChatAsync(request.Prompt);
45-
string responseText = completion.Content[0].Text;
48+
var credential = new AzureKeyCredential(apiKey);
49+
var client = new AzureOpenAIClient(new Uri(endpoint), credential);
50+
var chatClient = client.GetChatClient(deploymentName);
4651

52+
var chatCompletionOptions = new ChatCompletionOptions();
53+
var completion = await chatClient.CompleteChatAsync(
54+
new[] { new UserChatMessage(request.Prompt) },
55+
chatCompletionOptions
56+
);
57+
58+
string responseText = completion.Value.Content[0].Text;
4759
if (string.IsNullOrEmpty(responseText))
4860
{
49-
_logger.LogError("OpenAI API returned no text.");
50-
return BadRequest("No response from OpenAI.");
61+
_logger.LogError("Azure OpenAI API returned no text.");
62+
return BadRequest("No response from Azure OpenAI.");
5163
}
5264

53-
_logger.LogInformation("OpenAI response received: {Response}", responseText);
65+
_logger.LogInformation("Azure OpenAI response received: {Response}", responseText);
5466
return Json(responseText);
5567
}
5668
catch (Exception ex)
5769
{
58-
_logger.LogError("Exception in OpenAI call: {Message}", ex.Message);
70+
_logger.LogError("Exception in Azure OpenAI call: {Message}", ex.Message);
5971
return BadRequest($"Error generating response: {ex.Message}");
6072
}
6173
}

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/open-ai/razor

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,13 @@
5454
}
5555

5656
function onPromptRequest(args) {
57-
if (!token) {
58-
assistObj.addPromptResponse('⚠️ Antiforgery token not found.');
59-
return;
60-
}
6157

6258
fetch('/?handler=GetAIResponse', {
6359
method: 'POST',
6460
headers: {
65-
'Content-Type': 'application/json',
66-
'RequestVerificationToken': token
61+
'Content-Type': 'application/json'
6762
},
68-
body: JSON.stringify({ prompt: args.prompt || 'Hi' })
63+
body: JSON.stringify({ prompt: args.prompt })
6964
})
7065
.then(response => {
7166
if (!response.ok) {

0 commit comments

Comments
 (0)