diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts index 463e986e9ed..da9d735f27d 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts @@ -105,6 +105,7 @@ export class AppService { async getAIResponse(messages) { const params = { messages, + model: this.AzureOpenAIConfig.deployment, max_tokens: 1000, temperature: 0.7, }; diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/App.tsx b/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/App.tsx index 4cba5ee4f43..78e60d3e670 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/App.tsx +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/App.tsx @@ -31,6 +31,7 @@ const chatService = new AzureOpenAI(AzureOpenAIConfig); async function getAIResponse(messages) { const params = { messages, + model: AzureOpenAIConfig.deployment, max_tokens: 1000, temperature: 0.7, }; diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/App.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/App.js index 8f898bb238c..c6754f563b6 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/App.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/App.js @@ -4,19 +4,18 @@ import { AzureOpenAI } from 'openai'; import CustomStore from 'devextreme/data/custom_store'; import DataSource from 'devextreme/data/data_source'; import { loadMessages } from 'devextreme/localization'; -import { +import { user, assistant, AzureOpenAIConfig, REGENERATION_TEXT, CHAT_DISABLED_CLASS, - ALERT_TIMEOUT + ALERT_TIMEOUT, } from './data.js'; import Message from './Message.js'; const store = []; const messages = []; - loadMessages({ en: { 'dxChat-emptyListMessage': 'Chat is Empty', @@ -24,33 +23,29 @@ loadMessages({ 'dxChat-textareaPlaceholder': 'Ask AI Assistant...', }, }); - const chatService = new AzureOpenAI(AzureOpenAIConfig); - async function getAIResponse(messages) { const params = { messages, + model: AzureOpenAIConfig.deployment, max_tokens: 1000, temperature: 0.7, }; - const response = await chatService.chat.completions.create(params); const data = { choices: response.choices }; - return data.choices[0].message?.content; } - function updateLastMessage(text = REGENERATION_TEXT) { const items = dataSource.items(); const lastMessage = items.at(-1); - - dataSource.store().push([{ - type: 'update', - key: lastMessage.id, - data: { text }, - }]); + dataSource.store().push([ + { + type: 'update', + key: lastMessage.id, + data: { text }, + }, + ]); } - function renderAssistantMessage(text) { const message = { id: Date.now(), @@ -58,68 +53,54 @@ function renderAssistantMessage(text) { author: assistant, text, }; - dataSource.store().push([{ type: 'insert', data: message }]); } - const customStore = new CustomStore({ key: 'id', - load: () => { - return new Promise((resolve) => { - setTimeout(() => { - resolve([...store]); - }, 0); - }); - }, - insert: (message) => { - return new Promise((resolve) => { - setTimeout(() => { - store.push(message); - resolve(message); - }); + load: () => new Promise((resolve) => { + setTimeout(() => { + resolve([...store]); + }, 0); + }), + insert: (message) => new Promise((resolve) => { + setTimeout(() => { + store.push(message); + resolve(message); }); - }, + }), }); - const dataSource = new DataSource({ store: customStore, paginate: false, -}) - +}); export default function App() { const [alerts, setAlerts] = useState([]); const [typingUsers, setTypingUsers] = useState([]); const [classList, setClassList] = useState(''); - function alertLimitReached() { - setAlerts([{ - message: 'Request limit reached, try again in a minute.' - }]); - + setAlerts([ + { + message: 'Request limit reached, try again in a minute.', + }, + ]); setTimeout(() => { setAlerts([]); }, ALERT_TIMEOUT); } - function toggleDisabledState(disabled, event = undefined) { setClassList(disabled ? CHAT_DISABLED_CLASS : ''); - if (disabled) { event?.target.blur(); } else { event?.target.focus(); } - }; - + } async function processMessageSending(message, event) { toggleDisabledState(true, event); - messages.push({ role: 'user', content: message.text }); setTypingUsers([assistant]); - try { const aiResponse = await getAIResponse(messages); - setTimeout(() => { setTypingUsers([]); messages.push({ role: 'assistant', content: aiResponse }); @@ -133,13 +114,10 @@ export default function App() { toggleDisabledState(false, event); } } - async function regenerate() { toggleDisabledState(true); - try { const aiResponse = await getAIResponse(messages.slice(0, -1)); - updateLastMessage(aiResponse); messages.at(-1).content = aiResponse; } catch { @@ -149,20 +127,16 @@ export default function App() { toggleDisabledState(false); } } - function onMessageEntered({ message, event }) { dataSource.store().push([{ type: 'insert', data: { id: Date.now(), ...message } }]); - if (!alerts.length) { processMessageSending(message, event); } } - function onRegenerateButtonClick() { updateLastMessage(); regenerate(); } - return ( {REGENERATION_TEXT}; - } - - function onCopyButtonClick() { - navigator.clipboard?.writeText(message.text); - setIcon('check'); - - setTimeout(() => { - setIcon('copy'); - }, 2500); - } - - return ( - -
- {HTMLReactParser(convertToHtml(message.text))} -
-
-
-
- ) + const [icon, setIcon] = useState('copy'); + if (message.text === REGENERATION_TEXT) { + return {REGENERATION_TEXT}; + } + function onCopyButtonClick() { + navigator.clipboard?.writeText(message.text); + setIcon('check'); + setTimeout(() => { + setIcon('copy'); + }, 2500); + } + return ( + +
+ {HTMLReactParser(convertToHtml(message.text))} +
+
+
+
+ ); } - export default Message; diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/data.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/data.js index 3a66e2b837f..8478f018409 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/data.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/data.js @@ -4,16 +4,13 @@ export const AzureOpenAIConfig = { apiVersion: '2024-02-01', endpoint: 'https://public-api.devexpress.com/demo-openai', apiKey: 'DEMO', -} - +}; export const REGENERATION_TEXT = 'Regeneration...'; export const CHAT_DISABLED_CLASS = 'dx-chat-disabled'; export const ALERT_TIMEOUT = 1000 * 60; - export const user = { id: 'user', }; - export const assistant = { id: 'assistant', name: 'Virtual Assistant', diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/index.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/index.js index d9d7442ce76..b853e0be824 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/index.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/index.js @@ -1,9 +1,5 @@ import React from 'react'; import ReactDOM from 'react-dom'; - import App from './App.js'; -ReactDOM.render( - , - document.getElementById('app'), -); +ReactDOM.render(, document.getElementById('app')); diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue index 7d79c37f638..46bc2fb98fc 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue @@ -53,10 +53,9 @@ import { ref, onBeforeMount } from 'vue'; import DxChat from 'devextreme-vue/chat'; import DxButton from 'devextreme-vue/button'; import { loadMessages } from 'devextreme/localization'; -import { AzureOpenAI } from 'openai'; +import openai from 'openai'; import { dictionary, - store, messages, user, assistant, @@ -67,7 +66,7 @@ import { ALERT_TIMEOUT, } from './data.ts'; -const chatService = new AzureOpenAI(AzureOpenAIConfig); +const chatService = new openai.AzureOpenAI(AzureOpenAIConfig); const typingUsers = ref([]); const alerts = ref([]); @@ -81,6 +80,7 @@ onBeforeMount(() => { async function getAIResponse(messages) { const params = { messages, + model: AzureOpenAIConfig.deployment, max_tokens: 1000, temperature: 0.7, }; @@ -91,7 +91,7 @@ async function getAIResponse(messages) { return data.choices[0].message?.content; } -function toggleDisabledState(disabled, event) { +function toggleDisabledState(disabled, event = undefined) { isDisabled.value = disabled; if (disabled) { diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/data.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/data.ts index c4a995f167f..fd8554b8202 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/data.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/data.ts @@ -26,8 +26,7 @@ export const CHAT_DISABLED_CLASS = 'dx-chat-disabled'; export const ALERT_TIMEOUT = 1000 * 60; export const user = { - id: 'c94c0e76-fb49-4b9b-8f07-9f93ed93b4f3', - name: 'John Doe', + id: 'user', }; export const assistant = { diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js index 861136fb745..1c32677df51 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js @@ -21,6 +21,7 @@ $(() => { async function getAIResponse(messages) { const params = { messages, + model: deployment, max_tokens: 1000, temperature: 0.7, };