20250329 记忆会话

This commit is contained in:
liangjinglin 2025-03-29 22:18:18 +08:00
parent f321c1c3e7
commit 1aabf305a4
5 changed files with 65 additions and 5 deletions

View File

@ -0,0 +1,27 @@
package com.ai.config;
import com.ai.service.Assist;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class AssistantInit {
ChatLanguageModel qwenModel = QwenChatModel.builder()
.apiKey("sk-2f703a41fff0488e9b6888013d2ee58a")
.modelName("deepseek-v3")
.build();
@Bean
public Assist init() {
return AiServices.builder(Assist.class)
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
.chatLanguageModel(qwenModel).build();
}
}

View File

@ -2,6 +2,11 @@ package com.ai.controller;
import com.ai.service.Assist;
import com.ai.service.LangChainService;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -34,4 +39,24 @@ public class LangChainController {
System.out.println("start highlevel chat...");
return assist.chat(input);
}
// ChatLanguageModel qwenModel = QwenChatModel.builder()
// .apiKey("sk-2f703a41fff0488e9b6888013d2ee58a")
// .modelName("deepseek-v3")
// .build();
//
// ChatMemory chatMemory = MessageWindowChatMemory.builder()
// .maxMessages(10)
// .build();
//
// Assist assist = AiServices.builder(Assist.class)
// .chatLanguageModel(qwenModel) // the model
// .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10)) // memory
// .build();
@GetMapping("/high/memory-chat")
public String memoryChat(@RequestParam("memoryId") String memoryId, @RequestParam("input") String input) {
System.out.println("start highlevel memory chat...");
return assist.memoryChat(memoryId, input);
}
}

View File

@ -3,3 +3,10 @@ GET http://localhost:8080/langchain/chat?input=今天天气如何
### 测试 LangChainController 的 highlevel chat 接口
GET http://localhost:8080/langchain/high/chat?input=评价一下路飞
### 测试 LangChainController 的 highlevel memory chat 接口
GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=你好,我想要买电脑笔记本
### 测试 LangChainController 的 highlevel memory chat 接口
GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=我想要性价比高的
### 测试 LangChainController 的 highlevel memory chat 接口
GET http://localhost:8080/langchain/high/memory-chat?memoryId=1&input=详细介绍一下

View File

@ -1,11 +1,15 @@
package com.ai.service;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.spring.AiService;
@AiService
public interface Assist {
@SystemMessage("假设你是唐吉坷德多佛朗明哥")
@SystemMessage("假设你是多佛朗明哥,请你用多佛朗明哥的语气去回答问题")
String chat(String userMessage);
@SystemMessage("假设你是电商平台的客服,请你用电商平台客服的语气去回答问题")
String memoryChat(@MemoryId String memoryId, @UserMessage String userMessage);
}

View File

@ -1,8 +1,5 @@
package com.ai.service;
import dev.langchain4j.community.model.dashscope.QwenLanguageModel;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.language.LanguageModel;
import dev.langchain4j.model.output.Response;
import org.springframework.beans.factory.annotation.Autowired;