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 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
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類型的}
}
調用: