Skip to content

Commit 6ad3c31

Browse files
author
marker dao ®
committed
refactor(htmleditor && chat): Vue
1 parent 3b254ce commit 6ad3c31

File tree

5 files changed

+80
-74
lines changed

5 files changed

+80
-74
lines changed

apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949

5050
<script setup lang="ts">
5151
import { ref, onBeforeMount } from 'vue';
52-
import { AzureOpenAI } from 'openai';
5352
import DxChat, { type DxChatTypes } from 'devextreme-vue/chat';
5453
import DxButton from 'devextreme-vue/button';
5554
import { loadMessages } from 'devextreme-vue/common/core/localization';
@@ -62,12 +61,10 @@ import {
6261
assistant,
6362
dataSource,
6463
convertToHtml,
65-
AzureOpenAIConfig,
6664
REGENERATION_TEXT,
6765
ALERT_TIMEOUT,
6866
} from './data.ts';
69-
70-
const chatService = new AzureOpenAI(AzureOpenAIConfig);
67+
import { getAIResponse } from './service.ts';
7168
7269
const typingUsers = ref<{ id: string, name: string }[]>([]);
7370
const alerts = ref<{ message: string }[]>([]);
@@ -78,20 +75,6 @@ onBeforeMount(() => {
7875
loadMessages(dictionary);
7976
});
8077
81-
async function getAIResponse(messages: DxChatTypes.Message[]): Promise<string> {
82-
const params: Record<string, any> = {
83-
messages,
84-
model: AzureOpenAIConfig.deployment,
85-
max_tokens: 1000,
86-
temperature: 0.7,
87-
};
88-
89-
const response = await chatService.chat.completions.create(params as any);
90-
const data = { choices: response.choices };
91-
92-
return data.choices[0].message?.content || '';
93-
}
94-
9578
function toggleDisabledState(disabled: boolean, event?: Events.EventObject): void {
9679
isDisabled.value = disabled;
9780

apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/data.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ export const dictionary = {
1414
},
1515
};
1616

17-
export const AzureOpenAIConfig = {
18-
dangerouslyAllowBrowser: true,
19-
deployment: 'gpt-4o-mini',
20-
apiVersion: '2024-02-01',
21-
endpoint: 'https://public-api.devexpress.com/demo-openai',
22-
apiKey: 'DEMO',
23-
};
24-
2517
export const REGENERATION_TEXT = 'Regeneration...';
2618
export const CHAT_DISABLED_CLASS = 'chat-disabled';
2719
export const ALERT_TIMEOUT = 1000 * 60;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { AzureOpenAI } from 'openai';
2+
import { type DxChatTypes } from 'devextreme-vue/chat';
3+
4+
const AzureOpenAIConfig = {
5+
dangerouslyAllowBrowser: true,
6+
deployment: 'gpt-4o-mini',
7+
apiVersion: '2024-02-01',
8+
endpoint: 'https://public-api.devexpress.com/demo-openai',
9+
apiKey: 'DEMO',
10+
};
11+
12+
const chatService = new AzureOpenAI(AzureOpenAIConfig);
13+
14+
export async function getAIResponse(messages: DxChatTypes.Message[]): Promise<string> {
15+
const params: Record<string, any> = {
16+
messages,
17+
model: AzureOpenAIConfig.deployment,
18+
max_tokens: 1000,
19+
temperature: 0.7,
20+
};
21+
22+
const response = await chatService.chat.completions.create(params as any);
23+
const data = { choices: response.choices };
24+
25+
return data.choices[0].message?.content || '';
26+
}

apps/demos/Demos/HtmlEditor/AITextEditing/Vue/App.vue

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,58 +33,11 @@ import {
3333
DxToolbarItem,
3434
DxCommand,
3535
} from 'devextreme-vue/html-editor';
36-
import { AIIntegration, type Response } from 'devextreme-vue/common/ai-integration';
37-
import { AzureOpenAI, OpenAI } from 'openai';
3836
import {
3937
markup,
40-
AzureOpenAIConfig,
4138
extractKeywordsPrompt,
4239
} from './data.ts';
43-
44-
type AIMessage = (
45-
OpenAI.ChatCompletionUserMessageParam | OpenAI.ChatCompletionSystemMessageParam
46-
) & {
47-
content: string;
48-
};
49-
50-
const aiService = new AzureOpenAI(AzureOpenAIConfig);
51-
52-
async function getAIResponse(messages: AIMessage[], signal: AbortSignal): Promise<string> {
53-
const params = {
54-
messages,
55-
model: AzureOpenAIConfig.deployment,
56-
max_tokens: 1000,
57-
temperature: 0.7,
58-
};
59-
60-
const response = await aiService.chat.completions.create(params, { signal });
61-
const result = response.choices[0].message?.content || '';
62-
63-
return result;
64-
}
65-
66-
const aiIntegration = new AIIntegration({
67-
sendRequest({ prompt }) {
68-
const controller = new AbortController();
69-
const signal = controller.signal;
70-
71-
const aiPrompt: AIMessage[] = [
72-
{ role: 'system', content: prompt.system || '' },
73-
{ role: 'user', content: prompt.user || '' },
74-
];
75-
76-
const promise = getAIResponse(aiPrompt, signal);
77-
78-
const result: Response = {
79-
promise,
80-
abort: () => {
81-
controller.abort();
82-
},
83-
};
84-
85-
return result;
86-
},
87-
});
40+
import { aiIntegration } from './service.ts';
8841
8942
</script>
9043
<style>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { AzureOpenAI, OpenAI } from 'openai';
2+
import {
3+
AIIntegration,
4+
type RequestParams,
5+
type Response,
6+
} from 'devextreme-vue/common/ai-integration';
7+
import { AzureOpenAIConfig } from './data.ts';
8+
9+
type AIMessage = (
10+
OpenAI.ChatCompletionUserMessageParam | OpenAI.ChatCompletionSystemMessageParam
11+
) & {
12+
content: string;
13+
};
14+
15+
const aiService = new AzureOpenAI(AzureOpenAIConfig);
16+
17+
async function getAIResponse(messages: AIMessage[], signal: AbortSignal): Promise<string> {
18+
const params = {
19+
messages,
20+
model: AzureOpenAIConfig.deployment,
21+
max_tokens: 1000,
22+
temperature: 0.7,
23+
};
24+
25+
const response = await aiService.chat.completions.create(params, { signal });
26+
const result = response.choices[0].message?.content || '';
27+
28+
return result;
29+
}
30+
31+
export const aiIntegration = new AIIntegration({
32+
sendRequest({ prompt }: RequestParams): Response {
33+
const controller = new AbortController();
34+
const signal = controller.signal;
35+
36+
const aiPrompt: AIMessage[] = [
37+
{ role: 'system', content: prompt.system || '' },
38+
{ role: 'user', content: prompt.user || '' },
39+
];
40+
41+
const promise = getAIResponse(aiPrompt, signal);
42+
43+
const result: Response = {
44+
promise,
45+
abort: () => {
46+
controller.abort();
47+
},
48+
};
49+
50+
return result;
51+
},
52+
});

0 commit comments

Comments
 (0)