随着spring boot的流行,越来越多的开发者选择利用其快速开发和简化配置的优势,构建企业级应用。spring boot通过自动化配置和高度的模块化,使得开发者可以更快速地实现业务功能。而spring ai作为spring生态的一部分,提供了与人工智能技术结合的能力,可以帮助开发者轻松将ai能力集成到spring boot项目中。
本篇文章将引导你如何在Spring Boot中快速集成Spring AI,并通过简单的示例实现AI功能。
2. 环境准备我这里所准备的环境:
Java 17 或更高版本:Spring Boot和Spring AI都依赖于Java 17及更高版本。 Spring Boot 3.x:Spring AI支持Spring Boot 3.2.x和3.3.x。 集成的AI服务:我们将集成OpenAI的API来展示如何使用Spring AI进行智能对话,这里使用硅基流动API演示。3. 创建Spring Boot项目首先,使用Spring Initializr快速创建一个Spring Boot项目。具体创建步骤这里省略,Springboot版本选用3.2.x以上版本,这里选择3.4.2。
3.1. 添加依赖添加Springboot和Spring ai相关依赖。我们集成spring-ai-bom作为依赖版本管理,截止书写这边文章时,该以来版本为1.0.0-SNAPSHOT。
代码语言:xml复制<?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.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>springboot-ai</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-ai</name> <description>springboot-ai</description> <properties> <java.version>17</java.version> </properties> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot DevTools (Optional for auto-reloading during development) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-spring-boot-autoconfigure</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>1.0.0-SNAPSHOT</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>
这里只需要关注两个核心依赖:
代码语言:xml复制<!-- 封装了各大模型的交互接口 --><dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency><!-- 用于各大模型进行自动装配 --><dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-spring-boot-autoconfigure</artifactId></dependency>
为了能够调用OpenAI API,我们需要配置一个API密钥。
在application.properties中添加API密钥配置:
代码语言:properties复制spring.application.name=springboot-ai# 硅基流动API接口,默认为openai.com地址spring.ai.openai.base-url=https://*********# 这里是openai的api密钥spring.ai.openai.api-key=sk-**************# 模型名称,这里选用deepseek-V3模型。你也可以选用Qwen或GPTspring.ai.openai.chat.options.model=deepseek-ai/DeepSeek-V3#spring.ai.openai.chat.options.responseFormat.type=json_object
到此,以上简单几步就已经把Springboot和Spring ai快速集成起来了。这里编写交互类,来具体调用大模型接口:
代码语言:java复制package org.example.springbootai;import org.springframework.ai.chat.messages.UserMessage;import org.springframework.ai.chat.model.ChatResponse;import org.springframework.ai.chat.prompt.Prompt;import org.springframework.ai.openai.OpenAiChatModel;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import reactor.core.publisher.Flux;import java.util.Map;@RestControllerpublic class ChatController { private final OpenAiChatModel chatModel; @Autowired public ChatController(OpenAiChatModel chatModel) { this.chatModel = chatModel; } /** * 这里简单实现一个接口,让用户输入一个prompt,然后返回一个结果。prompt输入一个指令,让他给我们讲个笑话 * @param message * @return */ @GetMapping("/ai/generate") public Map<String,String> generate(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) { return Map.of("generation", this.chatModel.call(message)); }}
@SpringBootApplicationpublic class AiApplication { public static void main(String[] args) { SpringApplication.run(AiApplication.class, args); }}
到此,Springboot和Spring ai的集成已经结束了。我们运行AiApplication后,访问地址:http://localhost:8080/ai/generate。可以看到浏览器成功返回了大模型基于我们的prompt返回的文本内容:
本篇文章,我们展示了如何在Spring Boot项目中快速集成Spring AI,并结合OpenAI API构建一个简单的智能对话系统。通过几步简单的配置和代码实现,开发者可以快速将AI能力集成到现有的Spring Boot项目中。
以上就是Spring AI集成DeepSeek的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号