文心一言AI大模子,前端接入

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

文心一言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代码:
  1. <template>
  2.     <div>
  3.         <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" :disabled="isSearching===true">
  4.             <el-tab-pane label="文心一言" name="wenxin">
  5.             <div class="kefubox" ref="containerRef">
  6.                 <div v-for="message in chatHistoryWenxin" :key="message.id">
  7.                     <!-- 遍历用户发送的信息,如果role为user就返回信息 -->
  8.                     <div v-if="message.sender === 'user'" class="user-message">
  9.                         <!-- 用户发送的信息 -->
  10.                         <div class="userBox">
  11.                             <!-- icon图标 -->
  12.                             <svg t="1686817502794" class="icon" viewBox="0 0 1024 1024" version="1.1"
  13.                                 xmlns="http://www.w3.org/2000/svg" p-id="3466" width="30" height="30">
  14.                                 <path fill="#000000"
  15.                                     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"
  16.                                     p-id="3467">
  17.                                 </path>
  18.                             </svg>
  19.                         </div>
  20.                         <!-- 用户输入的问题 -->
  21.                         <span>{{ message.text }}</span>
  22.                     </div>
  23.                     <div v-else class="bot-message" style="margin: 5px 0px;padding:5px 10px;">
  24.                         <!-- icon图标 -->
  25.                         <div style="display: flex;align-items: center;margin-right: 10px;">
  26.                             <svg t="1686814946576" class="icon" viewBox="0 0 1024 1024" version="1.1"
  27.                                 xmlns="http://www.w3.org/2000/svg" p-id="2479" width="30" height="30">
  28.                                 <path
  29.                                     d="M959.716928 554.950208M532.429285 958.838931M64.446801 594.626859M270.969435 57.464783"
  30.                                     fill="#000000" p-id="2480"></path>
  31.                                 <path
  32.                                     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"
  33.                                     fill="#000000" p-id="2481"></path>
  34.                                 <path
  35.                                     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"
  36.                                     fill="#000000" p-id="2482"></path>
  37.                                 <path
  38.                                     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"
  39.                                     fill="#000000" p-id="2483"></path>
  40.                                 <path
  41.                                     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"
  42.                                     fill="#000000" p-id="2484"></path>
  43.                             </svg>
  44.                         </div>
  45.                         <!-- GPT回答的信息 -->
  46.                         <!-- <span v-html="message.text.replace(/\n/g, '<br>')"> -->
  47.                         <div class="bot-txt" v-html="md.render(message.text)" ></div>
  48.                     </div>
  49.                 </div>
  50.             </div>
  51.             <!-- 输入框 -->
  52.             <div class="iptbox" v-if="isSearching===true">
  53.                     <el-input class="input" disabled type="text" v-model="userInput"
  54.                         placeholder="思考中,请稍等。。。" />
  55.                     <el-button @click="sendMessageQianfan" disabled type="primary">发送</el-button>
  56.             </div>
  57.             <div class="iptbox" v-else>
  58.                     <el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageQianfan"
  59.                         placeholder="请输入要查询的问题..." />
  60.                     <el-button @click="sendMessageQianfan" type="primary">发送</el-button>
  61.             </div>
  62.             </el-tab-pane>
  63.             <el-tab-pane label="通义千问" name="tongyi">
  64.                 <div class="kefubox" ref="containerReftongyi">
  65.                 <div v-for="message in chatHistoryTongyi" :key="message.id">
  66.                     <!-- 遍历用户发送的信息,如果role为user就返回信息 -->
  67.                     <div v-if="message.sender === 'user'" class="user-message">
  68.                         <!-- 用户发送的信息 -->
  69.                         <div class="userBox">
  70.                             <!-- icon图标 -->
  71.                             <svg t="1686817502794" class="icon" viewBox="0 0 1024 1024" version="1.1"
  72.                                 xmlns="http://www.w3.org/2000/svg" p-id="3466" width="30" height="30">
  73.                                 <path fill="#000000"
  74.                                     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"
  75.                                     p-id="3467">
  76.                                 </path>
  77.                             </svg>
  78.                         </div>
  79.                         <!-- 用户输入的问题 -->
  80.                         <span>{{ message.text }}</span>
  81.                     </div>
  82.                     <div v-else class="bot-message" style="margin: 5px 0px;padding:5px 10px;">
  83.                         <!-- icon图标 -->
  84.                         <div style="display: flex;align-items: center;margin-right: 10px;">
  85.                             <svg t="1686814946576" class="icon" viewBox="0 0 1024 1024" version="1.1"
  86.                                 xmlns="http://www.w3.org/2000/svg" p-id="2479" width="30" height="30">
  87.                                 <path
  88.                                     d="M959.716928 554.950208M532.429285 958.838931M64.446801 594.626859M270.969435 57.464783"
  89.                                     fill="#000000" p-id="2480"></path>
  90.                                 <path
  91.                                     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"
  92.                                     fill="#000000" p-id="2481"></path>
  93.                                 <path
  94.                                     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"
  95.                                     fill="#000000" p-id="2482"></path>
  96.                                 <path
  97.                                     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"
  98.                                     fill="#000000" p-id="2483"></path>
  99.                                 <path
  100.                                     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"
  101.                                     fill="#000000" p-id="2484"></path>
  102.                             </svg>
  103.                         </div>
  104.                         <!-- Tongyi回答的信息 -->
  105.                         <!-- <span v-html="message.text.replace(/\n/g, '<br>')"> -->
  106.                         <div class="bot-txt" v-html="md.render(message.text)"></div>
  107.                     </div>
  108.                 </div>
  109.             </div>
  110.             <!-- 输入框 -->
  111.             <!-- 输入框 -->
  112.             <div class="iptbox" v-if="isSearching===true">
  113.                     <el-input class="input" disabled type="text" v-model="userInput"
  114.                         placeholder="思考中,请稍等。。。" />
  115.                     <el-button @click="sendMessageTongyi" disabled type="primary">发送</el-button>
  116.             </div>
  117.             <div class="iptbox" v-else>
  118.                     <el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageTongyi"
  119.                         placeholder="请输入要查询的问题..." />
  120.                     <el-button @click="sendMessageTongyi" type="primary">发送</el-button>
  121.             </div>
  122.             </el-tab-pane>
  123.             <!-- 图片 -->
  124.             <el-tab-pane label="AI画原型" name="chatgpt">
  125.                 <div class="kefubox" ref="containerRefImage">
  126.                 <div v-for="message in chatHistoryImage" :key="message.id">
  127.                     <!-- 遍历用户发送的信息,如果role为user就返回信息 -->
  128.                     <div v-if="message.sender === 'user'" class="user-message">
  129.                         <!-- 用户发送的信息 -->
  130.                         <div class="userBox">
  131.                             <!-- icon图标 -->
  132.                             <svg t="1686817502794" class="icon" viewBox="0 0 1024 1024" version="1.1"
  133.                                 xmlns="http://www.w3.org/2000/svg" p-id="3466" width="30" height="30">
  134.                                 <path fill="#000000"
  135.                                     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"
  136.                                     p-id="3467">
  137.                                 </path>
  138.                             </svg>
  139.                         </div>
  140.                         <!-- 用户输入的问题 -->
  141.                         <span>{{ message.text }}</span>
  142.                     </div>
  143.                     <div v-else class="bot-message" style="margin: 5px 0px;padding:5px 10px;">
  144.                         <!-- icon图标 -->
  145.                         <div style="display: flex;align-items: center;margin-right: 10px;">
  146.                             <svg t="1686814946576" class="icon" viewBox="0 0 1024 1024" version="1.1"
  147.                                 xmlns="http://www.w3.org/2000/svg" p-id="2479" width="30" height="30">
  148.                                 <path
  149.                                     d="M959.716928 554.950208M532.429285 958.838931M64.446801 594.626859M270.969435 57.464783"
  150.                                     fill="#000000" p-id="2480"></path>
  151.                                 <path
  152.                                     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"
  153.                                     fill="#000000" p-id="2481"></path>
  154.                                 <path
  155.                                     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"
  156.                                     fill="#000000" p-id="2482"></path>
  157.                                 <path
  158.                                     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"
  159.                                     fill="#000000" p-id="2483"></path>
  160.                                 <path
  161.                                     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"
  162.                                     fill="#000000" p-id="2484"></path>
  163.                             </svg>
  164.                         </div>
  165.                         <!-- GPT回答的信息 -->
  166.                         <!-- <span v-html="message.text.replace(/\n/g, '<br>')"> -->
  167.                         <div class="bot-txt"><img :src="message.text"/> </div>
  168.                     </div>
  169.                 </div>
  170.             </div>
  171.             <!-- 输入框 -->
  172.             <!-- <div class="iptbox">
  173.                     <el-input  class="input" type="text" v-model="userInput" @keyup.enter="sendMessageImage"
  174.                      placeholder="请输入要查询的问题..." />
  175.                     <el-button @click="sendMessageImage" type="primary">11发送</el-button>
  176.             </div> -->
  177.              <div class="iptbox" v-if="isSearching===true">
  178.                     <el-input class="input" disabled type="text" v-model="userInput"
  179.                         placeholder="思考中,请稍等。。。" />
  180.                     <el-button @click="sendMessageTongyi" disabled type="primary">发送</el-button>
  181.             </div>
  182.             <div class="iptbox" v-else>
  183.                     <el-input class="input" type="text" v-model="userInput" @keyup.enter="sendMessageImage"
  184.                         placeholder="请输入要查询的问题..." />
  185.                     <el-button @click="sendMessageImage" type="primary">发送</el-button>
  186.             </div>
  187.             </el-tab-pane>
  188.         </el-tabs>
  189.             
  190.     </div>
  191. </template>
  192. <script setup>
  193. import { defineComponent, ref, onUpdated } from 'vue';
  194. import axios from 'axios';
  195. import { md } from "/@/utils/markdown";
  196. import {ElMessage, ElMessageBox} from 'element-plus';
  197. const containerRef = ref(null)//给div绑定ref之后,需要先定义一下,才能够使用
  198. const containerReftongyi = ref(null)//给div绑定ref之后,需要先定义一下,才能够使用
  199. const containerRefChatgpt = ref(null)//给div绑定ref之后,需要先定义一下,才能够使用
  200. const containerRefImage = ref(null)//给div绑定ref之后,需要先定义一下,才能够使用
  201. // 聊天历史记录
  202. const chatHistoryWenxin = ref([]);
  203. const chatHistoryTongyi = ref([]);
  204. const chatHistoryChatgpt = ref([]);
  205. const chatHistoryImage = ref([]);
  206. const isSearching = ref(false);
  207. // 用户输入的问题
  208. const userInput = ref('');
  209. const activeName = ref('wenxin')
  210. const AK = ref("HeiEnBprXPDmcFm5wmV0Ak5D")
  211. const SK = ref("9ttnTF6jTWmb91XgRI3XQGtcB789Tc6s")
  212. const handleClick = (TabsPaneContext, event) => {
  213. //   console.log(TabsPaneContext, event)
  214. }
  215. // 1.可乐,点击发送文心一言事件
  216. const sendMessageWenxin = async () => {
  217.     const message = {
  218.         
  219.         id: chatHistoryWenxin.value.length + 1,
  220.         // 默认发送者设置成了user
  221.         sender: 'user',
  222.         // 此时是用户输入的问题
  223.         text: userInput.value
  224.     };
  225.    
  226.     // 将这些信息存入到聊天历史记录的数组中
  227.     chatHistoryWenxin.value.push(message);
  228.     // 然后清空输入框
  229.     userInput.value = '';
  230.     try {
  231.         // Luck Cola
  232.         const headers = {
  233.             'Content-Type': 'application/json',  //请求头这里不用改
  234.             'Accept': 'application/json'
  235.         };
  236.         // 发送请求传送的Data数据
  237.         const data = {
  238.             "ques": message.text,
  239.             // 官网-个人中心-Appkey获取
  240.             "appKey": "65324c559cfbe9915ead769b",
  241.             // 官网-个人中心-用户ID
  242.             "uid": "GFUFJU1697795157477R4iSgAXjYg",
  243.             // 是否支持上下文 值1表示支持,0表示不支持
  244.             "isLongChat": 1
  245.         };
  246.         const apiUrl = 'https://luckycola.com.cn/ai/openwxyy';
  247.         // 获取AI返回的回答
  248.         isSearching.value = true
  249.         const result = await axios.post(apiUrl, data, { headers })
  250.         // .then(() => {
  251.         //     isSearching.value = false
  252.         //     console.log('超时了:',);
  253.         // })
  254.         // console.log(result);
  255.         isSearching.value = false
  256.         // AI返回的所有信息
  257.         if(result.data.code === -1){
  258.             ElMessage.error(result.data.msg);
  259.             return
  260.         }
  261.         const botMessage = {
  262.             id: chatHistoryWenxin.value.length + 1,
  263.             sender: 'bot',
  264.             // text: result.data.choices[0].message.content // openAi
  265.             text: result.data.data.result
  266.         };
  267.         
  268.         // 将AI返回的信息存入到历史列表中
  269.         chatHistoryWenxin.value.push(botMessage);
  270.         // 自动滚屏
  271.         setTimeout(()=>{
  272.             containerRef.value.scrollTop = containerRef.value.scrollHeight
  273.         },200)
  274.     } catch (error) {
  275.         console.error(error);
  276.     }
  277. };
  278. // 1.点击发送千帆、文心一言事件
  279. const sendMessageQianfan = async () => {
  280.     const message = {
  281.         
  282.         id: chatHistoryWenxin.value.length + 1,
  283.         // 默认发送者设置成了user
  284.         sender: 'user',
  285.         // 此时是用户输入的问题
  286.         text: userInput.value
  287.     };
  288.    
  289.     // 将这些信息存入到聊天历史记录的数组中
  290.     chatHistoryWenxin.value.push(message);
  291.     // 然后清空输入框
  292.     userInput.value = '';
  293.     try {
  294.         // Luck Cola
  295.         const headers = {
  296.             'Content-Type': 'application/json',  //请求头这里不用改
  297.             'Accept': 'application/json'
  298.         };
  299.         // 发送请求传送的Data数据
  300.         const data = {
  301.                 "messages":[{
  302.                     "role": "user",
  303.                     "content": message.text,
  304.             }]
  305.         };
  306.         const apiUrl = '/wenxin1123_2/wenxinworkshop/chat/completions?access_token='+ await getAccessToken()
  307.         // 获取AI返回的回答
  308.         isSearching.value = true
  309.         const result = await axios.post(apiUrl, data, { headers })
  310.         // .then(() => {   
  311.         //     isSearching.value = false
  312.         //     console.log('超时了:',);
  313.         // })
  314.         // console.log(result);
  315.         isSearching.value = false
  316.         // AI返回的所有信息
  317.         if(result.data.code === -1){
  318.             ElMessage.error(result.data.msg);
  319.             return
  320.         }
  321.         const botMessage = {
  322.             id: chatHistoryWenxin.value.length + 1,
  323.             sender: 'bot',
  324.             // text: result.data.choices[0].message.content // openAi
  325.             text: result.data.data.respBody.result
  326.         };
  327.         
  328.         // 将AI返回的信息存入到历史列表中
  329.         chatHistoryWenxin.value.push(botMessage);
  330.         // 自动滚屏
  331.         setTimeout(()=>{
  332.             containerRef.value.scrollTop = containerRef.value.scrollHeight
  333.         },200)
  334.     } catch (error) {
  335.         console.error(error);
  336.     }
  337. };
  338. const getAccessToken = async () => {
  339.     const apiUrl = '/wenxin1123/oauth/2.0/token?grant_type=client_credentials&client_id=' + AK.value + '&client_secret=' + SK.value
  340.     const headers = {
  341.         'Content-Type': 'application/json',
  342.         'Accept': 'application/json'
  343.     }
  344.     let result = await axios.post(apiUrl, '', { headers })
  345.     console.log('result.data:', result.data.access_token)
  346.     return result.data.access_token
  347. }
  348. // 点击发送通义千问事件
  349. const sendMessageTongyi = async () => {
  350.     const message = {
  351.         
  352.         id: chatHistoryTongyi.value.length + 1,
  353.         // 默认发送者设置成了user
  354.         sender: 'user',
  355.         // 此时是用户输入的问题
  356.         text: userInput.value
  357.     };
  358.    
  359.     // 将这些信息存入到聊天历史记录的数组中
  360.     chatHistoryTongyi.value.push(message);
  361.     // 然后清空输入框
  362.     userInput.value = '';
  363.     const apiKey = 'sk-3e9b56374dc6470dad4d194f811d6feb';
  364.     try {
  365.         const headers = {
  366.             'Content-Type': 'application/json',  //请求头这里不用改
  367.             'Authorization': 'Bearer '+apiKey
  368.         };
  369.         const data = {
  370.             model: "qwen-turbo",
  371.             input: {
  372.                 messages: [      
  373.                     {
  374.                         role: "system",
  375.                         content: "You are a helpful assistant."
  376.                     },
  377.                     {
  378.                         role: "user",
  379.                         content: message.text
  380.                     }
  381.                 ]
  382.             },
  383.             "parameters": {
  384.             }
  385.         }         
  386.         const apiUrl = '/aliyunc/v1/services/aigc/text-generation/generation';
  387.         // 获取AI返回的回答
  388.         isSearching.value = true
  389.         const result = await axios.post(apiUrl, JSON.stringify(data), { headers })
  390.         // .then((response) => {
  391.         //     // The response data should now be correctly decoded as UTF-8
  392.         //     // console.log(response);
  393.         // })
  394.         // .catch((error) => {
  395.         //     console.error(error);
  396.         // });
  397.         // console.log(result);
  398.         isSearching.value = false
  399.         // AI返回的所有信息
  400.         // if(result.data.code === -1){
  401.         //     ElMessage.error(result.data.output.text);
  402.         //     return
  403.         // }
  404.         const botMessage = {
  405.             id: chatHistoryTongyi.value.length + 1,
  406.             sender: 'bot',
  407.             // text: result.data.choices[0].message.content // openAi
  408.             text: result.data.output.text
  409.         };
  410.         console.log('result:', result.data.output.text)
  411.         // 将AI返回的信息存入到历史列表中
  412.         chatHistoryTongyi.value.push(botMessage);
  413.         // 自动滚屏
  414.         setTimeout(()=>{
  415.             containerReftongyi.value.scrollTop = containerReftongyi.value.scrollHeight
  416.         },200)
  417.     } catch (error) {
  418.         console.error(error);
  419.     }
  420. };
  421. // 点击发送文心一言图片事件
  422. const sendMessageImage = async () => {
  423.     const message = {
  424.         
  425.         id: chatHistoryImage.value.length + 1,
  426.         // 默认发送者设置成了user
  427.         sender: 'user',
  428.         // 此时是用户输入的问题
  429.         text: userInput.value
  430.     };
  431.    
  432.     // 将这些信息存入到聊天历史记录的数组中
  433.     chatHistoryImage.value.push(message);
  434.     // 然后清空输入框
  435.     userInput.value = '';
  436.     try {
  437.         // Luck Cola
  438.         const headers = {
  439.             'Content-Type': 'application/json',  //请求头这里不用改
  440.             'Accept': 'application/json'
  441.         };
  442.         // 发送请求传送的Data数据
  443.         const data =
  444.         {
  445.             // 对生成图片的详细要求的描述词,描述越准确生成结果越精美
  446.             "imgTxt": message.text,
  447.             // 官网-个人中心-Appkey获取
  448.             "appKey": "65324c559cfbe9915ead769b",
  449.             // 官网-个人中心-用户ID获取
  450.             "uid": "GFUFJU1697795157477R4iSgAXjYg",
  451.             // 绘画风格 可选值: ['探索无限'、 '古风',、'二次元'、'写实风格'、'浮世绘'、 'low poly' ,、'未来主义'、 '像素风格',、'概念艺术'、'赛博朋克'、'洛丽塔风格'、 '巴洛克风格'、'超现实主义'、 '水彩画'、'蒸汽波艺术'、 '油画'、'卡通画']
  452.             "style": "写实风格",
  453.             // 生成图片的宽高比,值可选有:"1:1"、"3:2"、"2:3"
  454.             "ratio": "3:2"
  455.         }
  456.         const apiUrl = 'https://mock.apifox.com/m1/3235504-0-default/aiImg/getAiImage';
  457.         // const apiUrl = 'https://luckycola.com.cn/aiImg/getAiImage';
  458.         // 获取AI返回的回答
  459.         isSearching.value = true
  460.         const result = await axios.post(apiUrl, data, { headers })
  461.         // .then(() => {
  462.         //     isSearching.value = false
  463.         //     console.log('超时了:',);
  464.         // })
  465.         // console.log(result);
  466.         isSearching.value = false
  467.         // AI返回的所有信息
  468.         if(result.data.code === -1){
  469.             ElMessage.error(result.data.msg);
  470.             return
  471.         }
  472.         const botMessage = {
  473.             id: chatHistoryImage.value.length + 1,
  474.             sender: 'bot',
  475.             // text: result.data.choices[0].message.content // openAi
  476.             text: result.data.data.result.img
  477.         };
  478.         
  479.         // 将AI返回的信息存入到历史列表中
  480.         chatHistoryImage.value.push(botMessage);
  481.         // 自动滚屏
  482.         if(isNaN(containerRefImage.value.scrollTop)){
  483.             containerRefImage.value.scrollTop = 0
  484.         }
  485.         setTimeout(()=>{
  486.             containerRefImage.value.scrollTop = containerRefImage.value.scrollHeight
  487.         },200)
  488.     } catch (error) {
  489.         console.error(error);
  490.     }
  491. };
  492. // 点击发送chatGpt事件
  493. const sendMessageChatgpt = async () => {
  494.     const message = {
  495.         id: chatHistoryChatgpt.value.length + 1,
  496.         // 默认发送者设置成了user
  497.         sender: 'user',
  498.         // 此时是用户输入的问题
  499.         text: userInput.value
  500.     };
  501.    
  502.     // 将这些信息存入到聊天历史记录的数组中
  503.     chatHistoryChatgpt.value.push(message);
  504.     // 然后清空输入框
  505.     userInput.value = '';
  506.     try {
  507.         // chatgpt
  508.         const apiKey = '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'; // 替换为你的 OpenAI API 密钥
  509.         const prompt = message.text;//用户输入的问题  要问的问题
  510.         const apiUrl = 'https://api.openai.com/v1/chat/completions'; //固定的地址,api模型
  511.         const headers = {
  512.             'Content-Type': 'application/json',  //请求头这里不用改
  513.             'Authorization': `Bearer ${apiKey}`  //一定要将变量apikey填写正确才可以请求到
  514.         };
  515.         // 发送请求传送的Data数据
  516.         const data = {
  517.             messages: [
  518.                 // 定义的角色
  519.                 { role: "system", content: "You are a helpful assistant." },
  520.                 { role: "user", content: prompt },
  521.                 { role: "assistant", content: "The Los Angeles Dodgers won the World Series in 2020." },
  522.             ],
  523.             max_tokens: 100,
  524.             model: "gpt-3.5-turbo"
  525.         };
  526.         // prompt(必需):作为输入给模型的文本提示。它是一个字符串,描述了你想要问模型的问题或提供给模型的上下文信息。
  527.         // max_tokens(可选):用于控制生成回复的长度。它是一个整数,表示模型生成的最大标记数量。默认值为 16。
  528.         // temperature(可选):用于控制生成回复的创造性和随机性。它是一个浮点数,值越高,生成的结果越随机,值越低,生成的结果越保守。默认值为 0.6。
  529.         // top_p(可选):用于控制生成回复的多样性。它是一个浮点数,介于 0 和 1 之间。较低的值会生成更保守和一致的回复,较高的值会生成更多样化的回复。默认值为 1.0。
  530.         // n(可选):用于控制生成回复的数量。它是一个整数,表示你想要生成的回复的数量。默认值为 1。
  531.         
  532.         isSearching.value = true
  533.         // 获取AI返回的回答
  534.         const result = await axios.post(apiUrl, data, { headers })
  535.         // console.log(result);
  536.         const botMessage = {
  537.             id: chatHistoryChatgpt.value.length + 1,
  538.             sender: 'bot',
  539.             text: result.data.choices[0].message.content // openAi
  540.         };
  541.         if(result.data.error){
  542.             botMessage.text = result.data.error.message
  543.         }
  544.         isSearching.value = false
  545.         // 将AI返回的信息存入到历史列表中
  546.         chatHistoryChatgpt.value.push(botMessage);
  547.         setTimeout(()=>{
  548.             containerRefChatgpt.value.scrollTop = containerRefChatgpt.value.scrollHeight
  549.         },200)
  550.     } catch (error) {
  551.         console.error(error);
  552.     }
  553. };
  554. </script>
  555. <style scoped>
  556. .el-tabs__header{
  557.     margin-left: 40px !important;
  558. }
  559. .kefubox{
  560.     overflow-y:auto;
  561.     width:95%;
  562.     margin: auto;
  563.     height:calc(100vh - 246px);
  564. }
  565. .box {
  566.     height: 500px;
  567. }
  568. .bot-txt{
  569.     padding-left: 16px;
  570.     margin:30px 0;
  571. }
  572. .iptbox {
  573.     width: 80vw;
  574.     display: flex;
  575.     bottom: 5px;
  576.     padding-left: 42px;
  577.     margin:auto;
  578.     /* justify-content: center; */
  579.     align-items: center;
  580.     position: fixed;
  581. }
  582. .input{
  583.     width:calc(90% -200px);
  584. }
  585. .iptbox input {
  586.     width: 490px;
  587.     height: 40px;
  588.     padding-left: 5px;
  589.     padding-right: 70px;
  590.     border-radius: 10px;
  591. }
  592. .iptbox button {
  593.     margin-left: 20px;
  594. }
  595. .bot-message,
  596. .user-message {
  597.     display: flex;
  598.     align-items: center;
  599. }
  600. .bot-message {
  601.     background-color: #ebeef5;
  602.     color: #000000;
  603.     border-radius: 10px;
  604. }
  605. .user-message {
  606.     background-color: #d5eaff;
  607.     color: #000000;
  608.     padding: 5px 10px;
  609.     border-radius: 10px;
  610. }
  611. .userBox {
  612.     margin-right: 10px;
  613.     display: flex;
  614.     align-items: center;
  615. }
  616. pre.hljs {
  617.   background-color: #f4f4f4;
  618.   padding: 10px;
  619. }
  620. pre.hljs code {
  621.   font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
  622.   font-size: 14px;
  623. }
  624. </style>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表