DevExtreme React - 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:
- import { AIIntegration } from 'devextreme-react/common/ai-integration';
To use REST APIs to connect to an AI service provider, implement the fetch API within aiIntegration.aiProvider.sendRequest:
- import { AIIntegration } from 'devextreme-react/common/ai-integration';
- const provider = {
- sendRequest: ({ prompt }) => {
- const headers = {
- 'Content-Type': 'application/json',
- // Add any custom headers here
- };
- const promise = fetch('https://example.org/post', {
- method: 'POST',
- headers,
- body: JSON.stringify({ prompt }),
- signal: controller.signal,
- })
- .then(async (response) => {
- const result = await response.json();
- return result.output || '';
- });
- return {
- promise,
- abort: () => {
- // Add an abort request
- },
- };
- },
- };
- const aiIntegration = new AIIntegration(provider);
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:
- import { AIIntegration } from 'devextreme-react/common/ai-integration';
- import { AzureOpenAI } from 'openai';
- 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 provider = {
- sendRequest: ({ prompt }) => {
- const aiPrompt = [
- { role: 'system', content: prompt.system },
- { role: 'user', content: 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();
- },
- };
- },
- };
- const aiIntegration = new AIIntegration(provider);
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.