feat(ai): 集成 Spring AI 并配置 DeepSeek 模型支持

- 在 pom.xml 中添加 spring-ai 版本管理及依赖
- 在 application-dev.yml 中配置 DeepSeek AI 相关参数
- 新增 AI 模型测试用例,支持同步、流式及自定义模型选项调用
- 引入 spring-ai-ollama 和 spring-ai-starter-model-deepseek依赖以支持多种模型接入
This commit is contained in:
lijunming 2025-09-23 19:21:09 +08:00
parent 003d2bd27c
commit 7e43fd59a4
4 changed files with 92 additions and 3 deletions

View File

@ -35,6 +35,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
<spring-ai.version>1.0.0</spring-ai.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
@ -53,6 +54,13 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId> <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <artifactId>mybatis-spring-boot-starter</artifactId>

View File

@ -12,6 +12,15 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>

View File

@ -30,6 +30,15 @@ spring:
connection-test-query: SELECT 1 connection-test-query: SELECT 1
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
ai:
deepseek:
api-key: your-api-key
base-url: https://api.deepseek.com # DeepSeek 的请求 URL, 可不填,默认值为 api.deepseek.com
chat:
options:
model: deepseek-reasoner # 使用深度思考模型
temperature: 0.8 # 温度值
# MyBatis 配置【如需使用记得打开】 # MyBatis 配置【如需使用记得打开】
#mybatis: #mybatis:
# mapper-locations: classpath:/mybatis/mapper/*.xml # mapper-locations: classpath:/mybatis/mapper/*.xml
@ -39,4 +48,5 @@ spring:
logging: logging:
level: level:
root: info root: info
config: classpath:logback-spring.xml config: classpath:logback-spring.xml

View File

@ -1,19 +1,81 @@
package com.touka.test; package com.touka.test;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Flux;
@Slf4j @Slf4j
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class ApiTest { public class ApiTest {
@Resource
private DeepSeekChatModel chatModel;
/**
* 测试同步生成响应
*/
@Test @Test
public void test() { public void testGenerate() {
log.info("测试完成"); String message = "Tell me a joke";
// 方式1: 直接传入字符串
String response1 = chatModel.call(message);
System.out.println("Response1: " + response1);
// 方式2: 使用 Prompt
ChatResponse response2 = chatModel.call(new Prompt(message));
System.out.println("Response2: " + response2.getResult().getOutput().getText());
}
/**
* 测试流式生成响应
*/
@Test
public void testGenerateStream() {
String message = "Tell me a joke";
// 使用 PromptTemplate 构建提示词
Prompt prompt = new PromptTemplate(message).create();
// 流式输出
Flux<ChatResponse> responseFlux = chatModel.stream(prompt);
// 订阅并打印流式响应
responseFlux.doOnNext(response -> {
System.out.println("Stream Response: " + response.getResult().getOutput().getText());
}).blockLast(); // 在测试中阻塞等待完成
}
/**
* 测试使用特定模型选项
*/
@Test
public void testGenerateWithModelOptions() {
String message = "1+1=?";
String model = "deepseek-r1:1.5b";
ChatResponse response = chatModel.call(new Prompt(
message,
OllamaOptions.builder()
.model(model)
.build()
));
System.out.println("Response with model options: " + response.getResult().getOutput().getText());
} }
} }