🔹 5. Memory(多轮记忆)
类型 | 功能 | 适用场景 |
ConversationBufferMemory | 保存所有历史消息 | 最通用 |
ConversationSummaryMemory | 总结历史内容 | 节省 token |
VectorStoreRetrieverMemory | 向量形式记忆 | 类似长期知识库 |
示例:
python 复制编辑 from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
可与
initialize_agent() 搭配:python 复制编辑 agent = initialize_agent( tools=tools, llm=llm, agent="chat-conversational-react-description", memory=memory, verbose=True )
✅ 使用场景举例
场景 | 组合模块 |
LLM 能调用函数查天气、股票等 | Tool + Agent |
文档问答+函数调用+多轮记忆 | RetrievalChain + Tool + Memory |
多功能助手(搜索+算数+写代码) | Toolkits + AgentExecutor |
RAG 系统中动态检索和调用 | Retriever + Tool + Agent |