Spring AI是干嘛的
官网最权威,直接粘贴:"Spring AI"项目旨在简化那些包含人工智能功能的应用程序的开发过程,同时避免不必要的复杂性。
AI相关领域的功能对python的支持是最好的,相关供应商在出了啥功能的时候,都会优先支持python。 在java这块儿都是大佬们搞的社区去支持,现在spring官网推出了springAI,让我们java开发者也有了对AI操作的框架可用了。
前期准备
1、springAI在官网上的也是一个project,咱们现在开发项目都是用springboot,springAI对springboot的版本是有要求的,官网最权威:
Spring AI supports Spring Boot 3.4.x. When Spring Boot 3.5.x is released, we will support that as well.
所以得搞个springboot3.4.x以上的版本,这个版本也要求jdk得是17以上的,我是直接搞了个springboot3.5.4 + jdk21的版本。
2、springAI是搞大模型的,所以我们得有个大模型,去硅基流动搞一个免费的。
搜索一个免费的,对话的大模型。


搞个密钥

创建项目
搞java的,IntelliJ IDEA总得有吧,就基于这个去创建了


点击创建后,你就应该得到一个下面的项目了

pom文件
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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chat</groupId>
<artifactId>ai-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ai-demo</name>
<description>ai-demo</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
<spring-ai.version>1.0.0</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
配置大模型
在 application.properties 里面配置一下

写个对话接口
在写之前,有一个巨重要的接口得介绍一下:ChatClient
ChatClient 提供了一套流畅的 API,用于与人工智能模型进行通信。它支持同步和流式两种编程模式,我们今天搞一个最简单的同步方式
创建一个ChatClient
XML
package com.chat.aidemo.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: yin79
* @Date: 2025/7/31
*/
@RestController
@RequestMapping("/ai")
public class HelloAIController {
private final ChatClient chatClient;
public HelloAIController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
@GetMapping("/hi")
public String sayHi(@RequestParam(required = false, defaultValue = "讲个笑话") String message) {
return chatClient.prompt()
.user(message) // 用户的输入,可以理解为用户提示词
.call() // 调用大模型
.content(); // 获取大模型的回复, string类型的
}
}
调用:
