llamaindex实战-Agent的长期记忆(内存)演示
概述
本文使用本地嵌入大模型来演示llamaindex的Agent长期内存记忆:VectorMemory如何使用。
本地环境:16C32G Linux机器
背景
向量内存模块使用向量搜索(由向量数据库支持)来检索给定用户输入的相关对话项目。
本文演示了如何使用 VectorMemory 类。我们向您展示如何使用其各个功能。向量内存的一个典型用例是作为聊天消息的长期内存存储。
VectorMemory最终会把数据保存到向量数据库中:
本地运行代码
注意:先把嵌入模型:BAAI/bge-base-en-v1.5下载到本地目录。
(1)开始时,初始化一个原始内存模块并演示其功能 - 放置和检索 ChatMessage 对象。
(2)尝试重置并重试。这次,我们将添加一条助理消息。请注意,默认情况下,用户/助理消息是捆绑的。
from llama_index.core.memory import VectorMemory
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.ollama import Ollama
local_model = "/opt/models/BAAI/bge-base-en-v1.5"
# local bge-base embedding model
Settings.embed_model = HuggingFaceEmbedding(model_name=local_model)
vector_memory = VectorMemory.from_defaults(
vector_store=None, # leave as None to use default in-memory vector store
embed_model=Settings.embed_model,
retriever_kwargs={"similarity_top_k": 1},
)
from llama_index.core.llms import ChatMessage
msgs = [
ChatMessage.from_str("Jerry likes juice.", "user"),
ChatMessage.from_str("Bob likes burgers.", "user"),
ChatMessage.from_str("Alice likes apples.", "user"),
]
# load into memory
for m in msgs:
vector_memory.put(m)
# retrieve from memory
print("retrieve from memory ...")
msgs = vector_memory.get("What does Jerry like?")
print(msgs)
msgs = [
ChatMessage.from_str("Jerry likes burgers.", "user"),
ChatMessage.from_str("Bob likes apples.", "user"),
ChatMessage.from_str("Indeed, Bob likes apples.", "assistant"),
ChatMessage.from_str("Alice likes juice.", "user"),
]
vector_memory.set(msgs)
print("resetting and trying again ...")
msgs = vector_memory.get("What does Bob like?")
print(msgs)
运行结果:
可以看到都得到了正确的结果。
retrieve from memory ...
[ChatMessage(role=<MessageRole.USER: 'user'>, content='Jerry likes juice.', additional_kwargs={})]
resetting and trying again ...
[ChatMessage(role=<MessageRole.USER: 'user'>, content='Bob likes apples.', additional_kwargs={}),
ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, content='Indeed, Bob likes apples.', additional_kwargs={})]
参考文献
原文地址:https://blog.csdn.net/zg_hover/article/details/143655381
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!