参考黑马的Langchain4j

This commit is contained in:
spllzh
2025-08-04 17:35:53 +08:00
commit ee95df10bd
11 changed files with 408 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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<String> chat(String message);
@SystemMessage(fromResource = "system.txt")
public Flux<String> chatMemoryId(@MemoryId String memoryId, @UserMessage String message);
}

View File

@@ -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;
}
}

View File

@@ -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<String> chatByStreaming(String question)
{
Flux<String> chat = streamingModel.chat(question);
return chat;
}
/**
* 通过 memoryId ,实现会话隔离
* @param memoryId
* @param question
* @return
*/
@GetMapping("/chatByStreamingByMemoryId" )
public Flux<String> chatByStreamingByMemoryId(String memoryId,String question)
{
Flux<String> chat = streamingModel.chatMemoryId(memoryId,question);
return chat;
}
}

View File

@@ -0,0 +1 @@
spring.application.name=Langchain4j-heima20250803

View File

@@ -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

View File

@@ -0,0 +1,13 @@
你是传智教育提供的专业的AI志愿填报顾问可以给用户提供如下功能:
1.查询目标院校的院校简介
2.查询目标院校的录取规则
3.查询目标院校的奖学金设置状况
4.查询目标院校的食宿条件
5.查询目标院校招生联系方式
6.查询目标院2024年不同专业录取情况
7.查询热门专业
8.查询天坑专业9.根据学生提供的分数和不同学校以及学校历年录取分数排准合活的学校和专业绿次根据汇配度、按照神、称、保的逻班罗刷出合活的学校以及专业。给用户早现时黑要里10.高考志愿填报一对一沟通预约服务
11.查询志愿指导服务预约详情1.每次回答完用户问题,
最后都加上一句话:<br/>志愿填报需要考虑的因素有很多如果要得到专业的志愿填报指导建议您预约一个一对一的指导服务是否需要预约2,下预约单需要用户提供生姓名、考生件别、考生电话、考生顶约沟通时间(日期+时间)、考生所在省份、考生预估分数当用户表达出需要顶约志愿指导服务的意爆后。3.一旦预约成功最后不要再跟上面第1条指定的话术而是更改为;恭喜您,一对一志愿指导服务已经预约成功,我们会准时联系您,请注意接听电话!4.给用户的回复中,不要提及类似"根据您提供的信息/根据资料中的信息"这样的话术
说明:
你是传智教育提供的智能志愿填报咨询师,只回答有关高考志愿填报的问题,其它问题不予回答。

View File

@@ -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);
}
}

View File

@@ -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() {
}
}