重磅!Spring 原生支持 DeepSeek,AI 能力再升级!
在最新的 Spring 官方更新中,Spring AI 现已原生支持 DeepSeek!今天就带大家了解如何在 Spring Boot 3.4 项目中集成 DeepSeek AI 服务,整个过程十分简单便捷。Spring AI 介绍
Spring AI 是由 Spring 官方推出的开源框架,旨在为 Java 开发者提供便捷的 AI 集成方案。其核心目标是通过抽象化和模块化的设计,使得 AI 功能能够更简单地融入 Spring 生态体系。以下是 Spring AI 的核心特点:
[*]统一的 API 抽象支持 OpenAI、DeepSeek、Google、Ollama 等多个 AI 服务,提供标准化的调用接口。
[*]核心功能模块支持模型交互、向量处理、检索增强生成(RAG)以及函数调用等。
[*]低代码集成通过 Spring Boot Starter 依赖,可快速完成 AI 服务接入。
[*]结构化数据输出能够将模型响应直接映射为 Java 对象,方便后续数据处理。
[*]流式数据响应支持 Flux 流式输出,适用于实时对话等高并发场景。
获取 API KEY
由于 DeepSeek 官方服务可能存在调用压力,我们可以选择阿里云百炼平台提供的 DeepSeek API。首先前往 阿里云百炼,搜索 DeepSeek 模型,点击“立即体验”。然后在页面右上角点击钥匙图标,即可创建并获取 API KEY。
Spring Boot 3.4 项目中集成 DeepSeek
添加 Spring AI 依赖
在 Spring Boot 3.4 项目的 pom.xml 文件中,引入 Spring AI 相关依赖:
org.springframework.ai spring-ai-openai-spring-boot-starter 1.0.0-M6
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
配置 AI 服务信息
在 application.yml 配置文件中,添加 DeepSeek API 相关参数:
spring:ai: openai: api-key: # 你的 API KEY base-url: https://dashscope.aliyuncs.com/compatible-mode# 阿里云百炼 DeepSeek API 地址 chat: options: model: deepseek-r1# DeepSeek 可选模型(deepseek-r1 或 deepseek-v3) temperature: 0.8# 控制文本生成的创造性,值越低越严谨
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
[*]6.
[*]7.
[*]8.
[*]9.
创建 DeepSeek 接口
在 com.icoderoad.controller 包下,创建 DeepSeekController 控制器:
package com.icoderoad.controller;import org.springframework.ai.openai.OpenAiChatModel;import org.springframework.ai.chat.prompt.Prompt;import org.springframework.ai.chat.messages.UserMessage;import org.springframework.ai.chat.ChatResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.*;import reactor.core.publisher.Flux;import java.util.Map;@RestController@RequestMapping("/ai")public class DeepSeekController { private final OpenAiChatModel chatModel; @Autowired public DeepSeekController(OpenAiChatModel chatModel) { this.chatModel = chatModel; } /** * 直接返回 AI 生成的答案 */ @GetMapping("/chat") public Map chat(@RequestParam("message") String message) { return Map.of("response", chatModel.call(message)); } /** * 流式输出 AI 生成的答案 */ @GetMapping(value = "/chatFlux", produces = MediaType.TEXT_EVENT_STREAM_VALUE + "; charset=UTF-8") public Flux chatFlux(@RequestParam("message") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatModel.stream(prompt); }}
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
[*]6.
[*]7.
[*]8.
[*]9.
[*]10.
[*]11.
[*]12.
[*]13.
[*]14.
[*]15.
[*]16.
[*]17.
[*]18.
[*]19.
[*]20.
[*]21.
[*]22.
[*]23.
[*]24.
[*]25.
[*]26.
[*]27.
[*]28.
[*]29.
[*]30.
[*]31.
[*]32.
[*]33.
[*]34.
[*]35.
[*]36.
[*]37.
[*]38.
[*]39.
[*]40.
[*]41.
[*]42.
[*]43.
[*]44.
[*]45.
[*]46.
[*]47.
[*]48.
测试接口
直接调用 AI 接口
启动 Spring Boot 项目后,使用 Postman 或浏览器访问:
GET http://localhost:8080/ai/chat?message=你好
[*]1.
服务器将返回 AI 生成的答案,可能会有一定的延迟。
采用流式输出
调用流式输出接口,获取持续输出的 AI 生成内容:
GET http://localhost:8080/ai/chatFlux?message=你好
[*]1.
此方式适用于需要实时更新的聊天机器人、流式文本生成等场景。
总结
本次介绍了如何在 Spring Boot 3.4 中使用 Spring AI 集成 DeepSeek AI 服务,并提供了同步和流式调用两种方式。由于流式输出响应更快,适用于大多数实时 AI 交互场景。希望本文能帮助大家更好地利用 Spring AI 进行 AI 能力升级!
页:
[1]