本地搭建deepseek-r1

ops/2025/2/3 3:38:41/

一、下载ollama(官网下载比较慢,可以找个网盘资源下)
二、安装ollama
三、打开cmd,拉取模型deepseek-r1:14b(根据显存大小选择模型大小)

ollama pull deepseek-r1:14b
四、运行模型
ollama run deepseek-r1:14b

五、使用网页api访问,可以使用openweb-ui,也可以自己实现页面,下面是使用vue实现的页面
在这里插入图片描述

const ChildOllama = {data() {return {userInput: '',chatHistory: [],// 请根据实际情况修改 Ollama API 的 URLollamaApiUrl: 'http://localhost:11434/v1/chat',currentResponse: ''}},watch: {// 监听 messages 数组的变化chatHistory: {handler() {// 数据变化时将滚动条移动到最下方this.scrollToBottom();},deep: true}},methods: {// 将滚动条移动到最下方的方法scrollToBottom() {// 获取滚动 div 的引用const scrollDiv = this.$refs.scrollDiv;// 设置滚动条位置到最下方scrollDiv.scrollTop = scrollDiv.scrollHeight;},async sendMessage() {if (this.userInput.trim() === '') return;// 添加用户消息到历史记录const userMessage = { role: 'user', content: this.userInput };this.chatHistory.push(userMessage);try {// 构造请求数据const response = await fetch('http://localhost:11434/api/chat', {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({model: 'deepseek-r1:14b', // 使用的模型messages: this.chatHistory,stream: true, // 启用流式传输}),});if (!response.ok) {throw new Error(`请求失败,状态码: ${response.status}`);}this.currentResponse = '';const reader = response.body.getReader();const decoder = new TextDecoder();let done = false;while (!done) {const { value, done: readerDone } = await reader.read();done = readerDone;if (value) {const chunk = decoder.decode(value, { stream: true });const lines = chunk.split('\n').filter(line => line.trim() !== '');for (const line of lines) {if (line) {try {const data = JSON.parse(line.replace(/^data: /, ''));if (data.done) {// 流式响应结束,添加完整回复到历史记录const ollamaMessage = { role: 'assistant', content: this.currentResponse };this.chatHistory.push(ollamaMessage);} else {this.currentResponse += data.message.content || '';}} catch (error) {console.error('Error parsing JSON:', error);}}}}}} catch (error) {console.error('Error sending message:', error);}// 清空输入框this.userInput = '';// try {//     // 将当前输入添加到对话历史中//     this.conversationHistory.push({ role: 'user', content: this.inputText });////     // 构造完整的提示信息,包含对话历史//     let fullPrompt = '';//     this.conversationHistory.forEach((item) => {//         fullPrompt += `${item.role === 'user' ? '用户: ' : '助手: '}${item.content}\n`;//     });////     // 清空之前的响应文本//     this.responseText = '';//     // 发起流式请求//     const response = await fetch('http://localhost:11434/api/generate', {//         method: 'POST',//         headers: {//             'Content-Type': 'application/json'//         },//         body: JSON.stringify({//             model: 'deepseek-r1:14b',//             prompt: fullPrompt,//             stream: true // 开启流式响应//         })//     });////     if (!response.ok) {//         throw new Error(`请求失败,状态码: ${response.status}`);//     }////     // 处理流式数据//     const reader = response.body.getReader();//     const decoder = new TextDecoder();//     let done = false;//     let currentResponse = '';//     while (!done) {//         const { value, done: readerDone } = await reader.read();//         done = readerDone;//         if (value) {//             const chunk = decoder.decode(value, { stream: true });//             // 解析 JSON 数据//             const lines = chunk.split('\n').filter(line => line.trim() !== '');//             for (const line of lines) {//                 try {//                     const data = JSON.parse(line);//                     if (data.response) {//                         // 更新响应文本//                         currentResponse += data.response;//                         this.responseText += data.response;//                     }//                 } catch (error) {//                     console.error('解析 JSON 数据时出错:', error);//                 }//             }//         }//     }//     // 将助手的回复添加到对话历史中//     this.conversationHistory.push({ role: 'assistant', content: currentResponse });// } catch (error) {//     console.error('请求出错:', error);// }}},template:`<div class="ollama-chat"><!-- 显示对话历史的区域 -->
<!--    <div class="history-area">-->
<!--      <div v-for="(item, index) in conversationHistory" :key="index" class="response-area">-->
<!--        <span :class="item.role === 'user' ? 'user-message' : 'assistant-message'">-->
<!--          {{ item.role === 'user' ? '用户: ' : '助手: ' }}-->
<!--        </span>-->
<!--        <span>{{ item.content }}</span>-->
<!--      </div>-->
<!--    </div>-->
<!--        <textarea v-model="inputText" placeholder="请输入问题"></textarea>-->
<!--    <button @click="sendRequest">发送请求</button>-->
<!--    -->
<!--    <div class="response-area">{{ responseText }}</div>--><h1>Ollama Chat</h1><div ref="scrollDiv" class="chat-history response-area"><div v-for="(message, index) in chatHistory" :key="index"><p><strong>{{ message.role === 'user' ? 'You' : 'Ollama' }}:</strong> {{ message.content }}</p></div></div><inputv-model="userInput"placeholder="Type your message..."@keyup.enter="sendMessage" style="width: 70%;padding: 5px;margin-right: 10px;"/><button @click="sendMessage">Send</button><button @click="scrollToBottom">botom</button><div class="chat-history response-area">{{ currentResponse }}</div>
</div>`
}

http://www.ppmy.cn/ops/155207.html

相关文章

【数据分享】1929-2024年全球站点的逐月平均能见度(Shp\Excel\免费获取)

气象数据是在各项研究中都经常使用的数据&#xff0c;气象指标包括气温、风速、降水、湿度等指标&#xff01;说到气象数据&#xff0c;最详细的气象数据是具体到气象监测站点的数据&#xff01; 有关气象指标的监测站点数据&#xff0c;之前我们分享过1929-2024年全球气象站点…

MapReduce,Yarn,Spark理解与执行流程

MapReduce的API理解 Mapper 如果是单词计数&#xff1a;hello&#xff1a;1&#xff0c; hello&#xff1a;1&#xff0c; world&#xff1a;1 public void map(Object key, // 首字符偏移量Text value, // 文件的一行内容Context context) // Mapper端的上下文&#xff0c;…

vscode+WSL2(ubuntu22.04)+pytorch+conda+cuda+cudnn安装系列

最近在家过年闲的没事&#xff0c;于是研究起深度学习开发工具链的配置和安装&#xff0c;之前欲与天公试比高&#xff0c;尝试在win上用vscodecuda11.6vs2019的cl编译器搭建cuda c编程环境&#xff0c;最后惨败&#xff0c;沦为笑柄&#xff0c;痛定思痛&#xff0c;这次直接和…

自适应细粒度通道注意力机制FCA详解及代码复现

机制定义 自适应细粒度通道注意(FCA)机制是一种创新的深度学习技术,旨在提高模型在图像处理任务中的性能。它通过 捕捉全局和局部信息之间的交互 ,优化特征权重分配,从而提升模型的表现。 FCA机制的核心在于其独特的设计原理: 利用相关矩阵捕捉信息 :FCA通过构建相关矩阵…

C++并发:设计无锁数据结构

只要摆脱锁&#xff0c;实现支持安全并发访问的数据结构&#xff0c;就有可能解决大粒度锁影响并发程度以及错误的加锁方式导致死锁的问题。这种数据结构称为无锁数据结构。 在了解本文时&#xff0c;务必读懂内存次序章节。 在设计无锁数据结构时&#xff0c;需要极为小心谨…

HBase基础shell命令

文章目录 前言一、基本命令1. 创建名称空间2. 删除名称空间3. 查询名称空间下的所有的表4. 列出所有表5. 查看表是否存在6. 查询表中的记录数7. 创建表8. 删除表&#xff08;先禁再删&#xff09;9. 新增/修改数据10. 查询一行数据11. 删除特定单元格12. 删除一整行数据 前言 …

设计模式的艺术-策略模式

行为型模式的名称、定义、学习难度和使用频率如下表所示&#xff1a; 1.如何理解策略模式 在策略模式中&#xff0c;可以定义一些独立的类来封装不同的算法&#xff0c;每个类封装一种具体的算法。在这里&#xff0c;每个封装算法的类都可以称之为一种策略&#xff08;Strategy…

AWTK 骨骼动画控件发布

Spine 是一款广泛使用的 2D 骨骼动画工具&#xff0c;专为游戏开发和动态图形设计设计。它通过基于骨骼的动画系统&#xff0c;帮助开发者创建流畅、高效的角色动画。本项目是基于 Spine 实现的 AWTK 骨骼动画控件。 代码&#xff1a;https://gitee.com/zlgopen/awtk-widget-s…