update experience for chat

This commit is contained in:
AceDataCloud committed 2023-10-03 20:13:23 +08:00
1 parent 7ca606ae20
commit 71dec1cb67
8 files changed
+151 -42

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
<template>
<div class="box"></div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'AnwseringMark',
data() {
return {};
},
computed: {
conversationId() {
return this.$route.params?.id?.toString();
}
}
});
</script>
<style lang="scss">
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.box {
width: 2px;
height: 16px;
margin-top: 3px;
background-color: var(--el-color-black);
animation: blink 1s infinite;
}
</style>
+7 -4
View File
@@ -2,23 +2,25 @@
<div :class="'message ' + message.role"> <div :class="'message ' + message.role">
<div class="content"> <div class="content">
<markdown-renderer :content="message?.content" /> <markdown-renderer :content="message?.content" />
<answering-mark v-if="message.state === messageState.PENDING" />
</div> </div>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { IContent, IError, IMessage, IMessageState } from '@/operators/message/models'; import { IContent, IError, IMessage } from '@/operators/message/models';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import AnsweringMark from './AnsweringMark.vue';
// import MessageContent from './MessageContent.vue'; // import MessageContent from './MessageContent.vue';
import { ElImage, ElButton } from 'element-plus'; import { ElImage, ElButton } from 'element-plus';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import copy from 'copy-to-clipboard'; import copy from 'copy-to-clipboard';
import MarkdownRenderer from '@/components/common/MarkdownRenderer.vue'; import MarkdownRenderer from '@/components/common/MarkdownRenderer.vue';
import { IChatMessage } from '@/operators'; import { IChatMessage, IChatMessageState } from '@/operators';
interface IData { interface IData {
copied: boolean; copied: boolean;
messageState: typeof IMessageState; messageState: typeof IChatMessageState;
} }
export default defineComponent({ export default defineComponent({
@@ -28,6 +30,7 @@ export default defineComponent({
// ElImage, // ElImage,
// ElButton, // ElButton,
// FontAwesomeIcon // FontAwesomeIcon
AnsweringMark,
MarkdownRenderer MarkdownRenderer
}, },
props: { props: {
@@ -40,7 +43,7 @@ export default defineComponent({
data(): IData { data(): IData {
return { return {
copied: false, copied: false,
messageState: IMessageState messageState: IChatMessageState
}; };
}, },
computed: { computed: {
+2 -2
View File
@@ -92,14 +92,14 @@ export default defineComponent({
const options = group.options; const options = group.options;
if (options && options.length > 0) { if (options && options.length > 0) {
this.value = options[0]; this.value = options[0];
this.$emit('select', options[0]);
this.$emit('update:modelValue', options[0]); this.$emit('update:modelValue', options[0]);
this.$emit('select', options[0]);
} }
}, },
onCommandChange(command: IChatModel) { onCommandChange(command: IChatModel) {
this.value = command; this.value = command;
this.$emit('select', command);
this.$emit('update:modelValue', command); this.$emit('update:modelValue', command);
this.$emit('select', command);
} }
} }
}); });
+3 -10
View File
@@ -1,5 +1,5 @@
<template> <template>
<span v-if="application && !initializing" class="info"> <span v-if="application" class="info">
{{ $t('conversation.message.usedCount') }}: {{ application?.used_amount }} {{ $t('conversation.message.usedCount') }}: {{ application?.used_amount }}
{{ $t('conversation.message.remainingCount') }}: {{ application?.remaining_amount }} {{ $t('conversation.message.remainingCount') }}: {{ application?.remaining_amount }}
</span> </span>
@@ -8,21 +8,14 @@
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import { IApplication } from '@/operators'; import { IApplication } from '@/operators';
import { ElSkeleton } from 'element-plus';
export default defineComponent({ export default defineComponent({
name: 'ApiStatus', name: 'ApiStatus',
components: { components: {},
ElSkeleton
},
props: { props: {
application: { application: {
type: Object as () => IApplication, type: Object as () => IApplication,
required: true required: true
},
initializing: {
type: Boolean,
required: true
} }
}, },
data() { data() {
@@ -36,6 +29,6 @@ export default defineComponent({
<style lang="scss"> <style lang="scss">
.info { .info {
font-size: 14px; font-size: 14px;
color: var(--el-color-black); color: var(--el-text-color-regular);
} }
</style> </style>
@@ -6,7 +6,7 @@
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
export default defineComponent({ export default defineComponent({
name: 'MessageContent', name: 'AnwseringMark',
data() { data() {
return {}; return {};
}, },
+24 -6
View File
@@ -41,20 +41,38 @@ export interface IChatMessage {
error?: IError; error?: IError;
} }
export interface IChatOptions { export interface IChatHistory {
stream?: (response: IChatResponse) => void; conversation_id: string;
token: string; messages: IChatMessage[];
endpoint?: string;
} }
export interface IChatRequest { export interface IChatAskOptions {
stream?: (response: IChatAskResponse) => void;
token: string;
endpoint: string;
path: string;
}
export interface IChatHistoryOptions {
endpoint: string;
path: string;
}
export interface IChatAskRequest {
question: string; question: string;
stateful?: boolean; stateful?: boolean;
conversation_id?: string; conversation_id?: string;
} }
export interface IChatResponse { export interface IChatAskResponse {
answer: string; answer: string;
delta_answer: string; delta_answer: string;
conversation_id?: string; conversation_id?: string;
} }
export interface IChatHistoryRequest {
action: string;
conversation_id: string;
}
export type IChatHistoryResponse = IChatHistory;
+20 -5
View File
@@ -1,10 +1,16 @@
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'; import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { IChatOptions, IChatRequest, IChatResponse } from './models'; import {
import store from '@/store'; IChatHistoryRequest,
IChatHistoryResponse,
IChatAskOptions,
IChatAskRequest,
IChatAskResponse,
IChatHistoryOptions
} from './models';
class ChatOperator { class ChatOperator {
async request(data: IChatRequest, options: IChatOptions): Promise<AxiosResponse<IChatResponse>> { async ask(data: IChatAskRequest, options: IChatAskOptions): Promise<AxiosResponse<IChatAskResponse>> {
return await axios.post(`/chatgpt`, data, { return await axios.post(options.path, data, {
headers: { headers: {
authorization: `Bearer ${options.token}`, authorization: `Bearer ${options.token}`,
accept: 'application/x-ndjson', accept: 'application/x-ndjson',
@@ -19,12 +25,21 @@ class ChatOperator {
if (lastLine) { if (lastLine) {
const jsonData = JSON.parse(lastLine); const jsonData = JSON.parse(lastLine);
if (options?.stream) { if (options?.stream) {
options?.stream(jsonData as IChatResponse); options?.stream(jsonData as IChatAskResponse);
} }
} }
} }
}); });
} }
async history(data: IChatHistoryRequest, options: IChatHistoryOptions): Promise<AxiosResponse<IChatHistoryResponse>> {
return await axios.post(options.path, data, {
headers: {
'content-type': 'application/json'
},
baseURL: options.endpoint
});
}
} }
export const chatOperator = new ChatOperator(); export const chatOperator = new ChatOperator();
+54 -14
View File
@@ -1,7 +1,7 @@
<template> <template>
<div class="page"> <div class="page">
<model-selector v-model="model" class="model-selector" @select="onLoadModel" /> <model-selector v-model="model" class="model-selector" @select="onSelectModel" />
<api-status v-if="application" :application="application" :initializing="initializing" /> <api-status :application="application" />
<div class="main"> <div class="main">
<introduction v-if="messages && messages.length === 0" /> <introduction v-if="messages && messages.length === 0" />
<div v-else class="messages"> <div v-else class="messages">
@@ -17,18 +17,18 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { IMessage, IMessageState, ROLE_ASSISTANT, ROLE_USER } from '@/operators/message/models'; import { ROLE_ASSISTANT, ROLE_USER } from '@/operators/message/models';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import Message from '@/components/chat/Message.vue'; import Message from '@/components/chat/Message.vue';
import { import {
IChatModel, IChatModel,
IChatMessage, IChatMessage,
CHAT_MODEL_CHATGPT, CHAT_MODEL_CHATGPT,
IChatResponse,
API_ID_CHATGPT, API_ID_CHATGPT,
applicationOperator, applicationOperator,
IApplication, IApplication,
IChatMessageState IChatMessageState,
IChatAskResponse
} from '@/operators'; } from '@/operators';
import InputBox from '@/components/chat/InputBox.vue'; import InputBox from '@/components/chat/InputBox.vue';
import ModelSelector from '@/components/chat/ModelSelector.vue'; import ModelSelector from '@/components/chat/ModelSelector.vue';
@@ -36,7 +36,7 @@ import { chatOperator } from '@/operators';
import { ERROR_CODE_CANCELED, ERROR_CODE_UNKNOWN } from '@/constants/errorCode'; import { ERROR_CODE_CANCELED, ERROR_CODE_UNKNOWN } from '@/constants/errorCode';
import axios from 'axios'; import axios from 'axios';
import ApiStatus from '@/components/common/ApiStatus.vue'; import ApiStatus from '@/components/common/ApiStatus.vue';
import { ROUTE_CHAT_CONVERSATION } from '@/router'; import { ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_CONVERSATION_NEW } from '@/router';
export interface IData { export interface IData {
messages: IChatMessage[]; messages: IChatMessage[];
@@ -71,11 +71,45 @@ export default defineComponent({
return this.$route.params.id?.toString(); return this.$route.params.id?.toString();
} }
}, },
mounted() { async mounted() {
console.log('mounted'); console.log('mounted');
this.onLoadModel(); await this.onLoadModel();
await this.onLoadHistory();
}, },
methods: { methods: {
async onCreateNewConversation() {
this.messages = [];
await this.$router.push({
name: ROUTE_CHAT_CONVERSATION_NEW
});
},
async onLoadHistory() {
if (!this.conversationId) {
return;
}
const endpoint = this.application?.api?.endpoint;
const path = this.application?.api?.path;
console.log(endpoint, path);
if (!endpoint || !path) {
console.error('no endpoint or path');
return;
}
const { data: data } = await chatOperator.history(
{
action: 'retrieve',
conversation_id: this.conversationId
},
{
endpoint,
path: `${path}/history`
}
);
this.messages = data.messages || [];
},
async onSelectModel() {
await this.onCreateNewConversation();
await this.onLoadModel();
},
async onLoadModel() { async onLoadModel() {
this.initializing = true; this.initializing = true;
const { data: applications } = await applicationOperator.getAll({ const { data: applications } = await applicationOperator.getAll({
@@ -95,14 +129,14 @@ export default defineComponent({
role: ROLE_USER role: ROLE_USER
}); });
this.question = ''; this.question = '';
await this.onFetchAnswer(); await this.onFetchAnswer();
}, },
async onFetchAnswer() { async onFetchAnswer() {
const token = this.application?.credential?.token; const token = this.application?.credential?.token;
const endpoint = this.application?.api?.endpoint; const endpoint = this.application?.api?.endpoint;
const path = this.application?.api?.path;
const question = this.messages[this.messages.length - 1].content; const question = this.messages[this.messages.length - 1].content;
if (!token || !endpoint || !question) { if (!token || !endpoint || !question || !path) {
console.error('no token or endpoint or question'); console.error('no token or endpoint or question');
return; return;
} }
@@ -113,7 +147,7 @@ export default defineComponent({
}); });
// request server to get answer // request server to get answer
chatOperator chatOperator
.request( .ask(
{ {
question, question,
conversation_id: this.conversationId, conversation_id: this.conversationId,
@@ -122,8 +156,13 @@ export default defineComponent({
{ {
token, token,
endpoint, endpoint,
stream: (response: IChatResponse) => { path,
this.messages[this.messages.length - 1].content = response.answer; stream: (response: IChatAskResponse) => {
this.messages[this.messages.length - 1] = {
role: ROLE_ASSISTANT,
content: response.answer,
state: IChatMessageState.ANSWERING
};
if (!this.conversationId) { if (!this.conversationId) {
this.$router.push({ this.$router.push({
name: ROUTE_CHAT_CONVERSATION, name: ROUTE_CHAT_CONVERSATION,
@@ -137,6 +176,7 @@ export default defineComponent({
) )
.then(() => { .then(() => {
this.messages[this.messages.length - 1].state = IChatMessageState.FINISHED; this.messages[this.messages.length - 1].state = IChatMessageState.FINISHED;
this.onLoadModel();
}) })
.catch((error) => { .catch((error) => {
if (this.messages && this.messages.length > 0) { if (this.messages && this.messages.length > 0) {
@@ -176,7 +216,7 @@ export default defineComponent({
flex: 1; flex: 1;
width: 100%; width: 100%;
overflow-y: scroll; overflow-y: scroll;
padding: 15px 0; padding: 15px;
.messages { .messages {
.message { .message {
margin-bottom: 15px; margin-bottom: 15px;