商户如何做h5商城网站是什么手机网站底部导航菜单
2026/6/20 11:00:45 网站建设 项目流程
商户如何做h5商城网站是什么,手机网站底部导航菜单,哪个网站可以兼职做家教,wordpress 去掉骄傲的在最开始我们就通过实验知道LLM 本身是没有记忆的#xff0c;每一次LLM的API调用都是一个全新的会话。但在某些应用程序中#xff0c;如#xff1a;聊天机器人#xff0c;让LLM记住以前的历史交互是非常重要#xff0c;无论是在短期的还是长期的。langchain中的“Memory”…在最开始我们就通过实验知道LLM 本身是没有记忆的每一次LLM的API调用都是一个全新的会话。但在某些应用程序中如聊天机器人让LLM记住以前的历史交互是非常重要无论是在短期的还是长期的。langchain中的“Memory”即对话历史message history就是为了实现这一点。在与大模型进行对话和交互的过程中一个关键步骤是能够引用交互过程中先前的信息至少需要能够直接回溯到过去某些对话的内容。对于复杂应用而言所需的是一个能够不断自我更新的模型以便执行如维护相关信息、实体及其关系等任务。这种存储并回溯过去交互信息的能力就叫做“记忆Memory”。Memory作为存储记忆数据的一个是抽象模块其作为一个独立模块使用是没有任何意义的因为本质上它的定位就是一个存储对话数据的空间。LangChain Memory 的作用上下文管理通过保存历史对话模型可以基于之前的对话内容来生成更相关的响应。状态跟踪对于需要持续跟踪用户状态的应用程序来说Memory 可以帮助维护会话的状态信息。个性化体验通过记录用户的偏好或历史选择可以提供更加个性化的用户体验。ChatMessageHistory-对话消息历史管理在LangChain中ChatMessageHistory通常是一个数据结构用于存储和检索对话消息。这些消息可以按照时间顺序排列以便在对话过程中引用和更新。# 初始化大模型 fromlangchain_openaiimportChatOpenAI fromlangchain.promptsimportChatPromptTemplate, MessagesPlaceholder # 本地ollama拉取过什么模型就使用什么模型 API_KEYsk-4b79f3axxx35366ebb425b3 llmChatOpenAI(modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com) # 聊天模型提示词 template [ MessagesPlaceholder(variable_namehistory), ] promptChatPromptTemplate.from_messages(messagestemplate) chainprompt|llm # 记录会话历史 fromlangchain_community.chat_message_historiesimportChatMessageHistory fromlangchain_core.messagesimportSystemMessage historyChatMessageHistory() history.messages [SystemMessage(你是由John开发的智能助手机器人叫多啦A梦你每次都会精简而快速的告诉用户你是一个专业的机器人以及用户问题的答案。)] history.add_user_message(我叫John请你记住。) history.add_user_message(我叫什么名字以及你叫什么名字) reschain.invoke({history: history.messages}) history.add_ai_message(res) print(res.content) history.add_user_message(我现在改名了叫Johnny请问我是谁) reschain.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_KEYsk-4b79fxxx935366ebb425b3 llmChatOpenAI(modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com) # 聊天模型提示词 fromlangchain.promptsimportChatPromptTemplate, MessagesPlaceholder template [ (system, 你叫多啦A梦,今年1岁了是John开发的智能机器人能精准回复用户的问题), MessagesPlaceholder(variable_namehistory), ] promptChatPromptTemplate.from_messages(messagestemplate) chainprompt|llm # 记录会话历史 fromlangchain_community.chat_message_historiesimportChatMessageHistory #session_id设置不同的消息集 john_historyChatMessageHistory(session_idJohn) john_history.add_user_message(我叫John今年100岁,很高兴和你聊天) john_reschain.invoke({history: john_history.messages}) john_history.add_ai_message(john_res) print(john_res.content) print() Yuki_historyChatMessageHistory(session_idYuki) Yuki_history.add_user_message(你好呀我的名字叫Yuki我今年200岁。你叫什么) Yuki_reschain.invoke({history: Yuki_history.messages}) Yuki_history.add_ai_message(Yuki_res) print(Yuki_res.content) print() john_history.add_user_message(你还记得我的名字和年龄吗) john_reschain.invoke({history: john_history.messages}) john_history.add_ai_message(john_res) print(john_res.content) print() Yuki_history.add_user_message(你还记得我的名字和年龄吗) Yuki_reschain.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_KEYsk-4b79f3xxx1935366ebb425b3 llmChatOpenAI(modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com) # 聊天模型提示词 template [ (system, 你叫多啦A梦,今年1岁了是John开发的智能机器人能精准回复用户的问题), MessagesPlaceholder(variable_namehistory), (human, {input}), ] promptChatPromptTemplate.from_messages(messagestemplate) chainprompt|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] chainsRunnableWithMessageHistory( chain, get_session_history, input_messages_keyinput, history_messages_keyhistory, ) res1chains.invoke({input: 什么是余弦相似度?}, config{configurable: {session_id: john}}) print(res1) print() res2chains.invoke({input: 再回答一次刚才的问题}, config{configurable: {session_id: john}}) print(res2)ConversationChain中的记忆ConversationChain提供了包含AI角色和人类角色的对话摘要格式这个对话格式和记忆机制结合得非常紧密。ConversationChain实际上是对Memory和LLMChain进行了封装简化了初始化Memory的步骤。该方法已经在langchain1.0版本废除使用RunnableWithMessageHistory对其进行替代# 初始化大模型 fromlangchain_openaiimportChatOpenAI # 本地ollama拉取过什么模型就使用什么模型 API_KEYsk-4b79f3a3xxx935366ebb425b3 llmChatOpenAI(modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com) # 导入所需的库 fromlangchain.chains.conversation.baseimportConversationChain # 初始化对话链 conv_chainConversationChain(llmllm) # 打印对话的模板 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_KEYsk-4b79f3axxx935366ebb425b3 llmChatOpenAI(modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com) #实例化一个对话缓冲区用于存储对话历史 memoryConversationBufferMemory() #创建一个对话链将大语言模型和对话缓冲区关联起来。 conversationConversationChain( llmllm, memorymemory, ) 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) # 实例化一个对话缓冲区用于存储对话历史 memoryConversationBufferMemory() # 创建一个对话链将大语言模型和对话缓冲区关联起来。 conversationConversationChain( llmllm, memorymemory, ) print(欢迎使用对话系统输入 退出 结束对话。) whileTrue: user_inputinput(你: ) ifuser_input.lower() in [退出, exit, quit]: print(再见) break responseconversation.predict(inputuser_input) print(fAI: {response}) # 打印出对话历史即 memory.buffer 的内容 print(对话历史:, memory.buffer)携带提示词模版的对轮对话(LLMChain对话链)fromlangchain.promptsimportPromptTemplate fromlangchain.chainsimportLLMChain fromlangchain.memoryimportConversationBufferMemory fromlangchain_openaiimportChatOpenAI importos importwarnings warnings.filterwarnings(ignore) # 初始化大模型 API_KEYsk-4b79f3a3fxxx1935366ebb425b3 llmChatOpenAI( modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com ) # 实例化一个对话缓冲区用于存储对话历史 memoryConversationBufferMemory() # 定义提示词模板 template{history} 用户: {input} AI: prompt_templatePromptTemplate( input_variables[history, input], templatetemplate ) # 创建一个包含提示词模板的对话链 conversationLLMChain( llmllm, promptprompt_template, verboseTrue, # 如果需要调试可以设置为 True memorymemory ) print(欢迎使用对话系统输入 退出 结束对话。) whileTrue: user_inputinput(你: ) ifuser_input.lower() in [退出, exit, quit]: print(再见) break try: # 调用对话链获取响应 responseconversation.run(inputuser_input) print(fAI: {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_KEYsk-4b79f3a3xxxa1935366ebb425b3 llmChatOpenAI( modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com ) # 使用ChatPromptTemplate设置聊天提示 promptChatPromptTemplate.from_messages( [ SystemMessage(content你是一个与人类对话的机器人。), MessagesPlaceholder(variable_namechat_history), HumanMessagePromptTemplate.from_template({question}), ] ) # 创建ConversationBufferMemory memoryConversationBufferMemory(memory_keychat_history, return_messagesTrue) # 初始化链 chainLLMChain(llmllm, promptprompt, memorymemory) # 提问 reschain.invoke({question: 你是LangChain专家}) print(str(res) \n) reschain.invoke({question: 你是谁?}) print(res)多轮对话Token限制解决在了解了ConversationBufferMemory记忆类后我们知道了它能够无限的将历史对话信息填充到History中从而给大模型提供上下文的背景。但问题是每个大模型都存在最大输入的Token限制且过久远的对话数据往往并不能够对当前轮次的问答提供有效的信息这种我们大家都能非常容易想到的问题LangChain的开发人员自然也能想到那么他们给出的解决方式是ConversationBufferWindowMemory模块。该记忆类会保存一段时间内对话交互的列表仅使用最后 K 个交互。所以它可以保存最近交互的滑动窗口避免缓存区不会变得太大。fromlangchain.memoryimportConversationBufferWindowMemory importwarnings warnings.filterwarnings(ignore) #实例化一个对话缓冲区用于存储对话历史 #k1所以在读取时仅能提取到最近一轮的记忆信息 #return_messagesTrue参数将对话转化为消息列表形式 memoryConversationBufferWindowMemory(k1, return_messagesTrue) conversationConversationChain( llmllm, memorymemory, ) # 示例对话 response1conversation.predict(input你好) response2conversation.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_KEYsk-4b79f3a3xxx1935366ebb425b3 llmChatOpenAI( modeldeepseek-chat, openai_api_keyAPI_KEY, openai_api_basehttps://api.deepseek.com ) conversationConversationChain( llmllm, promptENTITY_MEMORY_CONVERSATION_TEMPLATE, memoryConversationEntityMemory(llmllm) ) # 开始对话 conversation.predict(input你好,我是小明。我最近在学习 LangChain。) conversation.predict(input我最喜欢的编程语言是 Python。) conversation.predict(input我住在北京。) # 查询对话中提到的实体 resconversation.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扫描下方二维码即可加上后会一个个给大家发

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询