当前位置: 首页 > 工具软件 > vue-chat > 使用案例 >

用vue写一个内网请求chatgpt的网页可以返回图片请求【内含一个key】

巫马淳
2023-12-01

最近不是chatgpt很活吗?可是必须要科技上网才可以进入,所以我就参考其他人写的页面改写了一个网页版不需要chatgpt请求的网页,还可以请求图片哦。部署以后就可以直接使用废话不多说了直接上代码

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<html>
	<head lang="en">
	    <meta charset="UTF-8">
	    <title>海洋生物详情</title>
	</head>
	<body>
		<div id="app" style="display: flex;flex-flow: column;margin: 20 ">
		    <scroll-view scroll-with-animation scroll-y="true" style="width: 100%;">
		        <!-- 用来获取消息体高度 -->
		        <view id="okk" scroll-with-animation>
		            <!-- 消息 -->
		            <view v-for="(x,i) in msgList" :key="i">
		                <!-- 用户消息 头像可选加入-->
		                <view v-if="x.my" style="display: flex;
		                flex-direction: column;
		                align-items: flex-end;">
		
		                    <view style="width: 400rpx;">
		                        <view style="border-radius: 35rpx;">
		                            <text style="word-break: break-all;">{{x.msg}}</text>
		                        </view>
		                    </view>
		
		                </view>
		                <!-- 机器人消息 -->
		                <view v-if="!x.my" style="display: flex;
		                flex-direction: row;
		                align-items: flex-start;">
		
		                    <view style="width: 500rpx;" v-if="x.url">
		                        <view style="border-radius: 35rpx;background-color: #f9f9f9;">
		                           <img :src=x.url alt="" srcset="" style="width: 40%;">
		                        </view>
		                    </view>
							<view style="width: 500rpx;" v-if="!x.url">
							    <view style="border-radius: 35rpx;background-color: #f9f9f9;">
							        <text style="word-break: break-all;">{{x.msg}}</text>
							    </view>
							</view>
		                </view>
		            </view>
		
		
		            <view style="height: 130rpx;">
		
		            </view>
		        </view>
		
		    </scroll-view>
		
		    <!-- 底部导航栏 -->
		    <view style="position: fixed;bottom:0px;width: 100%;display: flex;
		    flex-direction: column;
		    justify-content: center;
		    align-items: center;">
				<view style="font-size: 55rpx;display: flex;
		        flex-direction: row;
		        justify-content: space-around;
		        align-items: center;width: 75%;
		    margin: 0;"><input @blur='entapi' required v-model="api" type="text" style="width: 75%;
		            height: 45px;
		            border-radius: 50px;
		            padding-left: 20px;
		            margin-left: 10px;background-color: #f0f0f0;" @confirm="sendMsg" confirm-type="search"
		                placeholder-class="my-neirong-sm" placeholder="输入apikey 样式:sk-s5S5BoV... 需要购买加微信:sq888522   10块一个 "  /></view>
		        <view style="font-size: 55rpx;display: flex;
		        flex-direction: row;
		        justify-content: space-around;
		        align-items: center;width: 75%;
		    margin: 20;">
		
		            <input v-on:keyup.enter="sendMsg"  v-model="msg" type="text" style="width: 75%;
		            height: 45px;
		            border-radius: 50px;
		            padding-left: 20px;
		            margin-left: 10px;background-color: #f0f0f0;" @confirm="sendMsg" confirm-type="search"
		                placeholder-class="my-neirong-sm" placeholder="用一句简短的话描述您的问题,回车或者点击后面发送按钮" />
		            <button @click="sendMsg" :disabled="msgLoad" style="height: 45px;
		            width: 20%;;
		    color: #030303;    border-radius: 2500px;" >{{sentext}}</button>
			<button @click="sendMsgTP" :disabled="msgLoad" style="height: 45px;
			        width: 20%;;
			color: #030303;    border-radius: 2500px;" >{{sentexttp}}</button>
		        </view>
		    </view>
		    </view>
		</div>
		
	</body>
</html>

<script type="text/javascript" charset="UTF-8" >
    const { createApp } = Vue
    createApp({
        data() {
            return {
                api: '',
                msgLoad: false,
                anData: {},
                sentext: '先输入api再发送问题',
sentexttp:"生成图片按钮",
                animationData: {},
                showTow: false,
				imgurl:"",
                msgList: [{
                    my: false,
                    msg: "你好我是openAI机器人,请问有什么问题可以帮助您?"
                }],
                msgContent: "",
                msg: ""
            }
        },
        methods: {
				
			entapi(){
				console.log('11');
				this.sentext = 'api检测中'
				axios.post('https://api.openai.com/v1/completions', {
				    prompt: '你好呀', max_tokens: 2048, model: "text-davinci-003"
				}, {
				    headers: { 'content-type': 'application/json', 'Authorization': 'Bearer ' + this.api }
				}).then(res => {
					console.log('suss',res);
					this.msgLoad = false
					this.sentext = '发送'
					this.sentexttp = '发送图片问题'
					
				}).catch(error =>{
					console.log('error',error);
					console.log(error.code);
					if(error.code == 'ERR_BAD_REQUEST'){
						this.sentext = 'apikey错误,检查确认后重新输入'
						this.msgLoad = true
						
						return 0;
					}
				
					
				})

			},
            sendMsgTP() {

                // 消息为空不做任何操作
                if (this.msg == "") {
                    return 0;
                }
				if (this.api == ""){
					this.sentexttp = '请输入apikey再进行请求'
					return 0;
				}
                this.sentexttp = '请求中'
                this.msgList.push({
                    "msg": this.msg,
                    "my": true
                })
                console.log(this.msg);
                this.msgContent += ('YOU:' + this.msg + "\n")
                this.msgLoad = true
                // 清除消息
                this.msg = ""
                axios.post('https://api.openai.com/v1/images/generations', {
					prompt: this.msgContent,
					n: 2,
					size: "1024x1024"
                }, {
                    headers: { 'content-type': 'application/json', 'Authorization': 'Bearer ' + this.api }
                }).then(res => {
                    console.log(res.data.data[0].url,"777777");
                    let text = res.data.data[0].url
                    console.log(text);
                    this.msgList.push({
                        "url": text,
                        "my": false
                    })
                    // this.msgContent += (text + "\n")
                    this.msgLoad = false
                    this.sentexttp = '发送图片问题'
                }).catch(error =>{
					console.log('error',error);
					console.log(error.code);
					this.sentexttp = '请求失败,重新输入问题发送'
					this.msgLoad = false
					
					
				
					
				})

            },
			
			
			
		  sendMsg() {
		
		                // 消息为空不做任何操作
		                if (this.msg == "") {
		                    return 0;
		                }
		                this.sentext = '请求中'
		                this.msgList.push({
		                    "msg": this.msg,
		                    "my": true
		                })
		                console.log(this.msg);
		                this.msgContent += ('YOU:' + this.msg + "\n")
		                this.msgLoad = true
		                // 清除消息
		                this.msg = ""
		                axios.post('https://api.openai.com/v1/completions', {
		                    prompt: this.msgContent, max_tokens: 2048, model: "text-davinci-003"
		                }, {
		                    headers: { 'content-type': 'application/json', 'Authorization': 'Bearer ' + this.api }
		                }).then(res => {
		                    console.log(res);
		                    let text = res.data.choices[0].text.replace("openai:", "").replace("openai:", "").replace(/^\n|\n$/g, "")
		                    console.log(text);
		                    this.msgList.push({
		                        "msg": text,
		                        "my": false
		                    })
		                    this.msgContent += (text + "\n")
		                    this.msgLoad = false
		                    this.sentext = '发送'
		                })
		
		            },



        }
    }).mount('#app')
</script>

如果很懒的话没关系啊,这里有部署好的地址直接使用就行了,http://sq888522.store/chatgpt.html,对了附赠一个key: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

 类似资料: