DevExtreme Angular - 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-angular/common/ai-integration';
To use REST APIs to connect to an AI service provider, implement the HttpClient service within aiIntegration.aiProvider.sendRequest:
- import { Component } from '@angular/core';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- import { AIIntegration } from 'devextreme-angular/common/ai-integration';
- // ...
- export class AppComponent {
- constructor(private http: HttpClient) {}
- provider = {
- sendRequest: ({ prompt }) => {
- const headers = new HttpHeaders({
- // Add any custom headers here
- });
- const body = JSON.stringify({ prompt });
- const promise = this.http
- .post<{ output?: string }>('https://example.org/post', body, {
- headers,
- signal: controller.signal,
- })
- .toPromise()
- .then((result) => result.output || '');
- return {
- promise,
- abort: () => {
- // Add an abort request
- },
- };
- },
- };
- 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 { Component } from '@angular/core';
- import { AIIntegration } from 'devextreme-angular/common/ai-integration';
- import { AzureOpenAI } from 'openai';
- // ...
- export class AppComponent {
- private deployment = 'my-deployment-name';
- private aiService = new AzureOpenAI({
- dangerouslyAllowBrowser: true, // Disable in production
- deployment,
- apiVersion: 'version',
- endpoint: 'https://example.com/endpoint',
- apiKey: 'AZURE_OPENAI_API_KEY',
- });
- 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: this.deployment,
- max_completion_tokens: 1024,
- };
- const promise = this.aiService.chat.completions.create(chatParams, { signal })
- .then((response) => response.choices[0].message?.content ?? '');
- return {
- promise,
- abort: () => {
- controller.abort();
- },
- };
- },
- };
- aiIntegration = new AIIntegration(this.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.