稀土掘金技术社区 02月24日
deepseek本地部署(在线、离线)、知识库搭建(个人、组织)与代码接入
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文详细介绍了DeepSeek模型本地化容器化部署的实践方法,包括使用Ollama简化部署流程、内网迁移方案以及Linux环境下的容器化部署。同时,探讨了如何搭建个人和组织知识库,利用AnythingLLM和Open WebUI实现文档检索。此外,还介绍了如何通过Docker Compose一键部署Ollama和Open WebUI,以及内网服务器离线部署的详细步骤。最后,阐述了SpringAI的接入方法,包括依赖配置和公共调用组件的封装,为读者提供了一份全面的DeepSeek模型应用指南。

🐳 使用Ollama进行DeepSeek本地化部署,简化部署流程和内网迁移,通过配置环境变量和模型存储位置,并根据硬件配置选择合适的模型版本(如1.5B、8B、14B、32B、70B),利用`ollama run deepseek-r1:${tag}`命令在本地命令行终端安装DeepSeek模型。

📚 搭建个人知识库使用AnythingLLM工具,配置Ollama Base URL为本地部署的模型地址,上传知识库文件后进行向量化处理,从而实现基于本地知识库的问答。

🏢 搭建组织知识库使用Open WebUI,通过Docker部署,配置模型外部链接和权限,构建组织知识库,实现团队内部的知识共享和检索。文档上传和使用过程与个人知识库类似。

⚙️ 通过Docker Compose编排Ollama和Open WebUI容器,实现一键部署,简化部署流程,并为后续离线部署降低工作量。配置包括镜像、容器名称、端口映射、数据卷挂载以及网络配置等。

📦 实现内网服务器离线部署,首先转储Ollama和Open WebUI的Docker镜像,然后备份数据卷,将数据转移到内网服务器后恢复数据,加载镜像,创建数据卷和网络,最后启动服务并验证。

原创 WinsonWu 2025-02-22 09:03 重庆

点击关注公众号,“技术干货” 及时达!

本文主要介绍deepSeek本地化容器化部署,知识库搭建与springAI接入。因笔者工作内容原因,也会介绍内网部署方式。

一、deepSeek本地化部署

本地部署方式我们采用ollama,简化部署、内网迁移流程。其他部署方式与对比,可参考:

7种大模型的部署方案汇总

需要提高模型推理效率,可参考:

KTransformers

1.1 windows与mac部署


「注意:」 如果没有梯子,下载较慢,可以直接通过网盘链接下载:

windows

macOS

    配置模型存储位置

Ollama会默认将模型存储到C:\Users%username%.ollama\models,防止影响性能,我们将模型路径修改到D盘:


    配置跨域请求

Ollama 默认只允许来自127.0.0.10.0.0.0的跨域请求,这里因为笔者Open WebUI在内网其他服务器,配置OLLAMA_ORIGINS*


这里因为是测试服务器搭建,我们选择一个较小的模型,如果是本机部署,没有显卡的情况下,建议选择「1.5b」(15亿参数)的蒸馏模型。具体模型配置可参考:

DeepSeek模型各版本硬件要求

参考量显存需求显卡推荐命令
1.5B通常4GB左右GTX 1050ollama run deepseek-r1:1.5b
8B一般8-10GBGTX 1660ollama run deepseek-r1:8b
14B12GB以上,16GB以上运行流畅RTX 3060ollama run deepseek-r1:14b
32B16GB以上,21GB以上运行流畅RTX 3060ollama run deepseek-r1:32b
70B24GB以上,40GB以上运行流畅RTX 3090、RTX 4090ollama run deepseek-r1:70b
671B1342GB16张NVIDIA A100 80GB显卡ollama run deepseek-r1:671b
ollama run deepseek-r1:${tag} // ollama run deepseek-r1:32b


可以看到成功返回,token rate38.46tokens/s

或者使用任务管理器->性能模块:

可以看到GPU显存占用在22G左右。

1.2 linux部署

容器化基础可参考第四小节——「附录」

1.2.1 部署deepseek


<!---->
sudo docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ollama/ollama sudo docker run -d -v ollama:/ollama -p 11434:11434 --name ollama swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ollama/ollama

这里因为没有梯子会比较慢,我们使用华为云的ollama镜像。


<!---->
docker ps | grep ollama // 确认容器状态 docker exec -it ${containerId} /bin/bash // 进入容器 ollama run deepseek-r1:${tag} // ollama run deepseek-r1:7b 安装deepseek模型


二、知识库搭建

用于内部涉密资料的检索,因为模型训练成本要求较高,这里我们搭建本地的个人与组织知识库,不过文档的解析、检索差强人意,对外提供服务可基于LlamaIndexLangChain进行开发。如需训练模型,可考虑全参数微调或者loRA等方式。

2.1 个人知识库搭建

个人知识库RAG应用搭建,可以借助anythingLLM等工具,这里我们介绍使用anythingLLM进行个人知识库的搭建。



安装AnythingLLM


点击聊天设置,LLM提供者选择ollama

点击工作区文件上传按钮,添加知识库文件:

这里我们添加一个测试文件:


添加完成后点击Move to Workspace,然后右下角点击Save and Embed进行向量化处理:


答案是「王五」,测试完成。

2.2 组织知识库搭建

组织知识库的搭建,我们使用Open WebUI,提供一个简洁、好看的页面。

Open WebUI项目地址:Open WebUI.


<!---->
docker run -d -p 3030:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.nju.edu.cn/open-webui/open-webui:main

这里我们不使用梯子,仍旧使用国内南京大学的镜像。

浏览器输入http://${webUI-deploy-ip}:3030/

首次进入,需要注册,这里我已经注册,直接进入:


进入管理员面板:

点击设置,配置外部链接:

点击设置->模型,便于测试,这里我们直接设置成public



依次点击工作空间 -> 知识库,即可构建组织知识库:


文档上传、使用过程按下不表,与「个人知识库」类似。

前面我们使用docker部署了ollmaOpen WebUI,这里我们将两个容器通过docker compose编排,一键部署,也为后续离线部署降低工作量。整合容器配置如下:


version: '3' services: ollama: image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ollama/ollama container_name: ollama restart: always ports: - "11434:11434" volumes: - ollama:/ollama networks: - app-network open-webui: image: ghcr.nju.edu.cn/open-webui/open-webui:main container_name: open-webui restart: always ports: - "3030:8080" volumes: - open-webui:/app/backend/data depends_on: - ollama extra_hosts: - "host.docker.internal:host-gateway" networks: - app-network volumes: ollama: name:ollama-ds external: true open-webui: name: webui-ds external: true networks: app-network: external: true name: ds-net

启动服务使用:

   docker-compose up -d

关闭服务使用:

  docker-compose down

    我们先转储镜像:


<!---->
docker save -o ollama_image.tar swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ollama/ollama docker save -o open-webui_image.tar ghcr.nju.edu.cn/open-webui/open-webui:main

2.  接下来我们备份数据卷:


<!---->
sudo tar -czvf ollama-backup.tar.gz -C /var/lib/docker/volumes/ollama-ds/_data . sudo tar -czvf webui-backup.tar.gz -C /var/lib/docker/volumes/webui-ds/_data .

3.  将数据转移到内网服务器后,恢复数据:


<!---->
// 恢复数据卷 sudo tar -xzvf ollama-backup.tar.gz -C /var/lib/docker/volumes/ollama-ds/_data sudo tar -xzvf webui-backup.tar.gz -C /var/lib/docker/volumes/webui-ds/_data // 加载镜像 docker load -i ollama.tar docker load -i open-webui.tar // 创建数据卷和网络 docker volume create --name=ollama-ds docker volume create --name=webui-ds docker network create ds-net // 启动服务 docker-compose up -d // 验证服务 docker ps

三、代码接入


<!---->
// 主pom ... <properties> <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version> </properties> ... </dependencyManagement> </dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>${spring-ai.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ... // ai模块 ... <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> </dependency> ...

    公共调用组件封装


<!---->
@Component public class Completion {
@Resource private OllamaChatModel aiClient;
/** * 最大消息记录数 */ private final static Integer MAX_SIZE = 10;
/** * 消息记录 */ private List<Message> messages = new ArrayList<>();

/** * 初始化存入系统消息 */ @PostConstruct private void addSystemMessage() { String message = "李白(701年2月28日—762年12月),字太白,号青莲居士,出生于蜀郡绵州昌隆县(今四川省绵阳市江油市青莲镇),一说出生于西域碎叶 ,祖籍陇西成纪(今甘肃省秦安县)。唐朝伟大的浪漫主义诗人,凉武昭王李暠九世孙 。" + "为人爽朗大方,乐于交友,爱好饮酒作诗,名列“酒中八仙” 。曾经得到唐玄宗李隆基赏识,担任翰林供奉,赐金放还后,游历全国,先后迎娶宰相许圉师、宗楚客的孙女。唐肃宗李亨即位后,卷入永王之乱,流放夜郎,辗转到达当涂县令李阳冰家。上元二年,去世,时年六十二 。" + "著有《李太白集》,代表作有《望庐山瀑布》《行路难》《蜀道难》《将进酒》《早发白帝城》《黄鹤楼送孟浩然之广陵》等。李白所作词赋,就其开创意义及艺术成就而言,享有极为崇高的地位,后世誉为“诗仙”,与诗圣杜甫并称“李杜”。"; Message systemMessage = new SystemMessage(message); messages.add(systemMessage); }
/** * 存储用户发送的消息 */ private void addUserMessage(String message) { Message userMessage = new UserMessage(message); messages.add(userMessage); }
/** * 存储AI回复的消息 */ private void addAssistantMessage(String message) { Message assistantMessage = new AssistantMessage(message); messages.add(assistantMessage); }
/** * 聊天接口 */ public String chat(String message) { addUserMessage(message); String result = aiClient.call(new Prompt(messages)).getResult().getOutput().getText(); addAssistantMessage(result); update(); return result; }
/** * 流式聊天接口 */ public Flux<String> chatStream(String message) { addUserMessage(message);
StringBuffer fullReply = new StringBuffer();
Flux<String> fluxResult = aiClient.stream(new Prompt(messages)) .flatMap(response -> { String reply = response.getResult().getOutput().getText();
//拼接回复内容 fullReply.append(reply);
return Flux.just(reply); }) .doOnComplete(() -> { //监听流式响应完成,完整回复存入消息记录 System.out.println(fullReply); addAssistantMessage(String.valueOf(fullReply)); });
update(); return fluxResult; }
/** * 更新消息记录 */ private void update() { if (messages.size() > MAX_SIZE) { messages = messages.subList(messages.size() - MAX_SIZE, messages.size()); } } }

2.  创建测试用controller:


<!---->
@Slf4j @RestController @RequestMapping("/api") public class OllamaTestController { @Autowired private OllamaChatModel ollamaChatClient;
@RequestMapping("/chat") public String chat(){ String systemPrompt = "{prompt}"; SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
String userPrompt = "广州有什么特产?"; Message userMessage = new UserMessage(userPrompt);
Message systemMessage = systemPromptTemplate.createMessage(MapUtil.of("prompt", "you are a helpful AI assistant"));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
List<Generation> response = ollamaChatClient.call(prompt).getResults();
String result = "";
for (Generation generation : response){ String content = generation.getOutput().getText(); result += content; }
return result; }

@RequestMapping("/stream") public SseEmitter stream(HttpServletResponse response){ response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8"); SseEmitter emitter = new SseEmitter();

String systemPrompt = "{prompt}"; SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
String userPrompt = "广州有什么特产?"; Message userMessage = new UserMessage(userPrompt);
Message systemMessage = systemPromptTemplate.createMessage(MapUtil.of("prompt", "you are a helpful AI assistant")); Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
ollamaChatClient.stream(prompt).subscribe(x -> { try { log.info("response: {}",x); List<Generation> generations = x.getResults(); if(CollUtil.isNotEmpty(generations)){ for(Generation generation:generations){ AssistantMessage assistantMessage = generation.getOutput(); String content = assistantMessage.getText(); if(StringUtils.isNotEmpty(content)){ emitter.send(content); }else{ if(StringUtils.equals(content,"null")) emitter.complete(); // Complete the SSE connection } } }

} catch (Exception e) { emitter.complete(); log.error("流式返回结果异常",e); } });
return emitter; } }

3.  测试


四、附录

4.1 容器化基础(有基础可跳过)

4.1.1 Docker安装

step 1:卸载已有资源 yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine.

step 2:添加 Docker 安装源 yum-config-manager \ --add-repo \ <https://download.docker.com/linux/centos/docker-ce.repo>.

如果提示yum-config-manager不存在,执行yum -y install yum-utils.

step 3: 安装最新版本Docker yum install docker-ce docker-ce-cli containerd.io.

如果想要安装指定版本的Docker,可以使用以下命令:

yum list docker-ce --showduplicates | sort -r.

yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io.

step 4: 启动docker

systemctl start docker

启动完成后,运行hello world容器,检查是否安装正确,命令如下:

docker run hello-world

Docker 首先会检查本地是否有hello-world这个镜像,如果发现本地没有这个镜像,Docker 就会去 Docker Hub 官方仓库下载此镜像,然后运行它。最后输出 "Hello from Docker!" 并退出。

如需卸载docker,执行yum -y remove docker-engine,,如需删除镜像、容器、卷以及自定义文件,可执行以下命令:

rm -rf /var/lib/docker

4.1.2 Docker基础操作

拉取镜像,使用docker pull命令拉取远程仓库的镜像到本地 ;

命令格式:docker pull [Registry]/[Repository]/[Image]:[Tag]

重命名镜像,使用docker tag命令“重命名”镜像 ;

命令格式:docker tag [SOURCE_IMAGE](#) [TARGET_IMAGE](#)

查看镜像,使用docker image lsdocker images命令查看本地已经存在的镜像 ;

删除镜像,使用docker rmi命令删除镜像 ;

构建镜像,构建镜像有两种方式。第一种方式是使用docker build命令基于 Dockerfile 构建镜像,也是我比较推荐的镜像构建方式;第二种方式是使用docker commit命令基于已经运行的容器提交为镜像。

Dockerfile常用指令:

eg:


FROM centos:7 COPY nginx.repo /etc/yum.repos.d/nginx.repo RUN yum install -y nginx EXPOSE 80 ENV HOST=iotnginx CMD ["nginx","-g","daemon off;"]

第一行表示基于 centos:7 这个镜像来构建自定义镜像。这里需要注意,每个 Dockerfile 的第一行除了注释都必须以 FROM 开头。

第二行表示拷贝本地文件 nginx.repo 文件到容器内的 /etc/yum.repos.d 目录下。这里拷贝 nginx.repo 文件是为了添加 nginx 的安装源。

第三行表示在容器内运行yum install -y nginx命令,安装 nginx 服务到容器内,执行完第三行命令,容器内的 nginx 已经安装完成。

第四行声明容器内业务(nginx)使用 80 端口对外提供服务。

第五行定义容器启动时的环境变量 HOST=iotnginx,容器启动后可以获取到环境变量 HOST 的值为 iotnginx。

第六行定义容器的启动命令,命令格式为 json 数组。这里设置了容器的启动命令为 nginx ,并且添加了 nginx 的启动参数 -g 'daemon off;' ,使得 nginx 以前台的方式启动。

4.2 单机服务编排入门(有基础可跳过)

单机服务编排工具Docker-compose,这里我们介绍他的安装与基础命令。

  curl -L "https://github.com/docker/compose/releases/download/${release_tag}/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose

替换release_tag为对应版本,这里我们使用最新的v2.6.0.

  curl -L "https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose

「常用命令:」

深入学习容器化与服务编排,请查阅「参考资料」

五、参考资料

点击关注公众号,“技术干货” 及时达!

阅读原文

跳转微信打开

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

DeepSeek Ollama SpringAI 知识库 容器化
相关文章