-
Notifications
You must be signed in to change notification settings - Fork 663
HtmlEditor and Chat AI Demos: Create an AI service to make demo code more readable #32154
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
marker-dao
wants to merge
4
commits into
DevExpress:26_1
Choose a base branch
from
marker-dao:26_1_ai_demos_editors_refactoring
base: 26_1
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.
+355
−325
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
33 changes: 33 additions & 0 deletions
33
apps/demos/Demos/Chat/AIAndChatbotIntegration/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,33 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { AzureOpenAI } from 'openai'; | ||
|
|
||
| @Injectable() | ||
| export class AiService { | ||
| chatService: AzureOpenAI; | ||
|
|
||
| AzureOpenAIConfig = { | ||
| dangerouslyAllowBrowser: true, | ||
| deployment: 'gpt-4o-mini', | ||
| apiVersion: '2024-02-01', | ||
| endpoint: 'https://public-api.devexpress.com/demo-openai', | ||
| apiKey: 'DEMO', | ||
| }; | ||
|
|
||
| constructor() { | ||
| this.chatService = new AzureOpenAI(this.AzureOpenAIConfig); | ||
| } | ||
|
|
||
| async getAIResponse(messages: any[]) { | ||
| const params = { | ||
| messages, | ||
| model: this.AzureOpenAIConfig.deployment, | ||
| max_tokens: 1000, | ||
| temperature: 0.7, | ||
| }; | ||
|
|
||
| const response = await this.chatService.chat.completions.create(params); | ||
| const data = { choices: response.choices }; | ||
|
|
||
| return data.choices[0].message?.content; | ||
| } | ||
| } |
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
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
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
41 changes: 41 additions & 0 deletions
41
apps/demos/Demos/Chat/AIAndChatbotIntegration/React/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,41 @@ | ||
| import { AzureOpenAI, OpenAI } from 'openai'; | ||
| import type { AIResponse } from 'devextreme/common/ai-integration'; | ||
|
|
||
| type Message = (OpenAI.ChatCompletionUserMessageParam | OpenAI.ChatCompletionAssistantMessageParam) & { | ||
| content: string; | ||
| }; | ||
|
|
||
| export const AzureOpenAIConfig = { | ||
| dangerouslyAllowBrowser: true, | ||
| deployment: 'gpt-4o-mini', | ||
| apiVersion: '2024-02-01', | ||
| endpoint: 'https://public-api.devexpress.com/demo-openai', | ||
| apiKey: 'DEMO', | ||
| }; | ||
|
|
||
| const chatService = new AzureOpenAI(AzureOpenAIConfig); | ||
|
|
||
| const wait = (delay: number): Promise<void> => | ||
| new Promise((resolve): void => { | ||
| setTimeout(resolve, delay); | ||
| }); | ||
|
|
||
| export async function getAIResponse(messages: Message[], delay?: number): Promise<AIResponse> { | ||
| 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 }; | ||
|
|
||
| if (delay) { | ||
| await wait(delay); | ||
| } | ||
|
|
||
| return data.choices[0].message?.content ?? ''; | ||
| } | ||
|
|
||
| export type { Message }; | ||
34 changes: 2 additions & 32 deletions
34
apps/demos/Demos/Chat/AIAndChatbotIntegration/React/useApi.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
7 changes: 0 additions & 7 deletions
7
apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/data.js
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
28 changes: 28 additions & 0 deletions
28
apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/service.js
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,28 @@ | ||
| import { AzureOpenAI } from 'openai'; | ||
|
|
||
| export const AzureOpenAIConfig = { | ||
| dangerouslyAllowBrowser: true, | ||
| deployment: 'gpt-4o-mini', | ||
| apiVersion: '2024-02-01', | ||
| endpoint: 'https://public-api.devexpress.com/demo-openai', | ||
| apiKey: 'DEMO', | ||
| }; | ||
|
Comment on lines
+3
to
+9
|
||
| const chatService = new AzureOpenAI(AzureOpenAIConfig); | ||
| const wait = (delay) => | ||
| new Promise((resolve) => { | ||
| setTimeout(resolve, delay); | ||
| }); | ||
| export async function getAIResponse(messages, delay) { | ||
| 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 }; | ||
| if (delay) { | ||
| await wait(delay); | ||
| } | ||
| return data.choices[0].message?.content ?? ''; | ||
| } | ||
25 changes: 2 additions & 23 deletions
25
apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/useApi.js
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
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
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
26 changes: 26 additions & 0 deletions
26
apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/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,26 @@ | ||
| import { AzureOpenAI } from 'openai'; | ||
| import { type DxChatTypes } from 'devextreme-vue/chat'; | ||
|
|
||
| const AzureOpenAIConfig = { | ||
| dangerouslyAllowBrowser: true, | ||
| deployment: 'gpt-4o-mini', | ||
| apiVersion: '2024-02-01', | ||
| endpoint: 'https://public-api.devexpress.com/demo-openai', | ||
| apiKey: 'DEMO', | ||
| }; | ||
|
|
||
| const chatService = new AzureOpenAI(AzureOpenAIConfig); | ||
|
|
||
| export async function getAIResponse(messages: DxChatTypes.Message[]): Promise<string> { | ||
| const params: Record<string, any> = { | ||
| messages, | ||
| model: AzureOpenAIConfig.deployment, | ||
| max_tokens: 1000, | ||
| temperature: 0.7, | ||
| }; | ||
|
|
||
| const response = await chatService.chat.completions.create(params as any); | ||
| const data = { choices: response.choices }; | ||
|
|
||
| return data.choices[0].message?.content || ''; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AzureOpenAIConfig is exported but not used externally based on the data.ts changes. Remove the export keyword to avoid exposing internal implementation details.