|
| 1 | +To synchronize Chat and OpenAI, declare the `processMessageSending()` function. This function configures [typingUsers](/Documentation/ApiReference/UI_Components/dxChat/Configuration/#typingUsers), pushes the assistant message to the store, and renders the message. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +##### jQuery |
| 6 | + |
| 7 | + <!-- tab: index.js --> |
| 8 | + async function processMessageSending() { |
| 9 | + toggleDisabledState(true); |
| 10 | + instance.option({ typingUsers: [assistant] }); |
| 11 | + try { |
| 12 | + const aiResponse = await getAIResponse(messages); |
| 13 | + setTimeout(() => { |
| 14 | + instance.option({ typingUsers: [] }); |
| 15 | + messages.push({ role: 'assistant', content: aiResponse }); |
| 16 | + renderMessage(aiResponse); |
| 17 | + }, 200); |
| 18 | + } catch { |
| 19 | + instance.option({ typingUsers: [] }); |
| 20 | + alertLimitReached(); |
| 21 | + } finally { |
| 22 | + toggleDisabledState(false); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | +##### Angular |
| 27 | + |
| 28 | + <!--TypeScript--> |
| 29 | + async processMessageSending() { |
| 30 | + this.toggleDisabledState(true); |
| 31 | + this.typingUsersSubject.next([this.assistant]); |
| 32 | + try { |
| 33 | + const aiResponse = await this.getAIResponse(this.messages); |
| 34 | + setTimeout(() => { |
| 35 | + this.typingUsersSubject.next([]); |
| 36 | + this.messages.push({ role: "assistant", content: aiResponse ?? "" }); |
| 37 | + this.renderAssistantMessage(aiResponse ?? ""); |
| 38 | + }, 200); |
| 39 | + } catch { |
| 40 | + this.typingUsersSubject.next([]); |
| 41 | + this.alertLimitReached(); |
| 42 | + } finally { |
| 43 | + this.toggleDisabledState(false); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +##### Vue |
| 48 | + |
| 49 | + <!--TypeScript--> |
| 50 | + const processMessageSending = async() => { |
| 51 | + toggleDisabledState(true); |
| 52 | + typingUsers.value = [assistant]; |
| 53 | + try { |
| 54 | + const aiResponse = await getAIResponse(messages.value); |
| 55 | + setTimeout(() => { |
| 56 | + typingUsers.value = []; |
| 57 | + messages.value.push({ role: 'assistant', content: aiResponse ?? '' }); |
| 58 | + renderAssistantMessage(aiResponse ?? ''); |
| 59 | + }, 200); |
| 60 | + } catch { |
| 61 | + typingUsers.value = []; |
| 62 | + alertLimitReached(); |
| 63 | + } finally { |
| 64 | + toggleDisabledState(false); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | +##### React |
| 69 | + |
| 70 | + <!--TypeScript--> |
| 71 | + async processMessageSending(): Promise<void> { |
| 72 | + this.toggleDisabledState(true); |
| 73 | + this.typingUsersSubject.next([this.assistant]); |
| 74 | + try { |
| 75 | + const aiResponse = await this.getAIResponse(this.messages); |
| 76 | + setTimeout(() => { |
| 77 | + this.typingUsersSubject.next([]); |
| 78 | + this.messages.push({ role: 'assistant', content: aiResponse ?? '' }); |
| 79 | + this.renderAssistantMessage(aiResponse ?? ''); |
| 80 | + }, 200); |
| 81 | + } catch { |
| 82 | + this.typingUsersSubject.next([]); |
| 83 | + this.alertLimitReached(); |
| 84 | + } finally { |
| 85 | + this.toggleDisabledState(false); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +Call `processMessageSending()` after a user message is sent in the [onMessageEntered](/Documentation/ApiReference/UI_Components/dxChat/Configuration/#onMessageEntered) event handler: |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +##### jQuery |
| 96 | + |
| 97 | + <!-- tab: index.js --> |
| 98 | + const instance = $('#dx-ai-chat').dxChat({ |
| 99 | + // ... |
| 100 | + dataSource: customStore, |
| 101 | + reloadOnChange: false, |
| 102 | + onMessageEntered: (e) => { |
| 103 | + const { message } = e; |
| 104 | + customStore.push([{ type: 'insert', data: { id: Date.now(), ...message } }]); |
| 105 | + messages.push({ role: 'user', content: message.text }); |
| 106 | + processMessageSending(); |
| 107 | + } |
| 108 | + }).dxChat('instance'); |
| 109 | + |
| 110 | +##### Angular |
| 111 | + |
| 112 | + <!--TypeScript--> |
| 113 | + async onMessageEntered({ message, event }: MessageEnteredEvent) { |
| 114 | + this.dataSource |
| 115 | + ?.store() |
| 116 | + .push([{ type: "insert", data: { id: Date.now(), ...message } }]); |
| 117 | + |
| 118 | + this.messages.push({ role: "user", content: message?.text ?? "" }); |
| 119 | + this.processMessageSending(); |
| 120 | + } |
| 121 | + |
| 122 | +##### Vue |
| 123 | + |
| 124 | + <!--TypeScript--> |
| 125 | + const onMessageEntered = async(e: MessageEnteredEvent) => { |
| 126 | + let { message } = e; |
| 127 | + dataSource.value?.store().push([{ |
| 128 | + type: 'insert', |
| 129 | + data: { id: Date.now(), ...message } |
| 130 | + }]); |
| 131 | + |
| 132 | + messages.value.push({ role: 'user', content: message?.text ?? '' }); |
| 133 | + await processMessageSending(); |
| 134 | + }; |
| 135 | + |
| 136 | +##### React |
| 137 | + |
| 138 | + <!--TypeScript--> |
| 139 | + onMessageEntered({ message }: MessageEnteredEvent): void { |
| 140 | + this.dataSource |
| 141 | + ?.store() |
| 142 | + .push([{ type: 'insert', data: { id: Date.now(), ...message } }]); |
| 143 | + |
| 144 | + this.messages.push({ role: 'user', content: message?.text ?? '' }); |
| 145 | + void this.processMessageSending(); |
| 146 | + } |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +You can also implement additional UI capabilities to further improve user experience: |
| 151 | + |
| 152 | +- Add a Markdown converter for assistant outputs. For more information, refer to the [Markdown Support](/Documentation/Guide/UI_Components/Chat/Markdown_Support/) help topic. |
| 153 | +- Define a [messageTemplate](/Documentation/ApiReference/UI_Components/dxChat/Configuration/#messageTemplate) for the assistant’s responses and add two buttons: copy and regenerate response. See the example code in the GitHub repository: |
| 154 | + |
| 155 | +#include btn-open-github with { |
| 156 | + href: "https://github.com/DevExpress-Examples/devextreme-chat-openai-integration" |
| 157 | +} |
0 commit comments