Methods in EJ2 TypeScript AI AssistView control
13 Dec 202410 minutes to read
Adding prompt response
You can use the addPromptResponse public method to add the prompts and responses to the AI AssistView. You can add the it either as a string or object collection.
Adding responses as string.
You can add string response, by passing it as argument for the addPromptResponse('Response') method which adds as the response of last added prompt.
import { AIAssistView, ToolbarItemClickedEventArgs } from '@syncfusion/ej2-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
// Initializes the AI Assist control
let aiAssistView: AIAssistView = new AIAssistView({
promptRequest: () => {
setTimeout(() => {
let defaultResponse = 'For real-time prompt processing, connect the AIAssistView component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
aiAssistView.addPromptResponse(defaultResponse);
}, 1000);
}
});
// Render initialized AI Assist.
aiAssistView.appendTo('#response-string');
document.addEventListener('click', (event) => {
if (event.target && (event.target as HTMLElement).id === 'addStringResponse') {
aiAssistView.addPromptResponse('Dynamic response');
}
});<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 AI AssistView</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript AI AssistView Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-notifications/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<style>
#addStringResponse {
margin-bottom: 10px;
}
</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container' style="height: 350px; width: 650px;">
<button id="addStringResponse">Add String Response</button>
<div id="response-string"></div>
</div>
</body>
</html>Adding responses as object.
You can add object response, by passing the prompt and response as a collection as argument for the addPromptResponse({prompt: 'Prompt text', response: 'Response text'}) method which adds as a new prompt and response in the AI AssistView.
import { AIAssistView, PromptChangedEventArgs } from '@syncfusion/ej2-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
// Initializes the AI Assist control
let aiAssistView: AIAssistView = new AIAssistView({
promptRequest: () => {
setTimeout(() => {
let defaultResponse = 'For real-time prompt processing, connect the AIAssistView component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
aiAssistView.addPromptResponse(defaultResponse);
}, 1000);
}
});
// Render initialized AI Assist.
aiAssistView.appendTo('#response-object');
document.addEventListener('click', (event) => {
if (event.target && (event.target as HTMLElement).id === 'addObjectResponse') {
aiAssistView.addPromptResponse({ prompt: 'What is AI?', response: 'AI stands for Artificial Intelligence, enabling machines to mimic human intelligence for tasks such as learning, problem-solving, and decision-making.' });
}
});<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 AI AssistView</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript AI AssistView Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-notifications/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<style>
#addObjectResponse {
margin-bottom: 10px;
}
</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container' style="height: 350px; width: 650px;">
<button id="addObjectResponse">Add Object Response</button>
<div id="response-object"></div>
</div>
</body>
</html>Executing prompt
You can use the executePrompt method to execute the prompts dynamically in the AI AssistView. It accepts prompts as string values, which triggers the promptRequest event and performs the callback actions.
import { AIAssistView, ToolbarItemClickedEventArgs } from '@syncfusion/ej2-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
// Initializes the AI Assist control
let aiAssistView: AIAssistView = new AIAssistView({
promptRequest: () => {
setTimeout(() => {
let defaultResponse = 'For real-time prompt processing, connect the AIAssistView component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
aiAssistView.addPromptResponse(defaultResponse);
}, 1000);
}
});
// Render initialized AI Assist.
aiAssistView.appendTo('#execute-prompt');
document.addEventListener('click', (event) => {
if (event.target && (event.target as HTMLElement).id === 'executePrompt') {
aiAssistView.executePrompt('What is the current temperature?');
}
});<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 AI AssistView</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript AI AssistView Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/31.2.12/ej2-notifications/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<style>
#executePrompt {
margin-bottom: 10px;
}
</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container' style="height: 350px; width: 650px;">
<button id="executePrompt">Execute Prompt</button>
<div id="execute-prompt"></div>
</div>
</body>
</html>