一起学springAI系列一:初体验

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类型的
    }
}

调用:

相关推荐
奋斗的小高3 分钟前
Docker 安装与使用
java
paperxie_xiexuo4 分钟前
如何高效完成科研数据的初步分析?深度体验PaperXie AI科研工具中数据分析模块在统计描述、可视化与方法推荐场景下的实际应用表现
大数据·数据库·人工智能·数据分析
强化学习与机器人控制仿真4 分钟前
Meta 最新开源 SAM 3 图像视频可提示分割模型
人工智能·深度学习·神经网络·opencv·目标检测·计算机视觉·目标跟踪
人工智能训练4 分钟前
Windows中如何将Docker安装在E盘并将Docker的镜像和容器存储在E盘的安装目录下
linux·运维·前端·人工智能·windows·docker·容器
毕设源码-钟学长15 分钟前
【开题答辩全过程】以 浮生馆汉服租赁管理系统为例,包含答辩的问题和答案
android·java·tomcat
蜂蜜黄油呀土豆15 分钟前
深入理解 Agent 相关协议:从单体 Agent 到 Multi-Agent、MCP、A2A 与 Agentic AI 的系统化实践
人工智能·ai agent·大模型应用·agentic ai
90后小陈老师15 分钟前
用户管理系统 07 项目前端初始化 | 新手实战 | 期末实训 | Java+SpringBoot+Vue
java·前端·spring boot
WWZZ202523 分钟前
快速上手大模型:深度学习5(实践:过、欠拟合)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
Coder-coco27 分钟前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·vue.js·spring boot·小程序·论文·毕设·电子点餐系统
Halo_tjn28 分钟前
Set集合专项实验
java·开发语言·前端·python