在最开始我们就通过实验知道LLM 本身是没有记忆的,每一次LLM的API调用都是一个全新的会话。但在某些应用程序中,如:聊天机器人,让LLM记住以前的历史交互是非常重要,无论是在短期的还是长期的。langchain中的“Memory”即对话历史(message history)就是为了实现这一点。
在与大模型进行对话和交互的过程中,一个关键步骤是能够引用交互过程中先前的信息,至少需要能够直接回溯到过去某些对话的内容。对于复杂应用而言,所需的是一个能够不断自我更新的模型,以便执行如维护相关信息、实体及其关系等任务。这种存储并回溯过去交互信息的能力,就叫做“记忆(Memory)”。
Memory作为存储记忆数据的一个是抽象模块,其作为一个独立模块使用是没有任何意义的,因为本质上它的定位就是一个存储对话数据的空间。
LangChain Memory 的作用
- 上下文管理:通过保存历史对话,模型可以基于之前的对话内容来生成更相关的响应。
- 状态跟踪:对于需要持续跟踪用户状态的应用程序来说,Memory 可以帮助维护会话的状态信息。
- 个性化体验:通过记录用户的偏好或历史选择,可以提供更加个性化的用户体验。
ChatMessageHistory-对话消息历史管理
在LangChain中,ChatMessageHistory通常是一个数据结构,用于存储和检索对话消息。这些消息可以按照时间顺序排列,以便在对话过程中引用和更新。
# 初始化大模型 fromlangchain_openaiimportChatOpenAI fromlangchain.promptsimportChatPromptTemplate, MessagesPlaceholder # 本地ollama拉取过什么模型就使用什么模型 API_KEY="sk-4b79f3axxx35366ebb425b3" llm=ChatOpenAI(model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com") # 聊天模型提示词 template= [ MessagesPlaceholder(variable_name="history"), ] prompt=ChatPromptTemplate.from_messages(messages=template) chain=prompt|llm # 记录会话历史 fromlangchain_community.chat_message_historiesimportChatMessageHistory fromlangchain_core.messagesimportSystemMessage history=ChatMessageHistory() history.messages= [SystemMessage("你是由John开发的智能助手机器人,叫多啦A梦,你每次都会精简而快速的告诉用户你是一个专业的机器人以及用户问题的答案。")] history.add_user_message("我叫John,请你记住。") history.add_user_message("我叫什么名字,以及你叫什么名字?") res=chain.invoke({"history": history.messages}) history.add_ai_message(res) print(res.content) history.add_user_message("我现在改名了,叫Johnny,请问我是谁?") res=chain.invoke({"history": history.messages}) history.add_ai_message(res) print(res.content) formessageinhistory.messages: print("会话记录",message.content)多个用户多轮对话
有了对话消息历史管理对象,不仅可以管理和存储单个用户和LLM的历史对话信息以此来维持会话状态,还可以实现管理多用户与LLM的独立历史对话信息。
# 初始化大模型 fromlangchain_openaiimportChatOpenAI fromlangchain.promptsimportChatPromptTemplate, MessagesPlaceholder # 本地ollama拉取过什么模型就使用什么模型 API_KEY="sk-4b79fxxx935366ebb425b3" llm=ChatOpenAI(model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com") # 聊天模型提示词 fromlangchain.promptsimportChatPromptTemplate, MessagesPlaceholder template= [ ("system", "你叫多啦A梦,今年1岁了,是John开发的智能机器人,能精准回复用户的问题"), MessagesPlaceholder(variable_name="history"), ] prompt=ChatPromptTemplate.from_messages(messages=template) chain=prompt|llm # 记录会话历史 fromlangchain_community.chat_message_historiesimportChatMessageHistory #session_id设置不同的消息集 john_history=ChatMessageHistory(session_id="John") john_history.add_user_message('我叫John,今年100岁,很高兴和你聊天') john_res=chain.invoke({"history": john_history.messages}) john_history.add_ai_message(john_res) print(john_res.content) print('=======================================') Yuki_history=ChatMessageHistory(session_id="Yuki") Yuki_history.add_user_message('你好呀,我的名字叫Yuki,我今年200岁。你叫什么?') Yuki_res=chain.invoke({"history": Yuki_history.messages}) Yuki_history.add_ai_message(Yuki_res) print(Yuki_res.content) print('=======================================') john_history.add_user_message("你还记得我的名字和年龄吗?") john_res=chain.invoke({"history": john_history.messages}) john_history.add_ai_message(john_res) print(john_res.content) print('=======================================') Yuki_history.add_user_message("你还记得我的名字和年龄吗?") Yuki_res=chain.invoke({"history": Yuki_history.messages}) Yuki_history.add_ai_message(Yuki_res) print(Yuki_res.content) print('=======================================')上面虽然使用了ChatMessageHistory保存对话历史数据,但是与Chains的操作是独立的,并且每次产生新的对话消息都要手动add添加记录,所以为了方便使用,langchain还提供了RunnableWithMessageHistory可以自动为Chains添加对话历史记录。
# 初始化大模型 fromlangchain_openaiimportChatOpenAI fromlangchain.promptsimportChatPromptTemplate, MessagesPlaceholder fromlangchain_core.output_parsersimportStrOutputParser # 本地ollama拉取过什么模型就使用什么模型 API_KEY="sk-4b79f3xxx1935366ebb425b3" llm=ChatOpenAI(model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com") # 聊天模型提示词 template= [ ("system", "你叫多啦A梦,今年1岁了,是John开发的智能机器人,能精准回复用户的问题"), MessagesPlaceholder(variable_name="history"), ("human", "{input}"), ] prompt=ChatPromptTemplate.from_messages(messages=template) chain=prompt|llm|StrOutputParser() # 记录会话历史 fromlangchain_core.runnables.historyimportRunnableWithMessageHistory fromlangchain_community.chat_message_historiesimportChatMessageHistory # 用于记录不同的用户(session_id)对话历史 store= {} defget_session_history(session_id): ifsession_idnotinstore: store[session_id] =ChatMessageHistory() returnstore[session_id] chains=RunnableWithMessageHistory( chain, get_session_history, input_messages_key="input", history_messages_key="history", ) res1=chains.invoke({"input": "什么是余弦相似度?"}, config={'configurable': {'session_id': 'john'}}) print(res1) print('====================================================') res2=chains.invoke({"input": "再回答一次刚才的问题"}, config={'configurable': {'session_id': 'john'}}) print(res2)ConversationChain中的记忆
ConversationChain提供了包含AI角色和人类角色的对话摘要格式,这个对话格式和记忆机制结合得非常紧密。ConversationChain实际上是对Memory和LLMChain进行了封装,简化了初始化Memory的步骤。
该方法已经在langchain1.0版本废除,使用RunnableWithMessageHistory对其进行替代!
# 初始化大模型 fromlangchain_openaiimportChatOpenAI # 本地ollama拉取过什么模型就使用什么模型 API_KEY="sk-4b79f3a3xxx935366ebb425b3" llm=ChatOpenAI(model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com") # 导入所需的库 fromlangchain.chains.conversation.baseimportConversationChain # 初始化对话链 conv_chain=ConversationChain(llm=llm) # 打印对话的模板 print(conv_chain.prompt.template)ConversationChain中的内置提示模板中的两个参数:
- {history}:存储会话记忆的地方,也就是人类和人工智能之间对话历史的信息。
- {input} :新输入的地方,可以把它看成是和ChatGPT对话时,文本框中的输入。
缓冲记忆:ConversationBufferMemory
在LangChain中,ConversationBufferMemory是一种非常简单的缓冲记忆,可以实现最简单的记忆机制,它只在缓冲区中保存聊天消息列表并将其传递到提示模板中。
通过记忆机制,LLM能够理解之前的对话内容。直接将存储的所有内容给LLM,因为大量信息意味着新输入中包含更多的Token,导致响应时间变慢和成本增加。此外,当达到LLM的Token数限制时,太长的对话无法被记住。
#用于创建对话链 fromlangchain.chainsimportConversationChain #用于存储对话历史,以便在后续对话中参考 fromlangchain.memoryimportConversationBufferMemory fromlangchain_openaiimportChatOpenAI importwarnings warnings.filterwarnings("ignore") # 初始化大模型(需配置OPENAI_API_KEY) API_KEY="sk-4b79f3axxx935366ebb425b3" llm=ChatOpenAI(model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com") #实例化一个对话缓冲区,用于存储对话历史 memory=ConversationBufferMemory() #创建一个对话链,将大语言模型和对话缓冲区关联起来。 conversation=ConversationChain( llm=llm, memory=memory, ) conversation.invoke("今天早上猪八戒吃了2个人参果。") print("记忆1: ", conversation.memory.buffer) print() conversation.invoke("下午猪八戒吃了1个人参果。") print("记忆2: ", conversation.memory.buffer) print() conversation.invoke("晚上猪八戒吃了3个人参果。") print("记忆3: ", conversation.memory.buffer) print() conversation.invoke("猪八戒今天一共吃了几个人参果?") print("记忆4: ", conversation.memory.buffer)功能设计:多轮对话
fromlangchain.chainsimportConversationChain fromlangchain.memoryimportConversationBufferMemory fromlangchain_openaiimportChatOpenAI importwarnings warnings.filterwarnings("ignore") # 实例化一个对话缓冲区,用于存储对话历史 memory=ConversationBufferMemory() # 创建一个对话链,将大语言模型和对话缓冲区关联起来。 conversation=ConversationChain( llm=llm, memory=memory, ) print("欢迎使用对话系统!输入 '退出' 结束对话。") whileTrue: user_input=input("你: ") ifuser_input.lower() in ['退出', 'exit', 'quit']: print("再见!") break response=conversation.predict(input=user_input) print(f"AI: {response}") # 打印出对话历史,即 memory.buffer 的内容 print("对话历史:", memory.buffer)携带提示词模版的对轮对话(LLMChain对话链)
fromlangchain.promptsimportPromptTemplate fromlangchain.chainsimportLLMChain fromlangchain.memoryimportConversationBufferMemory fromlangchain_openaiimportChatOpenAI importos importwarnings warnings.filterwarnings("ignore") # 初始化大模型 API_KEY="sk-4b79f3a3fxxx1935366ebb425b3" llm=ChatOpenAI( model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com" ) # 实例化一个对话缓冲区,用于存储对话历史 memory=ConversationBufferMemory() # 定义提示词模板 template="""{history} 用户: {input} AI:""" prompt_template=PromptTemplate( input_variables=["history", "input"], template=template ) # 创建一个包含提示词模板的对话链 conversation=LLMChain( llm=llm, prompt=prompt_template, verbose=True, # 如果需要调试,可以设置为 True memory=memory ) print("欢迎使用对话系统!输入 '退出' 结束对话。") whileTrue: user_input=input("你: ") ifuser_input.lower() in ['退出', 'exit', 'quit']: print("再见!") break try: # 调用对话链获取响应 response=conversation.run(input=user_input) print(f"AI: {response}") exceptExceptionase: print(f"发生错误: {e}") # 打印出对话历史,即 memory.buffer 的内容 print("对话历史:", memory.buffer)如果使用聊天模型,使用结构化的聊天消息可能会有更好的性能:
fromlangchain_openaiimportChatOpenAI fromlangchain.memoryimportConversationBufferMemory fromlangchain.chains.llmimportLLMChain fromlangchain_core.messagesimportSystemMessage fromlangchain_core.promptsimportMessagesPlaceholder, HumanMessagePromptTemplate, ChatPromptTemplate importwarnings warnings.filterwarnings("ignore") # 初始化大模型 API_KEY="sk-4b79f3a3xxxa1935366ebb425b3" llm=ChatOpenAI( model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com" ) # 使用ChatPromptTemplate设置聊天提示 prompt=ChatPromptTemplate.from_messages( [ SystemMessage(content="你是一个与人类对话的机器人。"), MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{question}"), ] ) # 创建ConversationBufferMemory memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True) # 初始化链 chain=LLMChain(llm=llm, prompt=prompt, memory=memory) # 提问 res=chain.invoke({"question": "你是LangChain专家"}) print(str(res) +"\n") res=chain.invoke({"question": "你是谁?"}) print(res)多轮对话Token限制解决
在了解了ConversationBufferMemory记忆类后,我们知道了它能够无限的将历史对话信息填充到History中,从而给大模型提供上下文的背景。但问题是:每个大模型都存在最大输入的Token限制,且过久远的对话数据往往并不能够对当前轮次的问答提供有效的信息,这种我们大家都能非常容易想到的问题,LangChain的开发人员自然也能想到,那么他们给出的解决方式是:ConversationBufferWindowMemory模块。该记忆类会保存一段时间内对话交互的列表,仅使用最后 K 个交互。所以它可以保存最近交互的滑动窗口,避免缓存区不会变得太大。
fromlangchain.memoryimportConversationBufferWindowMemory importwarnings warnings.filterwarnings("ignore") #实例化一个对话缓冲区,用于存储对话历史 #k=1,所以在读取时仅能提取到最近一轮的记忆信息 #return_messages=True参数,将对话转化为消息列表形式 memory=ConversationBufferWindowMemory(k=1, return_messages=True) conversation=ConversationChain( llm=llm, memory=memory, ) # 示例对话 response1=conversation.predict(input="你好") response2=conversation.predict(input="你在哪里?") print("对话历史:", memory.buffer)实体记忆:ConversationEntityMemory
在LangChain 中,ConversationEntityMemory是实体记忆,它可以跟踪对话中提到的实体,在对话中记住关于特定实体的给定事实。它提取关于实体的信息(使用LLM),并随着时间的推移建立对该实体的知识(使用LLM)。
使用它来存储和查询对话中引用的各种信息,比如人物、地点、事件等。
fromlangchain.chains.conversation.baseimportConversationChain fromlangchain.memoryimportConversationEntityMemory fromlangchain.memory.promptimportENTITY_MEMORY_CONVERSATION_TEMPLATE fromlangchain_openaiimportOpenAI importwarnings warnings.filterwarnings("ignore") # 初始化大模型 API_KEY="sk-4b79f3a3xxx1935366ebb425b3" llm=ChatOpenAI( model="deepseek-chat", openai_api_key=API_KEY, openai_api_base="https://api.deepseek.com" ) conversation=ConversationChain( llm=llm, prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE, memory=ConversationEntityMemory(llm=llm) ) # 开始对话 conversation.predict(input="你好,我是小明。我最近在学习 LangChain。") conversation.predict(input="我最喜欢的编程语言是 Python。") conversation.predict(input="我住在北京。") # 查询对话中提到的实体 res=conversation.memory.entity_store.store print(res)普通人如何抓住AI大模型的风口?
领取方式在文末
为什么要学习大模型?
目前AI大模型的技术岗位与能力培养随着人工智能技术的迅速发展和应用 , 大模型作为其中的重要组成部分 , 正逐渐成为推动人工智能发展的重要引擎 。大模型以其强大的数据处理和模式识别能力, 广泛应用于自然语言处理 、计算机视觉 、 智能推荐等领域 ,为各行各业带来了革命性的改变和机遇 。
目前,开源人工智能大模型已应用于医疗、政务、法律、汽车、娱乐、金融、互联网、教育、制造业、企业服务等多个场景,其中,应用于金融、企业服务、制造业和法律领域的大模型在本次调研中占比超过30%。
随着AI大模型技术的迅速发展,相关岗位的需求也日益增加。大模型产业链催生了一批高薪新职业:
人工智能大潮已来,不加入就可能被淘汰。如果你是技术人,尤其是互联网从业者,现在就开始学习AI大模型技术,真的是给你的人生一个重要建议!
最后
只要你真心想学习AI大模型技术,这份精心整理的学习资料我愿意无偿分享给你,但是想学技术去乱搞的人别来找我!
在当前这个人工智能高速发展的时代,AI大模型正在深刻改变各行各业。我国对高水平AI人才的需求也日益增长,真正懂技术、能落地的人才依旧紧缺。我也希望通过这份资料,能够帮助更多有志于AI领域的朋友入门并深入学习。
真诚无偿分享!!!
vx扫描下方二维码即可
加上后会一个个给大家发
大模型全套学习资料展示
自我们与MoPaaS魔泊云合作以来,我们不断打磨课程体系与技术内容,在细节上精益求精,同时在技术层面也新增了许多前沿且实用的内容,力求为大家带来更系统、更实战、更落地的大模型学习体验。
希望这份系统、实用的大模型学习路径,能够帮助你从零入门,进阶到实战,真正掌握AI时代的核心技能!
01教学内容
从零到精通完整闭环:【基础理论 →RAG开发 → Agent设计 → 模型微调与私有化部署调→热门技术】5大模块,内容比传统教材更贴近企业实战!
大量真实项目案例:带你亲自上手搞数据清洗、模型调优这些硬核操作,把课本知识变成真本事!
02适学人群
应届毕业生:无工作经验但想要系统学习AI大模型技术,期待通过实战项目掌握核心技术。
零基础转型:非技术背景但关注AI应用场景,计划通过低代码工具实现“AI+行业”跨界。
业务赋能突破瓶颈:传统开发者(Java/前端等)学习Transformer架构与LangChain框架,向AI全栈工程师转型。
vx扫描下方二维码即可
本教程比较珍贵,仅限大家自行学习,不要传播!更严禁商用!
03入门到进阶学习路线图
大模型学习路线图,整体分为5个大的阶段:
04视频和书籍PDF合集
从0到掌握主流大模型技术视频教程(涵盖模型训练、微调、RAG、LangChain、Agent开发等实战方向)
新手必备的大模型学习PDF书单来了!全是硬核知识,帮你少走弯路(不吹牛,真有用)
05行业报告+白皮书合集
收集70+报告与白皮书,了解行业最新动态!
0690+份面试题/经验
AI大模型岗位面试经验总结(谁学技术不是为了赚$呢,找个好的岗位很重要)
07 deepseek部署包+技巧大全
由于篇幅有限
只展示部分资料
并且还在持续更新中…
真诚无偿分享!!!
vx扫描下方二维码即可
加上后会一个个给大家发