Skip to content

Commit 46aa077

Browse files
Review Changes addressed
1 parent d5fa2c9 commit 46aa077

File tree

2 files changed

+283
-120
lines changed

2 files changed

+283
-120
lines changed

MAUI/Autocomplete/AI-Powered-Smart-Searching.md

Lines changed: 143 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,148 @@ documentation: ug
1010
# Implementing AI-Powered Smart Search in .NET MAUI Autocomplete
1111

1212
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.
31+
32+
{% tabs %}
33+
{% highlight c# %}
34+
35+
// AzureBaseService.cs
36+
public abstract class AzureBaseService
37+
{
38+
internal const string Endpoint = "YOUR_END_POINT_NAME";
39+
40+
internal const string DeploymentName = "DEPLOYMENT_NAME";
41+
42+
internal const string Key = "API_KEY";
43+
44+
public AzureBaseService()
45+
{
46+
ValidateCredential();
47+
}
48+
49+
private async void ValidateCredential()
50+
{
51+
this.GetAzureOpenAIKernal();
52+
53+
if (isAlreadyValidated)
54+
{
55+
return;
56+
}
57+
58+
try
59+
{
60+
if (Client != null)
61+
{
62+
await Client!.CompleteAsync("Hello, Test Check");
63+
ChatHistory = string.Empty;
64+
IsCredentialValid = true;
65+
isAlreadyValidated = true;
66+
}
67+
else
68+
{
69+
ShowAlertAsync();
70+
}
71+
}
72+
catch (Exception)
73+
{
74+
return;
75+
}
76+
}
77+
78+
/// <summary>
79+
/// To get the Azure open ai kernal method
80+
/// </summary>
81+
private void GetAzureOpenAIKernal()
82+
{
83+
try
84+
{
85+
var client = new AzureOpenAIClient(new Uri(Endpoint), new AzureKeyCredential(Key)).AsChatClient(modelId: DeploymentName);
86+
this.Client = client;
87+
}
88+
catch (Exception)
89+
{
90+
}
91+
}
92+
93+
}
94+
95+
{% endhighlight %}
96+
97+
{% endtabs %}
98+
99+
{% tabs %}
100+
{% highlight c# %}
101+
102+
//AzureAIService.cs
103+
104+
public class AzureAIService : AzureBaseService
105+
{
106+
/// <summary>
107+
/// Gets a completion response from the AzureAI service based on the provided prompt.
108+
/// </summary>
109+
/// <param name="prompt"></param>
110+
/// <param name="cancellationToken"></param>
111+
/// <returns></returns>
112+
public async Task<string> GetCompletion(string prompt, CancellationToken cancellationToken)
113+
{
114+
ChatHistory = string.Empty;
115+
if(ChatHistory != null)
116+
{
117+
ChatHistory = ChatHistory + "You are a filtering assistant.";
118+
ChatHistory = ChatHistory + prompt;
119+
try
120+
{
121+
if (Client != null)
122+
{
123+
cancellationToken.ThrowIfCancellationRequested();
124+
var chatresponse = await Client.CompleteAsync(prompt);
125+
return chatresponse.ToString();
126+
}
127+
}
128+
catch (RequestFailedException ex)
129+
{
130+
// Log the error message and rethrow the exception or handle it appropriately
131+
Debug.WriteLine($"Request failed: {ex.Message}");
132+
throw;
133+
}
134+
catch (Exception ex)
135+
{
136+
// Handle other potential exceptions
137+
Debug.WriteLine($"An error occurred: {ex.Message}");
138+
throw;
139+
}
140+
}
141+
return "";
142+
}
143+
}
144+
145+
{% endhighlight %}
146+
147+
{% endtabs %}
148+
13149
## Implementing custom filtering in .NET MAUI Autocomplete
14150

15151
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.
16152

153+
**Step 1:** Let’s create a new business model to search country names. Refer to the following code example.
154+
17155
{% tabs %}
18156
{% highlight c# %}
19157

@@ -63,9 +201,9 @@ internal class CountryViewModel : INotifyPropertyChanged
63201

64202
{% endtabs %}
65203

66-
### Connecting the Custom Filter to Azure OpenAI
204+
**Step 2:** Connecting the Custom Filter to Azure OpenAI
67205

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.
69207

70208
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.
71209

@@ -198,10 +336,12 @@ public class CustomFilter : IAutocompleteFilterBehavior
198336

199337
{% endtabs %}
200338

339+
**Step:3** Applying Custom Filtering to AutoComplte
340+
201341
Applying custom filtering to the [Autocomplete](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Inputs.SfAutocomplete.html) control by using the `FilterBehavior` property.
202342

203343
{% tabs %}
204-
{% highlight c# %}
344+
{% highlight xaml %}
205345

206346
<editors:SfAutocomplete x:Name="autoComplete"
207347
DropDownPlacement="Bottom"
@@ -219,64 +359,6 @@ Applying custom filtering to the [Autocomplete](https://help.syncfusion.com/cr/m
219359

220360
{% endtabs %}
221361

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-
/// <param name="prompt"></param>
241-
/// <param name="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
262-
Debug.WriteLine($"Request failed: {ex.Message}");
263-
throw;
264-
}
265-
catch (Exception ex)
266-
{
267-
// Handle other potential exceptions
268-
Debug.WriteLine($"An error occurred: {ex.Message}");
269-
throw;
270-
}
271-
}
272-
return "";
273-
}
274-
}
275-
276-
{% endhighlight %}
277-
278-
{% endtabs %}
279-
280362
The following image demonstrates the output of the above AI-based search using a custom filtering sample.
281363

282364
![.NET MAUI AutoComplete With AI Smart Search.](Images/AISmartSearch/ai_smart_search.png)

0 commit comments

Comments
 (0)