手把手教你 Spring Boot 集成 Ollama 调用 DeepSeek

人工智能 开发
本文将详细介绍如何在 Spring Boot 中集成 Ollama 并调用 DeepSeek,开启 AI 智能搜索的新纪元。

在当今数字化时代,AI 技术的飞速发展正不断改变着我们的生活和工作方式。其中,智能搜索作为一项关键技术,正逐渐成为企业和开发者关注的焦点。而 Spring Boot 作为一种流行的 Java 开发框架,其简洁、高效的特点深受广大开发者的喜爱。本文将详细介绍如何在 Spring Boot 中集成 Ollama 并调用 DeepSeek,开启 AI 智能搜索的新纪元。

一、环境准备

1. 安装 Ollama

访问 Ollama 官网,下载与你操作系统匹配的版本(支持 Windows、macOS 和 Linux)。

https://ollama.com/download/mac       

下载完成后,运行安装程序并按照提示完成安装。对于 Linux 系统用户,可以通过以下命令进行安装:

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

验证 Ollama 安装是否成功,打开终端或命令提示符,输入以下命令:

ollama --version

如果成功安装,终端会返回 Ollama 的版本号。

2. 下载 DeepSeek-R1

确认 Ollama 安装无误后,开始下载 DeepSeek-R1。在终端中运行以下命令:

ollama run deepseek-r1

根据你的网络速度,这个过程可能需要一些时间,请耐心等待,直到下载完成。

二、Spring Boot 项目构建

1. 创建 Spring Boot 项目

使用 Spring Initializr 创建一个 Spring Boot 项目,并添加 Spring AI 依赖。确保在 pom.xml 中包含以下依赖项:

<dependency>
         <groupId>org.springframework.ai</groupId>
         <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
     </dependency>
2. 配置 Ollama

在 Spring Boot 的 application.properties 文件中,配置 Ollama 的服务地址和要调用的模型:

spring.ai.ollama.base-url=http://localhost:11434
     spring.ai.ollama.chat.model=deepseek-r1:1.5b

三、编写代码调用 DeepSeek-R1 模型

1. 创建服务类

在 Spring Boot 项目中,创建一个服务类,用于调用 DeepSeek-R1 模型。以下是一个简单的示例:

import org.springframework.ai.ollama.OllamaChatModel;
     import org.springframework.stereotype.Service;

     @Service
     public class DeepSeekService {
         private final OllamaChatModel ollamaChatModel;

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

         public String search(String prompt) {
             return ollamaChatModel.call(prompt);
         }
     }

在上述代码中,我们通过 @Service 注解将该类标记为一个服务类,并通过构造函数注入的方式获取 OllamaChatModel 实例。search 方法接收一个 prompt 参数,将其传递给 ollamaChatModel.call 方法,从而调用 DeepSeek-R1 模型进行搜索。

2. 创建控制器类

创建一个控制器类,用于接收客户端的请求并调用服务类中的方法。以下是一个示例:

import org.springframework.web.bind.annotation.PostMapping;
     import org.springframework.web.bind.annotation.RequestBody;
     import org.springframework.web.bind.annotation.RestController;

     @RestController
     public class DeepSeekController {
         private final DeepSeekService deepSeekService;

         public DeepSeekController(DeepSeekService deepSeekService) {
             this.deepSeekService = deepSeekService;
         }

         @PostMapping("/search")
         public String search(@RequestBody String prompt) {
             return deepSeekService.search(prompt);
         }
     }

在上述代码中,我们通过 @RestController 注解将该类标记为一个控制器类,并通过构造函数注入的方式获取 DeepSeekService 实例。search 方法接收一个 @RequestBody 注解的 prompt 参数,将其传递给 deepSeekService.search 方法,从而实现对 DeepSeek-R1 模型的调用。

四、测试与运行

1. 启动项目

启动 Spring Boot 项目,确保项目能够正常运行。

2. 测试接口

使用 Postman 或其他工具,向 /search 接口发送 POST 请求,并在请求体中传递搜索关键词。例如,发送以下请求:

{
         "prompt": "人工智能的发展历程"
     }

服务器将返回 DeepSeek-R1 模型的搜索结果,例如:

{
         "response": "人工智能的发展历程可以追溯到 20 世纪 50 年代......"
     }

五、总结

通过上述步骤,我们成功地在 Spring Boot 中集成了 Ollama 并调用了 DeepSeek-R1 模型,实现了 AI 智能搜索功能。这一过程不仅展示了 Spring Boot 与 Ollama 的强大结合,也为我们开启了一扇通往 AI 智能搜索新纪元的大门。在未来的发展中,我们可以进一步探索和优化这一技术,为用户提供更加智能、高效的搜索体验。

责任编辑:赵宁宁 来源: Java技术营地
相关推荐

2023-03-27 08:28:57

spring代码,starter

2021-06-29 12:27:19

Spring BootCAS 登录

2022-01-08 20:04:20

拦截系统调用

2019-11-12 10:50:13

Spring BootstarterJava

2011-01-10 14:41:26

2011-05-03 15:59:00

黑盒打印机

2021-07-14 09:00:00

JavaFX开发应用

2011-02-22 14:36:40

ASP.NETmsdnC#

2021-02-26 11:54:38

MyBatis 插件接口

2011-02-22 13:46:27

微软SQL.NET

2021-12-28 08:38:26

Linux 中断唤醒系统Linux 系统

2023-04-26 12:46:43

DockerSpringKubernetes

2022-07-27 08:16:22

搜索引擎Lucene

2022-12-07 08:42:35

2022-03-14 14:47:21

HarmonyOS操作系统鸿蒙

2011-03-25 12:45:49

Oracle SOA

2020-04-14 10:20:12

MySQL数据库死锁

2016-04-27 09:49:16

用户模型产品总结

2020-07-09 08:59:52

if else模板Service

2021-09-30 18:27:38

数据仓库ETL
点赞
收藏

51CTO技术栈公众号