首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >基于uniapp+deepseek+vue3跨平台ai流式对话

基于uniapp+deepseek+vue3跨平台ai流式对话

原创
作者头像
andy2018
发布于 2025-05-06 15:07:48
发布于 2025-05-06 15:07:48
9680
举报
文章被收录于专栏:h5h5

基于uniapp+vue3集成deepseek-v3实战跨端流式输出AI对话系统。支持暗黑+亮色模式、代码高亮、本地会话存储等功能。支持编译到小程序+h5+app端。

编译到h5/小程序/app端效果如下:

技术栈

  • 开发工具:Hbuilder X 4.57
  • 技术框架:Uniapp+Vue3+Pinia2+Vite5.x
  • 大模型框架:DeepSeek-V3
  • UI组件库:uni-ui+uv-ui
  • 高亮插件:highlight.js
  • markdown解析:ua-markdown
  • 本地缓存:pinia-plugin-unistorage
  • 支持编译:H5+小程序+APP端

项目框架结构

使用uniapp+vue3搭建项目模板,集成deepseek大语言模型。

.env配置文件

代码语言:txt
AI代码解释
复制
# 项目名称
VITE_APPNAME = 'Uniapp-DeepSeek'

# 运行端口
VITE_PORT = 5173

# DeepSeek API配置
VITE_DEEPSEEK_API_KEY = 替换为你的APIKey
VITE_DEEPSEEK_BASE_URL = https://api.deepseek.com

配置main.js

代码语言:txt
AI代码解释
复制
import App from './App'
import { createSSRApp } from 'vue'

// 引入pinia状态管理
import pinia from '@/pinia'

export function createApp() {
    const app = createSSRApp(App)
    app.use(pinia)
    return {
        app,
        pinia
    }
}

项目结构目录

代码语言:txt
AI代码解释
复制
<template>
    <uv3-layout>
        <!-- 导航栏 -->
        <template #header>
            <Toolbar :title="chatSession?.title" />
        </template>
        
        <view v-if="chatSession && !isEmpty(chatSession.data)" class="vu__chatview flexbox flex-col">
            <scroll-view :scroll-into-view="scrollIntoView" scroll-y="true" @scrolltolower="onScrollToLower" @scroll="onScroll" style="height: 100%;">
                <view class="vu__chatbot">
                    ...
                </view>
                <view id="scrollbottom-placeholder" style="height: 1px;"></view>
            </scroll-view>
            <!-- 滚动到底部 -->
            <view class="vu__scrollbottom" @click="scrollToBottom"><text class="iconfont ai-arrD fw-700"></text></view>
        </view>
        <!-- 欢迎信息 -->
        <view v-else class="vu__welcomeinfo">
            <view class="intro flex-c flex-col">
                <view class="logo flex-c" style="gap: 15px;">
                    <view class="iconfont ai-deepseek" style="font-size: 40px;"></view>
                    <text style="color: #999; font-size: 20px;">+</text>
                    <image src="/static/uni.png" mode="widthFix" style="height: 30px; width: 30px;" />
                </view>
                <view class="name"><text class="txt text-gradient">嘿~ Uniapp-DeepSeek</text></view>
                <view class="desc">我可以帮你写代码、答疑解惑、写作各种创意内容,请把你的任务交给我吧~</view>
            </view>
            <view class="prompt">
                <view class="tip flex-c"><text class="flex1">试试这样问</text><view class="flex-c" @click="refreshPrompt">换一换<uni-icons type="refreshempty" color="#999" size="14" /></view></view>
                <view v-for="(item,index) in promptList" :key="index">
                    <view class="option" @click="changePrompt(item.prompt)">{{item.emoji}} {{item.prompt}} <text class="arrow iconfont ai-arrR c-999"></text></view>
                </view>
            </view>
        </view>
        
        <template #footer>
            <view :style="{'padding-bottom': keyboardHeight + 'px'}">
                <ChatEditor ref="editorRef" v-model="promptValue" :scrollBottom="scrollToBottom" />
            </view>
        </template>
    </uv3-layout>
</template>

uniapp解析流式markdown

  • 支持代码块横向滚动
  • 支持显示代码行号(关闭提升性能)
  • 支持代码复制功能(h5/app端)
  • 支持图片渲染宽度100%
  • 支持图片预览功能(h5/app端)
  • 支持链接跳转功能(h5/app端)

uniapp对接deepseek api

H5+App端

代码语言:txt
AI代码解释
复制
// H5和APP端调用renderjs里的fetch
// #ifdef APP-PLUS || H5
this.fetchAppH5({
    url: baseURL+'/v1/chat/completions',
    method: 'POST',
    headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${apiKEY}`,
    },
    body: {
        // 多轮会话
        messages: this.multiConversation ? this.historySession : [{role: 'user', content: editorValue}],
        model: 'deepseek-chat', // deepseek-chat对话模型 deepseek-reasoner推理模型
        stream: true, // 流式输出
        max_tokens: 8192, // 限制一次请求中模型生成 completion 的最大 token 数(默认使用 4096)
        temperature: 0.4, // 严谨采样 越低越严谨(默认1)
    }
})
// #endif

小程序端

代码语言:txt
AI代码解释
复制
// #ifdef MP-WEIXIN
try {
    this.loading = true
    
    const requestTask = await uni.request({
        url: baseURL+'/v1/chat/completions',
        method: 'POST',
        header: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${apiKEY}`,
        },
        data: {
            // 多轮会话
            messages: this.multiConversation ? this.historySession : [{role: 'user', content: editorValue}],
            model: 'deepseek-chat', // deepseek-chat对话模型 deepseek-reasoner推理模型
            stream: true, // 流式输出
            max_tokens: 8192, // 限制一次请求中模型生成 completion 的最大 token 数(默认使用 4096)
            temperature: 0.4, // 严谨采样 越低越严谨(默认1)
        },
        enableChunked: true, //开启分块传输 transfer-encoding chunked
        success: (res) => {
            console.log('request success', res)
        },
        fail: (error) => {
            console.log('request fail', error)
            // ...
        }
    })
    requestTask.onChunkReceived((res) => {
        // console.log('Received chunk', res)
        
        const uint8Array = new Uint8Array(res.data)
        let text = String.fromCharCode.apply(null, uint8Array)
        const buffer = decodeURIComponent(escape(text))
        
        this.parseBuffer(buffer)
    })
} catch (error) {
    this.loading = false
    this.chatState.updateSession(this.botKey, {loading: false})
    throw new Error(`request error: ${error.message || '请求异常'}`)
}
// #endif

electron35+deepseek桌面端ai模板:https://cloud.tencent.com/developer/article/2514843

vue3.5+deepseek网页版ai流式对话:https://cloud.tencent.com/developer/article/2508594

flutter3.27+getx仿抖音app短视频+直播商城:https://cloud.tencent.com/developer/article/2493971 Electron32桌面端os系统:https://cloud.tencent.com/developer/article/2449406 electron31+vue3客户端聊天Exe实例:https://cloud.tencent.com/developer/article/2435159 tauri2.0+vue3客户端admin后台系统:https://cloud.tencent.com/developer/article/2458392 uniapp+vue3仿携程预订客房模板:https://cloud.tencent.com/developer/article/2474766

以上就是uniapp集成deepseek实战智能AI聊天会话的一些知识分享,希望对大家有所帮助!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 技术栈
  • 项目框架结构
  • .env配置文件
  • 配置main.js
  • 项目结构目录
  • uniapp解析流式markdown
  • uniapp对接deepseek api
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档