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:
parent
003d2bd27c
commit
7e43fd59a4
8
pom.xml
8
pom.xml
|
@ -35,6 +35,7 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<spring-ai.version>1.0.0</spring-ai.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
@ -53,6 +54,13 @@
|
|||
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
|
|
|
@ -12,6 +12,15 @@
|
|||
<packaging>jar</packaging>
|
||||
|
||||
<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>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
|
|
@ -30,6 +30,15 @@ spring:
|
|||
connection-test-query: SELECT 1
|
||||
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:
|
||||
# mapper-locations: classpath:/mybatis/mapper/*.xml
|
||||
|
@ -39,4 +48,5 @@ spring:
|
|||
logging:
|
||||
level:
|
||||
root: info
|
||||
config: classpath:logback-spring.xml
|
||||
config: classpath:logback-spring.xml
|
||||
|
||||
|
|
|
@ -1,19 +1,81 @@
|
|||
package com.touka.test;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@Slf4j
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApiTest {
|
||||
|
||||
@Resource
|
||||
private DeepSeekChatModel chatModel;
|
||||
|
||||
/**
|
||||
* 测试同步生成响应
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
log.info("测试完成");
|
||||
public void testGenerate() {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue