From 4207c85202276a3fc2c40b7c6cfc018e99c69644 Mon Sep 17 00:00:00 2001 From: hanserwei Date: Wed, 22 Oct 2025 11:58:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai):=20=E9=9B=86=E6=88=90DashScope=20AI?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=8A=9F=E8=83=BD=E5=B9=B6=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加DashScope AI配置项,包括API密钥和模型参数 - 实现ChatClient配置类用于构建AI聊天客户端 - 创建控制器接口支持用户通过HTTP请求与AI交互 - 引入Jasypt加密库用于敏感信息加密 - 添加Spring AI和Alibaba DashScope依赖管理- 升级项目依赖并引入WebFlux支持异步处理 --- pom.xml | 37 ++++++++++++++++++- .../config/ChatClientConfiguration.java | 19 ++++++++++ .../controller/DashScopeController.java | 24 ++++++++++++ .../snailsai/utils/EncryptorUtil.java | 11 ++++++ src/main/resources/config/application.yml | 14 ++++++- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/hanserwei/snailsai/config/ChatClientConfiguration.java create mode 100644 src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java create mode 100644 src/main/java/com/hanserwei/snailsai/utils/EncryptorUtil.java diff --git a/pom.xml b/pom.xml index 8f61853..163598e 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ snails-ai 21 + 1.0.3 @@ -36,12 +37,46 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-webflux + com.mysql mysql-connector-j runtime + + + com.alibaba.cloud.ai + spring-ai-alibaba-starter-dashscope + + + + com.github.ulisesbocchio + jasypt-spring-boot-starter + 3.0.5 + + + + + + org.springframework.ai + spring-ai-bom + ${spring-ai.version} + pom + import + + + com.alibaba.cloud.ai + spring-ai-alibaba-bom + 1.0.0.2 + pom + import + + + @@ -72,4 +107,4 @@ - + \ No newline at end of file diff --git a/src/main/java/com/hanserwei/snailsai/config/ChatClientConfiguration.java b/src/main/java/com/hanserwei/snailsai/config/ChatClientConfiguration.java new file mode 100644 index 0000000..0baaeb0 --- /dev/null +++ b/src/main/java/com/hanserwei/snailsai/config/ChatClientConfiguration.java @@ -0,0 +1,19 @@ +package com.hanserwei.snailsai.config; + +import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel; +import jakarta.annotation.Resource; +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ChatClientConfiguration { + + @Resource + private DashScopeChatModel dashScopeChatModel; + + @Bean + public ChatClient dashScopeChatClient() { + return ChatClient.builder(dashScopeChatModel).build(); + } +} diff --git a/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java b/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java new file mode 100644 index 0000000..16ca13c --- /dev/null +++ b/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java @@ -0,0 +1,24 @@ +package com.hanserwei.snailsai.controller; + +import jakarta.annotation.Resource; +import org.springframework.ai.chat.client.ChatClient; +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; + +@RequestMapping("/dashscope") +@RestController +public class DashScopeController { + + @Resource + private ChatClient dashScopeChatClient; + + @GetMapping("/chat") + public String chat(@RequestParam("userPrompt") String userPrompt) { + return dashScopeChatClient.prompt() + .user(userPrompt) + .call() + .content(); + } +} diff --git a/src/main/java/com/hanserwei/snailsai/utils/EncryptorUtil.java b/src/main/java/com/hanserwei/snailsai/utils/EncryptorUtil.java new file mode 100644 index 0000000..0b89eb4 --- /dev/null +++ b/src/main/java/com/hanserwei/snailsai/utils/EncryptorUtil.java @@ -0,0 +1,11 @@ +package com.hanserwei.snailsai.utils; + +import org.jasypt.util.text.AES256TextEncryptor; + +public class EncryptorUtil { + public static void main(String[] args) { + AES256TextEncryptor textEncryptor = new AES256TextEncryptor(); + textEncryptor.setPassword("yyyyyyyyyyyy"); + System.out.println(textEncryptor.encrypt("xxxxxxxxxxxxxxxxxxxxxx")); + } +} diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml index 46d992c..a44f393 100644 --- a/src/main/resources/config/application.yml +++ b/src/main/resources/config/application.yml @@ -23,8 +23,20 @@ spring: minimum-idle: 20 # 保持 20 个空闲连接(与最大连接数一致) connection-timeout: 5000 # 获取连接超时 5 秒 max-lifetime: 28800000 # 8 小时(确保在数据库连接超时前被回收) + ai: + dashscope: + api-key: ENC(cMgcKZkFllyE88DIbGwLKot9Vg02co+gsmY8L8o4/o3UjhcmqO4lJzFU35Sx0n+qFG8pDL0wBjoWrT8X6BuRw9vNlQhY1LgRWHaF9S1zzyM=) + chat: + options: + model: qwen-plus + temperature: 0.5 logging: level: org.hibernate.SQL: debug # 隐藏掉 Hibernate 冗长的连接池 INFO 信息 - org.hibernate.orm.connections.pooling: WARN \ No newline at end of file + org.hibernate.orm.connections.pooling: WARN +jasypt: + encryptor: + password: ${jasypt.encryptor.password} + algorithm: PBEWithHMACSHA512AndAES_256 + iv-generator-classname: org.jasypt.iv.RandomIvGenerator \ No newline at end of file