@@ -20,31 +20,31 @@ The `Telerik.AI.SmartComponents.Extensions` library provides AI-powered function
2020
2121## Installation
2222
23- 1 . Add the [ ` Telerik.AI.SmartComponents.Extensions ` NuGet package] ( https://www.nuget.org/packages/Telerik.AI.SmartComponents.Extensions ) to your project. It adds the following dependencies:
24- * ` Microsoft.Extensions.AI `
25- * ` Azure.AI.OpenAI `
26- 1 . Add the [ ` Microsoft.Extensions. AI.OpenAI` NuGet package ] ( https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI ) . Note that it's a preview (pre-release) package.
23+ Add the [ ` Telerik.AI.SmartComponents.Extensions ` NuGet package] ( https://www.nuget.org/packages/Telerik.AI.SmartComponents.Extensions ) to your project. It adds the following dependencies:
24+
25+ * ` Microsoft.Extensions.AI `
26+ * ` Azure. AI.OpenAI`
2727
2828## Configuration
2929
3030### 1. Configure AI Services in Program.cs
3131
32- ``` csharp
32+ ``` csharp.skip-repl
3333using Microsoft.Extensions.AI;
3434using Azure.AI.OpenAI;
3535
3636var builder = WebApplication.CreateBuilder(args);
3737
3838// Configure Azure OpenAI Chat Client
39- builder .Services .AddChatClient (serviceProvider =>
39+ builder.Services.AddSingleton<IChatClient> (serviceProvider =>
4040{
4141 IConfiguration configuration = serviceProvider.GetRequiredService<IConfiguration>();
42- string endpoint = configuration [" AI:AzureOpenAI:Endpoint" ] ?? " " ;
43- string apiKey = configuration [" AI:AzureOpenAI:Key" ] ?? " " ;
44- string modelId = configuration [" AI:AzureOpenAI:Chat:ModelId" ] ?? " " ;
42+ string endpoint = configuration["AI:AzureOpenAI:Endpoint"];
43+ string apiKey = configuration["AI:AzureOpenAI:Key"];
44+ string modelId = configuration["AI:AzureOpenAI:Chat:ModelId"];
4545
4646 var client = new AzureOpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(apiKey));
47- return client .GetChatClient (modelId ). AsIChatClient ( );
47+ return client.AsChatClient (modelId);
4848});
4949
5050builder.Services.AddControllers();
@@ -53,7 +53,7 @@ var app = builder.Build();
5353
5454### 2. Configure AI Properties in appsettings.json
5555
56- ``` json
56+ ``` json.skip-repl
5757{
5858 "AI": {
5959 "AzureOpenAI": {
@@ -71,10 +71,9 @@ var app = builder.Build();
7171
7272### 1. Create a Grid Controller
7373
74- ``` csharp
74+ ``` csharp.skip-repl
7575using Microsoft.AspNetCore.Mvc;
7676using Microsoft.Extensions.AI;
77- using OpenAI .Chat ;
7877using Telerik.AI.SmartComponents.Extensions;
7978
8079[ApiController]
@@ -93,37 +92,30 @@ public class GridController : Controller
9392 public async Task<IActionResult> SmartState([FromBody] GridAIRequest request)
9493 {
9594 // Create chat completion options
96- var options = new ChatCompletionOptions ();
97-
95+ var options = new ChatOptions ();
96+
9897 // Add Grid-specific chat tools for AI processing
99- ChatTools .AddGridChatTools (
100- request .Columns .Select (x => new GridAIColumn {
101- Field = x .Field ,
102- Values = x .Values
103- }).ToList (),
104- options
105- );
98+ options.AddGridChatTools(request.Columns);
10699
107100 // Convert request contents to chat messages
108101 var conversationMessages = request.Contents
109- .Select (m => new UserChatMessage ( m .Text ))
102+ .Select(m => new ChatMessage(ChatRole.User, m.Text))
110103 .ToList();
111104
112- // Get the chat service and process the request
113- var chatService = _chatClient .GetService <ChatClient >();
114- var completion = await chatService .CompleteChatAsync (conversationMessages , options );
115-
116- // Extract Grid options from AI response
117- var gridOptions = completion .ToolCalls .ExtractGridOptions ();
118-
119- return Json (gridOptions );
105+ // Process the request
106+ ChatResponse completion = await _chatClient.GetResponseAsync(conversationMessages, options);
107+
108+ // Extract Grid response from AI response
109+ GridAIResponse response = completion.ExtractGridResponse();
110+
111+ return Json(response);
120112 }
121113}
122114```
123115
124116### 2. Define Your Data Model
125117
126- ``` csharp
118+ ``` csharp.skip-repl
127119public class Employee
128120{
129121 public string FirstName { get; set; }
@@ -138,7 +130,7 @@ public class Employee
138130
139131### 3. Create Grid AI Request
140132
141- ``` csharp
133+ ``` csharp.skip-repl
142134var request = new GridAIRequest
143135{
144136 Columns = new List<GridAIColumn>
@@ -164,7 +156,7 @@ var request = new GridAIRequest
164156
165157The library supports various natural language filtering queries:
166158
167- ``` csharp
159+ ``` csharp.skip-repl
168160// Example queries that work with the AI:
169161"Show me employees older than 30"
170162"Filter people in IT department"
@@ -174,7 +166,7 @@ The library supports various natural language filtering queries:
174166
175167### 2. Sorting Operations
176168
177- ``` csharp
169+ ``` csharp.skip-repl
178170// Natural language sorting examples:
179171"Sort by age descending"
180172"Order by salary ascending"
@@ -183,7 +175,7 @@ The library supports various natural language filtering queries:
183175
184176### 3. Grouping Operations
185177
186- ``` csharp
178+ ``` csharp.skip-repl
187179// Grouping examples:
188180"Group by department"
189181"Group by city, then by age"
@@ -192,7 +184,7 @@ The library supports various natural language filtering queries:
192184
193185### 4. Highlighting Operations
194186
195- ``` csharp
187+ ``` csharp.skip-repl
196188// Highlighting examples:
197189"Highlight employees whose name starts with A"
198190"Mark salary cells of people older than 30"
@@ -201,26 +193,23 @@ The library supports various natural language filtering queries:
201193
202194## Working with Grid Responses
203195
204- The AI service returns a ` GridAIResponse ` object containing the processed operations:
196+ The AI service returns a ` GridAIResponse ` object containing a list of commands that represent the operations:
205197
206- ``` csharp
198+ ``` csharp.skip-repl
207199public async Task<GridAIResponse> ProcessGridRequest(GridAIRequest request)
208200{
209- var options = new ChatCompletionOptions ();
210- ChatTools .AddGridChatTools (request .Columns , options );
211-
212- var messages = request .Contents .Select (m => new UserChatMessage ( m .Text )).ToList ();
213- var completion = await _chatClient .CompleteChatAsync ( messages , options );
214-
215- var response = completion .ToolCalls . ExtractGridOptions ();
216-
201+ var options = new ChatOptions ();
202+ options .AddGridChatTools(request.Columns);
203+
204+ var messages = request.Contents.Select(m => new ChatMessage(ChatRole.User, m.Text)).ToList();
205+ var completion = await _chatClient.GetResponseAsync(conversationMessages , options);
206+
207+ var response = response = completion.ExtractGridResponse ();
208+
217209 // The response contains:
218- // - response.Filter: Composite filter conditions
219- // - response.Sort: Array of sort descriptors
220- // - response.Group: Array of group descriptors
221- // - response.Highlight: Array of highlight descriptors
210+ // - response.Commands: A list of commands, containing information about the type of operation, and parameters associated with it.
222211 // - response.Messages: Status/info messages
223-
212+
224213 return response;
225214}
226215```
@@ -244,22 +233,22 @@ The library supports various filter operators:
244233
245234When the options for the column are of Enum type provide meaningful column values to help the AI understand your data:
246235
247- ``` csharp
248- new GridAIColumn
249- {
250- Field = " Status" ,
236+ ``` csharp.skip-repl
237+ new GridAIColumn
238+ {
239+ Field = "Status",
251240 // only when only a set of values are used
252- Values = new [] { " Active" , " Inactive" , " Pending" }
241+ Values = new[] { "Active", "Inactive", "Pending" }
253242}
254243```
255244
256245### 2. Error Handling
257246
258- ``` csharp
247+ ``` csharp.skip-repl
259248try
260249{
261- var completion = await chatService . CompleteChatAsync ( messages , options );
262- var response = completion .ToolCalls . ExtractGridOptions ();
250+ var completion = await _chatClient.GetResponseAsync(conversationMessages , options);
251+ var response = completion.ExtractGridResponse ();
263252 return Json(response);
264253}
265254catch (Exception ex)
@@ -270,7 +259,7 @@ catch (Exception ex)
270259
271260### 3. Input Validation
272261
273- ``` csharp
262+ ``` csharp.skip-repl
274263if (request?.Columns == null || !request.Columns.Any())
275264{
276265 return BadRequest("Columns are required");
@@ -286,14 +275,57 @@ if (request.Contents == null || !request.Contents.Any())
286275
287276The library includes comprehensive test coverage. You can run tests to verify functionality:
288277
289- ``` bash
278+ ``` bash.skip-repl
290279cd tests
291280dotnet test
292281```
293282
294283For integration testing with your specific data model, create test cases that verify AI responses match expected Grid operations.
295284
296- ## Troubleshooting
285+ ## Example Client Usage
286+
287+ ``` typescript.skip-repl
288+ interface GridAIRequest {
289+ columns: GridAIColumn[];
290+ contents: GridAIRequestContent[];
291+ role?: string;
292+ }
293+
294+ interface GridAIColumn {
295+ field: string;
296+ values: string[];
297+ }
298+
299+ interface GridAIRequestContent {
300+ $type: string;
301+ text: string;
302+ }
303+
304+ async function processGridQuery(query: string, columns: GridAIColumn[]) {
305+ const request: GridAIRequest = {
306+ columns: columns,
307+ contents: [{ $type: "text", text: query }],
308+ role: "user",
309+ };
310+
311+ const response = await fetch("/grid/smart-state", {
312+ method: "POST",
313+ headers: { "Content-Type": "application/json" },
314+ body: JSON.stringify(request),
315+ });
316+
317+ return await response.json();
318+ }
319+
320+ // Usage
321+ const columns = [
322+ { field: "Name", values: ["John", "Jane", "Bob"] },
323+ { field: "Age", values: ["25", "30", "35"] },
324+ { field: "Department", values: ["IT", "HR", "Finance"] },
325+ ];
326+
327+ const result = await processGridQuery("Show me IT employees", columns);
328+ ```
297329
298330### Common Issues
299331
0 commit comments