参考黑马的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;
}
}