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

调用:

相关推荐
牛哥带你学代码6 小时前
VLA 与机器人基础模型:具身智能的下一代操作系统
人工智能·机器人
Cutecat_6 小时前
YouTube 多语种自动配音功能实测:AI 如何让一条视频“开口说”多种语言
人工智能·科技·音视频
Maynor9966 小时前
AI Coding 零基础实战教程|第五部分:完整项目案例实操
java·前端·人工智能·claude code·ai coding
三品吉他手会点灯6 小时前
嵌入式机器学习 - 学习笔记1.0.2 - 学习路线导览:从机器学习基础到 TinyML应用
人工智能·笔记·嵌入式硬件·学习·机器学习
生戎马6 小时前
Tomcat Container容器之Engine:StandardEngine _
java·tomcat
lunzi_08266 小时前
【AI 杂谈】AI 的边界正在被谁定义?
人工智能
zhangjw346 小时前
第29篇:Java伪共享与对象分配:并发性能优化的关键
java·开发语言·性能优化
静Yu6 小时前
给 AI 装上「身体」:我用魔珐星云做数字人的实践与接入教程
人工智能
OpenMiniServer6 小时前
终极演化动力学:绝对悖论与信息重构
人工智能·重构
阿乔外贸日记6 小时前
意大利进口主力产品及主要合作供应国
大数据·人工智能·物联网·搜索引擎·云计算