1.简介
Spring AI 是一个由 Spring 社区开发的开源项目,旨在简化在 Spring 应用程序中集成和使用人工智能 (AI) 功能的过程。它提供了一套高级抽象和工具,让开发者能够更轻松地与各种 AI 模型(特别是大型语言模型 LLM 和嵌入模型)进行交互
核心思想
Spring AI 的核心思想是提供一个统一的编程模型和抽象层,让开发者能够:
- 屏蔽底层 AI 模型的复杂性: 开发者无需直接学习和处理各种 AI 服务提供商(如 OpenAI、Azure OpenAI、Hugging Face、Google Gemini、或者像你之前提到的硅基流动等)复杂的 REST API 细节。
- 轻松切换 AI 提供商: 只需要修改少量配置和依赖,就可以在不同的 AI 模型或服务提供商之间切换,而无需大幅改动业务逻辑代码。
- 拥抱 Spring 生态系统: 将 AI 功能无缝融入到 Spring 应用程序中,利用 Spring Boot 的自动配置、依赖注入等特性,降低开发门槛。
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.创建SpringBoot项目
使用Idea快速创建Java Maven项目,具体步骤省略,Springboot版本选用3.2.x以上版本,这里选择最新版3.5.3版本
3.1 添加依赖
添加Springboot和Spring ai相关依赖,集成spring-ai-bom作为依赖版本管理
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 http://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.3</version>
</parent>
<groupId>com.ly</groupId>
<artifactId>Spring-AI</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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-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>
<!-- Spring AI OpenAI -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai</artifactId>
<version>1.0.0</version>
</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>
3.2配置密匙
使用Open AI时,我们需要配置一个api key密匙
yml
server:
port: 9001
spring:
application:
name: spring-ai
ai:
openai:
# 硅基流动API接口
base-url: https://api.siliconflow.cn
# api-key 密匙
api-key: 你的密匙
chat:
options:
# 模型名称(注意使用场景,这里是对话场景)
model: Qwen/QwQ-32B
3.3编写实现类
上面配置基本结束,只需要提供实现和启动即可
java
package com.ly.ai.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author: LY
* @date: 2025/7/15 9:40
* @description: open-ai硅基流动测试
*/
@RequestMapping("/chat")
@RestController
public class ChatController {
private final OpenAiChatModel chatModel;
@Autowired
public ChatController(OpenAiChatModel chatModel) {
this.chatModel = chatModel;
}
/**
* 这里简单实现一个接口,让用户输入一个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));
}
}
3.4创建启动类
java
@SpringBootApplication
public class AIApplication {
public static void main(String[] args) {
System.out.println("====开始启动====");
SpringApplication.run(AIApplication.class, args);
System.out.println("====启动结束====");
}
}
3.5测试执行
到此,Springboot和Spring ai的集成已经结束,只需要启动输入http://127.0.0.1:9001/chat/ai/generate?message=硅基流动怎么用
地址访问即可
结语
这篇文章主要是快速实现通过SpringBoot集成OpenAI API实现对话,只需要实现简单的配置即可,后续更详细的则需要自己进行扩展,再根据需要集成到业务当中去。