写一个使用vue调用chatgpt的api的html文件
时间: 2024-05-22 15:15:45 浏览: 110
VUE使用axios调用后台API接口的方法
5星 · 资源好评率100%
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat with GPT</title>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<h1>Chat with GPT</h1>
<input type="text" v-model="message" @keyup.enter="sendMessage">
<ul>
<li v-for="(msg, index) in messages" :key="index">{{msg}}</li>
</ul>
</div>
<script>
new Vue({
el: '#app',
data: {
message: '',
messages: []
},
methods: {
sendMessage: function() {
let self = this;
axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
prompt: 'User: ' + self.message + '\nAI:',
max_tokens: 50,
n: 1,
stop: '\n',
}, {
headers: {
'Authorization': 'Bearer API_KEY'
}
})
.then(function (response) {
self.messages.push('User: ' + self.message);
self.messages.push('AI: ' + response.data.choices[0].text);
self.message = '';
})
.catch(function (error) {
console.log(error);
});
}
}
})
</script>
</body>
</html>
阅读全文