Methods in React AI AssistView component
28 Aug 202514 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 a String
You can add a response as a string by passing it as an argument to the addPromptResponse('Response') method. This will append the response to the last prompt added to the conversation.
import { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef(null);
const addDynamicResponse = () => {
assistInstance.current.addPromptResponse('Dynamic response');
}
const onPromptRequest = (args) => {
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.';
assistInstance.current.addPromptResponse(defaultResponse);
}, 1000);
};
return (
<div className='aiAssistContainer'>
<button id="addStringResponse" onClick={addDynamicResponse}>Add String Response</button>
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}></AIAssistViewComponent>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { AIAssistViewComponent, PromptRequestEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef<AIAssistViewComponent>(null);
const addDynamicResponse = () => {
assistInstance.current.addPromptResponse('Dynamic response');
}
const onPromptRequest = (args: PromptRequestEventArgs) => {
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.';
assistInstance.current.addPromptResponse(defaultResponse);
}, 1000);
};
return (
<div className='aiAssistContainer'>
<button id="addStringResponse" onClick={addDynamicResponse}>Add String Response</button>
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}></AIAssistViewComponent>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));/* Represents the styles for loader */
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.aiAssistContainer {
height: 350px;
width: 650px;
margin: 20px auto;
}
#addStringResponse {
margin-bottom: 10px;
}Adding Responses as object.
You can add an object response by passing the prompt and response as a collection to the addPromptResponse({prompt: 'Prompt text', response: 'Response text'}) method. This will add a new prompt and its corresponding response to the AI AssistView.
import { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef(null);
const addDynamicResponse = () => {
assistInstance.current.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.' });
}
const onPromptRequest = (args) => {
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.';
assistInstance.current.addPromptResponse(defaultResponse);
}, 1000);
};
return (
<div className='aiAssistContainer'>
<button id="addObjectResponse" onClick={addDynamicResponse}>Add Object Response</button>
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}></AIAssistViewComponent>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { AIAssistViewComponent, PromptRequestEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef<AIAssistViewComponent>(null);
const addDynamicResponse = () => {
assistInstance.current.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.' });
}
const onPromptRequest = (args: PromptRequestEventArgs) => {
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.';
assistInstance.current.addPromptResponse(defaultResponse);
}, 1000);
};
return (
<div className='aiAssistContainer'>
<button id="addObjectResponse" onClick={addDynamicResponse}>Add Object Response</button>
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}></AIAssistViewComponent>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));/* Represents the styles for loader */
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.aiAssistContainer {
height: 350px;
width: 650px;
margin: 20px auto;
}
#addObjectResponse {
margin-bottom: 10px;
}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 { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef(null);
const executePrompt = () => {
assistInstance.current.executePrompt('What is the current temperature?');
}
const onPromptRequest = (args) => {
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.';
assistInstance.current.addPromptResponse(defaultResponse);
}, 1000);
};
return (
<div className='aiAssistContainer'>
<button id="executePrompt" onClick={executePrompt}>Execute Prompt</button>
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}></AIAssistViewComponent>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { AIAssistViewComponent, PromptRequestEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef<AIAssistViewComponent>(null);
const executePrompt = () => {
assistInstance.current.executePrompt('What is the current temperature?');
}
const onPromptRequest = (args: PromptRequestEventArgs) => {
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.';
assistInstance.current.addPromptResponse(defaultResponse);
}, 1000);
};
return (
<div className='aiAssistContainer'>
<button id="executePrompt" onClick={executePrompt}>Execute Prompt</button>
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest}></AIAssistViewComponent>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));/* Represents the styles for loader */
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.aiAssistContainer {
height: 350px;
width: 650px;
margin: 20px auto;
}
#executePrompt {
margin-bottom: 10px;
}