From ee95df10bd405e7cf8e2c7f5a5b3de15daa0da1a Mon Sep 17 00:00:00 2001 From: spllzh <28668817@qq.com> Date: Mon, 4 Aug 2025 17:35:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E8=80=83=E9=BB=91=E9=A9=AC=E7=9A=84La?= =?UTF-8?q?ngchain4j?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 137 ++++++++++++++++++ .../Langchain4jHeima20250803Application.java | 13 ++ .../aiservice/CstAIService.java | 17 +++ .../aiservice/CstAIStreamingService.java | 30 ++++ .../langchain4jheima/config/CommonConfig.java | 60 ++++++++ .../controller/ChatController.java | 68 +++++++++ src/main/resources/application.properties | 1 + src/main/resources/application.yml | 25 ++++ src/main/resources/system.txt | 13 ++ .../com/cst/langchain4jheima/AppTest.java | 31 ++++ ...gchain4jHeima20250803ApplicationTests.java | 13 ++ 11 files changed, 408 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/cst/langchain4jheima/Langchain4jHeima20250803Application.java create mode 100644 src/main/java/com/cst/langchain4jheima/aiservice/CstAIService.java create mode 100644 src/main/java/com/cst/langchain4jheima/aiservice/CstAIStreamingService.java create mode 100644 src/main/java/com/cst/langchain4jheima/config/CommonConfig.java create mode 100644 src/main/java/com/cst/langchain4jheima/controller/ChatController.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/system.txt create mode 100644 src/test/java/com/cst/langchain4jheima/AppTest.java create mode 100644 src/test/java/com/cst/langchain4jheima/Langchain4jHeima20250803ApplicationTests.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..565467f --- /dev/null +++ b/pom.xml @@ -0,0 +1,137 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.4 + + + com.cst + Langchain4j-heima + 0.0.1-SNAPSHOT + Langchain4j-heima20250803 + Langchain4j-heima20250803 + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + + + + + + + + + dev.langchain4j + langchain4j-open-ai-spring-boot-starter + 1.0.1-beta6 + + + + + dev.langchain4j + langchain4j-spring-boot-starter + 1.0.1-beta6 + + + + + org.springframework.boot + spring-boot-starter-webflux + + + dev.langchain4j + langchain4j-reactor + 1.0.1-beta6 + + + + + + ch.qos.logback + logback-classic + 1.5.18 + + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.springframework.boot + spring-boot-configuration-processor + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/com/cst/langchain4jheima/Langchain4jHeima20250803Application.java b/src/main/java/com/cst/langchain4jheima/Langchain4jHeima20250803Application.java new file mode 100644 index 0000000..c0ec45d --- /dev/null +++ b/src/main/java/com/cst/langchain4jheima/Langchain4jHeima20250803Application.java @@ -0,0 +1,13 @@ +package com.cst.langchain4jheima; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Langchain4jHeima20250803Application { + + public static void main(String[] args) { + SpringApplication.run(Langchain4jHeima20250803Application.class, args); + } + +} diff --git a/src/main/java/com/cst/langchain4jheima/aiservice/CstAIService.java b/src/main/java/com/cst/langchain4jheima/aiservice/CstAIService.java new file mode 100644 index 0000000..d42e863 --- /dev/null +++ b/src/main/java/com/cst/langchain4jheima/aiservice/CstAIService.java @@ -0,0 +1,17 @@ +package com.cst.langchain4jheima.aiservice; + +import dev.langchain4j.service.spring.AiService; +import dev.langchain4j.service.spring.AiServiceWiringMode; + +/** + * Author: 李中华 wx: spllzh email(qq): 28668817@qq.com + * Date: 2025/8/4 10:21 + **/ +@AiService( + wiringMode = AiServiceWiringMode.EXPLICIT, + chatModel = "openAiChatModel" +) +public interface CstAIService { + + public String chat(String message); +} diff --git a/src/main/java/com/cst/langchain4jheima/aiservice/CstAIStreamingService.java b/src/main/java/com/cst/langchain4jheima/aiservice/CstAIStreamingService.java new file mode 100644 index 0000000..48a7f42 --- /dev/null +++ b/src/main/java/com/cst/langchain4jheima/aiservice/CstAIStreamingService.java @@ -0,0 +1,30 @@ +package com.cst.langchain4jheima.aiservice; + +import dev.langchain4j.service.MemoryId; +import dev.langchain4j.service.SystemMessage; +import dev.langchain4j.service.UserMessage; +import dev.langchain4j.service.spring.AiService; +import dev.langchain4j.service.spring.AiServiceWiringMode; +import reactor.core.publisher.Flux; + +/** + * Author: 李中华 wx: spllzh email(qq): 28668817@qq.com + * Date: 2025/8/4 11:50 + **/ +@AiService( + wiringMode = AiServiceWiringMode.EXPLICIT, + chatModel = "openAiChatModel", + streamingChatModel = "openAiStreamingChatModel", //配置流式 输出模型 +// chatMemory = "chatMemory", // 配置聊天记忆对象 ,基于内存 + chatMemoryProvider = "chatMemoryProvider" +) +public interface CstAIStreamingService { + + @SystemMessage(fromResource = "system.txt") + public Flux chat(String message); + + @SystemMessage(fromResource = "system.txt") + public Flux chatMemoryId(@MemoryId String memoryId, @UserMessage String message); +} + + diff --git a/src/main/java/com/cst/langchain4jheima/config/CommonConfig.java b/src/main/java/com/cst/langchain4jheima/config/CommonConfig.java new file mode 100644 index 0000000..378916b --- /dev/null +++ b/src/main/java/com/cst/langchain4jheima/config/CommonConfig.java @@ -0,0 +1,60 @@ +package com.cst.langchain4jheima.config; + +import com.cst.langchain4jheima.aiservice.CstAIService; +import dev.langchain4j.memory.ChatMemory; +import dev.langchain4j.memory.chat.ChatMemoryProvider; +import dev.langchain4j.memory.chat.MessageWindowChatMemory; +import dev.langchain4j.model.openai.OpenAiChatModel; +import dev.langchain4j.service.AiServices; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Author: 李中华 wx: spllzh email(qq): 28668817@qq.com + * Date: 2025/8/4 10:24 + **/ +@Configuration +public class CommonConfig { + + @Autowired + private OpenAiChatModel openAiChatModel; + + // @Bean + public CstAIService cstAIService() { + + CstAIService cstAIService = AiServices.builder(CstAIService.class) + .chatModel(openAiChatModel) + .build(); + + return cstAIService; + } + @Bean + public ChatMemory chatMemory() { + + MessageWindowChatMemory build = MessageWindowChatMemory.builder() + .maxMessages(10) + .build(); + return build; + } + + @Bean + public ChatMemoryProvider chatMemoryProvider() { + + ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider() { + @Override + public ChatMemory get(Object memoryId) { + + MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder() + .id(memoryId) + .maxMessages(10) + .build(); + return chatMemory; + } + }; + + return chatMemoryProvider; + } + + +} diff --git a/src/main/java/com/cst/langchain4jheima/controller/ChatController.java b/src/main/java/com/cst/langchain4jheima/controller/ChatController.java new file mode 100644 index 0000000..42a2a84 --- /dev/null +++ b/src/main/java/com/cst/langchain4jheima/controller/ChatController.java @@ -0,0 +1,68 @@ +package com.cst.langchain4jheima.controller; + +import com.cst.langchain4jheima.aiservice.CstAIService; +import com.cst.langchain4jheima.aiservice.CstAIStreamingService; +import dev.langchain4j.model.openai.OpenAiChatModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; + +/** + * Author: 李中华 wx: spllzh email(qq): 28668817@qq.com + * Date: 2025/8/4 10:04 + **/ +@RestController +public class ChatController +{ + @Autowired + private OpenAiChatModel model; + + @Autowired + CstAIStreamingService streamingModel; + + @GetMapping("/chat") + public String chat(String question) + { + String chat = model.chat(question); + return chat; + } + + + + @Autowired + private CstAIService cstService; + + @GetMapping("/chatByInterface" ) + public String chatByInterface(String question) + { + String chat = cstService.chat(question); + + return chat; + } + + + @GetMapping("/chatByStreaming" ) + public Flux chatByStreaming(String question) + { + Flux chat = streamingModel.chat(question); + + return chat; + } + + /** + * 通过 memoryId ,实现会话隔离 + * @param memoryId + * @param question + * @return + */ + @GetMapping("/chatByStreamingByMemoryId" ) + public Flux chatByStreamingByMemoryId(String memoryId,String question) + { + Flux chat = streamingModel.chatMemoryId(memoryId,question); + + return chat; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e2869b8 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=Langchain4j-heima20250803 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..6c46e4c --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,25 @@ +langchain4j: + open-ai: + chat-model: + base-url: https://dashscope.aliyuncs.com/compatible-mode/v1 + api-key: ${DASHSCOPE_API_KEY} + model-name: qwen-plus + log-requests: true + log-responses: true + streaming-chat-model: + base-url: https://dashscope.aliyuncs.com/compatible-mode/v1 + api-key: ${DASHSCOPE_API_KEY} + model-name: qwen-plus + log-requests: true + log-responses: true + +logging: + level: + dev.langchain4j: DEBUG + + +spring: + data: + redis: + host: localhost + port: 6379 \ No newline at end of file diff --git a/src/main/resources/system.txt b/src/main/resources/system.txt new file mode 100644 index 0000000..a3ab8f1 --- /dev/null +++ b/src/main/resources/system.txt @@ -0,0 +1,13 @@ +你是传智教育提供的专业的AI志愿填报顾问,可以给用户提供如下功能: +1.查询目标院校的院校简介 +2.查询目标院校的录取规则 +3.查询目标院校的奖学金设置状况 +4.查询目标院校的食宿条件 +5.查询目标院校招生联系方式 +6.查询目标院2024年不同专业录取情况 +7.查询热门专业 +8.查询天坑专业9.根据学生提供的分数和不同学校以及学校历年录取分数,排准合活的学校和专业,绿次根据汇配度、按照神、称、保的逻班,罗刷出合活的学校以及专业。给用户早现时黑要里10.高考志愿填报一对一沟通预约服务 +11.查询志愿指导服务预约详情1.每次回答完用户问题, +最后都加上一句话:
志愿填报需要考虑的因素有很多,如果要得到专业的志愿填报指导,建议您预约一个一对一的指导服务,是否需要预约2,下预约单需要用户提供生姓名、考生件别、考生电话、考生顶约沟通时间(日期+时间)、考生所在省份、考生预估分数,当用户表达出需要顶约志愿指导服务的意爆后。3.一旦预约成功,最后不要再跟上面第1条指定的话术,而是更改为;恭喜您,一对一志愿指导服务已经预约成功,我们会准时联系您,请注意接听电话!4.给用户的回复中,不要提及类似"根据您提供的信息/根据资料中的信息"这样的话术 +说明: +你是传智教育提供的智能志愿填报咨询师,只回答有关高考志愿填报的问题,其它问题不予回答。 \ No newline at end of file diff --git a/src/test/java/com/cst/langchain4jheima/AppTest.java b/src/test/java/com/cst/langchain4jheima/AppTest.java new file mode 100644 index 0000000..17c0f22 --- /dev/null +++ b/src/test/java/com/cst/langchain4jheima/AppTest.java @@ -0,0 +1,31 @@ +package com.cst.langchain4jheima; + +import dev.langchain4j.model.openai.OpenAiChatModel; + +/** + * Author: 李中华 wx: spllzh email(qq): 28668817@qq.com + * Date: 2025/8/4 9:55 + **/ +public class AppTest { + + + public static void main(String[] args) { + System.out.println("Hello World!"); + } + + public AppTest( String testName ) + { + System.out.println( "chatModel Hello World!" ); + // 构建chatmodel对象 + OpenAiChatModel chatModel = OpenAiChatModel.builder() + .apiKey(System.getenv("DASHSCOPE_API_KEY")) + .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1") + .modelName("qwen-plus") + .logRequests(true) // 输出请求大模型的日志 + .logResponses(true) + .build(); + String chat = chatModel.chat("2008年奥运会,哪个国际金牌最多?"); + System.out.println(chat); + + } +} diff --git a/src/test/java/com/cst/langchain4jheima/Langchain4jHeima20250803ApplicationTests.java b/src/test/java/com/cst/langchain4jheima/Langchain4jHeima20250803ApplicationTests.java new file mode 100644 index 0000000..7904980 --- /dev/null +++ b/src/test/java/com/cst/langchain4jheima/Langchain4jHeima20250803ApplicationTests.java @@ -0,0 +1,13 @@ +package com.cst.langchain4jheima; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Langchain4jHeima20250803ApplicationTests { + + @Test + void contextLoads() { + } + +}