|
| 1 | +$(() => { |
| 2 | + const deployment = 'gpt-4o-mini'; |
| 3 | + const apiVersion = '2024-02-01'; |
| 4 | + const endpoint = 'https://public-api.devexpress.com/demo-openai'; |
| 5 | + const apiKey = 'DEMO'; |
| 6 | + |
| 7 | + const aiService = new AzureOpenAI({ |
| 8 | + dangerouslyAllowBrowser: true, |
| 9 | + deployment, |
| 10 | + endpoint, |
| 11 | + apiVersion, |
| 12 | + apiKey, |
| 13 | + }); |
| 14 | + |
| 15 | + async function getAIResponse(messages, signal, responseSchema) { |
| 16 | + const params = { |
| 17 | + messages, |
| 18 | + model: deployment, |
| 19 | + max_tokens: 1000, |
| 20 | + temperature: 0.7, |
| 21 | + }; |
| 22 | + |
| 23 | + params.response_format = { |
| 24 | + type: 'json_schema', |
| 25 | + json_schema: { |
| 26 | + name: 'grid_assistant_response', |
| 27 | + strict: true, |
| 28 | + schema: responseSchema, |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + const response = await aiService.chat.completions |
| 33 | + .create(params, { signal }); |
| 34 | + const result = response.choices[0].message?.content; |
| 35 | + |
| 36 | + return result; |
| 37 | + } |
| 38 | + |
| 39 | + async function getAIResponseRecursive(messages, signal, responseSchema) { |
| 40 | + return getAIResponse(messages, signal, responseSchema) |
| 41 | + .catch(async (error) => { |
| 42 | + if (!error.message.includes('Connection error')) { |
| 43 | + return Promise.reject(error); |
| 44 | + } |
| 45 | + |
| 46 | + DevExpress.ui.notify({ |
| 47 | + message: 'Our demo AI service reached a temporary request limit. Retrying in 30 seconds.', |
| 48 | + width: 'auto', |
| 49 | + type: 'error', |
| 50 | + displayTime: 5000, |
| 51 | + }); |
| 52 | + |
| 53 | + await new Promise((resolve) => setTimeout(resolve, 30000)); |
| 54 | + |
| 55 | + return getAIResponseRecursive(messages, signal, responseSchema); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + const aiIntegration = new DevExpress.aiIntegration.AIIntegration({ |
| 60 | + sendRequest({ prompt, data }) { |
| 61 | + const isValidRequest = JSON.stringify(prompt.user).length < 5000; |
| 62 | + if (!isValidRequest) { |
| 63 | + return { |
| 64 | + promise: Promise.reject(new Error('Request is too long. Specify a shorter prompt.')), |
| 65 | + abort: () => {}, |
| 66 | + }; |
| 67 | + } |
| 68 | + const controller = new AbortController(); |
| 69 | + const signal = controller.signal; |
| 70 | + |
| 71 | + const aiPrompt = [ |
| 72 | + { role: 'system', content: prompt.system }, |
| 73 | + { role: 'user', content: prompt.user }, |
| 74 | + ]; |
| 75 | + const promise = getAIResponseRecursive(aiPrompt, signal, data?.responseSchema); |
| 76 | + |
| 77 | + const result = { |
| 78 | + promise, |
| 79 | + abort: () => { |
| 80 | + controller.abort(); |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + return result; |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + let chatInstance; |
| 89 | + |
| 90 | + $('#gridContainer').dxDataGrid({ |
| 91 | + dataSource: sales, |
| 92 | + showBorders: true, |
| 93 | + keyExpr: 'Id', |
| 94 | + searchPanel: { |
| 95 | + visible: true, |
| 96 | + width: 240, |
| 97 | + placeholder: 'Search...', |
| 98 | + }, |
| 99 | + groupPanel: { |
| 100 | + visible: true, |
| 101 | + }, |
| 102 | + headerFilter: { |
| 103 | + visible: true, |
| 104 | + }, |
| 105 | + filterRow: { |
| 106 | + visible: true, |
| 107 | + }, |
| 108 | + paging: { |
| 109 | + pageSize: 10, |
| 110 | + }, |
| 111 | + pager: { |
| 112 | + visible: true, |
| 113 | + allowedPageSizes: [10, 25, 50, 100], |
| 114 | + showPageSizeSelector: true, |
| 115 | + }, |
| 116 | + aiAssistant: { |
| 117 | + enabled: true, |
| 118 | + aiIntegration, |
| 119 | + chat: { |
| 120 | + onInitialized(e) { |
| 121 | + chatInstance = e.component; |
| 122 | + }, |
| 123 | + suggestions: { |
| 124 | + items: [ |
| 125 | + { |
| 126 | + text: '💡 Help', |
| 127 | + prompt: `💡 The DataGrid AI Assistant allows you to control the component using natural language. You can execute commands such as the following: |
| 128 | + • Sort records |
| 129 | + • Apply a filter |
| 130 | + • Search for a specific value |
| 131 | + • Group records by a field |
| 132 | + • Focus and select rows |
| 133 | + • Modify paging settings |
| 134 | + • Pin, resize, and reorder columns |
| 135 | + • Configure data summaries |
| 136 | + • Pick a suggestion or enter a custom request to get started.`, |
| 137 | + }, |
| 138 | + { |
| 139 | + text: '🔍 Filter Sector by Health', |
| 140 | + prompt: 'Filter Sector by Health', |
| 141 | + }, |
| 142 | + { |
| 143 | + text: '↕️ Sort by Region', |
| 144 | + prompt: 'Sort by Region', |
| 145 | + }, |
| 146 | + { |
| 147 | + text: '🧩 Group by Product', |
| 148 | + prompt: 'Group by Product', |
| 149 | + width: 170, |
| 150 | + }, |
| 151 | + ], |
| 152 | + onItemClick(e) { |
| 153 | + const { prompt, text } = e.itemData; |
| 154 | + |
| 155 | + if (text === '💡 Help') { |
| 156 | + const message = { |
| 157 | + id: Date.now(), |
| 158 | + timestamp: new Date(), |
| 159 | + author: { id: 'user' }, |
| 160 | + text: prompt, |
| 161 | + }; |
| 162 | + |
| 163 | + chatInstance.getDataSource().store().push([{ type: 'insert', data: message }]); |
| 164 | + } else { |
| 165 | + chatInstance.option('inputFieldText', prompt); |
| 166 | + } |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + columns: [ |
| 172 | + { |
| 173 | + dataField: 'Product', |
| 174 | + }, |
| 175 | + { |
| 176 | + dataField: 'Amount', |
| 177 | + caption: 'Sale Amount', |
| 178 | + dataType: 'number', |
| 179 | + format: 'currency', |
| 180 | + }, |
| 181 | + { |
| 182 | + dataField: 'Region', |
| 183 | + dataType: 'string', |
| 184 | + }, |
| 185 | + { |
| 186 | + dataField: 'Sector', |
| 187 | + dataType: 'string', |
| 188 | + }, |
| 189 | + { |
| 190 | + dataField: 'SaleDate', |
| 191 | + dataType: 'date', |
| 192 | + }, |
| 193 | + { |
| 194 | + dataField: 'Customer', |
| 195 | + dataType: 'string', |
| 196 | + }, |
| 197 | + ], |
| 198 | + }); |
| 199 | +}); |
0 commit comments