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

做微商网站的软文石家庄网站服务

做微商网站的软文,石家庄网站服务,wordpress 文件下载插件,不适合学编程的人Chat Memory介绍 大型语言模型(LLM)是无状态的,这意味着它们不保留关于以前互动的信息。为了解决这个问题,Spring AI提供了Chat Memory(聊天记忆)功能。通过Chat Memory,用户可以实现在与LLM的…

Chat Memory介绍

大型语言模型(LLM)是无状态的,这意味着它们不保留关于以前互动的信息。为了解决这个问题,Spring AI提供了Chat Memory(聊天记忆)功能。通过Chat Memory,用户可以实现在与LLM的多次交互中存储和检索信息。

聊天记忆的底层存储由ChatMemoryRepository处理,其唯一责任是存储和检索消息。决定保留哪些消息及何时删除这些消息的权利在于ChatMemory的实现。策略可能包括保留最近的N条消息,保留一定时间段的消息,或者保留指定最大令牌数的消息。

ChatMemory当前有一种实现:MessageWindowChatMemory。MessageWindowChatMemory 维护一个最多可达到指定最大大小(默认:20 条消息)的消息窗口。当消息数量超过此限制时,旧消息会被驱逐,但系统消息会被保留。如果添加了一条新的系统消息,则会从聊天记忆中删除所有以前的系统消息。这确保了最新的上下文始终可用于对话,同时保持聊天记忆使用在可控范围内。

ChatModel对象使用Chat Memory

不使用Chat Memory的现象

    @GetMapping("/chat")public String chat() {String answer = this.chatModel.call("你好,我是老任与码");System.out.println(answer);String answer2 = this.chatModel.call("我是谁");System.out.println(answer2);return "success";}

输出结果:

根据输出结果可以看到,第一次提问时,虽然已经告知大模型自己的名字,但是第二次提问时,大模型并不能回答出正确答案。这就说明本例中,两次提问是相互独立的,大模型是无状态的。

使用Chat Memory

不需要任何配置,即可直接注入ChatMemory对象:

@RestController
@RequestMapping("/memory")
public class MemoryController {@Resourceprivate ZhiPuAiChatModel chatModel;@Resourceprivate ChatClient chatClient;// 可以直接注入,也可以自定义创建@Resourceprivate ChatMemory chatMemory;......}

也可以在配置类中创建ChatMemory对象后,再注入:

    @Beanpublic ChatMemory chatMemory() {MessageWindowChatMemory memory = MessageWindowChatMemory.builder().maxMessages(10).chatMemoryRepository(new InMemoryChatMemoryRepository()).build();return memory;}

 本例根据MessageWindowChatMemory创建ChatMemory对象,通过maxMessage(10)方法指定存储的最大的消息条数是10条,通过chatMemoryRepository(new InMemoryChatMemoryRepository())方法,表示聊天的上下文消息存储在内存中。

测试代码:

    @GetMapping("/chat3")public String chat3(String conversationId) {UserMessage userMessage1 = new UserMessage("你好,我是老任与码" + conversationId);// 将消息添加到ChatMemory中// conversationId表示和大模型的会话id,一般可以使用用户id表示,用于区分不同的用户的聊天上下文信息chatMemory.add(conversationId, userMessage1);// 提问ChatResponse response1 = this.chatModel.call(new Prompt(chatMemory.get(conversationId)));System.out.println("answer1:" + response1.getResult().getOutput().getText());// 将回答也添加到ChatMemory中chatMemory.add(conversationId, response1.getResult().getOutput());// 第二次提问,也需要将消息添加到ChatMemoryUserMessage userMessage2 = new UserMessage("我是谁");chatMemory.add(conversationId, userMessage2);System.out.println(chatMemory.get(conversationId));ChatResponse response2 = this.chatModel.call(new Prompt(chatMemory.get(conversationId)));System.out.println("answer2:" + response2.getResult().getOutput().getText());return "success";}

输出结果:

根据输出结果,大模型根据上下文信息,给出了可能的答案。

通过调试查看ChatMemory对象中的信息:

根据调试可以看到,ChatMemory对象中的聊天上下文数据存储在ConcurrentHashMap对象中,key值表示conversationId,value值是我们聊天过程中的提问和回答的消息。

ChatMemory支持的存储方式

  • InMemoryChatMemoryRepository表示上下文消息存储在内存中
  • JdbcChatMemoryRepository表示使用 JDBC 在关系数据库中存储消息,支持PostgreSQL、MySQL / MariaDB、SQL Server、HSQLDB等数据库
  • CassandraChatMemoryRepository表示使用 Apache Cassandra 分布式数据库存储消息
  • Neo4jChatMemoryRepository表示将聊天消息作为节点和关系存储在 Neo4j 图数据库

ChatClient对象使用Chat Memory

在ChatClient中使用ChatMemory,需要指定对应的Advisor,支持的Advisor包括:

  • MessageChatMemoryAdvisor:此Advisor使用提供的实现类来管理对话记忆。在每次互动中,它从记忆中检索对话历史,并将其作为消息的集合包含在提示中。
  • ChatMemoryPromptChatMemoryAdvisor:此Advisor使用提供的实现来管理对话记忆。在每次互动中,它从记忆中检索对话历史,并将其作为普通文本附加到系统提示(SystemMessage)中。
  • ChatMemoryVectorStoreChatMemoryAdvisor:此Advisor使用提供的实现来管理对话记忆。在每次互动中,它从向量存储中检索对话历史,并将其作为普通文本附加到系统消息中。

本例使用MessageChatMemoryAdvisor:

    @Beanpublic ChatClient chatClient(ZhiPuAiChatModel chatModel) {return ChatClient.builder(chatModel)// MessageChatMemoryAdvisor 聊天记忆的advisor.defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory()).build()).build();}

测试代码:

    @GetMapping("/chat4")public String chat4(String conversationId) {String answer1 = chatClient.prompt().user("我叫老任与码").advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId)).call().content();System.out.println(answer1);String answer2 = chatClient.prompt().user("我叫什么名字").advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId)).call().content();System.out.println(answer2);System.out.println(chatMemory.get(conversationId));return "success";}

上述代码中,必须指定:

advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))

表示根据conversationId区分不同会话的上下文。


文章转载自:

http://hhsb5xwa.sjqpm.cn
http://grb8zfbC.sjqpm.cn
http://Kv4TifMA.sjqpm.cn
http://rOsXIb0B.sjqpm.cn
http://Qhbxm2RA.sjqpm.cn
http://CKTrCSot.sjqpm.cn
http://rxhMYYnz.sjqpm.cn
http://XcoMdxR8.sjqpm.cn
http://x46kFGKo.sjqpm.cn
http://OteA1Zp3.sjqpm.cn
http://L24dQDXR.sjqpm.cn
http://XJf54gIX.sjqpm.cn
http://UNIgB1bL.sjqpm.cn
http://AXTWXNnp.sjqpm.cn
http://1ApzzC4f.sjqpm.cn
http://LgfYKuQG.sjqpm.cn
http://s39uABt8.sjqpm.cn
http://3oCcUGnG.sjqpm.cn
http://GC84R2jK.sjqpm.cn
http://kTl9KvLl.sjqpm.cn
http://5K7l77KA.sjqpm.cn
http://Glw5x2TV.sjqpm.cn
http://y3c5Pzrm.sjqpm.cn
http://1cVKjxPC.sjqpm.cn
http://cP8RyESR.sjqpm.cn
http://5phawO0A.sjqpm.cn
http://d60zZKKw.sjqpm.cn
http://VRonOrLN.sjqpm.cn
http://1ZtBJaDu.sjqpm.cn
http://UPRxcpIv.sjqpm.cn
http://www.dtcms.com/wzjs/689708.html

相关文章:

  • 网站建设 后台免费建立网站平台
  • 网络建设文章网站郑州工作
  • 浙江城乡与住房建设部网站win7 做网站好吗
  • 嘉兴网站开发深圳企业建站招聘
  • 河北网站建设哪里好低价代网站
  • 卖酒网站排名给我一个可以在线观看的免费
  • 长春网站开发senluowx苏州网站建设介绍
  • 建立网站 费用wordpress公众号涨粉
  • 网站提示页面设计个人网站建设月租抵30元
  • 网站 建设 成品秦皇岛 网站制作
  • 企业建立网站的目的网站关键字如何选择
  • 泸州做网站公司邯郸单位网站建设
  • 常德网站建设多少钱摄影网站模板源码
  • 电商网站定制开发破洛洛wordpress
  • 鄞州区网站建设报价中国建设执业资格注册管理中心网站
  • 广州建站哪个济南兴田德润实惠吗中级经济师考试公告
  • 下载类网站 前置备案网站后期维护和管理怎么做
  • 网站设计规划思路阿里云怎么搭载wordpress
  • 山东兴润建设有限公司网站ui网页设计课程
  • mixkitcom素材网站微信公众号小程序怎么创建
  • 广州天河 网站建设淘宝做基础销量怎么网站
  • 上海网站开发建设找哪家网络广告公司排名
  • 建设网站服务手机版网站开发的功能点
  • 工商注册网站网页设计实训报告总结与体会
  • 如何做计算机网站什么是seo
  • 网站首页设计原则网页版
  • 潍坊行业网站西安注册公司流程
  • 免费空间域名可以做淘宝客网站推广吗建筑工程考试题库
  • 杭州网站制作 乐云践新去哪里找需要推广的app
  • 建设学校网站论文网站赚取广告费