文心一言AI大模型,前端接入
一、參考接口資料
模型廣場:https://console.bce.baidu.com/qianfan/modelcenter/model/buildIn/list
我的應(yīng)用:https://console.bce.baidu.com/qianfan/ais/console/onlineService
千帆大模型調(diào)用API介紹:
https://cloud.baidu.com/doc/WENXINWORKSHOP/s/flfmc9do2
在線調(diào)試:
https://console.bce.baidu.com/ticket/?#/ticket/tools/api
https://console.bce.baidu.com/tools/#/api?product=AI&project=%E5%8D%83%E5%B8%86%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%B9%B3%E5%8F%B0&parent=%E9%89%B4%E6%9D%83%E8%AE%A4%E8%AF%81%E6%9C%BA%E5%88%B6&api=oauth%2F2.0%2Ftoken&method=post文章來源:http://www.zghlxwxcb.cn/news/detail-764835.html
二、vue代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-764835.html
<template>
<div>
<el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" :disabled="isSearching===true">
<el-tab-pane label="文心一言" name="wenxin">
<div class="kefubox" ref="containerRef">
<div v-for="message in chatHistoryWenxin" :key="message.id">
<!-- 遍歷用戶發(fā)送的信息,如果role為user就返回信息 -->
<div v-if="message.sender === 'user'" class="user-message">
<!-- 用戶發(fā)送的信息 -->
<div class="userBox">
<!-- icon圖標 -->
<svg t="1686817502794" class="icon" viewBox="0 0 1024 1024" version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="3466" width="30" height="30">
<path fill="#000000"
d="M500 128.8c-95.2 5.6-173.6 83.2-180 178.4-7.2 112 80.8 205.6 191.2 205.6 106.4 0 192-86.4 192-192 0.8-110.4-92-198.4-203.2-192zM512 575.2c-128 0-383.2 64-383.2 192v96c0 17.6 14.4 32 32 32h702.4c17.6 0 32-14.4 32-32V766.4c0-127.2-255.2-191.2-383.2-191.2z"
p-id="3467">
</path>
</svg>
</div>
<!-- 用戶輸入的問題 -->
<span>{{ message.text }}</span>
</div>
<div v-else class="bot-message" style="margin: 5px 0px;padding:5px 10px;">
<!-- icon圖標 -->
<div style="display: flex;align-items: center;margin-right: 10px;">
<svg t="1686814946576" class="icon" viewBox="0 0 1024 1024" version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="2479" width="30" height="30">
<path
d="M959.716928 554.950208M532.429285 958.838931M64.446801 594.626859M270.969435 57.464783"
fill="#000000" p-id="2480"></path>
<path
d="M583.719438 385.762901C664.377456 493.897713 810.145377 538.433022 810.145377 538.433022 799.252283 351.240643 687.435615 259.769348 510.411829 258.869862 298.641681 257.789251 175.627996 429.173596 222.010373 597.866646 222.00935 597.866646 580.769246 462.953957 583.719438 385.762901z"
fill="#000000" p-id="2481"></path>
<path
d="M843.843874 721.201649c65.939805-16.944927 114.865097-15.887852 114.865097-150.871149 0-65.013713-38.867216-120.714316-93.980441-144.029324C824.128857 272.551466 697.718819 116.236328 507.890402 111.958908c-6.053879-0.135076-12.062733-0.157589-18.004049 0-183.887101 4.724605-309.892933 146.908908-324.069817 311.821865-58.44511 21.626553-102.44216 79.103616-100.462063 146.549727 4.432962 151.456481 66.321498 153.863297 148.712997 155.550729l0-293.098431c1.732457-125.304867 124.495432-264.293385 293.822932-264.293385 168.313404 0 261.415848 130.999566 302.464753 247.369947l0 301.380048c-8.28264 19.399839-30.449499 41.206494-70.936609 80.296791-52.819996 50.971905-128.907928 77.414137-128.907928 77.414137-8.844435 1.867534-15.078417 13.68365-12.603039 23.76424 2.475378 10.082636 13.253862 16.136515 21.605064 12.242835 0 0 80.274278-26.195615 140.430356-84.256986C819.492256 769.20392 843.640236 721.763444 843.843874 721.201649z"
fill="#000000" p-id="2482"></path>
<path
d="M375.327224 624.215758m-37.46631 0a36.613 36.613 0 1 0 74.93262 0 36.613 36.613 0 1 0-74.93262 0Z"
fill="#000000" p-id="2483"></path>
<path
d="M657.294131 624.215758m-37.46631 0a36.613 36.613 0 1 0 74.93262 0 36.613 36.613 0 1 0-74.93262 0Z"
fill="#000000" p-id="2484"></path>
</svg>
</div>
<!-- GPT回答的信息 -->
<!-- <span v-html="message.text.replace(/\n/g, '<br>')"> -->
<div class="bot-txt" v-html="md.render(message.text)" ></div>
</div>
</div>
</div>
<!-- 輸入框 -->
<div class="iptbox" v-if="isSearching===true">
<el-input class="input" disabled type="text" v-model="userInput"
placeholder="思考中,請稍等。。。" />
<el-button @click="sendMessageQianfan" disabled type="primary">發(fā)送</el-button>
</div>
<div class="iptbox" v-else>
<el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageQianfan"
placeholder="請輸入要查詢的問題..." />
<el-button @click="sendMessageQianfan" type="primary">發(fā)送</el-button>
</div>
</el-tab-pane>
<el-tab-pane label="通義千問" name="tongyi">
<div class="kefubox" ref="containerReftongyi">
<div v-for="message in chatHistoryTongyi" :key="message.id">
<!-- 遍歷用戶發(fā)送的信息,如果role為user就返回信息 -->
<div v-if="message.sender === 'user'" class="user-message">
<!-- 用戶發(fā)送的信息 -->
<div class="userBox">
<!-- icon圖標 -->
<svg t="1686817502794" class="icon" viewBox="0 0 1024 1024" version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="3466" width="30" height="30">
<path fill="#000000"
d="M500 128.8c-95.2 5.6-173.6 83.2-180 178.4-7.2 112 80.8 205.6 191.2 205.6 106.4 0 192-86.4 192-192 0.8-110.4-92-198.4-203.2-192zM512 575.2c-128 0-383.2 64-383.2 192v96c0 17.6 14.4 32 32 32h702.4c17.6 0 32-14.4 32-32V766.4c0-127.2-255.2-191.2-383.2-191.2z"
p-id="3467">
</path>
</svg>
</div>
<!-- 用戶輸入的問題 -->
<span>{{ message.text }}</span>
</div>
<div v-else class="bot-message" style="margin: 5px 0px;padding:5px 10px;">
<!-- icon圖標 -->
<div style="display: flex;align-items: center;margin-right: 10px;">
<svg t="1686814946576" class="icon" viewBox="0 0 1024 1024" version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="2479" width="30" height="30">
<path
d="M959.716928 554.950208M532.429285 958.838931M64.446801 594.626859M270.969435 57.464783"
fill="#000000" p-id="2480"></path>
<path
d="M583.719438 385.762901C664.377456 493.897713 810.145377 538.433022 810.145377 538.433022 799.252283 351.240643 687.435615 259.769348 510.411829 258.869862 298.641681 257.789251 175.627996 429.173596 222.010373 597.866646 222.00935 597.866646 580.769246 462.953957 583.719438 385.762901z"
fill="#000000" p-id="2481"></path>
<path
d="M843.843874 721.201649c65.939805-16.944927 114.865097-15.887852 114.865097-150.871149 0-65.013713-38.867216-120.714316-93.980441-144.029324C824.128857 272.551466 697.718819 116.236328 507.890402 111.958908c-6.053879-0.135076-12.062733-0.157589-18.004049 0-183.887101 4.724605-309.892933 146.908908-324.069817 311.821865-58.44511 21.626553-102.44216 79.103616-100.462063 146.549727 4.432962 151.456481 66.321498 153.863297 148.712997 155.550729l0-293.098431c1.732457-125.304867 124.495432-264.293385 293.822932-264.293385 168.313404 0 261.415848 130.999566 302.464753 247.369947l0 301.380048c-8.28264 19.399839-30.449499 41.206494-70.936609 80.296791-52.819996 50.971905-128.907928 77.414137-128.907928 77.414137-8.844435 1.867534-15.078417 13.68365-12.603039 23.76424 2.475378 10.082636 13.253862 16.136515 21.605064 12.242835 0 0 80.274278-26.195615 140.430356-84.256986C819.492256 769.20392 843.640236 721.763444 843.843874 721.201649z"
fill="#000000" p-id="2482"></path>
<path
d="M375.327224 624.215758m-37.46631 0a36.613 36.613 0 1 0 74.93262 0 36.613 36.613 0 1 0-74.93262 0Z"
fill="#000000" p-id="2483"></path>
<path
d="M657.294131 624.215758m-37.46631 0a36.613 36.613 0 1 0 74.93262 0 36.613 36.613 0 1 0-74.93262 0Z"
fill="#000000" p-id="2484"></path>
</svg>
</div>
<!-- Tongyi回答的信息 -->
<!-- <span v-html="message.text.replace(/\n/g, '<br>')"> -->
<div class="bot-txt" v-html="md.render(message.text)"></div>
</div>
</div>
</div>
<!-- 輸入框 -->
<!-- 輸入框 -->
<div class="iptbox" v-if="isSearching===true">
<el-input class="input" disabled type="text" v-model="userInput"
placeholder="思考中,請稍等。。。" />
<el-button @click="sendMessageTongyi" disabled type="primary">發(fā)送</el-button>
</div>
<div class="iptbox" v-else>
<el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageTongyi"
placeholder="請輸入要查詢的問題..." />
<el-button @click="sendMessageTongyi" type="primary">發(fā)送</el-button>
</div>
</el-tab-pane>
<!-- 圖片 -->
<el-tab-pane label="AI畫原型" name="chatgpt">
<div class="kefubox" ref="containerRefImage">
<div v-for="message in chatHistoryImage" :key="message.id">
<!-- 遍歷用戶發(fā)送的信息,如果role為user就返回信息 -->
<div v-if="message.sender === 'user'" class="user-message">
<!-- 用戶發(fā)送的信息 -->
<div class="userBox">
<!-- icon圖標 -->
<svg t="1686817502794" class="icon" viewBox="0 0 1024 1024" version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="3466" width="30" height="30">
<path fill="#000000"
d="M500 128.8c-95.2 5.6-173.6 83.2-180 178.4-7.2 112 80.8 205.6 191.2 205.6 106.4 0 192-86.4 192-192 0.8-110.4-92-198.4-203.2-192zM512 575.2c-128 0-383.2 64-383.2 192v96c0 17.6 14.4 32 32 32h702.4c17.6 0 32-14.4 32-32V766.4c0-127.2-255.2-191.2-383.2-191.2z"
p-id="3467">
</path>
</svg>
</div>
<!-- 用戶輸入的問題 -->
<span>{{ message.text }}</span>
</div>
<div v-else class="bot-message" style="margin: 5px 0px;padding:5px 10px;">
<!-- icon圖標 -->
<div style="display: flex;align-items: center;margin-right: 10px;">
<svg t="1686814946576" class="icon" viewBox="0 0 1024 1024" version="1.1"
xmlns="http://www.w3.org/2000/svg" p-id="2479" width="30" height="30">
<path
d="M959.716928 554.950208M532.429285 958.838931M64.446801 594.626859M270.969435 57.464783"
fill="#000000" p-id="2480"></path>
<path
d="M583.719438 385.762901C664.377456 493.897713 810.145377 538.433022 810.145377 538.433022 799.252283 351.240643 687.435615 259.769348 510.411829 258.869862 298.641681 257.789251 175.627996 429.173596 222.010373 597.866646 222.00935 597.866646 580.769246 462.953957 583.719438 385.762901z"
fill="#000000" p-id="2481"></path>
<path
d="M843.843874 721.201649c65.939805-16.944927 114.865097-15.887852 114.865097-150.871149 0-65.013713-38.867216-120.714316-93.980441-144.029324C824.128857 272.551466 697.718819 116.236328 507.890402 111.958908c-6.053879-0.135076-12.062733-0.157589-18.004049 0-183.887101 4.724605-309.892933 146.908908-324.069817 311.821865-58.44511 21.626553-102.44216 79.103616-100.462063 146.549727 4.432962 151.456481 66.321498 153.863297 148.712997 155.550729l0-293.098431c1.732457-125.304867 124.495432-264.293385 293.822932-264.293385 168.313404 0 261.415848 130.999566 302.464753 247.369947l0 301.380048c-8.28264 19.399839-30.449499 41.206494-70.936609 80.296791-52.819996 50.971905-128.907928 77.414137-128.907928 77.414137-8.844435 1.867534-15.078417 13.68365-12.603039 23.76424 2.475378 10.082636 13.253862 16.136515 21.605064 12.242835 0 0 80.274278-26.195615 140.430356-84.256986C819.492256 769.20392 843.640236 721.763444 843.843874 721.201649z"
fill="#000000" p-id="2482"></path>
<path
d="M375.327224 624.215758m-37.46631 0a36.613 36.613 0 1 0 74.93262 0 36.613 36.613 0 1 0-74.93262 0Z"
fill="#000000" p-id="2483"></path>
<path
d="M657.294131 624.215758m-37.46631 0a36.613 36.613 0 1 0 74.93262 0 36.613 36.613 0 1 0-74.93262 0Z"
fill="#000000" p-id="2484"></path>
</svg>
</div>
<!-- GPT回答的信息 -->
<!-- <span v-html="message.text.replace(/\n/g, '<br>')"> -->
<div class="bot-txt"><img :src="message.text"/> </div>
</div>
</div>
</div>
<!-- 輸入框 -->
<!-- <div class="iptbox">
<el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageImage"
placeholder="請輸入要查詢的問題..." />
<el-button @click="sendMessageImage" type="primary">11發(fā)送</el-button>
</div> -->
<div class="iptbox" v-if="isSearching===true">
<el-input class="input" disabled type="text" v-model="userInput"
placeholder="思考中,請稍等。。。" />
<el-button @click="sendMessageTongyi" disabled type="primary">發(fā)送</el-button>
</div>
<div class="iptbox" v-else>
<el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageImage"
placeholder="請輸入要查詢的問題..." />
<el-button @click="sendMessageImage" type="primary">發(fā)送</el-button>
</div>
</el-tab-pane>
</el-tabs>
</div>
</template>
<script setup>
import { defineComponent, ref, onUpdated } from 'vue';
import axios from 'axios';
import { md } from "/@/utils/markdown";
import {ElMessage, ElMessageBox} from 'element-plus';
const containerRef = ref(null)//給div綁定ref之后,需要先定義一下,才能夠使用
const containerReftongyi = ref(null)//給div綁定ref之后,需要先定義一下,才能夠使用
const containerRefChatgpt = ref(null)//給div綁定ref之后,需要先定義一下,才能夠使用
const containerRefImage = ref(null)//給div綁定ref之后,需要先定義一下,才能夠使用
// 聊天歷史記錄
const chatHistoryWenxin = ref([]);
const chatHistoryTongyi = ref([]);
const chatHistoryChatgpt = ref([]);
const chatHistoryImage = ref([]);
const isSearching = ref(false);
// 用戶輸入的問題
const userInput = ref('');
const activeName = ref('wenxin')
const AK = ref("HeiEnBprXPDmcFm5wmV0Ak5D")
const SK = ref("9ttnTF6jTWmb91XgRI3XQGtcB789Tc6s")
const handleClick = (TabsPaneContext, event) => {
// console.log(TabsPaneContext, event)
}
// 1.可樂,點擊發(fā)送文心一言事件
const sendMessageWenxin = async () => {
const message = {
id: chatHistoryWenxin.value.length + 1,
// 默認發(fā)送者設(shè)置成了user
sender: 'user',
// 此時是用戶輸入的問題
text: userInput.value
};
// 將這些信息存入到聊天歷史記錄的數(shù)組中
chatHistoryWenxin.value.push(message);
// 然后清空輸入框
userInput.value = '';
try {
// Luck Cola
const headers = {
'Content-Type': 'application/json', //請求頭這里不用改
'Accept': 'application/json'
};
// 發(fā)送請求傳送的Data數(shù)據(jù)
const data = {
"ques": message.text,
// 官網(wǎng)-個人中心-Appkey獲取
"appKey": "65324c559cfbe9915ead769b",
// 官網(wǎng)-個人中心-用戶ID
"uid": "GFUFJU1697795157477R4iSgAXjYg",
// 是否支持上下文 值1表示支持,0表示不支持
"isLongChat": 1
};
const apiUrl = 'https://luckycola.com.cn/ai/openwxyy';
// 獲取AI返回的回答
isSearching.value = true
const result = await axios.post(apiUrl, data, { headers })
// .then(() => {
// isSearching.value = false
// console.log('超時了:',);
// })
// console.log(result);
isSearching.value = false
// AI返回的所有信息
if(result.data.code === -1){
ElMessage.error(result.data.msg);
return
}
const botMessage = {
id: chatHistoryWenxin.value.length + 1,
sender: 'bot',
// text: result.data.choices[0].message.content // openAi
text: result.data.data.result
};
// 將AI返回的信息存入到歷史列表中
chatHistoryWenxin.value.push(botMessage);
// 自動滾屏
setTimeout(()=>{
containerRef.value.scrollTop = containerRef.value.scrollHeight
},200)
} catch (error) {
console.error(error);
}
};
// 1.點擊發(fā)送千帆、文心一言事件
const sendMessageQianfan = async () => {
const message = {
id: chatHistoryWenxin.value.length + 1,
// 默認發(fā)送者設(shè)置成了user
sender: 'user',
// 此時是用戶輸入的問題
text: userInput.value
};
// 將這些信息存入到聊天歷史記錄的數(shù)組中
chatHistoryWenxin.value.push(message);
// 然后清空輸入框
userInput.value = '';
try {
// Luck Cola
const headers = {
'Content-Type': 'application/json', //請求頭這里不用改
'Accept': 'application/json'
};
// 發(fā)送請求傳送的Data數(shù)據(jù)
const data = {
"messages":[{
"role": "user",
"content": message.text,
}]
};
const apiUrl = '/wenxin1123_2/wenxinworkshop/chat/completions?access_token='+ await getAccessToken()
// 獲取AI返回的回答
isSearching.value = true
const result = await axios.post(apiUrl, data, { headers })
// .then(() => {
// isSearching.value = false
// console.log('超時了:',);
// })
// console.log(result);
isSearching.value = false
// AI返回的所有信息
if(result.data.code === -1){
ElMessage.error(result.data.msg);
return
}
const botMessage = {
id: chatHistoryWenxin.value.length + 1,
sender: 'bot',
// text: result.data.choices[0].message.content // openAi
text: result.data.data.respBody.result
};
// 將AI返回的信息存入到歷史列表中
chatHistoryWenxin.value.push(botMessage);
// 自動滾屏
setTimeout(()=>{
containerRef.value.scrollTop = containerRef.value.scrollHeight
},200)
} catch (error) {
console.error(error);
}
};
const getAccessToken = async () => {
const apiUrl = '/wenxin1123/oauth/2.0/token?grant_type=client_credentials&client_id=' + AK.value + '&client_secret=' + SK.value
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
let result = await axios.post(apiUrl, '', { headers })
console.log('result.data:', result.data.access_token)
return result.data.access_token
}
// 點擊發(fā)送通義千問事件
const sendMessageTongyi = async () => {
const message = {
id: chatHistoryTongyi.value.length + 1,
// 默認發(fā)送者設(shè)置成了user
sender: 'user',
// 此時是用戶輸入的問題
text: userInput.value
};
// 將這些信息存入到聊天歷史記錄的數(shù)組中
chatHistoryTongyi.value.push(message);
// 然后清空輸入框
userInput.value = '';
const apiKey = 'sk-3e9b56374dc6470dad4d194f811d6feb';
try {
const headers = {
'Content-Type': 'application/json', //請求頭這里不用改
'Authorization': 'Bearer '+apiKey
};
const data = {
model: "qwen-turbo",
input: {
messages: [
{
role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: message.text
}
]
},
"parameters": {
}
}
const apiUrl = '/aliyunc/v1/services/aigc/text-generation/generation';
// 獲取AI返回的回答
isSearching.value = true
const result = await axios.post(apiUrl, JSON.stringify(data), { headers })
// .then((response) => {
// // The response data should now be correctly decoded as UTF-8
// // console.log(response);
// })
// .catch((error) => {
// console.error(error);
// });
// console.log(result);
isSearching.value = false
// AI返回的所有信息
// if(result.data.code === -1){
// ElMessage.error(result.data.output.text);
// return
// }
const botMessage = {
id: chatHistoryTongyi.value.length + 1,
sender: 'bot',
// text: result.data.choices[0].message.content // openAi
text: result.data.output.text
};
console.log('result:', result.data.output.text)
// 將AI返回的信息存入到歷史列表中
chatHistoryTongyi.value.push(botMessage);
// 自動滾屏
setTimeout(()=>{
containerReftongyi.value.scrollTop = containerReftongyi.value.scrollHeight
},200)
} catch (error) {
console.error(error);
}
};
// 點擊發(fā)送文心一言圖片事件
const sendMessageImage = async () => {
const message = {
id: chatHistoryImage.value.length + 1,
// 默認發(fā)送者設(shè)置成了user
sender: 'user',
// 此時是用戶輸入的問題
text: userInput.value
};
// 將這些信息存入到聊天歷史記錄的數(shù)組中
chatHistoryImage.value.push(message);
// 然后清空輸入框
userInput.value = '';
try {
// Luck Cola
const headers = {
'Content-Type': 'application/json', //請求頭這里不用改
'Accept': 'application/json'
};
// 發(fā)送請求傳送的Data數(shù)據(jù)
const data =
{
// 對生成圖片的詳細要求的描述詞,描述越準確生成結(jié)果越精美
"imgTxt": message.text,
// 官網(wǎng)-個人中心-Appkey獲取
"appKey": "65324c559cfbe9915ead769b",
// 官網(wǎng)-個人中心-用戶ID獲取
"uid": "GFUFJU1697795157477R4iSgAXjYg",
// 繪畫風格 可選值: ['探索無限'、 '古風',、'二次元'、'寫實風格'、'浮世繪'、 'low poly' ,、'未來主義'、 '像素風格',、'概念藝術(shù)'、'賽博朋克'、'洛麗塔風格'、 '巴洛克風格'、'超現(xiàn)實主義'、 '水彩畫'、'蒸汽波藝術(shù)'、 '油畫'、'卡通畫']
"style": "寫實風格",
// 生成圖片的寬高比,值可選有:"1:1"、"3:2"、"2:3"
"ratio": "3:2"
}
const apiUrl = 'https://mock.apifox.com/m1/3235504-0-default/aiImg/getAiImage';
// const apiUrl = 'https://luckycola.com.cn/aiImg/getAiImage';
// 獲取AI返回的回答
isSearching.value = true
const result = await axios.post(apiUrl, data, { headers })
// .then(() => {
// isSearching.value = false
// console.log('超時了:',);
// })
// console.log(result);
isSearching.value = false
// AI返回的所有信息
if(result.data.code === -1){
ElMessage.error(result.data.msg);
return
}
const botMessage = {
id: chatHistoryImage.value.length + 1,
sender: 'bot',
// text: result.data.choices[0].message.content // openAi
text: result.data.data.result.img
};
// 將AI返回的信息存入到歷史列表中
chatHistoryImage.value.push(botMessage);
// 自動滾屏
if(isNaN(containerRefImage.value.scrollTop)){
containerRefImage.value.scrollTop = 0
}
setTimeout(()=>{
containerRefImage.value.scrollTop = containerRefImage.value.scrollHeight
},200)
} catch (error) {
console.error(error);
}
};
// 點擊發(fā)送chatGpt事件
const sendMessageChatgpt = async () => {
const message = {
id: chatHistoryChatgpt.value.length + 1,
// 默認發(fā)送者設(shè)置成了user
sender: 'user',
// 此時是用戶輸入的問題
text: userInput.value
};
// 將這些信息存入到聊天歷史記錄的數(shù)組中
chatHistoryChatgpt.value.push(message);
// 然后清空輸入框
userInput.value = '';
try {
// chatgpt
const apiKey = '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'; // 替換為你的 OpenAI API 密鑰
const prompt = message.text;//用戶輸入的問題 要問的問題
const apiUrl = 'https://api.openai.com/v1/chat/completions'; //固定的地址,api模型
const headers = {
'Content-Type': 'application/json', //請求頭這里不用改
'Authorization': `Bearer ${apiKey}` //一定要將變量apikey填寫正確才可以請求到
};
// 發(fā)送請求傳送的Data數(shù)據(jù)
const data = {
messages: [
// 定義的角色
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: prompt },
{ role: "assistant", content: "The Los Angeles Dodgers won the World Series in 2020." },
],
max_tokens: 100,
model: "gpt-3.5-turbo"
};
// prompt(必需):作為輸入給模型的文本提示。它是一個字符串,描述了你想要問模型的問題或提供給模型的上下文信息。
// max_tokens(可選):用于控制生成回復的長度。它是一個整數(shù),表示模型生成的最大標記數(shù)量。默認值為 16。
// temperature(可選):用于控制生成回復的創(chuàng)造性和隨機性。它是一個浮點數(shù),值越高,生成的結(jié)果越隨機,值越低,生成的結(jié)果越保守。默認值為 0.6。
// top_p(可選):用于控制生成回復的多樣性。它是一個浮點數(shù),介于 0 和 1 之間。較低的值會生成更保守和一致的回復,較高的值會生成更多樣化的回復。默認值為 1.0。
// n(可選):用于控制生成回復的數(shù)量。它是一個整數(shù),表示你想要生成的回復的數(shù)量。默認值為 1。
isSearching.value = true
// 獲取AI返回的回答
const result = await axios.post(apiUrl, data, { headers })
// console.log(result);
const botMessage = {
id: chatHistoryChatgpt.value.length + 1,
sender: 'bot',
text: result.data.choices[0].message.content // openAi
};
if(result.data.error){
botMessage.text = result.data.error.message
}
isSearching.value = false
// 將AI返回的信息存入到歷史列表中
chatHistoryChatgpt.value.push(botMessage);
setTimeout(()=>{
containerRefChatgpt.value.scrollTop = containerRefChatgpt.value.scrollHeight
},200)
} catch (error) {
console.error(error);
}
};
</script>
<style scoped>
.el-tabs__header{
margin-left: 40px !important;
}
.kefubox{
overflow-y:auto;
width:95%;
margin: auto;
height:calc(100vh - 246px);
}
.box {
height: 500px;
}
.bot-txt{
padding-left: 16px;
margin:30px 0;
}
.iptbox {
width: 80vw;
display: flex;
bottom: 5px;
padding-left: 42px;
margin:auto;
/* justify-content: center; */
align-items: center;
position: fixed;
}
.input{
width:calc(90% -200px);
}
.iptbox input {
width: 490px;
height: 40px;
padding-left: 5px;
padding-right: 70px;
border-radius: 10px;
}
.iptbox button {
margin-left: 20px;
}
.bot-message,
.user-message {
display: flex;
align-items: center;
}
.bot-message {
background-color: #ebeef5;
color: #000000;
border-radius: 10px;
}
.user-message {
background-color: #d5eaff;
color: #000000;
padding: 5px 10px;
border-radius: 10px;
}
.userBox {
margin-right: 10px;
display: flex;
align-items: center;
}
pre.hljs {
background-color: #f4f4f4;
padding: 10px;
}
pre.hljs code {
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
font-size: 14px;
}
</style>
到了這里,關(guān)于文心一言AI大模型,前端接入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!