单页面实现ChatGPT聊天机器人,给你的网站加点活力!
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head>
<body> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="app" style="display: flex;flex-flow: column;margin: 20 "> <div style="display: flex;justify-content: center;align-items: center;margin: 20"> <textarea type="text" v-model="info" cols="40" rows="3" style="margin:15"></textarea> <button v-on:click="ask()">发送</button> </div> <div style="display: flex;justify-content: center;align-items: center;"> <textarea name="res" id="res" cols="100" rows="20">{{res}}</textarea> </div> </div> <script> const {createApp} = Vue createApp({ data() { return { MAX_TOKEN: 4096, total: 0, info: '如果一个面包发臭了,应该怎么办?', messages: [], res: '', api: '你的OpenAI key' } }, methods: { ask() { if (!this.info) { return; } else { this.messages.push({"role": "user", "content": this.info}) this.total++; } while (this.total.length > 1) { this.total = this.tota - 1; this.messages.shift() }
this.res = '请求中...' axios.post('https://api.openai.com/v1/chat/completions', { messages: this.messages, max_tokens: this.MAX_TOKEN / 2, model: "gpt-3.5-turbo-0301" }, { headers: {'content-type': 'application/json', 'Authorization': 'Bearer ' + this.api} }).then(response => { this.res = response.data['choices'][0]['message']['content']; this.messages.push(response.data['choices'][0]['message']); this.total ++; }) } } }).mount('#app') </script> </body>
</html>
|
缺点:
- 外网IP+国外手机号
- 命令限制,只能最高4096tokens
所以,我自己开发了一个聊天室,支持移动端和PC端。单独聊天或群聊都可以和ChatGPT聊天 地址:https://chat.wangwangyz.site/