DeepSeek + Vue实战开发

news2025/7/9 17:33:49

利用DeepSeek V3模型、siliconflow大模型一站式云服务平台以及vue3.0实现一个在线人工智能客服对话系统。

因为deepseek官网的api密钥使用起来比较缓慢,所以可以使用第三方的,具体操作请自行查阅资料。

siliconflow官网

SiliconFlow, Accelerate AGI to Benefit Humanity硅基流动致力于打造规模化、标准化、高效能 AI Infra 平台,提供高效能、低成本的多品类 AI 模型服务,助力开发者和企业聚焦产品创新。https://siliconflow.cn/zh-cn/

完整运行代码

运行之前请先修改请求头(headers)中的密钥(token)以及对话模型(model

请注意:这里的使用的是siliconflow中的token

<template>
  <div class="chatbot-container">
    <div
      class="avatar"
      @click="toggleChat"
      :style="{
        transform: isChatOpen
          ? 'scale(0.9) rotate(-8deg)'
          : 'scale(1) rotate(0)',
      }"
    ></div>
    <div
      class="chat-dialog"
      :class="{ active: isChatOpen }"
      v-click-outside="closeChat"
    >
      <div class="dialog-header">
        <h3>在线客服</h3>
        <div class="close-btn" @click="toggleChat">×</div>
      </div>
      <div class="dialog-body">
        <div class="message-container" ref="scrollbarRef">
          <div v-for="(message, index) in messagesWithTimestamps" :key="index">
            <!-- 时间戳 -->
            <div class="timestamp" v-if="message.showTime">
              {{ formatSendTime(message.timestamp) }}
            </div>
            <!-- 消息列表 -->
            <div
              :class="[
                'message-bubble',
                message.type === 'user' ? 'user-message' : 'bot-message',
              ]"
            >
              {{ message.content }}
            </div>
          </div>
        </div>

        <div class="input-area">
          <input
            v-model="inputMessage"
            type="text"
            class="message-input"
            placeholder="输入消息..."
            @keypress.enter="sendMessage"
          />
          <button class="send-button" @click="sendMessage">发送</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { nextTick, ref, computed } from "vue";

const isChatOpen = ref(false);
const inputMessage = ref("");
const messages = ref([]);

//创建新的对话数组,加上属性showTime
const messagesWithTimestamps = computed(() => {
  return messages.value.map((item, index) => ({
    ...item,
    showTime: index == 0 || shouldShowTime(index),
  }));
});
//计算两次会话时间是否超过3分钟方法
const shouldShowTime = (index) => {
  const current = new Date(messages.value[index - 1].timestamp);
  const next = new Date(messages.value[index].timestamp);
  const diff = next ? next - current : 0;
  return diff > 3 * 60 * 1000; // 如果间隔超过3分钟返回true
};
// 切换聊天窗口
const toggleChat = () => {
  isChatOpen.value = !isChatOpen.value;
  if (isChatOpen.value && messages.value.length === 0) {
    messages.value.push({
      type: "bot",
      content: "您好!我是智能客服小助手,很高兴为您服务。请问有什么可以帮您?",
      timestamp: new Date(), //时间戳
    });
  }
};
// 关闭聊天窗口
const closeChat = () => {
  isChatOpen.value = false;
};
// 发送消息
const sendMessage = () => {
  const message = inputMessage.value.trim();
  if (message) {
    messages.value.push({
      type: "user",
      content: message,
      timestamp: new Date(), //时间戳
    });
    inputMessage.value = "";
    const messageContainer = document.querySelector(".message-container");

    try {
      setTimeout(async () => {
        messages.value.push({
          type: "bot",
          content: "正在思考中...",
          timestamp: new Date(), //时间戳
        });
        nextTick(() => {
          // 滚动到底部
          messageContainer.scrollTop = messageContainer.scrollHeight;
        });
        // 发送请求
        const response = await fetch(
          "https://api.siliconflow.cn/v1/chat/completions",
          {
            method: "POST",
            headers: {
              Authorization:
                "Bearer sk-bnlbdzgfqyqokwboisvheyohbmbhnpzpbyhsuvkpidkncpij",
              "Content-Type": "application/json",
            },
            body: JSON.stringify({
              frequency_penalty: 0,
              max_tokens: 1024,
              model: "deepseek-ai/DeepSeek-V3",
              temperature: 0,
              top_k: 49,
              top_p: 0.7,
              messages: [
                {
                  role: "user",
                  content: message,
                },
              ],
            }),
          }
        );
        const data = await response.json();
        // 过滤 "正在思考中..." 消息
        messages.value = messages.value.filter((item) => {
          return item.content.indexOf("正在思考中") === -1;
        });
        if (response.choices) {
          // 处理响应数据
          messages.value.push({
            type: "bot",
            content: data.choices[0].message.content,
            timestamp: new Date(), //时间戳
          });
        } else {
          // 处理错误情况
          messages.value.push({
            type: "bot",
            content: "抱歉,我现在无法回答您的问题,请稍后再试。",
            timestamp: new Date(), //时间戳
          });
        }
        nextTick(() => {
          // 滚动到底部
          messageContainer.scrollTop = messageContainer.scrollHeight;
        });
      }, 1000);
      // 滚动到底部
    } catch (error) {
      console.error("Error:", error);
    }
  }
};

const formatTime = (date) => {
    // 格式化时间为“时:分”
    const hours = date.getHours().toString().padStart(2, "0");
    const minutes = date.getMinutes().toString().padStart(2, "0");
    return hours + ":" + minutes;
};
const getWeekday = (date) => {
    // 获取星期几的中文表示
    const weekdays = [
        "星期天",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
    ];
    return weekdays[date.getDay()];
};

const formatSendTime = (sendTime) => {
    const now = new Date();
    const sendDate = new Date(sendTime);
    // 计算时间差(以毫秒为单位)
    const timeDiff = now - sendDate;
    const startOfToday = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate()
    );
    const startOfTargetDate = new Date(
        sendDate.getFullYear(),
        sendDate.getMonth(),
        sendDate.getDate()
    );
    // 一天内的毫秒数
    const oneDay = 24 * 60 * 60 * 1000;
    // 如果发送时间在当前时间之前
    if (timeDiff < 0) {
        return "Invalid time"; // 或者其他错误处理
    }
    // 发生在同一天
    if (startOfToday.getTime() === startOfTargetDate.getTime()) {
        return formatTime(sendDate);
    }
    // 如果发送时间在一天内
    if (timeDiff < oneDay) {
        return "昨天 " + formatTime(sendDate);
    }
    // 如果发送时间在二天至七天内
    if (timeDiff < 7 * oneDay) {
        const weekday = getWeekday(sendDate);
        return weekday + " " + formatTime(sendDate);
    }
    // 如果发送时间超过七天
    return (
        sendDate.toLocaleDateString("zh-CN", {
            year: "numeric",
            month: "2-digit",
            day: "2-digit",
        }) +
        " " +
        formatTime(sendDate)
    );
};

</script>

<style>
/* 原有样式保持不变 */
:root {
  --primary-color: #4a90e2;
  --hover-color: #357abd;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f0f2f5;
  margin: 0;
  font-family: "Microsoft YaHei", sans-serif;
}

.chatbot-container {
  position: fixed;
  bottom: 30px;
  right: 30px;
  z-index: 1000;
}

/* 新增头像样式 */
.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  box-shadow: 0 8px 20px rgba(74, 144, 226, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  background-color: #357abd;
}

.avatar:hover {
  transform: scale(1.1) rotate(8deg);
  box-shadow: 0 12px 25px rgba(74, 144, 226, 0.4);
}

.avatar::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 30px;
  height: 30px;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white"><path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-3 12H7v-2h10v2zm0-3H7v-2h10v2zm0-3H7V6h10v2z"/></svg>')
    no-repeat center;
}

/* 聊天对话框 */
.chat-dialog {
  position: absolute;
  bottom: 80px;
  right: 0;
  width: 320px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
  transform: scale(0);
  transform-origin: bottom right;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-dialog.active {
  transform: scale(1);
}

.dialog-header {
  padding: 16px;
  background: var(--primary-color);
  border-radius: 12px 12px 0 0;
  color: white;
  display: flex;
  align-items: center;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}

.close-btn {
  margin-left: auto;
  cursor: pointer;
  padding: 0px 4px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.1);
}

.dialog-body {
  padding: 16px;
  height: 300px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.timestamp {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  margin-bottom: 3px;
}

.message-container {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
}

.input-area {
  display: flex;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid #eee;
}

.message-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
  transition: border-color 0.3s;
}

.message-input:focus {
  border-color: var(--primary-color);
}

.send-button {
  padding: 8px 16px;
  background: var(--primary-color);
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
}

.send-button:hover {
  background: var(--hover-color);
}

.message-bubble {
  padding: 8px 12px;
  margin-bottom: 8px;
  border-radius: 4px;
  animation: fadeIn 0.3s ease;
  max-width: 80%;
  display: inline-block;
  word-break: break-all;
  box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
  border: 1px solid #e4e7ed;
}

.user-message {
  background: var(--primary-color);
  color: #ffffff;
  float: right;
}

.bot-message {
  background: #ffffff;
  color: #303133;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .chatbot-container {
    bottom: 20px;
    right: 20px;
  }

  .chat-dialog {
    width: 90vw;
    bottom: 70px;
  }

  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>

效果在线预览

ps:文本对话请求可能会有连接超时的问题!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2300261.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【数据结构】(8) 二叉树

一、树形结构 1、什么是树形结构 根节点没有前驱&#xff0c;其它节点只有一个前驱&#xff08;双亲/父结点&#xff09;。所有节点可以有 0 ~ 多个后继&#xff0c;即分支&#xff08;孩子结点&#xff09;。每个结点作为子树的根节点&#xff0c;这些子树互不相交。 2、关于…

Web 后端 请求与响应

一 请求响应 1. 请求&#xff08;Request&#xff09; 客户端向服务器发送的HTTP请求&#xff0c;通常包含以下内容&#xff1a; 请求行&#xff1a;HTTP方法&#xff08;GET/POST等&#xff09;、请求的URL、协议版本。 请求头&#xff08;Headers&#xff09;&#xff1a;…

CEF132 编译指南 Linux 篇 - CEF 编译实战:构建 CEF(六)

1. 引言 经过前几篇的精心准备&#xff0c;我们已经完成了所有必要的环境配置和源码下载。现在&#xff0c;我们将进入激动人心的 CEF 编译阶段。本篇将详细指导你在 Linux 系统上编译 CEF 6834 分支&#xff08;对应 Chromium 132 版本&#xff09;&#xff0c;包括创建项目文…

【Spring+MyBatis】_图书管理系统(上篇)

目录 1. MyBatis与MySQL配置 1.1 创建数据库及数据表 1.2 配置MyBatis与数据库 1.2.1 增加MyBatis与MySQL相关依赖 1.2.2 配置application.yml文件 1.3 增加数据表对应实体类 2. 功能1&#xff1a;用户登录 2.1 约定前后端交互接口 2.2 后端接口 2.3 前端页面 2.4 单…

【苍穹外卖】学习

软件开发整体介绍 作为一名软件开发工程师,我们需要了解在软件开发过程中的开发流程&#xff0c; 以及软件开发过程中涉及到的岗位角色&#xff0c;角色的分工、职责&#xff0c; 并了解软件开发中涉及到的三种软件环境。那么这一小节&#xff0c;我们将从 软件开发流程、角色…

DeepSeek-V2-技术文档

DeekSeek-v2-简述 1. DeepSeek-V2是什么? DeepSeek-V2是一个基于混合专家(Mixture-of-Experts,简称MoE)架构的语言模型。它是一种新型的人工智能模型,专门用于处理自然语言处理(NLP)任务,比如文本生成、翻译、问答等。与传统的语言模型相比,DeepSeek-V2在训练成本和…

Linux中线程创建,线程退出,线程接合

线程的简单了解 之前我们了解过 task_struct 是用于描述进程的核心数据结构。它包含了一个进程的所有重要信息&#xff0c;并且在进程的生命周期内保持更新。我们想要获取进程相关信息往往从这里得到。 在Linux中&#xff0c;线程的实现方式与进程类似&#xff0c;每个线程都…

什么是蒸馏技术

蒸馏技术&#xff08;Knowledge Distillation, KD&#xff09;是一种模型压缩和知识迁移的方法&#xff0c;旨在将一个复杂模型&#xff08;通常称为“教师模型”&#xff09;的知识转移到一个小型模型&#xff08;通常称为“学生模型”&#xff09;中。蒸馏技术的核心思想是通…

Python——寻找矩阵的【鞍点】(教师:恒风)

在矩阵中&#xff0c;一个数在所在行中是最大值&#xff0c;在所在列中是最小值&#xff0c;则被称为鞍点 恒风的编程 思路&#xff1a; 使用while循环找到行中最大值&#xff0c;此时列的坐标已知&#xff0c;利用列表推导式生成列不变的纵列&#xff0c;利用min()函数得到纵…

处理项目中存在多个版本的jsqlparser依赖

异常提示 Correct the classpath of your application so that it contains a single, compatible version of net.sf.jsqlparser.statement.select.SelectExpressionIte实际问题 原因&#xff1a;项目中同时使用了 mybatis-plus 和 pagehelper&#xff0c;两者都用到了 jsqlpa…

【iOS】包大小和性能稳定性优化

包大小优化 图片 LSUnusedResources 扫描重复的图片 ImageOptim,压缩图片 压缩文件 优化音视频资源 &#xff0c;使用MP3 代替 WAV ffmpeg -i input.mp3 -b:a 128k output.mp3 视频 H.265&#xff08;HEVC&#xff09; 代替 H.264 ffmpeg ffmpeg -i input.mp4 -vcodec lib…

Jenkinsdebug:遇到ERROR: unable to select packages:怎么处理

报错信息&#xff1a; 报错信息解释&#xff1a; musl-1.2.5-r0 和 musl-dev-1.2.5-r1: 这里说明 musl-dev 需要一个特定版本的 musl&#xff0c;即 musl1.2.5-r1&#xff0c;但是当前版本的 musl&#xff08;1.2.5-r0&#xff09;并不满足这个条件。版本冲突: 当尝试安装新…

3、树莓派5 安装VNC查看器 开启VNC服务器

在前序文章中&#xff08; 2、树莓派5第一次开机&#xff09;&#xff0c;可以使用三种方式开机&#xff0c;其中使用网线及wifi的方式均需要使用到VNC查看器进行远程桌面控制&#xff0c;本文将介绍如何下载安装并配置及使用VNC查看器及服务器&#xff0c;对前序文章做一些补充…

数据结构——单向循环链表、双链表、双向循环链表

目录 一、单向循环链表 1.1 单向循环链表的概念 1.2 单向循环链表的操作 1.2.1 单向循环链表的创建 1.2.2 单向循环链表的头插 1.2.3 单向循环链表的遍历 1.2.4 单向循环链表的头删 1.2.5 单向循环链表的尾插 1.2.6 单向循环链表的尾删 1.2.7 约瑟夫环 1.3 单向循环列表所有程…

冒险岛079 V8 整合版源码搭建教程+IDEA启动

今天教大家来部署下一款超级怀旧游戏冒险岛&#xff0c;冒险岛源码是开源的&#xff0c;但是开源的代码会有各种&#xff0c;本人进行了加工整合&#xff0c;并且用idea进行了启动测试&#xff0c;经过修改后没有任何问题。 启动截图 后端控制台 前端游戏界面 声明 冒险岛源码…

Qwen2-VL 的重大省级,Qwen 发布新旗舰视觉语言模型 Qwen2.5-VL

Qwen2.5-VL 是 Qwen 的新旗舰视觉语言模型&#xff0c;也是上一代 Qwen2-VL 的重大飞跃。 Qwen2.5-VL主要特点 视觉理解事物&#xff1a;Qwen2.5-VL不仅能够熟练识别花、鸟、鱼、昆虫等常见物体&#xff0c;而且还能够分析图像中的文本、图表、图标、图形和布局。 代理性&…

STM32外设SPI FLASH应用实例

STM32外设SPI FLASH应用实例 1. 前言1.1 硬件准备1.2 软件准备 2. 硬件连接3. 软件实现3.1 SPI 初始化3.2 QW128 SPI FLASH 驱动3.3 乒乓存储实现 4. 测试与验证4.1 数据备份测试4.2 数据恢复测试 5 实例5.1 参数结构体定义5.2 存储参数到 SPI FLASH5.3 从 SPI FLASH 读取参数5…

Java零基础入门笔记:(1-2)入门(简介、基础知识)

前言 本笔记是学习狂神的java教程&#xff0c;建议配合视频&#xff0c;学习体验更佳。 【狂神说Java】Java零基础学习视频通俗易懂_哔哩哔哩_bilibili - Java简介 Java是一种广泛使用的高级编程语言&#xff0c;具有简单、面向对象、分布式、多线程、动态性、健壮性和安全…

Java 基于 SpringBoot+Vue 的动漫平台(附源码,文档)

博主介绍&#xff1a;✌程序员徐师兄、8年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战*✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447…

Ubuntu 系统 cuda12.2 安装 MMDetection3D

DataBall 助力快速掌握数据集的信息和使用方式&#xff0c;会员享有 百种数据集&#xff0c;持续增加中。 需要更多数据资源和技术解决方案&#xff0c;知识星球&#xff1a; “DataBall - X 数据球(free)” 贵在坚持&#xff01; ---------------------------------------…