七仔的博客

七仔的博客GithubPages分博

0%

简易AI对话的VueDemo

实现了连续对话,不过其中的GPT的Api已经失效想要使用的话需要另行寻找api

简易AI对话的VueDemo

一、说明

实现了连续对话,不过其中的GPT的Api已经失效想要使用的话需要另行寻找api。可以使用Electron打包成exe当成一个小程序使用。

二、展示

image.png

三、主要代码

注意就是将每条对话分为左边和右边两个vue,然后再组合起来,一共是三个vue

  • Chat.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<template>
<div class="container">
<div class="list" id="list" ref="list" >
<ul>
<li v-for="(item,index) in msglist" :key="index">
<RightItem :id="item.id" :type="item.type" :content="item.content" v-if="item.me"></RightItem>
<LeftItem :id="item.id" :type="item.type" :content="item.content" v-else></LeftItem>
<div v-scroll style="height: 0"></div>
</li>
</ul>
</div>
<div class="bottom">
<div class="line"></div>
<div class="input-send">
<van-field v-model="text" placeholder="请输入聊天内容..." class="input" @keyup.enter="send"/>
<van-button plain type="info" class="send" @click="send">发送</van-button>
</div>
</div>
</div>
</template>

<script>
import Vue from "vue";
// eslint-disable-next-line no-unused-vars
import {getChatResponse} from "@/api/ChatGPT";
import LeftItem from "@/components/LeftItem";
import RightItem from "@/components/RightItem";

Vue.directive('scroll', {
inserted(el) {
el.scrollIntoView()
}
})

export default {
name: "Chat",
components: {LeftItem, RightItem},
data: () => {
return {
text: '',
msglist: [{
id: 1,
type: 1,
content: '欢迎你!',
me: false
}],
rebootBusy: false
}
},
methods: {
send() {
if (this.text && !this.rebootBusy) {
var lastChat = this.getLastChat()
this.msglist.push({
id: this.msglist[this.msglist.length - 1].id + 1,
type: 1,
content: this.text,
me: true
})
this.getResponse(lastChat + "Human: " + this.text + "\nAI: ")
this.text = ''
}
},
getResponse(text) {
var obj = {
"model": "text-davinci-003",
"prompt": text,
"temperature": 0.8,
"max_tokens": 2048,
"top_p": 1,
"frequency_penalty": 0.4,
"presence_penalty": 0.3,
"stop": ["Human:", "AI:"]
}
console.log(text)
this.msglist.push({
id: this.msglist[this.msglist.length - 1].id + 1,
type: 1,
content: "机器人正在组织语言,请稍等...",
me: false
})
this.rebootBusy = true
getChatResponse(obj).then(
response => {
console.log(response)
this.rebootBusy = false
this.msglist.splice(-1, 1)
if (response.choices[0]['text'] == "error") {
this.msglist.splice(-1, 1)
alert("OpenAI服务器错误,请稍后再试")
return
}
this.msglist.push({
id: this.msglist[this.msglist.length - 1].id + 1,
type: 1,
content: response.choices[0]['text'].replace(/\s+$/,''),
me: false
})
})
},
getLastChat() {
let chat = ""
for (let i = 1; i < this.msglist.length; i++) {
if (this.msglist[i]['me']) {
chat = chat + "Human: " + this.msglist[i]['content'] + "\nAI: "
}
else {
chat = chat + this.msglist[i]['content'] + "\n"
}
}
if (chat.length > 2000) {
alert("连续对话超过显示,接下来无法进行连续对话,请重启")
return ""
}
return chat;
}
}
}
</script>

<style scoped lang="scss">
.container {
ul {
padding: 0;
margin: 0;
}

li {
list-style: none;

}

.list {
width: 100%;
height: 100%;
margin-bottom: 45px;
}

.bottom {
width: 100%;
position: fixed;
bottom: 0;

.line {
width: 100%;
height: 1px;
background-color: #ddd;
}

.input-send {
display: flex;
justify-content: space-between;
background-color: #fff;

.input {
padding-right: 10px;
}

.send {
width: 80px;
height: 30px;
margin-top: 7px;
margin-right: 10px;
}
}

}
}
</style>
  • LeftItem.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<template>
<div class="container">
<img class="head" src="https://media.baby7blog.com/chatgpt/chatGPT.png"/>

<div class="content">
<div class="text" v-if="type===1">
{{content}}
</div>
<img class="img" :src="content" v-else-if="type===2" @click="preview(content)"/>
</div>

</div>
</template>

<script>
import ImagePreview from "vant/lib/image-preview";

export default {
name: "LeftItem",
props: ['id', 'type', 'content'],
methods: {
preview(url){
ImagePreview([url])
}
}
}
</script>

<style scoped lang="scss">
.container {
display: flex;
padding: 10px 15px;
margin-right: 60px;

.head {
width: 40px;
height: 40px;
border-radius: 50%;
border: 1px solid #eee;
}

.content {
margin-left: 10px;
margin-top: 10px;

.text {
background-color: deepskyblue;
border-bottom-right-radius: 10px;
padding: 5px 5px;
font-size: 14px;
color: #fff;
white-space: pre-line;
}

.img {
width: 100px;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
border-bottom-left-radius: 10px;
}
}

}
</style>
  • RightItem.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<template>
<div class="container">
<img class="head"
src="https://media.baby7blog.com/chatgpt/baby7.png"/>
<div class="content">
<div class="text" v-if="type===1">
{{content}}
</div>
<img class="img" :src="content" v-else-if="type===2"/>
</div>
</div>
</template>

<script>

export default {
name: "RightItem",
props: ['id', 'type', 'content']
}
</script>

<style scoped lang="scss">
.container {
display: flex;
padding: 10px 15px;
margin-left: 60px;
flex-direction: row-reverse;

.head {
width: 40px;
height: 40px;
border-radius: 50%;
border: 1px solid #eee;
}

.content {
margin-right: 10px;
margin-top: 10px;

.text {
background-color: #eee;
border-bottom-left-radius: 10px;
padding: 5px 5px;
font-size: 14px;
color: #000;
white-space: pre-line;
}

.img {
width: 100px;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
border-bottom-left-radius: 10px;
}
}

}
</style>

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/140.html

欢迎关注我的其它发布渠道