Compare commits
2 Commits
0deb3301ed
...
62cf0ed548
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62cf0ed548 | ||
|
|
5158a9bcb3 |
45
pom.xml
45
pom.xml
@@ -6,28 +6,16 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>3.5.6</version>
|
<version>3.5.6</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.hanserwei</groupId>
|
<groupId>com.hanserwei</groupId>
|
||||||
<artifactId>ai-robot</artifactId>
|
<artifactId>ai-robot</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>han-ai-robot-springboot</name>
|
<name>han-ai-robot-springboot</name>
|
||||||
<description>han-ai-robot-springboot</description>
|
<description>han-ai-robot-springboot</description>
|
||||||
<url/>
|
|
||||||
<licenses>
|
|
||||||
<license/>
|
|
||||||
</licenses>
|
|
||||||
<developers>
|
|
||||||
<developer/>
|
|
||||||
</developers>
|
|
||||||
<scm>
|
|
||||||
<connection/>
|
|
||||||
<developerConnection/>
|
|
||||||
<tag/>
|
|
||||||
<url/>
|
|
||||||
</scm>
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>21</java.version>
|
<java.version>21</java.version>
|
||||||
|
<spring-ai.version>1.0.3</spring-ai.version>
|
||||||
|
<commons-lang3.version>3.19.0</commons-lang3.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -35,12 +23,41 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ulisesbocchio</groupId>
|
||||||
|
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||||
|
<version>3.0.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Deepseek 模型 -->
|
||||||
|
<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-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</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>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.hanserwei.airobot.controller;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
|
import org.springframework.ai.deepseek.DeepSeekChatModel;
|
||||||
|
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 reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/ai")
|
||||||
|
public class DeepSeekChatController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeepSeekChatModel chatModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 普通对话
|
||||||
|
* @param message 对话输入内容
|
||||||
|
* @return 对话结果
|
||||||
|
*/
|
||||||
|
@GetMapping("/generate")
|
||||||
|
public String generate(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||||
|
// 一次性返回结果
|
||||||
|
return chatModel.call(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流式对话
|
||||||
|
* @param message 对话输入内容
|
||||||
|
* @return 对话结果
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/generateStream", produces = "text/html;charset=utf-8")
|
||||||
|
public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||||
|
// 构建提示词
|
||||||
|
Prompt prompt = new Prompt(new UserMessage(message));
|
||||||
|
|
||||||
|
// 流式输出
|
||||||
|
return chatModel.stream(prompt)
|
||||||
|
.mapNotNull(chatResponse -> chatResponse.getResult().getOutput().getText());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.hanserwei.airobot.controller;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
|
import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
|
||||||
|
import org.springframework.ai.deepseek.DeepSeekChatModel;
|
||||||
|
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 reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/v1/ai")
|
||||||
|
public class DeepSeekR1ChatController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeepSeekChatModel chatModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流式对话
|
||||||
|
* @param message 对话输入内容
|
||||||
|
* @return 对话结果
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/generateStream", produces = "text/html;charset=utf-8")
|
||||||
|
public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||||
|
// 构建提示词
|
||||||
|
Prompt prompt = new Prompt(new UserMessage(message));
|
||||||
|
|
||||||
|
// 流式输出
|
||||||
|
return chatModel.stream(prompt)
|
||||||
|
.mapNotNull(chatResponse -> {
|
||||||
|
// 获取响应内容
|
||||||
|
DeepSeekAssistantMessage deepSeekAssistantMessage = (DeepSeekAssistantMessage) chatResponse.getResult().getOutput();
|
||||||
|
// 推理内容
|
||||||
|
String reasoningContent = deepSeekAssistantMessage.getReasoningContent();
|
||||||
|
// 推理结束后的正式回答
|
||||||
|
String text = deepSeekAssistantMessage.getText();
|
||||||
|
|
||||||
|
// 若推理内容有值,则响应推理内容,否则,说明推理结束了,响应正式回答
|
||||||
|
return StringUtils.isNotBlank(reasoningContent) ? reasoningContent : text;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/main/java/com/hanserwei/airobot/utils/EncryptorUtil.java
Normal file
11
src/main/java/com/hanserwei/airobot/utils/EncryptorUtil.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.hanserwei.airobot.utils;
|
||||||
|
|
||||||
|
import org.jasypt.util.text.AES256TextEncryptor;
|
||||||
|
|
||||||
|
public class EncryptorUtil {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
|
||||||
|
textEncryptor.setPassword("password");
|
||||||
|
System.out.println(textEncryptor.encrypt("sk-xxxxxxxxxxxxxx"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,17 @@
|
|||||||
|
#file: noinspection SpringBootConfigYamlInspection
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: han-ai-robot-springboot
|
name: han-ai-robot-springboot
|
||||||
|
ai:
|
||||||
|
deepseek:
|
||||||
|
api-key: ENC(MROXdiEHmWk08koE63bTzFqW52MaXLpMkM9Cyl40Ubj+Lw1yKeZuHLEcs6jTFY8ditY4gJ1365LMAY8Z9G1uwfYFYaYdb3NyijplX7GuDZA=) # 填写 DeepSeek Api Key, 改成你自己的
|
||||||
|
base-url: https://api.deepseek.com # DeepSeek 的请求 URL, 可不填,默认值为 api.deepseek.com
|
||||||
|
chat:
|
||||||
|
options:
|
||||||
|
model: deepseek-reasoner # 使用哪个模型
|
||||||
|
temperature: 0.8 # 温度值
|
||||||
|
jasypt:
|
||||||
|
encryptor:
|
||||||
|
password: ${jasypt.encryptor.password}
|
||||||
|
algorithm: PBEWithHMACSHA512AndAES_256
|
||||||
|
iv-generator-classname: org.jasypt.iv.RandomIvGenerator
|
||||||
Reference in New Issue
Block a user