当前位置: 首页 > news >正文

LangChain4j快速入门5(会话功能_会话记忆)

一.大模型记忆机制
大模型本身不具备记忆能力,要让它记住之前的聊天内容,必须把历史对话内容和新提示词一起发送给模型。

image

 

image

 

image

 

image

 相关代码

1.

package com.vivo.config;import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class CommonConfig {@Autowiredprivate OpenAiChatModel model;//    @Bean
//    public ConsultantService consultantService() {
//        return AiServices.builder(ConsultantService.class)
//                .chatModel(model)
//                .build();
//    }
@Beanpublic ChatMemory chatMemory() {MessageWindowChatMemory memory = MessageWindowChatMemory.builder().maxMessages(20).build();return memory;}@Beanpublic ChatMemoryProvider chatMemoryProvider() {ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider() {@Overridepublic ChatMemory get(Object memoryId) {return MessageWindowChatMemory.builder().id(memoryId).maxMessages(20).build();}};return chatMemoryProvider;}
}

 

2.接口

package com.vivo.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;@AiService(wiringMode = AiServiceWiringMode.EXPLICIT, // 手动装配chatModel = "openAiChatModel", // 指定模型streamingChatModel = "openAiStreamingChatModel",// chatMemory = "chatMemory" // 配置会话记忆对象chatMemoryProvider = "chatMemoryProvider" // 配置会话记忆提供者对象
)
public interface ConsultantService {// 方式1:直接在注解中写系统提示词@SystemMessage("你是毅哥的助手小甜甜,人美心善。")// 方式2:从资源文件中加载系统提示词//@SystemMessage("system.txt")// 方式3:使用UserMessage注解并占位符//@UserMessage("你是毅哥的助手小甜甜,人美心善。{{it}}")// 方式4:使用UserMessage注解+自定义占位符名称//@UserMessage("你是毅哥的助手小甜甜,人美心善。{{msg}}")public Flux<String> chat(/*@V("msg")*/ @MemoryId String memoryId, @UserMessage String message);
}

 

3.controller

package com.vivo.controller;import com.vivo.aiservice.ConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;@RestController
public class ChatController {@Autowiredprivate ConsultantService consultantService;@RequestMapping(value = "/chat", produces = "text/html;charset=utf-8")public Flux<String> chat(String memoryId, String message) {Flux<String> chat = consultantService.chat(memoryId, message);return chat;}
}

 

http://www.jsqmd.com/news/1322959/

相关文章:

  • Cap开源录屏工具:3分钟学会专业屏幕录制,免费替代Loom的终极选择
  • 2026甄选:管理咨询公司战略规划、数字化转型与组织变革服务机构深度解析 - 优企名品
  • 为什么选择easygo?5大优势让你的Go项目开发事半功倍
  • Qwen 3.7 Max、Kimi K3、DeepSeek V4 实测评估:34倍价差下的性能差异分析
  • 从CUDA到CANN:PyTorch项目迁移至昇腾平台的规则模式与实践指南
  • 为什么选择SlopeCraft?Minecraft地图画工具对比评测
  • 程序员副业实战指南:从技术变现到产品构建的完整路径
  • 后台网优工程师高效工作流:从数据处理到团队协作的软件工具链实战
  • 【图像分割】基于人工蜂群算法实现图像分割matlab代码
  • 死锁原理与解决方案:从多线程到分布式系统的并发难题
  • AI智能体重塑计算化学:自动化工作流与自主决策实践
  • Citizens2与其他插件联动:Essentials、WorldGuard整合教程
  • WebMCP协议解析:浏览器自动化革命与前端安全新挑战
  • Seraphine英雄联盟助手:免费战绩查询与智能BP辅助完整指南
  • 重大突破!HighReport 原生单元格填充,攻克中国式复杂报表制式排版难题
  • 德宏MA甲醛检测公司公共卫生检测如何选:国康CMA检测标准、流程、避坑指南 - 信誉隆金银铂奢回收
  • 2026年LLM API服务商四大类型解析与价格对比指南
  • Minecraft地图画神器SlopeCraft:从入门到精通的终极教程
  • 为什么选择SZTextView?揭秘这款占位符控件的5大优势
  • Kotlin vs Java:Stepper-Touch在两种语言中的实现对比
  • Python Minifier入门教程:从安装到第一个代码压缩示例
  • 如何使用StyleGAN3-Editing进行人脸编辑?从入门到精通的完整教程
  • web-RABC-Permissions-sdk实战教程:3个案例掌握按钮级权限控制
  • 2026泉州防水补漏全攻略|卫生间漏水免砸砖维修 阳台渗水补漏 外墙飘窗漏水修复 屋顶防水翻新 地下室堵漏 正规防水公司推荐 - 房屋-修缮
  • 2026年成都服务自动驾驶行业的媒体发稿渠道大全、正规合规服务商多维度实力盘点,附选商避坑指南与常见FAQ - U渠道
  • 如何在5分钟内为你的网站添加GitHub贡献日历:完整实现指南
  • 为什么选择Rust Cucumber?原生测试框架的5大优势与使用场景
  • SZTextView高级技巧:如何使用富文本占位符打造惊艳UI效果
  • SAP MIRO发票校验抬头文本下传会计凭证行项目增强实现
  • 明日方舟基建自动化终极指南:Arknights-Mower让您每天节省30分钟