DevExtreme jQuery/JS - aiIntegration Setup
The AIIntegration type powers AI features in the following components:
- DataGrid
- TreeList
- Form
- HTML Editor
You can configure one aiIntegration property in your application and implement the variable in multiple AI-enabled DevExtreme components. Alternatively, you can configure multiple properties to define AI settings specific to individual components/features.
AI Service Provider Requirements
DevExtreme AI-powered features support all AI service providers that offer REST or client APIs. You can also use self-hosted AI agents that have access to your application's data.
Connect to an AI Service Provider
To configure AIIntegration, import the DevExtreme AI integration module into your project:
- <head>
- <!-- ... -->
- <script type="text/javascript" src="../node_modules/devextreme-dist/js/dx.ai-integration.js" charset="utf-8"></script>
- <!-- or if using CDN -->
- <script src="https://cdn.jsdelivr.net/npm/devextreme-dist@26.1.6/js/dx.ai-integration.js"></script>
- </head>
To use REST APIs to connect to an AI service provider, implement the fetch API within aiIntegration.aiProvider.sendRequest:
- const aiIntegration = new DevExpress.aiIntegration.AIIntegration({
- sendRequest(params) {
- const promise = fetch('https://example.org/post', {
- method: 'POST',
- headers: { ... }, // Add custom headers here, including API authentication headers
- body: JSON.stringify(params.prompt),
- }).then(async (response) => {
- const result = await response.json();
- return result.output || '';
- })
- return {
- promise,
- abort: () => {
- // Add an abort request
- },
- };
- },
- });
You can also use third-party libraries to connect to an AI service provider. The following code snippet uses the OpenAI NPM package to connect to Azure OpenAI:
- <head>
- <!-- ... -->
- <script type="text/javascript" src="../node_modules/openai/index.min.js" charset="utf-8"></script>
- <!-- or if using CDN -->
- <script src="https://cdn.jsdelivr.net/npm/openai/index.min.js"></script>
- </head>
- const deployment = 'my-deployment-name';
- const aiService = new AzureOpenAI({
- dangerouslyAllowBrowser: true, // Disable in production
- deployment,
- apiVersion: 'version',
- endpoint: 'https://example.com/endpoint',
- apiKey: 'AZURE_OPENAI_API_KEY',
- });
- const aiIntegration = new DevExpress.aiIntegration.AIIntegration({
- sendRequest(params) {
- const aiPrompt = [
- { role: 'system', content: params.prompt.system },
- { role: 'user', content: params.prompt.user },
- ];
- const controller = new AbortController();
- const signal = controller.signal;
- const chatParams = {
- messages: aiPrompt,
- model: deployment,
- max_completion_tokens: 1024,
- }
- const promise = aiService.chat.completions.create(chatParams, { signal })
- .then((response) => response.choices[0].message?.content ?? '');
- return {
- promise,
- abort: () => {
- controller.abort();
- },
- };
- },
- });
dangerouslyAllowBrowser: true enables browser-side requests. This exposes your API key. For production, route requests through your backend.For additional AI integration examples, refer to Overview of AI-powered Features - Demos.