You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Implementing AI-Powered Smart Search in .NET MAUI Autocomplete
11
11
12
12
This document will walk you through the implementation of an advanced search functionality in the Syncfusion [.NET MAUI Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) control. The example leverages the power of Azure OpenAI for an intelligent, AI-driven search experience.
13
+
14
+
## Integrating Azure OpenAI with your .NET MAUI App
15
+
16
+
First, ensure you have access to [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/overview) and have created a deployment in the Azure portal.
17
+
18
+
If you don’t have access, please refer to the [create and deploy Azure OpenAI service](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/create-resource?pivots=web-portal) guide to set up a new account.
19
+
20
+
Note down the deployment name, endpoint URL, and API key.
21
+
22
+
we’ll use the [Azure.AI.OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI/1.0.0-beta.12) NuGet package from the [NuGet Gallery](https://www.nuget.org/). So, before getting started, install the Azure.AI.OpenAI NuGet package in your .NET MAUI app.
23
+
24
+
In your base service class (AzureBaseService), initialize the OpenAIClient. Replace the Endpoint, DeploymentName, Key with actual values from your Azure OpenAI resource.
25
+
26
+
This creates a chat client using your endpoint, API key, and deployment name. It’s stored in the Client property for use in other methods.
27
+
28
+
ComboBoxAzureAIService use this Client to send prompts and receive completions.
29
+
30
+
In the `GetCompletion` method, we will construct the prompt and send it to the Azure OpenAI Service. The ChatHistory helps maintain context but is cleared for each new prompt in this implementation to ensure each search is independent.
## Implementing custom filtering in .NET MAUI Autocomplete
14
150
15
151
The [.NET MAUI Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) control allows you to apply custom filter logic to suggest items based on your specific filter criteria by utilizing the `FilterBehavior` property, which is the entry point for our smart search logic.
16
152
153
+
**Step 1:** Let’s create a new business model to search country names. Refer to the following code example.
154
+
17
155
{% tabs %}
18
156
{% highlight c# %}
19
157
@@ -63,9 +201,9 @@ internal class CountryViewModel : INotifyPropertyChanged
63
201
64
202
{% endtabs %}
65
203
66
-
###Connecting the Custom Filter to Azure OpenAI
204
+
**Step 2:** Connecting the Custom Filter to Azure OpenAI
67
205
68
-
Implement the `GetMatchingItemsAsync` method from the interface. This method is the heart of the custom filter. It is invoked every time the text in the [Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) control changes.
206
+
Implement the `GetMatchingItemsAsync` method from the interface. This method is the heart of the custom filter. It is invoked every time the text in the [Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) control changes.
69
207
70
208
The logic within [Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) intelligently decides whether to perform an online AI search based on the availability of Azure credentials.
71
209
@@ -198,10 +336,12 @@ public class CustomFilter : IAutocompleteFilterBehavior
198
336
199
337
{% endtabs %}
200
338
339
+
**Step:3** Applying Custom Filtering to AutoComplte
340
+
201
341
Applying custom filtering to the [Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) control by using the `FilterBehavior` property.
202
342
203
343
{% tabs %}
204
-
{% highlight c# %}
344
+
{% highlight xaml %}
205
345
206
346
<editors:SfAutocomplete x:Name="autoComplete"
207
347
DropDownPlacement="Bottom"
@@ -219,64 +359,6 @@ Applying custom filtering to the [Autocomplete](https://help.syncfusion.com/cr/m
219
359
220
360
{% endtabs %}
221
361
222
-
## Online Search: Integrating with Azure OpenAI
223
-
224
-
When Azure credentials are available, the application performs a real-time, intelligent search using AI.
225
-
226
-
### The Azure AI Service
227
-
228
-
In the `GetCompletion` method, we will construct the prompt and send it to the Azure OpenAI Service. The ChatHistory helps maintain context but is cleared for each new prompt in this implementation to ensure each search is independent.
229
-
230
-
{% tabs %}
231
-
{% highlight c# %}
232
-
233
-
//AzureAIService.cs
234
-
235
-
public class AzureAIService : AzureBaseService
236
-
{
237
-
/// <summary>
238
-
/// Gets a completion response from the AzureAI service based on the provided prompt.
239
-
/// </summary>
240
-
/// <paramname="prompt"></param>
241
-
/// <paramname="cancellationToken"></param>
242
-
/// <returns></returns>
243
-
public async Task<string> GetCompletion(string prompt, CancellationToken cancellationToken)
244
-
{
245
-
ChatHistory = string.Empty;
246
-
if(ChatHistory != null)
247
-
{
248
-
ChatHistory = ChatHistory + "You are a filtering assistant.";
249
-
ChatHistory = ChatHistory + prompt;
250
-
try
251
-
{
252
-
if (Client != null)
253
-
{
254
-
cancellationToken.ThrowIfCancellationRequested();
255
-
var chatresponse = await Client.CompleteAsync(prompt);
256
-
return chatresponse.ToString();
257
-
}
258
-
}
259
-
catch (RequestFailedException ex)
260
-
{
261
-
// Log the error message and rethrow the exception or handle it appropriately
0 commit comments