uni-app+deepseek-v3跨三端ai流式输出对话模板
【摘要】 基于Flutter3.27+Dart3+Getx+Dio集成DeepSeek-V3对话模型,从0-1纯撸仿DeepSeek智能ai聊天系统。实现流式输出打字效果、代码高亮、本地存储会话等功能。
2025跨端ai实例:uniapp+vue3+deepseek-v3+markdown搭建多端流式ai聊天模板。
实战跨平台Electron35+DeepSeek-V3搭建客户端AI模板
deepseek-v3+vue3.5网页版webai流式对话模板
使用技术
- 开发工具: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端
项目框架结构
uni-vue3-deepseek支持编译到h5+小程序端+app端。
uniapp环境变量配置
# 项目名称
VITE_APPNAME = 'Uniapp-DeepSeek'
# 运行端口
VITE_PORT = 5173
# DeepSeek API配置
VITE_DEEPSEEK_API_KEY = 替换为你的APIKey
VITE_DEEPSEEK_BASE_URL = https://api.deepseek.com
入口配置
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
}
}
项目布局模板
<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对接deepseek流式ai
uniapp+vue3集成deepseek-v3实现流式输出ai模板。
在h5和app端使用fetch实现流式,在小程序端使用uni.request请求流。
// 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
// #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
作者:xiaoyan2017
链接:https://www.cnblogs.com/xiaoyan2017/p/18853514
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)