add switch api

This commit is contained in:
AceDataCloud committed 2023-04-19 02:48:49 +08:00
1 parent cf22897a6c
commit f047c9b62f
6 files changed
+166 -71

No files matched your search

+70 -57
View File
@@ -4,32 +4,36 @@
{{ $t('conversation.message.usedCount') }}: {{ application?.used_amount }}
{{ $t('conversation.message.remainingCount') }}: {{ application?.remaining_amount }}
</span>
<el-input
v-model="value"
type="textarea"
resize="none"
:disabled="initializing || !application || answering"
class="new-message-input"
:placeholder="$t('conversation.message.newMessagePlaceholder')"
@keydown.enter.exact.prevent="$emit('send')"
>
</el-input>
<span class="new-message-info-input">{{ $t('conversation.message.howToBreakLine') }}</span>
<div class="new-message-operations">
<voice-recognition v-if="false" class="mr-1" @finished="value = $event" />
<font-awesome-icon icon="fa-regular fa-paper-plane" class="icon-send" @click="$emit('send')" />
</div>
<div v-if="initializing" class="new-message-apply">
<span class="ml-1">{{ $t('conversation.message.initializing') }}</span>
</div>
<div v-else-if="!application" class="new-message-apply">
<span class="mr-2">{{ $t('conversation.message.apiInfo') }}</span>
<span>
<el-button type="primary" size="small" @click="confirming = true">
{{ $t('common.button.apply') }}
</el-button>
</span>
<span class="ml-1">{{ $t('conversation.message.tryForFree') }}</span>
<div class="flex">
<div class="new-message-input-box">
<el-input
v-model="value"
type="textarea"
resize="none"
:disabled="initializing || !application || answering"
class="new-message-input"
:placeholder="$t('conversation.message.newMessagePlaceholder')"
@keydown.enter.exact.prevent="$emit('send')"
>
</el-input>
<span class="new-message-info-input">{{ $t('conversation.message.howToBreakLine') }}</span>
<div class="new-message-operations">
<voice-recognition v-if="false" class="mr-1" @finished="value = $event" />
<font-awesome-icon icon="fa-regular fa-paper-plane" class="icon-send" @click="$emit('send')" />
</div>
<div v-if="initializing" class="new-message-apply">
<span class="ml-1">{{ $t('conversation.message.initializing') }}</span>
</div>
<div v-else-if="!application" class="new-message-apply">
<span class="mr-2">{{ $t('conversation.message.apiInfo') }}</span>
<span>
<el-button type="primary" size="small" @click="confirming = true">
{{ $t('common.button.apply') }}
</el-button>
</span>
<span class="ml-1">{{ $t('conversation.message.tryForFree') }}</span>
</div>
</div>
</div>
</div>
<application-confirm
@@ -43,12 +47,13 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { ElInput, ElButton } from 'element-plus';
import { ElInput, ElButton, ElSelect, ElOption } from 'element-plus';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { IApi, IApplication } from '@/operators';
import { IApplicationType } from '@/operators';
import ApplicationConfirm from '@/components/application/Confirm.vue';
import VoiceRecognition from './VoiceRecognition.vue';
import { APIS } from '@/constants/api';
interface IData {
applicationType: typeof IApplicationType;
@@ -100,6 +105,9 @@ export default defineComponent({
computed: {
conversationId() {
return this.$route.params?.id?.toString();
},
activeApiId() {
return this.$store.getters.activeApiId;
}
},
watch: {
@@ -120,38 +128,43 @@ export default defineComponent({
.new-message-box {
position: relative;
margin-bottom: 40px;
.new-message-input {
height: 50px;
}
.new-message-operations {
position: absolute;
bottom: 12px;
right: 10px;
color: var(--el-text-color-regular);
.icon-send {
cursor: pointer;
.new-message-input-box {
position: relative;
width: 100%;
height: 100%;
.new-message-input {
height: 50px;
}
.new-message-info-input {
position: absolute;
bottom: -25px;
left: 5px;
font-size: 12px;
color: var(--el-text-color-regular);
}
.new-message-operations {
position: absolute;
bottom: 12px;
right: 10px;
color: var(--el-text-color-regular);
.icon-send {
cursor: pointer;
}
}
.new-message-apply {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
text-align: center;
transform: translate(-50%, -50%);
font-size: 12px;
color: #666;
}
}
.new-message-info-input {
position: absolute;
bottom: -25px;
left: 5px;
font-size: 12px;
color: var(--el-text-color-regular);
}
.new-message-apply {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
text-align: center;
transform: translate(-50%, -50%);
font-size: 12px;
color: #666;
}
.new-message-box-usage-info {
color: #999;
font-size: 12px;
+15
View File
@@ -0,0 +1,15 @@
export const API_ID_CHATGPT = '1d58971c-e3cd-4713-a3ce-854a731adb14';
export const API_ID_CHATGPT4 = '1c4e8fa3-362b-4e0a-b0fd-9ff9fc173b77';
export const APIS = [
{
id: API_ID_CHATGPT,
name: 'ChatGPT-3.5'
},
{
id: API_ID_CHATGPT4,
name: 'ChatGPT-4'
}
];
export const DEFAULT_API = APIS[0];
+1
View File
@@ -2,3 +2,4 @@ export * from './errorCode';
export * from './env';
export * from './endpoint';
export * from './image';
export * from './api';
+11
View File
@@ -15,3 +15,14 @@ class ChatGPTOperator {
}
export const chatgptOperator = new ChatGPTOperator();
class ChatGPT4Operator {
async post(data: IRequest, query: IQuery, config?: AxiosRequestConfig): Promise<AxiosResponse<IResponse>> {
return await axios.post(`/chatgpt4`, data, {
...config,
params: query
});
}
}
export const chatgpt4Operator = new ChatGPT4Operator();
+57 -13
View File
@@ -1,5 +1,10 @@
<template>
<div class="conversation">
<div class="selection">
<el-select :model-value="activeApiId" @change="onSwitchApi">
<el-option v-for="item in apis" :key="item.id" :label="item.name" :value="item.id" />
</el-select>
</div>
<el-row v-if="!messages || messages.length === 0" class="introduction-wrapper" justify="center">
<el-col :md="14" :sm="22" :xs="22">
<introduction @draft="onDraft" />
@@ -40,6 +45,7 @@
:application="application"
@send="onSend"
@apply="onApply"
@switch-api="onSwitchApi"
/>
</el-col>
</el-row>
@@ -50,24 +56,25 @@
<script lang="ts">
import { IMessage, IMessageState } from '@/operators/message/models';
import { defineComponent } from 'vue';
import { ElRow, ElCol, ElMessage } from 'element-plus';
import { ElRow, ElCol, ElMessage, ElSelect, ElOption } from 'element-plus';
import Message from '@/components/conversation/Message.vue';
import { chatgptOperator } from '@/operators/api/chatgpt/operator';
import { chatgpt4Operator, chatgptOperator } from '@/operators/api/chatgpt/operator';
import NewMessageBox from '@/components/conversation/NewMessageBox.vue';
import { chatgptAvatar, userAvatar } from '@/constants/image';
import {
apiOperator,
applicationOperator,
IApi,
IApiDetailResponse,
IApplication,
IApplicationDetailResponse,
IApplicationListResponse,
IApplicationType,
userOperator
} from '@/operators';
import { ROUTE_AUTH_LOGIN, ROUTE_CONVERSATION_DETAIL } from '@/router/constants';
import { ROUTE_AUTH_LOGIN, ROUTE_CONVERSATION_DETAIL, ROUTE_CONVERSATION_NEW } from '@/router/constants';
import {
APIS,
API_ID_CHATGPT,
DEFAULT_API,
ENDPOINT,
ERROR_CODE_CANCELED,
ERROR_CODE_DUPLICATION,
@@ -89,7 +96,7 @@ export interface IData {
api: IApi | undefined;
application: IApplication | undefined;
canceler: AbortController | undefined;
api_id: string;
apis: { id: string; name: string }[];
}
export default defineComponent({
@@ -99,6 +106,8 @@ export default defineComponent({
Message,
ElRow,
ElCol,
ElSelect,
ElOption,
NewMessageBox
},
data(): IData {
@@ -111,7 +120,7 @@ export default defineComponent({
api: undefined,
application: undefined,
canceler: undefined,
api_id: '1d58971c-e3cd-4713-a3ce-854a731adb14'
apis: APIS
};
},
computed: {
@@ -123,6 +132,9 @@ export default defineComponent({
},
setting() {
return this.$store.getters.setting;
},
activeApiId() {
return this.$store.getters.activeApiId;
}
},
watch: {
@@ -140,6 +152,11 @@ export default defineComponent({
});
} else {
this.restoreMessages();
this.onInitialize();
}
},
methods: {
onInitialize() {
this.loading = true;
Promise.all([this.onGetApiInfo(), this.onCheckApplication()])
.then(() => {
@@ -148,9 +165,7 @@ export default defineComponent({
.catch(() => {
this.loading = false;
});
}
},
methods: {
},
scrollDown() {
const container = document.querySelector('.main') as HTMLDivElement;
container.scrollTop = container.scrollHeight;
@@ -168,7 +183,7 @@ export default defineComponent({
}
},
async onGetApiInfo(): Promise<void> {
const { data } = await apiOperator.get(this.api_id);
const { data } = await apiOperator.get(this.activeApiId);
this.api = data;
},
async onStop() {
@@ -181,7 +196,7 @@ export default defineComponent({
}
const { data } = await applicationOperator.getAll({
user_id: this.user.id,
api_id: this.api_id
api_id: this.activeApiId
});
if (data.items?.length > 0) {
const application = data.items[0];
@@ -192,6 +207,15 @@ export default defineComponent({
}
}
},
async onSwitchApi(id: string) {
await this.$store.dispatch('setActiveApiId', id);
this.api = undefined;
this.application = undefined;
this.onInitialize();
this.$router.push({
name: ROUTE_CONVERSATION_NEW
});
},
onApply() {
applicationOperator
.create({
@@ -305,7 +329,8 @@ export default defineComponent({
this.scrollDown();
}, 100);
this.canceler = new AbortController();
chatgptOperator
const operator = this.activeApiId === API_ID_CHATGPT ? chatgptOperator : chatgpt4Operator;
operator
.post(
{
question: input,
@@ -387,8 +412,24 @@ export default defineComponent({
position: relative;
flex-direction: column;
display: flex;
.selection {
position: fixed;
top: 10px;
text-align: center;
left: 300px;
width: calc(100% - 300px);
z-index: 1000;
}
@media (max-width: 767px) {
.selection {
left: 0px;
width: calc(100%);
}
}
.messages-wrapper {
margin-bottom: 100px;
padding-top: 50px;
}
.introduction-wrapper {
flex: 1;
@@ -399,6 +440,9 @@ export default defineComponent({
border-bottom-width: 1px;
padding-bottom: 1.5rem;
padding-top: 1.5rem;
&:first-child {
border-top-width: 1px;
}
&.bot {
background-color: #f7f7f8;
}
+12 -1
View File
@@ -3,7 +3,7 @@ import createPersistedState from 'vuex-persistedstate';
import { userOperator, IUser } from '@/operators';
import { removeCookies } from '@/utils/cookie';
import { IConversation } from '@/operators/conversation/models';
import { ENDPOINT } from '@/constants';
import { DEFAULT_API, ENDPOINT } from '@/constants';
export interface ISetting {
stream?: boolean;
@@ -14,6 +14,7 @@ export interface IState {
accessToken: string | undefined;
refreshToken: string | undefined;
user: IUser | undefined;
activeApiId: string | undefined;
conversations: IConversation[];
setting: ISetting;
}
@@ -24,6 +25,7 @@ const store = createStore({
accessToken: undefined,
refreshToken: undefined,
user: undefined,
activeApiId: DEFAULT_API.id,
conversations: [],
setting: {
endpoint: ENDPOINT,
@@ -49,6 +51,9 @@ const store = createStore({
},
setConversations(state: IState, payload: IConversation[]): void {
state.conversations = payload;
},
setActiveApiId(state: IState, payload: string): void {
state.activeApiId = payload;
}
},
actions: {
@@ -70,6 +75,9 @@ const store = createStore({
setSetting({ commit }: ActionContext<IState, IState>, payload: ISetting) {
commit('setSetting', payload);
},
setActiveApiId({ commit }: ActionContext<IState, IState>, payload: string) {
commit('setActiveApiId', payload);
},
setConversations({ commit }: ActionContext<IState, IState>, payload: IConversation[]) {
commit('setConversations', payload);
},
@@ -91,6 +99,9 @@ const store = createStore({
accessToken(state): string | undefined {
return state.accessToken;
},
activeApiId(state): string | undefined {
return state.activeApiId;
},
refreshToken(state): string | undefined {
return state.refreshToken;
},