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>
|