Skip to content

Commit 3190f68

Browse files
Merge pull request #6689 from syncfusion-content/983466-AI-Base
983466: Updated the AI Base UG Sections.
2 parents 178c4df + 85a0f91 commit 3190f68

File tree

5 files changed

+213
-190
lines changed

5 files changed

+213
-190
lines changed

blazor/smart-ai-solutions/ai/azure-openai.md

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: post
3-
title: Using Azure OpenAI with Syncfusion Blazor AI | Syncfusion
3+
title: Azure OpenAI Integration with Syncfusion Blazor AI | Syncfusion
44
description: Learn how to set up and use Syncfusion.Blazor.AI with Azure OpenAI for AI-powered features in your Blazor apps, including configuration and examples.
55
platform: Blazor
66
control: AI Integration
@@ -9,15 +9,12 @@ documentation: ug
99

1010
# Azure OpenAI Integration with Syncfusion® Blazor AI
1111

12-
## Introduction
13-
14-
This section demonstrates configuring and using the [Syncfusion.Blazor.AI](https://www.nuget.org/packages/Syncfusion.Blazor.AI) package with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource) to enable AI functionalities in Blazor applications. The package provides seamless integration with Azure OpenAI's API, empowering Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor components with intelligent features such as natural language querying, data analysis, and content processing.
12+
The [Syncfusion Blazor AI](https://www.nuget.org/packages/Syncfusion.Blazor.AI) library enables seamless integration with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource) to add AI-driven features to Blazor applications. These features include natural language querying (converting plain English queries to structured data filters), data analysis (processing and summarizing datasets), and content processing (generating or refining text).
1513

1614
## Prerequisites
1715

18-
Before integrating Azure OpenAI with your Blazor application, ensure you have:
19-
20-
* Installed the following NuGet packages:
16+
To integrate Azure OpenAI with a Blazor WebApp Server, ensure the following:
17+
- The following NuGet packages are installed:
2118
{% tabs %}
2219
{% highlight C# tabtitle="Package Manager" %}
2320

@@ -28,17 +25,15 @@ Install-Package Azure.AI.OpenAI
2825

2926
{% endhighlight %}
3027
{% endtabs %}
31-
* [Deployed an Azure OpenAI Service resource and model](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource) to obtain values for `apiKey`, `deploymentName` and `endpoint`
32-
* Met the [System Requirements](https://blazor.syncfusion.com/documentation/system-requirements) for Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor components
33-
* Configured secure credential storage using Azure Key Vault or environment variables
28+
- An [Azure OpenAI Service resource and model](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource) is deployed to obtain `apiKey`, `deploymentName`, and `endpoint` values.
29+
- The [Syncfusion Blazor system requirements](https://blazor.syncfusion.com/documentation/system-requirements) are met.
30+
- Credentials are stored securely using Azure Key Vault or environment variables (see [Microsoft’s guide on secure storage](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets)).
3431

3532
## Configuration Steps
3633

37-
Follow these steps to configure Azure OpenAI as your AI provider:
38-
3934
### Register AI Services in Program.cs
4035

41-
Open your Blazor application's `Program.cs` file and add the following configuration:
36+
To configure Azure OpenAI in a Blazor WebApp Server, update the `Program.cs` file as follows:
4237

4338
```csharp
4439
// Add required namespaces
@@ -48,9 +43,9 @@ using Microsoft.Extensions.AI;
4843
using System.ClientModel;
4944

5045
// Register Azure OpenAI credentials
51-
string azureOpenAiKey = "AZURE_OPENAI_KEY";
52-
string azureOpenAiEndpoint = "AZURE_OPENAI_ENDPOINT";
53-
string azureOpenAiModel = "AZURE_OPENAI_MODEL";
46+
string azureOpenAiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY"); // Use secure storage
47+
string azureOpenAiEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
48+
string azureOpenAiModel = "gpt-4"; // Specify a deployed Azure OpenAI model
5449
AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(
5550
new Uri(azureOpenAiEndpoint),
5651
new ApiKeyCredential(azureOpenAiKey)
@@ -64,10 +59,9 @@ builder.Services.AddSingleton<IChatInferenceService, SyncfusionAIService>();
6459

6560
## Natural Language Querying with Azure OpenAI
6661

67-
This example demonstrates using the **Syncfusion.Blazor.AI** package with Azure OpenAI to enable natural language querying in a Blazor application. The application features a Syncfusion<sup style="font-size:70%">&reg;</sup> Tab component with a textarea for entering natural language queries, a QueryBuilder component to visualize the generated query rules, and a Grid component to display filtered results based on the query processed by Azure OpenAI.
68-
69-
### Prerequisites
62+
This example demonstrates using the Syncfusion Blazor AI library with Azure OpenAI to enable natural language querying. The application uses a Syncfusion Tab component with a textarea for entering natural language queries, a QueryBuilder component to visualize generated query rules, and a Grid component to display filtered results based on the Azure OpenAI-processed query.
7063

64+
### Setup Prerequisites
7165
- Install the following NuGet packages:
7266
{% tabs %}
7367
{% highlight C# tabtitle="Package Manager" %}
@@ -82,31 +76,31 @@ Install-Package Azure.AI.OpenAI
8276

8377
{% endhighlight %}
8478
{% endtabs %}
85-
- Ensure your Blazor application meets the [System Requirements](https://blazor.syncfusion.com/documentation/system-requirements).
86-
- Add the following to `App.razor` for Syncfusion<sup style="font-size:70%">&reg;</sup> themes and scripts:
79+
- Ensure the Blazor application meets the [Syncfusion Blazor system requirements](https://blazor.syncfusion.com/documentation/system-requirements).
80+
- Add the following to `App.razor` for Syncfusion themes and scripts:
8781

8882
```html
8983
<head>
90-
....
84+
...
9185
<link href="_content/Syncfusion.Blazor.Themes/tailwind.css" rel="stylesheet" />
9286
</head>
9387

9488
<body>
95-
....
89+
...
9690
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
9791
</body>
9892
```
9993

100-
### Register Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor Service
94+
### Register Syncfusion Blazor Service
10195

102-
Register the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor Service in the **~/Program.cs** file of your Blazor Web App.
96+
Register the Syncfusion Blazor service in the `~/Program.cs` file based on the interactive render mode.
10397

104-
If the **Interactive Render Mode** is set to `WebAssembly` or `Auto`, you need to register the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor service in both **~/Program.cs** files of your Blazor Web App.
98+
**For WebAssembly or Auto Render Mode**:
99+
If the **Interactive Render Mode** is set to `WebAssembly` or `Auto`, the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor service should be registered in both **~/Program.cs** files.
105100

106101
{% tabs %}
107-
{% highlight c# tabtitle="Server(~/_Program.cs)" hl_lines="3 11" %}
102+
{% highlight c# tabtitle="Server (~/Program.cs)" hl_lines="3 11" %}
108103

109-
...
110104
...
111105
using Syncfusion.Blazor;
112106

@@ -119,10 +113,10 @@ builder.Services.AddRazorComponents()
119113
builder.Services.AddSyncfusionBlazor();
120114

121115
var app = builder.Build();
122-
....
116+
...
123117

124118
{% endhighlight %}
125-
{% highlight c# tabtitle="Client(~/_Program.cs)" hl_lines="2 5" %}
119+
{% highlight c# tabtitle="Client (~/Program.cs)" hl_lines="2 5" %}
126120

127121
...
128122
using Syncfusion.Blazor;
@@ -135,7 +129,8 @@ await builder.Build().RunAsync();
135129
{% endhighlight %}
136130
{% endtabs %}
137131

138-
If the **Interactive Render Mode** is set to `Server`, your project will contain a single **~/Program.cs** file. So, you should register the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor Service only in that **~/Program.cs** file.
132+
**For Server Render Mode**:
133+
If the **Interactive Render Mode** is set to `Server`, the project will contain a single **~/Program.cs** file. In this case, the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor Service should be registered within that `~/Program.cs` file.
139134

140135
{% tabs %}
141136
{% highlight c# tabtitle="~/_Program.cs" hl_lines="2 9" %}
@@ -151,12 +146,13 @@ builder.Services.AddRazorComponents()
151146
builder.Services.AddSyncfusionBlazor();
152147

153148
var app = builder.Build();
154-
....
149+
...
155150

156151
{% endhighlight %}
157152
{% endtabs %}
158153

159-
### Razor Component (`Home.razor`)
154+
### Razor Component (Home.razor)
155+
160156
```csharp
161157
@page "/"
162158

@@ -255,7 +251,8 @@ var app = builder.Build();
255251
</style>
256252
```
257253

258-
(`Home.razor.cs`)
254+
### Code-Behind (Home.razor.cs)
255+
259256
```csharp
260257
using Microsoft.Extensions.AI;
261258
using Syncfusion.Blazor.AI;
@@ -353,20 +350,27 @@ namespace AzureOpenAIExample.Components.Pages
353350
}
354351
```
355352

356-
![Natural Query Language - Output](images/natural-languagequery.gif)
353+
![Natural Language Query Output](images/natural-languagequery.gif)
357354

358355
## How It Works
359356

360-
The example above demonstrates how to use Azure OpenAI with Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor components for natural language query processing:
357+
This example illustrates how the Syncfusion Blazor AI library integrates with Azure OpenAI to process natural language queries:
361358

362-
1. **Setup**: Configuring the Azure OpenAI service in `Program.cs` with appropriate credentials.
363-
2. **Component Integration**: Using the `IChatInferenceService` to process natural language queries.
364-
3. **Prompt Engineering**: Creating appropriate prompts for the AI model to convert natural language to SQL.
365-
4. **Response Processing**: Parsing the SQL response and applying it to the QueryBuilder component.
359+
1. **Setup**: Configure Azure OpenAI in `Program.cs` using a secure API key, endpoint, and deployed model (e.g., `gpt-4`).
360+
2. **Component Integration**: Inject `IChatInferenceService` to process user queries entered in a textarea.
361+
3. **Prompt Engineering**: Craft a prompt to convert natural language input into a valid SQL query for filtering data.
362+
4. **Response Processing**: Parse the SQL query from Azure OpenAI, apply it to the QueryBuilder component, and update the Grid with filtered results.
366363

367364
### Key Components
368-
369-
- **IChatInferenceService**: Injected to interact with the Azure OpenAI models.
370-
- **ChatParameters**: Configures the AI request, including system prompt and user messages.
371-
- **GenerateResponseAsync**: Sends the request to Azure OpenAI and retrieves the response asynchronously.
372-
- **UI Components**: Syncfusion<sup style="font-size:70%">&reg;</sup> Tab, QueryBuilder, and Grid components work together with the AI service.
365+
- **IChatInferenceService**: Injected service for interacting with Azure OpenAI models.
366+
- **ChatParameters**: Configures the AI request with prompts and user messages.
367+
- **GenerateResponseAsync**: Sends asynchronous requests to Azure OpenAI and retrieves responses.
368+
- **UI Components**: Syncfusion Tab, QueryBuilder, and Grid components integrate with the AI service for dynamic query processing.
369+
370+
## Error Handling
371+
- **Invalid Credentials**: Ensure the API key, endpoint, and model are valid and correctly configured in environment variables or Azure Key Vault.
372+
- **Rate Limits**: Azure OpenAI usage may be limited based on your subscription. Check [Azure OpenAI pricing documentation](https://azure.microsoft.com/en-us/pricing/details/cognitive-services/openai-service/) for details.
373+
374+
## See Also
375+
- [Syncfusion Blazor QueryBuilder Documentation](https://blazor.syncfusion.com/documentation/query-builder/getting-started-webapp)
376+
- [Syncfusion Blazor Grid Documentation](https://blazor.syncfusion.com/documentation/datagrid/getting-started-with-web-app)
Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
---
22
layout: post
3-
title: Using Custom AI Services with Syncfusion Blazor AI | Syncfusion
4-
description: Learn how to set up and use Syncfusion.Blazor.AI with custom AI providers, including configuration, integration steps, and practical examples.
3+
title: Custom AI Service Integration with Syncfusion Blazor AI | Syncfusion
4+
description: Learn how to configure and use Syncfusion Blazor AI with custom AI providers, such as DeepSeek, to enable AI-driven features in Blazor applications.
55
platform: Blazor
66
control: AI Integration
77
documentation: ug
88
---
99

1010
# Custom AI Service Integration with Syncfusion® Blazor AI
1111

12-
## Introduction
13-
14-
This section demonstrates configuring and using the [Syncfusion.Blazor.AI](https://www.nuget.org/packages/Syncfusion.Blazor.AI) package with a **custom AI service** by implementing the `IChatInferenceService` interface, using DeepSeek as an example. This extensibility feature empowers developers to integrate any AI provider into Blazor applications, enabling Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor components to work with specialized or proprietary AI services beyond the standard providers.
12+
The [Syncfusion Blazor AI](https://www.nuget.org/packages/Syncfusion.Blazor.AI) library enables integration with custom AI providers by implementing the `IChatInferenceService` interface, using DeepSeek as an example. This extensibility feature allows developers to connect Blazor applications to proprietary or specialized AI services, such as enterprise-specific models or local AI servers, enhancing Syncfusion Blazor components with tailored AI functionality like text generation or data processing.
1513

1614
## Prerequisites
1715

18-
Before integrating a custom AI service with your Blazor application, ensure you have:
19-
20-
* Installed the following NuGet packages:
16+
To integrate a custom AI service with a Blazor application, ensure the following:
17+
- The following NuGet package is installed:
2118
{% tabs %}
2219
{% highlight C# tabtitle="Package Manager" %}
2320

2421
Install-Package Syncfusion.Blazor.AI -Version {{ site.releaseversion }}
2522

2623
{% endhighlight %}
2724
{% endtabs %}
28-
* Obtained an API key from your chosen AI provider (DeepSeek in this example) from the [DeepSeek platform](https://platform.deepseek.com)
29-
* Met the [System Requirements](https://blazor.syncfusion.com/documentation/system-requirements) for Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor components
30-
* Understanding of HTTP client usage and JSON serialization in .NET applications
25+
- An API key is obtained from the chosen AI provider (e.g., DeepSeek via the [DeepSeek API documentation](https://platform.deepseek.com)).
26+
- The [Syncfusion Blazor system requirements](https://blazor.syncfusion.com/documentation/system-requirements) are met.
27+
- Familiarity with HTTP client usage and JSON serialization in .NET applications.
3128

3229
## Configuration Steps
3330

34-
Follow these steps to integrate a custom AI service with Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor AI:
35-
3631
### Implement the Custom AI Service
3732

38-
Create a class that implements the `IChatInferenceService` interface for your custom AI provider. The implementation below demonstrates DeepSeek integration using `AIServiceCredentials` for secure configuration management.
39-
33+
Create a class that implements the `IChatInferenceService` interface for the custom AI provider. The example below demonstrates DeepSeek integration using `AIServiceCredentials` for secure configuration management.
4034

4135
```csharp
4236
using Microsoft.Extensions.AI;
@@ -49,31 +43,31 @@ using System.Threading.Tasks;
4943
public class DeepSeekAIService : IChatInferenceService
5044
{
5145
private readonly HttpClient _httpClient;
52-
private readonly AIServiceCredentials _credentials;
46+
private readonly string _apiKey = "Your API Key";
47+
private readonly string _modelName = "deepseek-chat"; // Example model
5348
54-
public DeepSeekAIService(HttpClient httpClient, AIServiceCredentials credentials)
49+
public DeepSeekAIService(HttpClient httpClient)
5550
{
5651
_httpClient = httpClient;
57-
_credentials = credentials;
5852
}
5953

6054
public async Task<string> GenerateResponseAsync(ChatParameters options)
6155
{
6256
var requestBody = new
6357
{
64-
model = _credentials.DeploymentName ?? "deepseek-chat", // Use deployment name from credentials
58+
model = _modelName, // Use deployment name from credentials
6559
messages = options.Messages.Select(m => new { role = m.Role.ToString().ToLower(), content = m.Content }).ToArray(),
6660
temperature = options.Temperature,
6761
max_tokens = options.MaxTokens
6862
};
6963

7064
var requestContent = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");
71-
if (!string.IsNullOrEmpty(_credentials.ApiKey))
65+
if (!string.IsNullOrEmpty(_apiKey))
7266
{
73-
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
67+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);
7468
}
7569

76-
var response = await _httpClient.PostAsync(_credentials.Endpoint?.ToString() ?? "https://api.deepseek.com/v1/chat/completions", requestContent);
70+
var response = await _httpClient.PostAsync("https://api.deepseek.com/v1/chat/completions", requestContent);
7771
response.EnsureSuccessStatusCode();
7872

7973
var responseJson = await response.Content.ReadAsStringAsync();
@@ -85,33 +79,30 @@ public class DeepSeekAIService : IChatInferenceService
8579

8680
### Register the Custom AI Service
8781

88-
Open your Blazor application's `Program.cs` and configure the DeepSeek AI service with `AIServiceCredentials`.
82+
Update the Blazor application's `Program.cs` to configure the DeepSeek AI service with `AIServiceCredentials`.
8983

9084
```csharp
9185
using Syncfusion.Blazor.AI;
9286

93-
builder.Services.AddSingleton(new AIServiceCredentials
94-
{
95-
ApiKey = "your-deepseek-api-key", // Replace with your DeepSeek API key
96-
DeploymentName = "deepseek-chat", // Specify the model (e.g., "deepseek-chat", "deepseek-coder")
97-
});
98-
99-
// Register the custom inference backend
87+
// Register the custom inference service
10088
builder.Services.AddSingleton<IChatInferenceService, DeepSeekAIService>();
10189
```
10290

103-
## Custom AI Integration with Syncfusion<sup style="font-size:70%">&reg;</sup> Components
104-
The `GenerateResponseAsync` method in the DeepSeekAIService class serves as the core interface for AI communication. This method:
91+
## How It Works
92+
93+
This example illustrates how the Syncfusion Blazor AI library integrates with a custom AI service (DeepSeek):
10594

106-
1. **Formats Requests:** Converts Syncfusion<sup style="font-size:70%">&reg;</sup> AI parameters into the custom provider's expected format
107-
2. **Handles Authentication:** Manages API key authentication securely
108-
3. **Processes Responses:** Parses the provider's response format into standard AI responses
95+
1. **Setup**: Implement and register the custom AI service in `Program.cs` using secure credentials.
96+
2. **Component Integration**: Inject `IChatInferenceService` to process AI requests within Syncfusion components like Smart TextArea.
97+
3. **Request Formatting**: Convert Syncfusion AI parameters into the custom provider’s API format (e.g., DeepSeek’s JSON structure).
98+
4. **Response Processing**: Parse the provider’s response and update the component (e.g., displaying text suggestions).
10999

110-
## See Also
100+
### Key Components
101+
- **IChatInferenceService**: Interface for interacting with custom AI providers.
102+
- **AIServiceCredentials**: Syncfusion class for managing API keys, endpoints, and model names.
103+
- **GenerateResponseAsync**: Sends asynchronous requests to the custom AI provider and retrieves responses.
111104

112-
- [Overview](https://blazor.syncfusion.com/documentation/smart-ai-solutions/ai/overview)
113-
- [OpenAI Integration](https://blazor.syncfusion.com/documentation/smart-ai-solutions/ai/openai)
114-
- [Azure OpenAI Integration](https://blazor.syncfusion.com/documentation/smart-ai-solutions/ai/azure-openai)
115-
- [Ollama Integration](https://blazor.syncfusion.com/documentation/smart-ai-solutions/ai/ollama)
116-
- [Smart TextArea](https://blazor.syncfusion.com/documentation/smart-textarea/getting-started)
117-
- [Smart Paste Button](https://blazor.syncfusion.com/documentation/smart-paste/getting-started)
105+
## Error Handling
106+
- **Invalid API Key**: Ensure the API key is valid and stored securely in environment variables or a configuration service.
107+
- **Rate Limits**: Check the custom AI provider’s documentation (e.g., [DeepSeek API docs](https://platform.deepseek.com)) for rate limit details.
108+
- **Network or Parsing Errors**: Handle HTTP or JSON errors gracefully, as shown in the `DeepSeekAIService` implementation.

0 commit comments

Comments
 (0)