-
Notifications
You must be signed in to change notification settings - Fork 670
Add Chat - Custom AI Assistant demo Angular #35219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
16adianay
wants to merge
8
commits into
DevExpress:feature/demo_custom_AI_assistant
Choose a base branch
from
16adianay:feature/demo_custom_AI_assistant_Angular
base: feature/demo_custom_AI_assistant
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ff5e55
Add Chat - Custom AI Assistant demo Angular
16adianay f889c0b
Fix: unused vars, color variables, CDR
16adianay dc64288
Update ai-assistant.component.ts
16adianay 989e174
Update grid-commands.ts
16adianay 4896482
Update types
16adianay 6ae6a9c
Update ai-assistant.component.ts
16adianay c27923a
Fixed styles, ChatCommandError, onOptionsChanged
16adianay 2f6eeb0
Fix comments
16adianay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
apps/demos/Demos/Chat/CustomAIAssistant/Angular/app/ai/ai.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { AzureOpenAI, type OpenAI } from 'openai'; | ||
| import { Injectable } from '@angular/core'; | ||
| import notify from 'devextreme/ui/notify'; | ||
| import { AIIntegration } from 'devextreme-angular/common/ai-integration'; | ||
| import type { RequestParams, AIResponse } from 'devextreme-angular/common/ai-integration'; | ||
| import { AI_SERVICE_CONFIG, ChatCommandError } from '../data'; | ||
|
|
||
| async function getAIResponse( | ||
| aiService: AzureOpenAI, | ||
| messages: OpenAI.ChatCompletionMessageParam[], | ||
| signal: AbortSignal, | ||
| ): Promise<AIResponse> { | ||
| const params = { | ||
| messages, | ||
| model: AI_SERVICE_CONFIG.deployment, | ||
| max_completion_tokens: 1000, | ||
| temperature: 0, | ||
| }; | ||
|
|
||
| const response = await aiService.chat.completions.create(params, { signal }); | ||
|
|
||
| return response.choices[0].message?.content ?? ''; | ||
| } | ||
|
|
||
| function getAIResponseRecursive( | ||
| aiService: AzureOpenAI, | ||
| messages: OpenAI.ChatCompletionMessageParam[], | ||
| signal: AbortSignal, | ||
| ): Promise<AIResponse> { | ||
| return getAIResponse(aiService, messages, signal).catch(async (error: Error) => { | ||
| if (!error.message.includes('Connection error')) { | ||
| throw error; | ||
| } | ||
|
|
||
| notify({ | ||
| message: 'Our demo AI service reached a temporary request limit. Retrying in 30 seconds.', | ||
| width: 'auto', | ||
| type: 'error', | ||
| displayTime: 5000, | ||
| }); | ||
|
|
||
| await new Promise((resolve) => { setTimeout(resolve, 30000); }); | ||
|
|
||
| return getAIResponseRecursive(aiService, messages, signal); | ||
| }); | ||
| } | ||
|
|
||
| const aiService = new AzureOpenAI({ | ||
| dangerouslyAllowBrowser: true, | ||
| deployment: AI_SERVICE_CONFIG.deployment, | ||
| endpoint: AI_SERVICE_CONFIG.endpoint, | ||
| apiVersion: AI_SERVICE_CONFIG.apiVersion, | ||
| apiKey: AI_SERVICE_CONFIG.apiKey, | ||
| }); | ||
|
|
||
| const aiIntegration = new AIIntegration({ | ||
| sendRequest(params: RequestParams) { | ||
| const { prompt, data } = params; | ||
| const isValidRequest = JSON.stringify(prompt.user).length < 20000; | ||
|
|
||
| if (!isValidRequest) { | ||
| return { | ||
| promise: Promise.reject( | ||
| new ChatCommandError('❌ This message is too long for me to process. Please shorten it and try again.'), | ||
| ), | ||
| abort: () => {}, | ||
| }; | ||
| } | ||
|
|
||
| const controller = new AbortController(); | ||
| const { signal } = controller; | ||
|
|
||
| const isSmartPasteRequest = Array.isArray((data as { fields?: unknown[] } | undefined)?.fields); | ||
| const system = isSmartPasteRequest | ||
| ? `${prompt.system ?? ''} IMPORTANT: reply on a SINGLE line with no line breaks of any kind - use ';;;' as the only separator between fields.` | ||
| : prompt.system ?? ''; | ||
|
|
||
| const aiPrompt: OpenAI.ChatCompletionMessageParam[] = [ | ||
| { role: 'system', content: system }, | ||
| { role: 'user', content: prompt.user ?? '' }, | ||
| ]; | ||
| const promise = getAIResponseRecursive(aiService, aiPrompt, signal); | ||
|
|
||
| return { | ||
| promise, | ||
| abort: () => { | ||
| controller.abort(); | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| @Injectable() | ||
| export class AiService { | ||
| getAiIntegration(): AIIntegration { | ||
| return aiIntegration; | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
apps/demos/Demos/Chat/CustomAIAssistant/Angular/app/app.component.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .demo-container { | ||
| margin: 20px; | ||
| height: 556px; | ||
| } |
7 changes: 7 additions & 0 deletions
7
apps/demos/Demos/Chat/CustomAIAssistant/Angular/app/app.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <div class="demo-container"> | ||
| <app-employee-form [aiIntegration]="aiIntegration"></app-employee-form> | ||
| <app-task-grid></app-task-grid> | ||
| <app-ai-assistant | ||
| (messageSubmitted)="onMessageSubmitted($event)" | ||
| ></app-ai-assistant> | ||
| </div> | ||
90 changes: 90 additions & 0 deletions
90
apps/demos/Demos/Chat/CustomAIAssistant/Angular/app/app.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { bootstrapApplication } from '@angular/platform-browser'; | ||
| import { | ||
| Component, ViewChild, enableProdMode, provideZoneChangeDetection, | ||
| } from '@angular/core'; | ||
| import config from 'devextreme/core/config'; | ||
| import { loadMessages } from 'devextreme-angular/common/core/localization'; | ||
| import type { AIIntegration } from 'devextreme-angular/common/ai-integration'; | ||
| import type { DxChatTypes } from 'devextreme-angular/ui/chat'; | ||
| import { AiAssistantComponent } from './components/ai-assistant/ai-assistant.component'; | ||
| import { EmployeeFormComponent } from './components/employee-form/employee-form.component'; | ||
| import { TaskGridComponent } from './components/task-grid/task-grid.component'; | ||
| import { AiService } from './ai/ai.service'; | ||
| import { routeMessage } from './app.service'; | ||
|
|
||
| if (!/localhost/.test(document.location.host)) { | ||
| enableProdMode(); | ||
| } | ||
|
|
||
| let modulePrefix = ''; | ||
| // @ts-ignore | ||
| if (window && window.config?.packageConfigPaths) { | ||
| modulePrefix = '/app'; | ||
| } | ||
|
|
||
| config({ | ||
| editorStylingMode: 'filled', | ||
| }); | ||
|
|
||
| config({ | ||
| floatingActionButtonConfig: { | ||
| position: { | ||
| my: 'right bottom', | ||
| at: 'right bottom', | ||
| of: '#grid-container', | ||
| offset: '-16 -16', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| loadMessages({ | ||
| en: { | ||
| 'dxChat-textareaPlaceholder': 'Enter a prompt...', | ||
| }, | ||
| }); | ||
|
|
||
| @Component({ | ||
| selector: 'demo-app', | ||
| templateUrl: `.${modulePrefix}/app.component.html`, | ||
| styleUrls: [`.${modulePrefix}/app.component.css`], | ||
| providers: [AiService], | ||
| imports: [ | ||
| AiAssistantComponent, | ||
| EmployeeFormComponent, | ||
| TaskGridComponent, | ||
| ], | ||
| }) | ||
| export class AppComponent { | ||
| @ViewChild(EmployeeFormComponent) private employeeForm!: EmployeeFormComponent; | ||
|
|
||
| @ViewChild(TaskGridComponent) private taskGrid!: TaskGridComponent; | ||
|
|
||
| @ViewChild(AiAssistantComponent) private aiAssistant!: AiAssistantComponent; | ||
|
|
||
| readonly aiIntegration: AIIntegration; | ||
|
|
||
| constructor(aiService: AiService) { | ||
| this.aiIntegration = aiService.getAiIntegration(); | ||
| } | ||
|
|
||
| async onMessageSubmitted(message: DxChatTypes.TextMessage): Promise<void> { | ||
| this.aiAssistant.setDisabled(true); | ||
|
|
||
| try { | ||
| await routeMessage(message.text ?? '', { | ||
| form: this.employeeForm.formComponent, | ||
| gridInstance: this.taskGrid.gridComponent, | ||
| aiIntegration: this.aiIntegration, | ||
| pushMessage: (message) => this.aiAssistant.pushMessage(message), | ||
| }); | ||
| } finally { | ||
| this.aiAssistant.setDisabled(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bootstrapApplication(AppComponent, { | ||
| providers: [ | ||
| provideZoneChangeDetection({ eventCoalescing: true }), | ||
| ], | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.