Ollama部署Deepseek代码实践

1.参考资料

教你用AutoDL+Ollama部署AI大模型DeepSeekR1教程

autodl:SSH远程连接文档

autodl:学术加速文档

ollama:deepseek相关文档

‍

2. 内容总结

  1. 使用环境

autodl:2080ti

deepseek模型:deepseek-r1:1.5b​

springboot版本:3.2.3​

JDK:17

spring-ai版本:0.8.1​

  1. 主要内容

① ollama基础和常见指令

② autodl服务器租借和远程连接及本地使用

③ 服务器上部署模型操作

④ ollama安装后的测试操作

⑤ springboot中集成ollama及接口测试

⑥ 出现的bug总结

  1. 正文

前提:ollama基础知识

  1. 相关指令
ollama list  
ps aux | grep ollama  # 查看是否运行
pkill ollama  #中止ollama匹配的进程
ollama serve  #启动 Ollama 服务,提供 API 接口
ollama pull <model>  #下载指定模型。

ollama run <model>  #运行模型进行交互式或单次推理。

ollama rm <model>  #删除本地模型。

curl http://localhost:<port>/api/generate  #调用 Ollama API 进行模型推理。

OLLAMA_DEBUG=true ollama serve  #启用调试模式输出详细日志。

cat ~/.ollama/logs/server.log  #查看 Ollama 服务日志。

  1. 什么是ollama

Ollama 是一个开源工具,用于在本地运行和管理大语言模型(LLMs) ,如 Llama、Mistral、Gemma 等。它简化了模型的部署、推理和 API 调用,适合开发人员、研究人员和爱好者在本地设备上使用 AI 模型。

  1. ollama的核心特点
  • 本地化运行:保护数据隐私,所有推理在本地完成。
  • 简单部署:通过 CLI 快速拉取和运行模型。
  • API 支持:提供 /api/generate 等端点,适合开发和测试。
  • 跨平台:兼容 Linux、macOS 和 Windows。
  • 高效优化:支持量化模型,降低资源需求

‍

1. autodl租借服务器

因为学习用的模型,选择了最低参数量1.5B,因此只需要用比较便宜的服务器即可(2080)。

选择环境后自动开机。

注意点:

① 不用的时候需要及时关闭, 按照使用的分钟计费

② 本次使用的是北京A区,12核11GB服务器(其它地区或者服务器也可以),如果出现下载或者速度问题,可以换个地区,因为学术加速是不稳定的

​在这里插入图片描述
在这里插入图片描述

‍

2. termius连接ssh和本地使用

如果需要远程连接ssh,可以使用以下方法,也可以直接在服务器的终端使用,点击JupyterLab。

① 文档

结合autodl的文档,使得能够在termius中操作服务器,参考文档如下。

如果是本地使用, 也可以直接复制ssh的命令到powershell执行。
在这里插入图片描述

​​

在这里插入图片描述

② termius连接

打开termius,或者其他ssh软件操作,连接示例如下:(也可以直接在服务器中操作)

​在这里插入图片描述
​

③ 连接后显示内容

​在这里插入图片描述
​

3. 部署模型操作

  • 使用学术加速(提高安装速度)
source /etc/network_turbo
  • 安装相关指令
sudo apt-get update
sudo  apt-get install lshw
apt-get install net-tools -y
apt-get install systemd -y
apt-get install systemctl -y

在这里插入图片描述
​

  • 安装ollama

注意: 如果这个命令执行很慢,需要检查上面的命令执行是否如上图所示,或者需要其它地区的主机。

​

curl -fsSL https://ollama.com/install.sh | sh

在这里插入图片描述
在这里插入图片描述

​

  • 运行ollama
systemctl status ollama.service
systemctl start ollama.service
  • 环境修改:添加阿里云镜像和端口
export OLLAMA_ORIGINS=https://mirror.registry.cn-hangzhou.aliyuncs.com  # 阿里云镜像
export OLLAMA_HOST=127.0.0.1:6006

为什么是6006端口? 参考说明:https://www.autodl.com/docs/port/

在这个端口有可访问的公网地址。

​在这里插入图片描述
​

  • 检验是否成功
sudo systemctl daemon-reload
sudo systemctl restart ollama
netstat -an | grep 6006

在这里插入图片描述

  • 拉取模型:1.5b即可

下载完成,在位置上输入测试问题,1+1=?,得到回答。

OLLAMA_HOST=127.0.0.1:6006 ollama run deepseek-r1:1.5b

在这里插入图片描述

‍

4. 安装ollama后测试

① 按照2.中进行本地部署

在powershell中执行ssh的命令,然后打开:

http://127.0.0.1:6006/api/tags,得到结果如下:

​在这里插入图片描述
​

② 使用curl指令测试

stream:true会显示思考过程

curl http://localhost:6006/api/generate \
  -H "Content-Type: application/json" \
  -d '{
        "model": "deepseek-r1:1.5b",
        "prompt": "1+1",
        "stream": false
      }'

在这里插入图片描述

​​

5. spring集成ollama

① 添加依赖和配置

注意:springboot版本需要是3.xx以上,JDK17

spring:
  ai:
    ollama:
      base-url: http://localhost:6006
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <mybaits-plus.version>3.5.2</mybaits-plus.version>
        <spring-ai.version>0.8.1</spring-ai.version>
    </properties>
   <dependencies>
 <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-core</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
   </dependencies>

② ollama的config文件

package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.ollama.api.OllamaApi;
@Configuration
@Slf4j
public class OllamaConfig {
    @Bean
    public OllamaApi ollamaApi(@Value("${spring.ai.ollama.base-url}") String baseUrl){
        log.info("OllamaApi base url: {}", baseUrl);
        return new OllamaApi(baseUrl);
    }
    @Bean
    public OllamaChatClient ollamaChatClient(OllamaApi ollamaApi){
        return new OllamaChatClient(ollamaApi);
    }

}

③ 接口测试设计

package org.example.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;


@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/ollama")
public class OllamaController {
    @Resource
    private OllamaChatClient ollamaChatClient;

    /**
     * 生成响应
     * @param model deepseek-r1:1.5b
     * @param message 消息
     * @return
     */
    @RequestMapping(value = "generate", method = RequestMethod.GET)
    public ChatResponse generate(@RequestParam String model, @RequestParam String message){
        return ollamaChatClient.call(new Prompt((message),OllamaOptions.create().withModel(model)));
    }

    /**
     * 生成流式响应
     * @param model deepseek-r1:1.5b
     * @param message
     * @return
     */
    @RequestMapping(value = "generate_stream", method = RequestMethod.GET)
    public Flux<ChatResponse> generateStream(@RequestParam String model, @RequestParam String message) {
        return ollamaChatClient.stream(new Prompt((message), OllamaOptions.create().withModel(model)));
    }
}

④ api调用

传入参数model:deepseek-r1:1.5b​,meassage是自定义
在这里插入图片描述
在这里插入图片描述

​

6. 出现bug总结

  • 出现下载ollama超时

解决:检查前面的命令是否更新完成,或者换个地区的服务器(可能是因为学术加速不稳定)

  • 安装完ollama在接口调用时,出现timeout问题

① 检查GPU使用情况

一般不会超出

nvidia-smi 

② 检查内存和进程

top
free -m

③ 终止ollama相关进程,重新启动

pkill ollama

④ 重新启动,并且指定端口

OLLAMA_HOST=0.0.0.0:6006 nohup ollama serve &

⑤ 还是不行,尝试重新安装

ollama pull deepseek-r1:1.5b

⑥ 测试

6006端口,一般会显示 ollama is running

curl http://localhost:6006

curl -v http://localhost:6006/api/generate \
  -H "Content-Type: application/json" \
  -d '{
        "model": "deepseek-r1:1.5b",
        "prompt": "1+1",
        "stream": false
      }'

在这里插入图片描述
​

Logo

免费领 150 小时云算力,进群参与显卡、AI PC 幸运抽奖

更多推荐