update integration

This commit is contained in:
AceDataCloud committed 2023-04-07 00:46:18 +08:00
1 parent ed43ab5afd
commit cef6e17736
18 files changed
+303 -216

No files matched your search

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

+1
View File
@@ -0,0 +1 @@
<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>

After

Width:  |  Height:  |  Size: 302 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

+7
View File
@@ -34,6 +34,13 @@ body {
.el-input__wrapper {
border-radius: 10px;
width: 100%;
&.is-focus {
box-shadow: 0 0 0 1px var(--el-input-border-color, var(--el-border-color)) inset;
}
textarea:focus,
input:focus {
outline: none;
}
}
}
+17 -4
View File
@@ -1,20 +1,22 @@
<template>
<div class="message">
<el-image :src="author.avatar" class="icon" />
<message-content :content="content" class="content" />
<message-content :content="content" class="content" :error="error" />
</div>
</template>
<script lang="ts">
import { IBot, IUser } from '@/operators';
import { IContent } from '@/operators/message/models';
import { IContent, IError } from '@/operators/message/models';
import { defineComponent } from 'vue';
import MessageContent from './MessageContent.vue';
import { ElImage } from 'element-plus';
export default defineComponent({
name: 'Message',
components: {
MessageContent
MessageContent,
ElImage
},
props: {
content: {
@@ -24,6 +26,13 @@ export default defineComponent({
author: {
type: Object as () => IUser | IBot,
required: true
},
error: {
type: Object as () => IError | undefined,
required: false,
default() {
return undefined;
}
}
},
data() {
@@ -42,7 +51,11 @@ export default defineComponent({
display: flex;
flex-direction: row;
.icon {
width: 100px;
width: 30px;
height: 30px;
margin-right: 20px;
border-radius: 3px;
min-width: 30px;
}
.content {
flex: 1;
+12 -2
View File
@@ -1,21 +1,31 @@
<template>
<markdown-renderer :content="content.value" />
<el-alert v-if="error" :title="error?.detail" type="error" />
</template>
<script lang="ts">
import { IContent } from '@/operators/message/models';
import { IContent, IError } from '@/operators/message/models';
import { defineComponent } from 'vue';
import MarkdownRenderer from '@/components/common/MarkdownRenderer.vue';
import { ElAlert } from 'element-plus';
export default defineComponent({
name: 'MessageContent',
components: {
MarkdownRenderer
MarkdownRenderer,
ElAlert
},
props: {
content: {
type: Object as () => IContent,
required: true
},
error: {
type: Object as () => IError | undefined,
required: false,
default() {
return undefined;
}
}
},
data() {
+21 -4
View File
@@ -1,7 +1,11 @@
<template>
<el-input v-model="value" placeholder="Type something">
<el-input
v-model="value"
:placeholder="$t('conversation.message.newMessagePlaceholder')"
@keyup.enter="$emit('send')"
>
<template #suffix>
<el-icon class="el-input__icon" @click="$emit('send')">send</el-icon>
<font-awesome-icon icon="fa-regular fa-paper-plane" class="icon-send" @click="$emit('send')" />
</template>
</el-input>
</template>
@@ -9,11 +13,14 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { ElInput, ElButton, ElIcon } from 'element-plus';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
export default defineComponent({
name: 'NewMessageBox',
components: {
ElInput,
ElIcon
ElIcon,
FontAwesomeIcon
},
props: {
modelValue: {
@@ -34,9 +41,19 @@ export default defineComponent({
},
watch: {
value(val) {
console.log('ccc', this.value);
this.$emit('update:modelValue', this.value);
},
modelValue(val) {
if (val !== this.value) {
this.value = val;
}
}
}
});
</script>
<style lang="scss" scoped>
.icon-send {
cursor: pointer;
}
</style>
+4
View File
@@ -0,0 +1,4 @@
import chatgptAvatar from '@/assets/images/chatgpt.png';
import userAvatar from '@/assets/images/avatar.png';
export { chatgptAvatar, userAvatar };
+5
View File
@@ -0,0 +1,5 @@
import message from './message';
export default {
message
};
+5
View File
@@ -0,0 +1,5 @@
export default {
noToken: '请先申请对应服务',
newMessagePlaceholder: '请输入你的问题...',
noInput: '问题不能为空'
};
+2
View File
@@ -6,9 +6,11 @@ import application from './application';
import api from './api';
import order from './order';
import proxy from './proxy';
import conversation from './conversation';
export default {
common,
conversation,
auth,
service,
console,
+4 -4
View File
@@ -1,5 +1,4 @@
import { AxiosResponse } from 'axios';
import { httpClient } from '../../instance';
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { IRequest, IResponse } from './models';
export interface IQuery {
@@ -9,8 +8,9 @@ export interface IQuery {
const baseUrl = 'https://api.zhishuyun.com';
class ChatGPTOperator {
async post(data: IRequest, query: IQuery): Promise<AxiosResponse<IResponse>> {
return await httpClient.post(`/chatgpt`, data, {
async post(data: IRequest, query: IQuery, config?: AxiosRequestConfig): Promise<AxiosResponse<IResponse>> {
return await axios.post(`/chatgpt`, data, {
...config,
params: query,
baseURL: baseUrl
});
+6
View File
@@ -5,7 +5,13 @@ export interface IContent {
type?: string;
}
export interface IError {
code: string;
detail: string;
}
export interface IMessage {
content: IContent;
author: IUser | IBot;
error?: IError;
}
-127
View File
@@ -1,127 +0,0 @@
<template>
<div class="conversation">
<div class="messages">
<div
v-for="(message, messageIndex) in messages"
:key="messageIndex"
:class="{
'message-wrapper': true,
user: message.author.type === 'user',
bot: message.author.type === 'bot'
}"
>
<el-row>
<el-col :span="18" :offset="3">
<message :content="message.content" :author="message.author"> </message>
</el-col>
</el-row>
</div>
</div>
<div class="footer">
<el-row>
<el-col :span="18" :offset="3">
<new-message-box v-model="input" class="new-message" @send="onSend" />
</el-col>
</el-row>
</div>
</div>
</template>
<script lang="ts">
import { IMessage } from '@/operators/message/models';
import { defineComponent } from 'vue';
import { ElRow, ElCol } from 'element-plus';
import Message from '@/components/conversation/Message.vue';
import { chatgptOperator } from '@/operators/api/chatgpt/operator';
import { IResponse } from '@/operators/api/chatgpt/models';
import NewMessageBox from '@/components/conversation/NewMessageBox.vue';
export interface IData {
input: string;
messages: IMessage[];
}
export default defineComponent({
name: 'ChatDetail',
components: {
Message,
ElRow,
ElCol,
NewMessageBox
},
data(): IData {
return {
input: '',
messages: [
{
author: {
type: 'bot',
nickname: 'ChatGPT'
},
content: {
value: '你好啊'
}
},
{
author: {
id: 'x',
type: 'user',
nickname: 'fdsf'
},
content: {
value: '很高兴为你服务'
}
}
]
};
},
computed: {
conversationId() {
return this.$route.params?.id?.toString();
}
},
methods: {
onSend() {
console.log('send', this.input);
chatgptOperator
.post(
{
question: this.input
},
{
token: 'ec5cc681720b4923b05f669a853c5629'
}
)
.then(({ data: data }: { data: IResponse }) => {
console.log('data', data);
});
}
}
});
</script>
<style lang="scss" scoped>
.conversation {
min-height: 100vh;
flex-direction: column;
display: flex;
.messages {
flex: 1;
}
.message-wrapper {
border-color: rgba(0, 0, 0, 0.1);
border-bottom-width: 1px;
padding-bottom: 1.5rem;
padding-top: 1.5rem;
&.bot {
background-color: #f7f7f8;
}
&.user {
background-color: #ffffff;
}
}
.new-message {
height: 50px;
}
}
</style>
+213
View File
@@ -0,0 +1,213 @@
<template>
<div class="conversation">
<div class="messages">
<div
v-for="(message, messageIndex) in messages"
:key="messageIndex"
:class="{
'message-wrapper': true,
user: message.author.type === 'user',
bot: message.author.type === 'bot'
}"
>
<el-row>
<el-col :span="12" :offset="6">
<message :content="message.content" :author="message.author" :error="message.error"> </message>
</el-col>
</el-row>
</div>
</div>
<div class="footer">
<el-row>
<el-col :span="12" :offset="6">
<new-message-box v-model="input" class="new-message" @send="onSend" />
</el-col>
</el-row>
</div>
</div>
</template>
<script lang="ts">
import { IMessage } from '@/operators/message/models';
import { defineComponent } from 'vue';
import { ElRow, ElCol, ElMessage } from 'element-plus';
import Message from '@/components/conversation/Message.vue';
import { chatgptOperator } from '@/operators/api/chatgpt/operator';
import NewMessageBox from '@/components/conversation/NewMessageBox.vue';
import { chatgptAvatar, userAvatar } from '@/constants/image';
import { applicationOperator, IApplicationListResponse } from '@/operators';
import { ROUTE_AUTH_LOGIN, ROUTE_CONVERSATION_DETAIL } from '@/router/constants';
export interface IData {
input: string;
messages: IMessage[];
loading: boolean;
token: string | undefined;
}
export default defineComponent({
name: 'ChatDetail',
components: {
Message,
ElRow,
ElCol,
NewMessageBox
},
data(): IData {
return {
input: '',
loading: false,
messages: [],
token: undefined
};
},
computed: {
conversationId() {
return this.$route.params?.id?.toString();
},
user() {
return this.$store.getters.user;
}
},
mounted() {
if (!this.$store.getters.authenticated) {
this.$router.push({
name: ROUTE_AUTH_LOGIN,
query: { redirect: this.$route.fullPath }
});
} else {
this.onCheckApplication();
}
},
methods: {
onCheckApplication() {
if (!this.user) {
return;
}
applicationOperator
.getAll({
user_id: this.user.id,
api_id: '1d58971c-e3cd-4713-a3ce-854a731adb14'
})
.then(({ data: data }: { data: IApplicationListResponse }) => {
if (data.items?.length > 0) {
const application = data.items[0];
const token = application.credential?.token;
if (token) {
this.token = token;
}
}
});
},
onSend() {
if (!this.input) {
ElMessage.error(this.$t('conversation.message.noInput'));
return;
}
if (!this.token) {
ElMessage.error(this.$t('conversation.message.noToken'));
return;
}
const input = this.input;
this.messages.push({
content: {
value: input
},
author: {
...this.user,
avatar: this.user?.avatar || userAvatar,
type: 'user'
}
});
this.messages.push({
content: {
value: ''
},
author: {
type: 'bot',
nickname: 'ChatGPT',
avatar: chatgptAvatar
}
});
this.loading = true;
this.input = '';
chatgptOperator
.post(
{
question: input,
stateful: true,
...(this.conversationId
? {
conversation_id: this.conversationId
}
: {})
},
{
token: this.token
},
{
headers: {
accept: 'application/x-ndjson',
'content-type': 'application/json'
},
responseType: 'stream',
onDownloadProgress: (event) => {
const response = event.target.response;
const lines = response.split('\r\n').filter((line: string) => !!line);
const lastLine = lines[lines.length - 1];
if (lastLine) {
const jsonData = JSON.parse(lastLine);
const answer = jsonData.answer;
const conversationId = jsonData.conversation_id;
this.messages[this.messages.length - 1].content.value = answer;
if (conversationId && this.$route.name !== ROUTE_CONVERSATION_DETAIL) {
this.$router.push({
name: ROUTE_CONVERSATION_DETAIL,
params: {
id: conversationId
}
});
}
}
}
}
)
.catch((error) => {
if (error?.response?.data) {
const data = error?.response?.data;
if (this.messages && this.messages.length > 0) {
this.messages[this.messages.length - 1].error = data;
}
}
});
}
}
});
</script>
<style lang="scss" scoped>
.conversation {
min-height: 100vh;
flex-direction: column;
display: flex;
.messages {
flex: 1;
}
.message-wrapper {
border-color: rgba(0, 0, 0, 0.1);
border-bottom-width: 1px;
padding-bottom: 1.5rem;
padding-top: 1.5rem;
&.bot {
background-color: #f7f7f8;
}
&.user {
background-color: #ffffff;
}
}
.new-message {
height: 50px;
margin-bottom: 30px;
}
}
</style>
-70
View File
@@ -1,70 +0,0 @@
<template>
<div class="conversation">
<div class="introduction"></div>
<div class="footer">
<el-row>
<el-col :span="18" :offset="3">
<new-message-box v-model="input" class="new-message" @send="onSend" />
</el-col>
</el-row>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { ElRow, ElCol } from 'element-plus';
import NewMessageBox from '@/components/conversation/NewMessageBox.vue';
import { chatgptOperator } from '@/operators/api/chatgpt/operator';
import { IResponse } from '@/operators/api/chatgpt/models';
export default defineComponent({
name: 'ChatMain',
components: {
NewMessageBox,
ElRow,
ElCol
},
data() {
return {
input: ''
};
},
computed: {},
methods: {
onSend() {
console.log('send', this.input);
chatgptOperator
.post(
{
question: this.input
},
{
token: 'ec5cc681720b4923b05f669a853c5629'
}
)
.then(({ data: data }: { data: IResponse }) => {
console.log('data', data);
});
}
}
});
</script>
<style lang="scss" scoped>
.conversation {
display: flex;
flex-direction: column;
min-height: 100vh;
.introduction {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
}
.footer {
height: 50px;
}
}
</style>
+3 -2
View File
@@ -2,7 +2,8 @@ import { library } from '@fortawesome/fontawesome-svg-core';
import {
faCopy as faRegularCopy,
faUser as faRegularUser,
faIdCard as faRegularIdCard
faIdCard as faRegularIdCard,
faPaperPlane as faRegularPaperPlane
} from '@fortawesome/free-regular-svg-icons';
import {
faRotateRight as faSolidRotateRight,
@@ -14,7 +15,6 @@ import {
faArrowsRotate as faSolidArrowsRotate,
faUpRightFromSquare as faSolidUpRightFromSquare
} from '@fortawesome/free-solid-svg-icons';
library.add(faRegularCopy);
library.add(faSolidRotateRight);
library.add(faSolidCheck);
@@ -26,3 +26,4 @@ library.add(faSolidStore);
library.add(faSolidQuestion);
library.add(faSolidUpRightFromSquare);
library.add(faSolidArrowsRotate);
library.add(faRegularPaperPlane);
+3 -3
View File
@@ -1,4 +1,4 @@
import { ROUTE_CONVERSATION_DETAIL, ROUTE_CONVERSATION_NEW, ROUTE_DOCUMENT_LIST } from './constants';
import { ROUTE_CONVERSATION_DETAIL, ROUTE_CONVERSATION_NEW } from './constants';
export default {
path: '/conversations',
@@ -7,12 +7,12 @@ export default {
{
path: '',
name: ROUTE_CONVERSATION_NEW,
component: () => import('@/pages/conversation/New.vue')
component: () => import('@/pages/conversation/Main.vue')
},
{
path: ':id',
name: ROUTE_CONVERSATION_DETAIL,
component: () => import('@/pages/conversation/Detail.vue')
component: () => import('@/pages/conversation/Main.vue')
}
]
};