|
| 1 | +--- |
| 2 | +title: Integration with Telerik.AI.SmartComponents.Extensions |
| 3 | +page_title: Integration with Telerik.AI.SmartComponents.Extensions |
| 4 | +description: How to integrate the UI for Blazor components with Telerik.AI.SmartComponents.Extensions |
| 5 | +slug: common-features-telerik-ai-smartcomponents-extensions-integration |
| 6 | +tags: telerik,blazor,aiprompt,ai,extensions,integration |
| 7 | +published: True |
| 8 | +position: 45 |
| 9 | +--- |
| 10 | + |
| 11 | +# Getting Started with Telerik.AI.SmartComponents.Extensions |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +The `Telerik.AI.SmartComponents.Extensions` library provides AI-powered functionality for Grid operations, enabling natural language processing for filtering, sorting, grouping, and highlighting data. This library integrates seamlessly with Microsoft.Extensions.AI and Azure OpenAI to provide intelligent Grid interactions. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- .NET 8.0 or later |
| 20 | +- `Microsoft.Extensions.AI` package |
| 21 | +- Azure OpenAI or OpenAI API access |
| 22 | +- ASP.NET Core (for web API scenarios) |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +1. Add the [`Telerik.AI.SmartComponents.Extensions` NuGet package](https://www.nuget.org/packages/Telerik.AI.SmartComponents.Extensions) to your project. |
| 27 | +1. Add the required dependency packages: |
| 28 | + * `Microsoft.Extensions.AI` |
| 29 | + * `Azure.AI.OpenAI` |
| 30 | + |
| 31 | +## Configuration |
| 32 | + |
| 33 | +### 1. Configure AI Services in Program.cs |
| 34 | + |
| 35 | +```csharp |
| 36 | +using Microsoft.Extensions.AI; |
| 37 | +using Azure.AI.OpenAI; |
| 38 | + |
| 39 | +var builder = WebApplication.CreateBuilder(args); |
| 40 | + |
| 41 | +// Configure Azure OpenAI Chat Client |
| 42 | +builder.Services.AddSingleton<IChatClient>(serviceProvider => |
| 43 | +{ |
| 44 | + var configuration = serviceProvider.GetRequiredService<IConfiguration>(); |
| 45 | + var endpoint = configuration["AI:AzureOpenAI:Endpoint"]; |
| 46 | + var apiKey = configuration["AI:AzureOpenAI:Key"]; |
| 47 | + var modelId = configuration["AI:AzureOpenAI:Chat:ModelId"]; |
| 48 | + |
| 49 | + var client = new AzureOpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(apiKey)); |
| 50 | + return client.AsChatClient(modelId); |
| 51 | +}); |
| 52 | + |
| 53 | +builder.Services.AddControllers(); |
| 54 | +var app = builder.Build(); |
| 55 | +``` |
| 56 | + |
| 57 | +### 2. Configure appsettings.json |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "AI": { |
| 62 | + "AzureOpenAI": { |
| 63 | + "Endpoint": "https://your-openai-resource.openai.azure.com/", |
| 64 | + "Key": "your-api-key-here", |
| 65 | + "Chat": { |
| 66 | + "ModelId": "gpt-4" |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Basic Usage |
| 74 | + |
| 75 | +### 1. Create a Grid Controller |
| 76 | + |
| 77 | +```csharp |
| 78 | +using Microsoft.AspNetCore.Mvc; |
| 79 | +using Microsoft.Extensions.AI; |
| 80 | +using OpenAI.Chat; |
| 81 | +using Telerik.AI.SmartComponents.Extensions; |
| 82 | + |
| 83 | +[ApiController] |
| 84 | +[Route("[controller]/[action]")] |
| 85 | +public class GridController : Controller |
| 86 | +{ |
| 87 | + private readonly IChatClient _chatClient; |
| 88 | + |
| 89 | + public GridController(IChatClient chatClient) |
| 90 | + { |
| 91 | + _chatClient = chatClient; |
| 92 | + } |
| 93 | + |
| 94 | + [HttpPost] |
| 95 | + [Route("/grid/smart-state")] |
| 96 | + public async Task<IActionResult> SmartState([FromBody] GridAIRequest request) |
| 97 | + { |
| 98 | + // Create chat completion options |
| 99 | + var options = new ChatCompletionOptions(); |
| 100 | + |
| 101 | + // Add Grid-specific chat tools for AI processing |
| 102 | + ChatTools.AddGridChatTools( |
| 103 | + request.Columns.Select(x => new GridAIColumn { |
| 104 | + Field = x.Field, |
| 105 | + Values = x.Values |
| 106 | + }).ToList(), |
| 107 | + options |
| 108 | + ); |
| 109 | + |
| 110 | + // Convert request contents to chat messages |
| 111 | + var conversationMessages = request.Contents |
| 112 | + .Select(m => new UserChatMessage(m.Text)) |
| 113 | + .ToList(); |
| 114 | + |
| 115 | + // Get the chat service and process the request |
| 116 | + var chatService = _chatClient.GetService<ChatClient>(); |
| 117 | + var completion = await chatService.CompleteChatAsync(conversationMessages, options); |
| 118 | + |
| 119 | + // Extract Grid options from AI response |
| 120 | + var gridOptions = completion.ToolCalls.ExtractGridOptions(); |
| 121 | + |
| 122 | + return Json(gridOptions); |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### 2. Define Your Data Model |
| 128 | + |
| 129 | +```csharp |
| 130 | +public class Employee |
| 131 | +{ |
| 132 | + public string FirstName { get; set; } |
| 133 | + public string LastName { get; set; } |
| 134 | + public int Age { get; set; } |
| 135 | + public string Department { get; set; } |
| 136 | + public decimal Salary { get; set; } |
| 137 | + public string City { get; set; } |
| 138 | + public string Gender { get; set; } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### 3. Create Grid AI Request |
| 143 | + |
| 144 | +```csharp |
| 145 | +var request = new GridAIRequest |
| 146 | +{ |
| 147 | + Columns = new List<GridAIColumn> |
| 148 | + { |
| 149 | + new() { Field = "FirstName" }, |
| 150 | + new() { Field = "LastName" }, |
| 151 | + new() { Field = "Age" }, |
| 152 | + new() { Field = "Department", Values = new[] { "IT", "HR", "Finance", "Marketing" } }, |
| 153 | + new() { Field = "Salary" }, |
| 154 | + new() { Field = "City", Values = new[] { "New York", "London", "Paris", "Tokyo" } }, |
| 155 | + new() { Field = "Gender", Values = new[] { "Male", "Female" } } |
| 156 | + }, |
| 157 | + Contents = new List<GridAIRequestContent> |
| 158 | + { |
| 159 | + new() { Type = "text", Text = "Show me all employees in IT department" } |
| 160 | + } |
| 161 | +}; |
| 162 | +``` |
| 163 | + |
| 164 | +## Advanced Features |
| 165 | + |
| 166 | +### 1. Filtering Operations |
| 167 | + |
| 168 | +The library supports various natural language filtering queries: |
| 169 | + |
| 170 | +```csharp |
| 171 | +// Example queries that work with the AI: |
| 172 | +"Show me employees older than 30" |
| 173 | +"Filter people in IT department" |
| 174 | +"Get employees whose name starts with J" |
| 175 | +"Show me men with salary greater than 60000" |
| 176 | +``` |
| 177 | + |
| 178 | +### 2. Sorting Operations |
| 179 | + |
| 180 | +```csharp |
| 181 | +// Natural language sorting examples: |
| 182 | +"Sort by age descending" |
| 183 | +"Order by salary ascending" |
| 184 | +"Sort by department, then by name" |
| 185 | +``` |
| 186 | + |
| 187 | +### 3. Grouping Operations |
| 188 | + |
| 189 | +```csharp |
| 190 | +// Grouping examples: |
| 191 | +"Group by department" |
| 192 | +"Group by city, then by age" |
| 193 | +"Group employees by gender descending" |
| 194 | +``` |
| 195 | + |
| 196 | +### 4. Highlighting Operations |
| 197 | + |
| 198 | +```csharp |
| 199 | +// Highlighting examples: |
| 200 | +"Highlight employees whose name starts with A" |
| 201 | +"Mark salary cells of people older than 30" |
| 202 | +"Highlight lastname cells of IT employees" |
| 203 | +``` |
| 204 | + |
| 205 | +## Working with Grid Responses |
| 206 | + |
| 207 | +The AI service returns a `GridAIResponse` object containing the processed operations: |
| 208 | + |
| 209 | +```csharp |
| 210 | +public async Task<GridAIResponse> ProcessGridRequest(GridAIRequest request) |
| 211 | +{ |
| 212 | + var options = new ChatCompletionOptions(); |
| 213 | + ChatTools.AddGridChatTools(request.Columns, options); |
| 214 | + |
| 215 | + var messages = request.Contents.Select(m => new UserChatMessage(m.Text)).ToList(); |
| 216 | + var completion = await _chatClient.CompleteChatAsync(messages, options); |
| 217 | + |
| 218 | + var response = completion.ToolCalls.ExtractGridOptions(); |
| 219 | + |
| 220 | + // The response contains: |
| 221 | + // - response.Filter: Composite filter conditions |
| 222 | + // - response.Sort: Array of sort descriptors |
| 223 | + // - response.Group: Array of group descriptors |
| 224 | + // - response.Highlight: Array of highlight descriptors |
| 225 | + // - response.Messages: Status/info messages |
| 226 | + |
| 227 | + return response; |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +## Filter Types |
| 232 | + |
| 233 | +The library supports various filter operators: |
| 234 | + |
| 235 | +- `equalto`: Exact match |
| 236 | +- `contains`: Contains substring |
| 237 | +- `startswith`: Starts with text |
| 238 | +- `endswith`: Ends with text |
| 239 | +- `greaterthan`: Greater than (numeric) |
| 240 | +- `lessthan`: Less than (numeric) |
| 241 | +- `greaterthanorequal`: Greater than or equal |
| 242 | +- `lessthanorequal`: Less than or equal |
| 243 | + |
| 244 | +## Best Practices |
| 245 | + |
| 246 | +### 1. Column Configuration |
| 247 | + |
| 248 | +When the options for the column are of Enum type provide meaningful column values to help the AI understand your data: |
| 249 | + |
| 250 | +```csharp |
| 251 | +new GridAIColumn |
| 252 | +{ |
| 253 | + Field = "Status", |
| 254 | + // only when only a set of values are used |
| 255 | + Values = new[] { "Active", "Inactive", "Pending" } |
| 256 | +} |
| 257 | +``` |
| 258 | + |
| 259 | +### 2. Error Handling |
| 260 | + |
| 261 | +```csharp |
| 262 | +try |
| 263 | +{ |
| 264 | + var completion = await chatService.CompleteChatAsync(messages, options); |
| 265 | + var response = completion.ToolCalls.ExtractGridOptions(); |
| 266 | + return Json(response); |
| 267 | +} |
| 268 | +catch (Exception ex) |
| 269 | +{ |
| 270 | + return StatusCode(500, $"AI processing failed: {ex.Message}"); |
| 271 | +} |
| 272 | +``` |
| 273 | + |
| 274 | +### 3. Input Validation |
| 275 | + |
| 276 | +```csharp |
| 277 | +if (request?.Columns == null || !request.Columns.Any()) |
| 278 | +{ |
| 279 | + return BadRequest("Columns are required"); |
| 280 | +} |
| 281 | + |
| 282 | +if (request.Contents == null || !request.Contents.Any()) |
| 283 | +{ |
| 284 | + return BadRequest("Content is required"); |
| 285 | +} |
| 286 | +``` |
| 287 | + |
| 288 | +## Testing |
| 289 | + |
| 290 | +The library includes comprehensive test coverage. You can run tests to verify functionality: |
| 291 | + |
| 292 | +```bash |
| 293 | +cd tests |
| 294 | +dotnet test |
| 295 | +``` |
| 296 | + |
| 297 | +For integration testing with your specific data model, create test cases that verify AI responses match expected Grid operations. |
| 298 | + |
| 299 | +## Troubleshooting |
| 300 | + |
| 301 | +### Common Issues |
| 302 | + |
| 303 | +* Invalid URI Format: Ensure your Azure OpenAI endpoint is correctly formatted in configuration. |
| 304 | +* API Key Issues: Verify your API key has proper permissions and is not expired. |
| 305 | +* Model Availability: Ensure the specified model ID is deployed in your Azure OpenAI resource. |
| 306 | +* Token Limits: Be mindful of token limits when processing large datasets. |
| 307 | + |
0 commit comments