Spring AI 作为 Java 生态中备受瞩目的 AI 应用开发框架,凭借其简洁的 API 设计和强大的功能,为开发者提供了构建智能应用的强大工具。与此同时,DeepSeek 作为领先的 AI 模型服务提供商,在自然语言处理、计算机视觉等领域展现了卓越的性能。本文将带你探索如何将 Spring AI 与 DeepSeek 无缝集成,体验其便捷的开发流程与高效的智能能力,助你快速构建功能强大的 AI 驱动应用。
1 创建项目
注意: Spring AI 需要 Java 17 或更高版本
Spring AI通过现有的OpenAI客户端与DeepSeek AI集成,所以选择OpenAI依赖包.
Maven 依赖
<?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.4.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.hengzq</groupId><artifactId>spring-ai-deepseek-example-001</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-ai-deepseek-example-001</name><description>spring-ai-deepseek-example-001</description><properties><java.version>17</java.version><spring-ai.version>1.0.0-M6</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-openai-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>
配置DeepSeek
spring:application:name: spring-ai-deepseek-example-001ai:openai:api-key: sk-e3b91498acca434a909ffa1115823937base-url: https://api.deepseek.comchat:options:model: deepseek-chat
获取DeepSeek API-KEY,请参考DeepSeek官网「获取DeepSeek API-KEY」
2 对话模型(Chat Model)
@RestController
@RequestMapping("/chat")
public class ChatController {private final ChatModel chatModel;public ChatController(ChatModel chatModel) {this.chatModel = chatModel;}@GetMapping("/conversation")public String conversation(@RequestParam("message") String message) {return chatModel.call(message);}}
调用http://localhost:8080/chat/conversation?message=自我介绍一下
项目源码地址: spring-ai-deepseek-example-001
3 Orange 开源AI项目在线体验
- Orange 官网: http://hengzq.cn
- 在线体验: http://tiny.hengzq.cn
- 项目文档: http://hengzq.cn/orange-monomer/
- 单体架构-后端源码下载【GitHub】: https://github.com/hengzq/orange-monomer
- 单体架构-后端源码下载【Gitee】: https://gitee.com/hengzq/orange-monomer
- 微服务版本-后端源码下载【GitHub】: https://github.com/hengzq/orange-cloud
- 微服务版本-后端源码下载【Gitee】: https://gitee.com/hengzq/orange-cloud
- 前端源码下载【GitHub】: https://github.com/hengzq/orange-cloud
- 前端源码下载【Gitee】: https://gitee.com/hengzq/orange-cloud
注:前端项目设计灵活,能够同时兼容后端的单体架构和微服务架构