项目配置说明
Maven依赖
<?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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tech</groupId>
<artifactId>jackal-spring-ai</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jackal-spring-ai</name>
<description>Spring AI demo</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
在https://repo.spring.io/milestone和https://repo.spring.io/snapshot仓库中并没有找到官方文档中的1.0.0-M8和1.0.0-SNAPSHOT版本,因此选用了里面最新的1.0.0-M5版本。
应用配置
spring:
application:
name: jackal-spring-ai
ai:
openai:
api-key: ${DEEPSEEK_KEY}
siliconflow:
api-key: ${SILICOFLOW_KEY}
endpoint: "https://api.siliconflow.cn"
embeddingModel: "BAAI/bge-m3"
spring.ai.openai.api-key必须配置,否则会报错,但该配置在项目中暂时无用。- 这里使用了硅基流动的嵌入模型,在 硅基流动控制台 获取API-KEY。
核心代码实现
注入模型配置
package com.tech.config;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.openai.OpenAiEmbeddingOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* ModelConfig
*
* @author Jonas
* @date 2025-05-11
* @version 1.0
*/
@Configuration
public class ModelConfig {
@Value("${spring.ai.siliconflow.api-key}")
private String siliconflowApiKey;
@Value("${spring.ai.siliconflow.endpoint}")
private String siliconflowEndpoint;
@Value("${spring.ai.siliconflow.embeddingModel}")
private String siliconflowEmbeddingModel;
@Bean(name = "siliconflowEmbeddingModel")
public EmbeddingModel siliconflowEmbeddingModel() {
OpenAiApi openAiApi = OpenAiApi.builder()
.baseUrl(siliconflowEndpoint)
.apiKey(siliconflowApiKey)
.build();
OpenAiEmbeddingOptions openAiEmbeddingOptions = OpenAiEmbeddingOptions.builder()
.model(siliconflowEmbeddingModel)
.build();
return new OpenAiEmbeddingModel(openAiApi, MetadataMode.EMBED, openAiEmbeddingOptions);
}
}
Service 层
@Service
public class ChatService {
private final EmbeddingModel siliconflowEmbeddingModel;
public ChatService(
@Qualifier("siliconflowEmbeddingModel") EmbeddingModel siliconflowEmbeddingModel) {
this.siliconflowEmbeddingModel = siliconflowEmbeddingModel;
}
public float[] embedBySiliconflow(String input) {
return siliconflowEmbeddingModel.embed(input);
}
}
Controller 层
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class SpringAiController {
public final ChatService chatService;
@PostMapping("/embed/siliconflow")
public float[] embedBySiliconflow(String input) {
return chatService.embedBySiliconflow(input);
}
}