暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Spring AI 接入 DeepSeek,很香~~~

码上Javaer 2025-03-01
26

前言


Spring 官方已经确定接入 DeepSeek了,不过目前只是预览版,并不是稳定版,想在正式环境接入,还需要时间,大家耐心等待。不过想了解的可以先上手试试。
Spring AI 接入DeepSeek官网地址https://docs.spring.io/spring-ai/reference/api/chat/deepseek-chat.html


01

Ollama 部署的 DeepSeek 以及使用


本地部署 DeepSeek,可以参考上篇:本地部署 DeepSeek

此处重点介绍几个 Ollama 的命令

  • 查看 Ollama 版本

    ollama -v
    复制
    • 启动 Ollama 服务

      ollama serve
      复制
      • 运行大模型

        ollama run deepseek-r1:7b  本地没有时会下载,然后运行大模型
        复制
        • 查看运行大模型

          ollama ps
          复制
          • 停止运行大模型

            ollama stop deepseek-r1:7b
            复制
            • 查看本地按照的大模型列表

              ollama list
              复制
              • 启动 deepseek-r1:7b 大模型,有如下提示表示启动成功

                 ollama run deepseek-r1:7b
                复制

                Ollama大模型启动后默认端口是11434,接口地址:http://localhost:11434/api/chat。

                大模型启动成功后,可以本地先测试下,采用 curl 方式,如下:

                  curl -X POST http://localhost:11434/api/chat \
                  -d '{
                     "model": "deepseek-r1:7b",// 运行大模型的名称
                  "messages": [
                         {"role": "user", "content": "分析下中国未来新能源的前景"}
                  ],
                     "stream": false
                  }'
                  复制

                  也可以使用 Postman 提问



                  02

                  Spring AI 接入 DeepSeek 服务


                  Spring AI 简介

                     官网地址: https://spring.io/projects/spring-ai,截止文档编写为止,只有快照版本

                  查阅官方文档,要使用 Spring AI,SpringBoot 版本要 3.2.x 以上,同样 JDK 要求 17 以上(本地需提前安装)

                  Spring AI 应用搭建

                  可以使用 spring 提供的 start 来搭建,地址:https://start.spring.io,依赖项要选择 Ollama

                  下载成功后导入到 Idea 中(具体不展开了)

                  配置 DeepSeek

                  在 application.properties 中添加 deepseek 本地配置

                    # 本地ollama服务地址
                    spring.ai.ollama.base-url: http://localhost:11434
                    // 本地大模型名称
                    spring.ai.ollama.chat.model: deepseek-r1:7b
                    复制

                    编写API服务

                      @RestController
                      @RequestMapping("/ai")
                      public class AiChatController {


                      private final ChatClient chatClient;


                      public AiChatController(ChatClient.Builder builder) {
                      this.chatClient = builder.build();
                      }


                      @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=UTF-8")
                      public ResponseEntity<Flux<String>> chat(@RequestParam(value = "message") String message) {
                      try {
                      Flux<String> response = chatClient.prompt(message).stream().content();
                      response.subscribe(data -> System.out.println("Response data:" + data));
                      return ResponseEntity.ok()
                      .contentType(MediaType.TEXT_EVENT_STREAM)
                      .header(HttpHeaders.CONTENT_ENCODING, "UTF-8")
                      .body(response);
                      } catch (Exception e) {
                      return ResponseEntity.badRequest().build();
                      }
                      }
                      }
                      复制

                      启动服务

                      报错了

                        ***************************
                        APPLICATION FAILED TO START
                        ***************************


                        Description:


                        Web application could not be started as there was no org.springframework.boot.web.reactive.server.ReactiveWebServerFactory bean defined in the context.
                        复制
                          // 需要引入 web 和 webflux 两个 jar 包
                          <dependency>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-web</artifactId>
                          </dependency>
                          <dependency>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-webflux</artifactId>
                          </dependency>
                          复制

                          再次启动,成功了

                          Postman 访问测试下

                          本地 Deepseek 可以正常回答了,但是奈何机器不给力,暂时演示到此,有兴趣的可以继续往下学习,可以自己写一个前端来实现聊天功能。


                          总结


                          Spring AI 依托强大的 Spring 框架,日后肯定会厚积薄发,大家耐心等待,Java 后续再 AI 时代的应用支撑也会越来越强大。感谢大家阅读本篇文章,希望以上内容能对大家有所帮助。如果你有任何疑问或者建议,欢迎在评论区留言交流。同时,别忘了点击下方的 “在看” 和 “分享”,让更多的程序员朋友受益。后续我们还会分享更多精彩的编程知识和经验,敬请关注!



                          文章转载自码上Javaer,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                          评论