Springboot + Ollama + IDEA + DeepSeek 搭建本地deepseek简单调用示例

1. 版本说明

springboot 版本 3.3.8

Java 版本 17

spring-ai 版本 1.0.0-M5

deepseek 模型 deepseek-r1:7b

需要注意一下Ollama的使用版本:

2. springboot项目搭建

可以集成在自己的项目里,也可以到 spring.io 生成一个项目

生成的话,如下图配置
这里放一个完整pom文件
pom.xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.3.8</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
		<spring-ai.version>1.0.0-M5</spring-ai.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

3. Ollama搭建

Ollama官网 需要下载两个,一个是Ollama的安装包,一个是模型。

下载对应操作系统的Ollama并一路默认安装,安装好后打开Models安装deepseek。


这里选择默认7b,如果电脑配置不高,选择1.5b,当然也可以往上选择。复制右边红框内的命令

打开cmd,粘贴刚刚复制的命令 ollama run deepseek-r1:7b

4. 代码部分

a. application.properties

.properties 复制代码
spring.application.name=demo
spring.ai.ollama.chat.options.model=deepseek-r1:latest
spring.ai.ollama.base-url=http://127.0.0.1:11434
spring.ai.ollama.chat.enabled=true
server.port=9091

关于model,填写自己当时下载的,比如上文下载的7b,冒号后面应该是7b,但是我之前下载的变成了latest,所以这里改成了latest,如果不清楚自己的模型,可以输入命令 ollama list 来查看模型,一定要和本机的模型对应上。

b. DeepSeekService

java 复制代码
public interface DeepSeekTestService {
    String getResponse(String message);
}

c.DeepSeekServiceImpl

java 复制代码
@Service
public class DeepSeekServiceImpl implements DeepSeekTestService {

    private final OllamaChatModel ollamaChatModel;

    public DeepSeekServiceImpl(OllamaChatModel ollamaChatModel) {
        this.ollamaChatModel = ollamaChatModel;
    }

    @Override
    public String getResponse(String message) {
        return ollamaChatModel.call(message);
    }
}

这里可能会提是无法自动注入,先不需要管它,后面编译正常就行。

d. controller

java 复制代码
@RestController
public class testController {

    @Autowired
    private DeepSeekTestService deepSeekTestService;

    @RequestMapping("/ask1")
    public String speak(@RequestParam String msg){
        return deepSeekTestService.getResponse(msg);
    }
}

编辑好后启动项目并输入问题,我这里在浏览器直接测试

相关推荐
Shannon@10 小时前
Deepseek基座:Deepseek-v2核心内容解析
deepseek
潘锦11 小时前
DeepSeek 和腾讯元宝都选择在用的SSE 到底是什么?
deepseek
轻语呢喃18 小时前
DeepSeek 接口调用:从 HTTP 请求到智能交互
javascript·deepseek
奔跑吧邓邓子1 天前
DeepSeek 赋能智能教育知识图谱:从构建到应用的革命性突破
人工智能·知识图谱·应用·deepseek·智能教育
中杯可乐多加冰1 天前
【解决方案-RAGFlow】RAGFlow显示Task is queued、 Microsoft Visual C++ 14.0 or greater is required.
人工智能·大模型·llm·rag·ragflow·deepseek
奔跑吧邓邓子2 天前
DeepSeek 赋能智能养老:情感陪伴机器人的温暖革新
人工智能·机器人·deepseek·智能养老·情感陪伴
kinghighland2 天前
【实操】deepseek + mcp + 本地知识库,实现对 pcap 格式的 VoLTE 呼叫信令的分析诊断
deepseek
struggle20252 天前
LLMControlsArm开源程序是DeepSeek 控制熊猫机械臂
人工智能·python·cmake·jupyternotebook·deepseek
奔跑吧邓邓子2 天前
DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
人工智能·智慧能源·deepseek·微电网优化调度
磊叔的技术博客2 天前
LLM 系列:LLM 的发展历程
llm·openai·deepseek