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

SpringAI 内置接入 DeepSeek,太赞了!

原创 迷三张 2025-02-13
624
    DeepSeek 目前是国产 AI 模型Top,越来越多的公司企业应用想要集成。然而Spring官方 Spring AI 最新快照版已经内置 DeepSeek 了,所以以后项目中对接 DeepSeek 就方便多了。
相关联文章推荐:
DeepSeek和ChatGPT对比
本地部署deepseek-r1模型,是否有必要?
无需GPU轻松本地部署多款大模型,DeepSeek、qwen、llama等支持!

Spring AI 介绍

    Spring AI 项目旨在简化包含人工智能功能的应用程序的开发,避免不必要的复杂性。解决了 AI 集成的根本挑战:Connecting your enterprise Data and APIs with AI Models
    它提供了抽象概念,作为开发 AI 应用程序的基础。这些抽象概念具有多种实现,只需进行少量代码更改即可轻松更换组件。
主要包含功能:
  • 跨 AI 提供商的可移植 API 支持聊天、文本转图像和嵌入模型。支持同步和流式 API 选项。还可访问特定于模型的功能。

  • 支持所有主要AI 模型提供商,例如 Anthropic、OpenAI、Microsoft、Amazon、Google 和 Ollama。支持的模型类型包括:聊天完成,嵌入,文本转图像,音频转录,文本转语音,适度。 最新预览版也已经支持了DeepSeek模型。
  • 结构化输出- 将 AI 模型输出映射到 POJO。

用于 AI 模型和向量存储的 Spring Boot 自动配置和启动器。

用于数据工程的文档提取ETL 框架

  • ChatClient API - 用于与 AI 聊天模型通信的流畅 API,惯用语类似于 WebClient 和 RestClient API



Spring AI 集成 DeepSeek

1、Spring AI是基于 Spring Boot3.x
 框架构建的,环境准备如下:
  • JDK 17 或更高版本
  • Maven 或 Gradle 构建工具
  • DeepSeek API Key(可通过官网注册获取),申请地址:https://platform.deepseek.com/usage    

2、集成方式
  • spring-ai-openai starter:伪装成 OpenAI,DeepSeek 提供了 OpenAI 兼容模式。
  • spring-ai-ollama-spring-boot-starter:通过 Ollama 本地部署一个 DeepSeek R1 蒸馏版。

3、DeepSeek 可集成模型
  • deepseek-chat(V3):适用于聊天机器人、智能客服、内容生成等,能够理解和生成日常对话内容。
  • deepseek-reasoner(R1):专为复杂推理任务设计,适合解决需要深度逻辑分析和推理的问题。

4、ai-openai集成
  • Maven依赖
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency> 
      <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
      </dependency>
    </dependencies>
    • 配置
      # 必填项  
      spring.ai.openai.api-key=you-apikey 
      spring.ai.openai.base-url=https://api.deepseek.com 
      # 模型选择(示例使用对话模型)       
      spring.ai.openai.chat.options.model=deepseek-chat 
      • 编码
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;




        @RestController 
        @RequestMapping("/v1/api/chat")
        public class ChatAiController {


            @Autowired
            private DeepSeekClient deepSeekClient;


            @PostMapping
            public String chat(@RequestBody String message) {
                return deepSeekClient.chatCompletion(message).getOutput().getContent();
            }


            @GetMapping(value = "/stream", produces = "text/event-stream")
            public Flux<StringchatStream(@RequestParam String message) {
                return deepSeekClient.chatFluxCompletion(message)
                .map(response -> response.getOutput().getContent());
            }
        }
            chat 接口用于处理普通的非流式请求; chatStream 接口则支持流式响应,能够实时返回 AI 的推理结果。
        5、ai-ollama集成
        • Ollama部署集成DeepSeek蒸馏模型,参考文章:
        无需GPU轻松本地部署多款大模型,DeepSeek、qwen、llama等支持!
        • Maven依赖添加
          <dependency>
              <groupId>org.springframework.ai</groupId>
              <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
          </dependency>
          • 配置
            spring.ai.ollama.base-url=http://localhost:11434 
            spring.ai.ollama.chat.model=deepseek-r1:1.5b  # 与本地模型名称对应
            • 编码
              @RestController 
              @RequestMapping("/v1/api/chat")
              public class ChatAiOllamaController {


                  @Autowired
                  private DeepSeekClient deepSeekClient;


                  @GetMapping("/stream/ollama")
                  public ResponseEntity<Flux<String>> chatStream(@RequestParam(value = "message"String message) {
                      try {
                          // 调用 deepSeekClient 生成响应,并以 Flux<String>(响应流)形式返回
                          Flux<String> response = deepSeekClient.chatFluxCompletionOllama(message);
                          return ResponseEntity.ok(response);
                      } catch (Exception e) {
                          return ResponseEntity.badRequest().build();
                      }
                  }
              }

              总结

                  此篇文章主要介绍了Spring AI 功能和旨在解决的问题、如何通过它集成DeepSeek模型【在线模型 和  本地模型】。
                  最后再说下 Spring Cloud Alibaba AI 中也支持这种方式,并且官网上提供了详细的方法,有兴趣可以去探索下~~~~。

              https://github.com/alibaba/spring-ai-alibaba/blob/main/README-zh.md

              Spring Cloud Alibaba AI

              https://docs.spring.io/spring-ai/reference/api/chatclient.html

              Spring AI

              欢迎微信扫描二维码,关注我的公众号~~


              最后修改时间:2025-02-17 13:11:34
              文章转载自迷三张,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论