本地搭建deepseek-r1

一、下载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 的 URL
            ollamaApiUrl: '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>
    <input
      v-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>`
}

### DeepSeek-R1本地部署教程 要在本地环境中成功搭建并运行 DeepSeek-R1 AI 模型,需遵循以下方法和注意事项: #### 1. 准备工作 在开始之前,需要确认具备必要的软硬件条件以及相关工具链的支持。以下是具体准备事项: - **硬件需求**: 部署 DeepSeek-R1 要求较高的计算能力,尤其是对于完整的 671B 参数版本。推荐至少配备 NVIDIA RTX 3090 或更高性能显卡[^3]。 - **软件依赖**: 安装 Python (建议版本 >= 3.8),同时确保已安装 PyTorch 和 Transformers 库。可以通过 pip 进行安装: ```bash pip install torch transformers ``` #### 2. 获取模型文件 DeepSeek 提供了多种参数规模的模型变体,包括但不限于原始的大规模版本及其蒸馏后的轻量化版本。用户可以根据实际需求选择适合自己的模型。 - 对于完整版模型,访问官方仓库获取最新发布的信息,并按照指引完成下载操作[^1]。 - 如果倾向于更高效的解决方案,则可以考虑采用经过优化处理过的较小尺寸版本,例如 `DeepSeek-R1-Distill-Qwen-7B`[^4]。该版本不仅保留了一定程度上的表现力,还显著降低了存储空间占用率与运算复杂度。 #### 3. 使用 Ollama 工具简化流程 为了进一步降低技术门槛,许多开发者推荐利用开源项目 Ollama 来管理整个过程中的各个环节。通过它能够轻松实现从网络上拉取目标镜像直至最终启动服务端口监听等一系列动作[^5]。 - 安装 Ollama 并验证其正常运作状态; - 执行命令以加载指定名称下的预构建包(如 deepseek-r1)至本地缓存目录下等待后续调用; ```bash ollama pull deepseek/r1 ``` #### 4. 测试功能是否完好无损 最后一步便是检验当前设置能否满足预期用途场景的要求。打开浏览器输入相应 URL 地址即可进入交互界面尝试提问解答等功能测试环节[^2]。 --- 以上即为关于如何在个人电脑或者其他私有服务器上面独立完成 DeepSeek-R1 的全部步骤说明文档内容摘要整理而成的结果展示形式之一而已;当然还有更多高级定制选项可供探索实践学习过程中不断积累经验成长进步!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值