文心一言AI大模型,前端接入
温馨提示:这篇文章已超过367天没有更新,请注意相关的内容是否还可用!
文心一言AI大模型,前端接入
一、参考接口资料
模型广场:https://console.bce.baidu.com/qianfan/modelcenter/model/buildIn/list
我的应用:https://console.bce.baidu.com/qianfan/ais/console/onlineService
千帆大模型调用API介绍:
https://cloud.baidu.com/doc/WENXINWORKSHOP/s/flfmc9do2
在线调试:
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
二、vue代码:
{{ message.text }}
发送
发送
{{ message.text }}
发送
发送
{{ message.text }}
发送
发送
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.可乐,点击发送文心一言事件
const sendMessageWenxin = async () => {
const message = {
id: chatHistoryWenxin.value.length + 1,
// 默认发送者设置成了user
sender: 'user',
// 此时是用户输入的问题
text: userInput.value
};
// 将这些信息存入到聊天历史记录的数组中
chatHistoryWenxin.value.push(message);
// 然后清空输入框
userInput.value = '';
try {
// Luck Cola
const headers = {
'Content-Type': 'application/json', //请求头这里不用改
'Accept': 'application/json'
};
// 发送请求传送的Data数据
const data = {
"ques": message.text,
// 官网-个人中心-Appkey获取
"appKey": "65324c559cfbe9915ead769b",
// 官网-个人中心-用户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.点击发送千帆、文心一言事件
const sendMessageQianfan = async () => {
const message = {
id: chatHistoryWenxin.value.length + 1,
// 默认发送者设置成了user
sender: 'user',
// 此时是用户输入的问题
text: userInput.value
};
// 将这些信息存入到聊天历史记录的数组中
chatHistoryWenxin.value.push(message);
// 然后清空输入框
userInput.value = '';
try {
// Luck Cola
const headers = {
'Content-Type': 'application/json', //请求头这里不用改
'Accept': 'application/json'
};
// 发送请求传送的Data数据
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
}
// 点击发送通义千问事件
const sendMessageTongyi = async () => {
const message = {
id: chatHistoryTongyi.value.length + 1,
// 默认发送者设置成了user
sender: 'user',
// 此时是用户输入的问题
text: userInput.value
};
// 将这些信息存入到聊天历史记录的数组中
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);
}
};
// 点击发送文心一言图片事件
const sendMessageImage = async () => {
const message = {
id: chatHistoryImage.value.length + 1,
// 默认发送者设置成了user
sender: 'user',
// 此时是用户输入的问题
text: userInput.value
};
// 将这些信息存入到聊天历史记录的数组中
chatHistoryImage.value.push(message);
// 然后清空输入框
userInput.value = '';
try {
// Luck Cola
const headers = {
'Content-Type': 'application/json', //请求头这里不用改
'Accept': 'application/json'
};
// 发送请求传送的Data数据
const data =
{
// 对生成图片的详细要求的描述词,描述越准确生成结果越精美
"imgTxt": message.text,
// 官网-个人中心-Appkey获取
"appKey": "65324c559cfbe9915ead769b",
// 官网-个人中心-用户ID获取
"uid": "GFUFJU1697795157477R4iSgAXjYg",
// 绘画风格 可选值: ['探索无限'、 '古风',、'二次元'、'写实风格'、'浮世绘'、 'low poly' ,、'未来主义'、 '像素风格',、'概念艺术'、'赛博朋克'、'洛丽塔风格'、 '巴洛克风格'、'超现实主义'、 '水彩画'、'蒸汽波艺术'、 '油画'、'卡通画']
"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);
}
};
// 点击发送chatGpt事件
const sendMessageChatgpt = async () => {
const message = {
id: chatHistoryChatgpt.value.length + 1,
// 默认发送者设置成了user
sender: 'user',
// 此时是用户输入的问题
text: userInput.value
};
// 将这些信息存入到聊天历史记录的数组中
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填写正确才可以请求到
};
// 发送请求传送的Data数据
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(可选):用于控制生成回复的长度。它是一个整数,表示模型生成的最大标记数量。默认值为 16。
// temperature(可选):用于控制生成回复的创造性和随机性。它是一个浮点数,值越高,生成的结果越随机,值越低,生成的结果越保守。默认值为 0.6。
// top_p(可选):用于控制生成回复的多样性。它是一个浮点数,介于 0 和 1 之间。较低的值会生成更保守和一致的回复,较高的值会生成更多样化的回复。默认值为 1.0。
// n(可选):用于控制生成回复的数量。它是一个整数,表示你想要生成的回复的数量。默认值为 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);
}
};
.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;
}
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
