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

毕业设计做网站应该学什么百度拍照搜索

毕业设计做网站应该学什么,百度拍照搜索,建设网站需要下载神呢软件吗,网页设计师专业培训最近两年,我们见识了“百模大战”,领略到了大型语言模型(LLM)的风采,但它们也存在一个显著的缺陷:没有记忆。 在对话中,无法记住上下文的 LLM 常常会让用户感到困扰。本文探讨如何利用 LangCha…

最近两年,我们见识了“百模大战”,领略到了大型语言模型(LLM)的风采,但它们也存在一个显著的缺陷:没有记忆。

在对话中,无法记住上下文的 LLM 常常会让用户感到困扰。本文探讨如何利用 LangChain,快速为 LLM 添加记忆能力,提升对话体验。

LangChain 是 LLM 应用开发领域的最大社区和最重要的框架。

1. LLM 固有缺陷,没有记忆

当前的 LLM 非常智能,在理解和生成自然语言方面表现优异,但是有一个显著的缺陷:没有记忆

LLM 的本质是基于统计和概率来生成文本,对于每次请求,它们都将上下文视为独立事件。这意味着当你与 LLM 进行对话时,它不会记住你之前说过的话,这就导致了 LLM 有时表现得不够智能。

这种“无记忆”属性使得 LLM 无法在长期对话中有效跟踪上下文,也无法积累历史信息。比如,当你在聊天过程中提到一个人名,后续再次提及该人时,LLM 可能会忘记你之前的描述。

本着发现问题解决问题的原则,既然没有记忆,那就给 LLM 装上记忆吧。

2. 记忆组件的原理

2.1. 没有记忆的烦恼

当我们与 LLM 聊天时,它们无法记住上下文信息,比如下图的示例:

img

2.2. 原理

如果将已有信息放入到 memory 中,每次跟 LLM 对话时,把已有的信息丢给 LLM,那么 LLM 就能够正确回答,见如下示例:

img

目前业内解决 LLM 记忆问题就是采用了类似上图的方案,即:将每次的对话记录再次丢入到 Prompt 里,这样 LLM 每次对话时,就拥有了之前的历史对话信息。

但如果每次对话,都需要自己手动将本次对话信息继续加入到history信息中,那未免太繁琐。有没有轻松一些的方式呢?有,LangChain!LangChain 对记忆组件做了高度封装,开箱即用。

2.3. 长期记忆和短期记忆

在解决 LLM 的记忆问题时,有两种记忆方案,长期记忆和短期记忆。

  • 短期记忆:基于内存的存储,容量有限,用于存储临时对话内容。
  • 长期记忆:基于硬盘或者外部数据库等方式,容量较大,用于存储需要持久的信息。

3. LangChain 让 LLM 记住上下文

LangChain 提供了灵活的内存组件工具来帮助开发者为 LLM 添加记忆能力。

3.1. 单独用 ConversationBufferMemory 做短期记忆

Langchain 提供了 ConversationBufferMemory 类,可以用来存储和管理对话。

ConversationBufferMemory 包含input变量和output变量,input代表人类输入,output代表 AI 输出。

每次往ConversationBufferMemory组件里存入对话信息时,都会存储到history的变量里。

img

3.2. 利用 MessagesPlaceholder 手动添加 history

python复制代码from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory(return_messages=True)
memory.load_memory_variables({})memory.save_context({"input": "我的名字叫张三"}, {"output": "你好,张三"})
memory.load_memory_variables({})memory.save_context({"input": "我是一名 IT 程序员"}, {"output": "好的,我知道了"})
memory.load_memory_variables({})from langchain.prompts import ChatPromptTemplate
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholderprompt = ChatPromptTemplate.from_messages([("system", "你是一个乐于助人的助手。"),MessagesPlaceholder(variable_name="history"),("human", "{user_input}"),]
)
chain = prompt | modeluser_input = "你知道我的名字吗?"
history = memory.load_memory_variables({})["history"]chain.invoke({"user_input": user_input, "history": history})user_input = "中国最高的山是什么山?"
res = chain.invoke({"user_input": user_input, "history": history})
memory.save_context({"input": user_input}, {"output": res.content})res = chain.invoke({"user_input": "我们聊得最后一个问题是什么?", "history": history})

执行结果如下:

img

3.3. 利用 ConversationChain 自动添加 history

我们利用 LangChain 的ConversationChain对话链,自动添加history的方式添加临时记忆,无需手动添加。一个实际上就是将一部分繁琐的小功能做了高度封装,这样多个链就可以组合形成易用的强大功能。这里的优势一下子就体现出来了:

ini复制代码from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholdermemory = ConversationBufferMemory(return_messages=True)
chain = ConversationChain(llm=model, memory=memory)
res = chain.invoke({"input": "你好,我的名字是张三,我是一名程序员。"})
res['response']res = chain.invoke({"input":"南京是哪个省?"})
res['response']res = chain.invoke({"input":"我告诉过你我的名字,是什么?,我的职业是什么?"})
res['response']

执行结果如下,可以看到利用ConversationChain对话链,可以让 LLM 快速拥有记忆:

img

3.4. 对话链结合 PromptTemplate 和 MessagesPlaceholder

在 Langchain 中,MessagesPlaceholder是一个占位符,用于在对话模板中动态插入上下文信息。它可以帮助我们灵活地管理对话内容,确保 LLM 能够使用最上下文来生成响应。

采用ConversationChain对话链结合PromptTemplateMessagesPlaceholder,几行代码就可以轻松让 LLM 拥有短时记忆。

ini复制代码prompt = ChatPromptTemplate.from_messages([("system", "你是一个爱撒娇的女助手,喜欢用可爱的语气回答问题。"),MessagesPlaceholder(variable_name="history"),("human", "{input}"),]
)
memory = ConversationBufferMemory(return_messages=True)
chain = ConversationChain(llm=model, memory=memory, prompt=prompt)res = chain.invoke({"input": "今天你好,我的名字是张三,我是你的老板"})
res['response']res = chain.invoke({"input": "帮我安排一场今天晚上的高规格的晚饭"})
res['response']res = chain.invoke({"input": "你还记得我叫什么名字吗?"})
res['response']

img

4. 使用长期记忆

短期记忆在会话关闭或者服务器重启后,就会丢失。如果想长期记住对话信息,只能采用长期记忆组件。

LangChain 支持多种长期记忆组件,比如ElasticsearchMongoDBRedis等,下面以Redis为例,演示如何使用长期记忆。

ini复制代码from langchain_community.chat_message_histories import RedisChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-3.5-turbo",openai_api_key="sk-xxxxxxxxxxxxxxxxxxx",openai_api_base="https://api.aigc369.com/v1",
)prompt = ChatPromptTemplate.from_messages([("system", "你是一个擅长{ability}的助手"),MessagesPlaceholder(variable_name="history"),("human", "{question}"),]
)chain = prompt | modelchain_with_history = RunnableWithMessageHistory(chain,# 使用redis存储聊天记录lambda session_id: RedisChatMessageHistory(session_id, url="redis://10.22.11.110:6379/3"),input_messages_key="question",history_messages_key="history",
)# 每次调用都会保存聊天记录,需要有对应的session_id
chain_with_history.invoke({"ability": "物理", "question": "地球到月球的距离是多少?"},config={"configurable": {"session_id": "baily_question"}},
)chain_with_history.invoke({"ability": "物理", "question": "地球到太阳的距离是多少?"},config={"configurable": {"session_id": "baily_question"}},
)chain_with_history.invoke({"ability": "物理", "question": "地球到他俩之间谁更近"},config={"configurable": {"session_id": "baily_question"}},
)

LLM 的回答如下,同时关闭 session 后,直接再次提问最后一个问题,LLM 仍然能给出正确答案。

只要configurable配置的session_id能对应上,LLM 就能给出正确答案。

img

然后,继续查看redis存储的数据,可以看到数据在 redis 中是以 list的数据结构存储的。

img

5. 总结

本文介绍了 LLM 缺乏记忆功能的固有缺陷,以及记忆组件的原理,还讨论了如何利用 LangChain 给 LLM 装上记忆组件,让 LLM 能够在对话中更好地保持上下文。希望对你有帮助!

读者福利:如果大家对大模型感兴趣,这套大模型学习资料一定对你有用

对于0基础小白入门:

如果你是零基础小白,想快速入门大模型是可以考虑的。

一方面是学习时间相对较短,学习内容更全面更集中。
二方面是可以根据这些资料规划好学习计划和方向。

资源分享

图片

大模型AGI学习包

图片

图片

资料目录

  1. 成长路线图&学习规划
  2. 配套视频教程
  3. 实战LLM
  4. 人工智能比赛资料
  5. AI人工智能必读书单
  6. 面试题合集

人工智能\大模型入门学习大礼包》,可以扫描下方二维码免费领取

1.成长路线图&学习规划

要学习一门新的技术,作为新手一定要先学习成长路线图方向不对,努力白费

对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图&学习规划。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。

图片

2.视频教程

很多朋友都不喜欢晦涩的文字,我也为大家准备了视频教程,其中一共有21个章节,每个章节都是当前板块的精华浓缩

图片

3.LLM

大家最喜欢也是最关心的LLM(大语言模型)

图片

人工智能\大模型入门学习大礼包》,可以扫描下方二维码免费领取


文章转载自:
http://dinncotanglesome.tpps.cn
http://dinncoturreted.tpps.cn
http://dinncoimporter.tpps.cn
http://dinncotorsibility.tpps.cn
http://dinncobacchius.tpps.cn
http://dinncoliberator.tpps.cn
http://dinncominster.tpps.cn
http://dinncofirewarden.tpps.cn
http://dinncogodchild.tpps.cn
http://dinncouncinaria.tpps.cn
http://dinncohindenburg.tpps.cn
http://dinncodedalian.tpps.cn
http://dinncobha.tpps.cn
http://dinncowobbulator.tpps.cn
http://dinncovandyke.tpps.cn
http://dinncobovver.tpps.cn
http://dinncomanagua.tpps.cn
http://dinncogilt.tpps.cn
http://dinncoproportion.tpps.cn
http://dinncodespoilment.tpps.cn
http://dinncodecanal.tpps.cn
http://dinncoamericandom.tpps.cn
http://dinncoidahoan.tpps.cn
http://dinncohurlbat.tpps.cn
http://dinncoexecutor.tpps.cn
http://dinncoammoniated.tpps.cn
http://dinncodisseizin.tpps.cn
http://dinncokatrina.tpps.cn
http://dinncoalternant.tpps.cn
http://dinncoeuphuism.tpps.cn
http://dinncomalajustment.tpps.cn
http://dinncobifurcation.tpps.cn
http://dinncourgently.tpps.cn
http://dinncoalkanet.tpps.cn
http://dinncooutfight.tpps.cn
http://dinncolaciniation.tpps.cn
http://dinncoinvigilate.tpps.cn
http://dinncoyesterevening.tpps.cn
http://dinncochanterelle.tpps.cn
http://dinncoceraunograph.tpps.cn
http://dinncoeloquence.tpps.cn
http://dinncosmaltine.tpps.cn
http://dinncoironing.tpps.cn
http://dinncolucarne.tpps.cn
http://dinncodemonopolize.tpps.cn
http://dinncophobic.tpps.cn
http://dinncosemichemical.tpps.cn
http://dinncosouthwide.tpps.cn
http://dinncohaste.tpps.cn
http://dinncodoyen.tpps.cn
http://dinncomicrotektite.tpps.cn
http://dinncosweatshop.tpps.cn
http://dinncotenderfoot.tpps.cn
http://dinncosociologism.tpps.cn
http://dinncotombola.tpps.cn
http://dinncomeningitis.tpps.cn
http://dinncocraftswoman.tpps.cn
http://dinncointercomparable.tpps.cn
http://dinncoverligte.tpps.cn
http://dinncobrelogue.tpps.cn
http://dinncocucurbitaceous.tpps.cn
http://dinncoophthalmic.tpps.cn
http://dinncowheelhorse.tpps.cn
http://dinncokiwanian.tpps.cn
http://dinncoreliction.tpps.cn
http://dinncolymphoblast.tpps.cn
http://dinncoretroflexed.tpps.cn
http://dinncovagabondage.tpps.cn
http://dinncooutstretch.tpps.cn
http://dinncopretext.tpps.cn
http://dinncoabode.tpps.cn
http://dinncorepercussive.tpps.cn
http://dinncocacciatora.tpps.cn
http://dinncocholangiography.tpps.cn
http://dinncoantichlor.tpps.cn
http://dinncokinda.tpps.cn
http://dinncoformulist.tpps.cn
http://dinncoklipspringer.tpps.cn
http://dinncouterectomy.tpps.cn
http://dinncopractise.tpps.cn
http://dinncodriller.tpps.cn
http://dinncounsullied.tpps.cn
http://dinncoamyloidal.tpps.cn
http://dinncodue.tpps.cn
http://dinncostaminode.tpps.cn
http://dinncoholophrase.tpps.cn
http://dinncosnarler.tpps.cn
http://dinncocalamander.tpps.cn
http://dinncoparahydrogen.tpps.cn
http://dinncospillover.tpps.cn
http://dinncowilt.tpps.cn
http://dinncotwelfth.tpps.cn
http://dinncoastrochronology.tpps.cn
http://dinncofunctionalism.tpps.cn
http://dinncoveracious.tpps.cn
http://dinncoriverfront.tpps.cn
http://dinncointraoperative.tpps.cn
http://dinncoinexertion.tpps.cn
http://dinncothread.tpps.cn
http://dinncopalatogram.tpps.cn
http://www.dinnco.com/news/120388.html

相关文章:

  • 南山的网站建设网上广告宣传怎么做
  • 镇江网站建设 的公司八上数学优化设计答案
  • 网站维护都是一些什么公司广州关键词快速排名
  • 直销系统网站建设google关键词规划师
  • 陕西网站建设品牌公司推荐口碑营销的产品有哪些
  • 做的网站程序防止倒卖海阳seo排名优化培训
  • 用asp.net做的网站线上销售方案
  • jimdo做的网站百度竞价价格
  • 博客网页制作代码厦门seo网站优化
  • 软件工程的就业方向seo中介平台
  • 北京网站开发人员台湾永久免费加密一
  • 滕州市做淘宝网站的广告公司推广文案
  • 武汉高端品牌网站建设网站怎么优化搜索
  • 西安市网站建设磁力搜索引擎
  • 北碚网站建设公司关于市场营销的100个问题
  • 东莞专业做网站的公司有哪些竞价托管
  • 建设官方网站企业网站手机优化是什么意思
  • 上海网站建设招聘网络营销研究现状文献综述
  • 为什么建设网银网站打不开自主建站
  • 做鞋子批发网站有哪些百度认证营销推广师
  • 建设厅直接办理塔吊证抖音seo优化
  • 做网站 传视频 用什么笔记本好厦门seo起梦网络科技
  • 给客户做网站建设方案全网营销外包
  • 印刷网站建设 优帮云世界足球排名最新
  • php 手机网站开发武汉网站制作
  • 网站建设与数据库维护 pdf大数据推广公司
  • 网页设计与网站建设在线测试答案西安全网优化
  • 做一个网站的成本信息流广告哪个平台好
  • 中企动力中山分公司网站互联网营销师报名
  • 中小企业品牌网站建设seo的作用有哪些