|
3 | 3 | [](https://docs.devexpress.com/GeneralInformation/403183)
|
4 | 4 | [](#does-this-example-address-your-development-requirementsobjectives)
|
5 | 5 | <!-- default badges end -->
|
6 |
| -# Product/Platform - Task |
| 6 | +# Dashboard for ASP.NET Core -- Integrate AI Assistant based on Azure OpenAI |
7 | 7 |
|
8 |
| -This is the repository template for creating new examples. Describe the solved task here. |
| 8 | +This example is an ASP.NET Core application with integrated DevExpress Reports and an AI assistant. User requests and assistant responses are displayed on-screen using the DevExtreme [`dxChat`](https://js.devexpress.com/jQuery/Documentation/24_2/ApiReference/UI_Components/dxChat/) component. |
9 | 9 |
|
10 |
| -Put a screenshot that illustrates the result here. |
| 10 | + |
11 | 11 |
|
12 |
| -Then, add implementation details (steps, code snippets, and other technical information in a free form), or add a link to an existing document with implementation details. |
| 12 | +The AI Assistant reviews and analyzes all the data displayed in the dashboard to answer your questions. For a more focused analysis, you can select a specific dashboard item. Simply click the **Values** button in the AI Assistant custom item's caption and choose the desired widget. Any changes to dashboard data—such as updates to parameters or master filters—will automatically trigger a recreation of the AI Assistant. |
| 13 | + |
| 14 | +**Please note that AI Assistant initialization takes time. The assistant is ready for interaction once Microsoft Azure scans the source document on the server side.** |
| 15 | + |
| 16 | +## Implementation Details |
| 17 | + |
| 18 | +### Add Personal Keys |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> DevExpress AI-powered extensions follow the "bring your own key" principle. DevExpress does not offer a REST API and does not ship any built-in LLMs/SLMs. You need an active Azure/Open AI subscription to obtain the REST API endpoint, key, and model deployment name. These variables must be specified at application startup to register AI clients and enable DevExpress AI-powered Extensions in your application. |
| 22 | +
|
| 23 | +You need to create an Azure OpenAI resource in the Azure portal to use AI Assistants for DevExpress BI Dashboard. Refer to the following help topic for details: [Microsoft - Create and deploy an Azure OpenAI Service resource](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal). |
| 24 | + |
| 25 | +Once you obtain a private endpoint and an API key, register them as `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_APIKEY` environment variables. The [EnvSettings.cs](./CS/ReportingApp/EnvSettings.cs) reads these settings. `DeploymentName` in this file is a name of your Azure model, for example, `GPT4o`: |
| 26 | + |
| 27 | +```cs |
| 28 | +public static class EnvSettings { |
| 29 | + public static string AzureOpenAIEndpoint { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); } } |
| 30 | + public static string AzureOpenAIKey { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY"); } } |
| 31 | + public static string DeploymentName { get { return "GPT4o"; } } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +>[!NOTE] |
| 36 | +> Availability of Azure Open AI Assistants depends on the region. Refer to the following article for more details: [Assistants (Preview)](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models?tabs=global-standard%2Cstandard-chat-completions#assistants-preview). |
| 37 | +
|
| 38 | +Files to Review: |
| 39 | +- [EnvSettings.cs](./CS/EnvSettings.cs) |
| 40 | + |
| 41 | +### Register AI Services |
| 42 | + |
| 43 | +Register AI services in your application. Add the following code to the _Program.cs_ file: |
| 44 | + |
| 45 | +```cs |
| 46 | +using DevExpress.AIIntegration; |
| 47 | +using Azure; |
| 48 | +using Azure.AI.OpenAI; |
| 49 | +using Microsoft.Extensions.AI; |
| 50 | +using System; |
| 51 | +// ... |
| 52 | +var azureOpenAIClient = new AzureOpenAIClient( |
| 53 | + new Uri(EnvSettings.AzureOpenAIEndpoint), |
| 54 | + new AzureKeyCredential(EnvSettings.AzureOpenAIKey)); |
| 55 | + |
| 56 | +var chatClient = azureOpenAIClient.AsChatClient(EnvSettings.DeploymentName); |
| 57 | + |
| 58 | +builder.Services.AddDevExpressAI(config => |
| 59 | +{ |
| 60 | + config.RegisterOpenAIAssistants(azureOpenAIClient, EnvSettings.DeploymentName); |
| 61 | +}); |
| 62 | +// ... |
| 63 | +``` |
| 64 | + |
| 65 | +Files to Review: |
| 66 | +- [Program.cs](./CS/Program.cs) |
| 67 | + |
| 68 | +### AI Assistant Provider |
| 69 | + |
| 70 | +On the server side, the `AIAssistantProvider` service manages assistants. An `IAIAssistantFactory` instance creates assistants with keys specified in previous steps. |
| 71 | + |
| 72 | +```cs |
| 73 | +public interface IAIAssistantProvider { |
| 74 | + IAIAssistant GetAssistant(string assistantName); |
| 75 | + Task<string> CreateAssistant(AssistantType assistantType, Stream data); |
| 76 | + Task<string> CreateAssistant(AssistantType assistantType); |
| 77 | + void DisposeAssistant(string assistantName); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Files to Review: |
| 82 | +- [AIAssistantProvider.cs](./CS/Services/AIAssistantProvider.cs) |
| 83 | +- [IAIAssistantProvider.cs](./CS/IAIAssistantProvider.cs) |
| 84 | + |
| 85 | +### Create a Custom Item with AI Assistant |
| 86 | + |
| 87 | +This example implements a [custom item](https://docs.devexpress.com/Dashboard/117546/web-dashboard/advanced-customization/create-a-custom-item) based on the [`dxChat`](https://js.devexpress.com/jQuery/Documentation/Guide/UI_Components/Chat/Overview/) component. |
| 88 | + |
| 89 | +For the **AI Assistant** custom item implementation, refer to the following file: [aiChatCustomItem.js](./CS/wwwroot/js/aiChatCustomItem.js) |
| 90 | + |
| 91 | +### Register the Custom Item |
| 92 | + |
| 93 | +Register the created custom item extension in the Wen Dashboard: |
| 94 | + |
| 95 | +```html |
| 96 | +<script type="text/javascript"> |
| 97 | + // ... |
| 98 | + function handleBeforeRender(dashboardControl) { |
| 99 | + chatAIItem = new AIChatItem(dashboardControl); |
| 100 | + dashboardControl.registerExtension(chatAIItem); |
| 101 | + // ... |
| 102 | + } |
| 103 | + // ... |
| 104 | +</script> |
| 105 | + |
| 106 | +<div style="position: relative; height: calc(100vh - 55px);"> |
| 107 | +@(Html.DevExpress().Dashboard("dashboardControl1") |
| 108 | + .ControllerName("DefaultDashboard") |
| 109 | + .OnBeforeRender("handleBeforeRender") |
| 110 | + .OnDashboardInitialized("handleDashboardInitialized") |
| 111 | +) |
| 112 | +</div> |
| 113 | +``` |
| 114 | + |
| 115 | +After you registered the extension the Assistant icon appears in the Dashboard Toolbox: |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +Click the item to add an AI Assistant to the dashboard. Only one AI Assistant item is available per dashboard. You can ask the assistant questions in the Viewer mode. |
| 120 | + |
| 121 | +File to Review: |
| 122 | +- [Index.cshtml](./CS/Pages/Index.cshtml) |
| 123 | + |
| 124 | +### Communicate with Assistant |
| 125 | + |
| 126 | +Each time a user sends a message, the [`onMessageEntered`](https://js.devexpress.com/jQuery/Documentation/24_2/ApiReference/UI_Components/dxChat/Configuration/#onMessageEntered) event handler passes the request to the assistant: |
| 127 | + |
| 128 | +```js |
| 129 | +// ... |
| 130 | +getAnswer(chatId, question) { |
| 131 | + const formData = new FormData(); |
| 132 | + formData.append('chatId', chatId); |
| 133 | + formData.append('question', question); |
| 134 | + return this._tryFetch(async () => { |
| 135 | + const response = await fetch('/AIChat/GetAnswer', { |
| 136 | + method: 'POST', |
| 137 | + body: formData |
| 138 | + }); |
| 139 | + return await response.text(); |
| 140 | + }, 'GetAnswer'); |
| 141 | +} |
| 142 | +// ... |
| 143 | +async getAIResponse(question) { |
| 144 | + this.lastUserQuery = question; |
| 145 | + |
| 146 | + if(!this.chatId) |
| 147 | + this.chatId = await this.createChat(this.dashboardControl.getDashboardId(), this.dashboardControl.getDashboardState()); |
| 148 | + if(this.chatId) |
| 149 | + return await this.getAnswer(this.chatId, question); |
| 150 | +}; |
| 151 | +// ... |
| 152 | +async onMessageEntered(e) { |
| 153 | + const instance = e.component; |
| 154 | + this.component.option('alerts', []); |
| 155 | + instance.renderMessage(e.message); |
| 156 | + instance.option({ typingUsers: [assistant] }); |
| 157 | + const userInput = e.message.text + ((this.model.selectedSheet && "\nDiscuss sheet " + this.model.selectedSheet) |
| 158 | + || "\nLet's discuss all sheets"); |
| 159 | + const response = await this.getAIResponse(userInput); |
| 160 | + this.renderAssistantMessage(instance, response); |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +[`AIChatController.GetAnswer`]() receives answers from the assistant. |
13 | 165 |
|
14 | 166 | ## Files to Review
|
15 | 167 |
|
16 |
| -- link.cs (VB: link.vb) |
17 |
| -- link.js |
18 |
| -- ... |
| 168 | +- [AIAssistantProvider.cs](./CS/Services/AIAssistantProvider.cs) |
| 169 | +- [IAIAssistantProvider.cs](./CS/IAIAssistantProvider.cs) |
| 170 | +- [Program.cs](./CS/Program.cs) |
19 | 171 |
|
20 | 172 | ## Documentation
|
21 | 173 |
|
22 |
| -- link |
23 |
| -- link |
24 |
| -- ... |
| 174 | +- [AI Integration](https://docs.devexpress.com/CoreLibraries/405204/ai-powered-extensions) |
| 175 | +- [Create a Custom Item for the Web Dashboard](https://docs.devexpress.com/Dashboard/117546/web-dashboard/advanced-customization/create-a-custom-item) |
| 176 | +- [Getting Started with JavaScript/jQuery Chat](https://js.devexpress.com/jQuery/Documentation/Guide/UI_Components/Chat/Getting_Started_with_Chat/) |
25 | 177 |
|
26 | 178 | ## More Examples
|
27 | 179 |
|
28 |
| -- link |
29 |
| -- link |
30 |
| -- ... |
| 180 | +- [DevExtreme Chat - Getting Started](https://github.com/DevExpress-Examples/devextreme-getting-started-with-chat) |
| 181 | +- [Reporting for ASP.NET Core - Integrate AI Assistant based on Azure OpenAI](https://github.com/DevExpress-Examples/web-reporting-integrate-ai-assistant) |
| 182 | + |
31 | 183 | <!-- feedback -->
|
32 | 184 | ## Does this example address your development requirements/objectives?
|
33 | 185 |
|
34 | 186 | [<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=example-repository-template&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=example-repository-template&~~~was_helpful=no)
|
35 | 187 |
|
36 | 188 | (you will be redirected to DevExpress.com to submit your response)
|
37 |
| -<!-- feedback end --> |
| 189 | +<!-- feedback end --> |
0 commit comments