大号在练葵花宝典 发表于 2024-7-11 15:12:41

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

文心一言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代码:
<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">
                  <!-- 遍历用户发送的信息,如果role为user就返回信息 -->
                  <div v-if="message.sender === 'user'" class="user-message">
                        <!-- 用户发送的信息 -->
                        <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">发送</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">发送</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">
                  <!-- 遍历用户发送的信息,如果role为user就返回信息 -->
                  <div v-if="message.sender === 'user'" class="user-message">
                        <!-- 用户发送的信息 -->
                        <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">发送</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">发送</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">
                  <!-- 遍历用户发送的信息,如果role为user就返回信息 -->
                  <div v-if="message.sender === 'user'" class="user-message">
                        <!-- 用户发送的信息 -->
                        <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-inputclass="input" type="text" v-model="userInput" @keyup.enter="sendMessageImage"
                     placeholder="请输入要查询的问题..." />
                  <el-button @click="sendMessageImage" type="primary">11发送</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">发送</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">发送</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.可乐,点击发送文心一言事件
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.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.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.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.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.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>


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 文心一言AI大模子,前端接入