Zep
Zep - 用于 LLM 应用的长期记忆存储。
关于 Zep
的更多信息:
Zep
存储、总结、嵌入、索引和丰富对话型 AI 聊天历史,并通过简单、低延迟的 API 公开它们。
主要特点:
- 长期记忆持久性,无论您的摘要策略如何,都可以访问历史消息。
- 基于可配置的消息窗口自动总结记忆消息。存储一系列摘要,为未来的摘要策略提供灵活性。
- 对记忆进行向量搜索,消息在创建时自动嵌入。
- 自动计算记忆和摘要的标记数量,允许更精细地控制提示组装。
- 提供 Python 和 JavaScript SDK。
Zep
的 Go 提取器模型易于扩展,提供简单、清晰的接口来构建新的丰富功能,如摘要器、实体提取器、嵌入器等。
Zep
项目:https://github.com/getzep/zep
检索器示例
本文档演示如何使用Zep 长期记忆存储搜索历史聊天消息记录。
我们将演示以下内容:
- 将对话历史添加到 Zep 记忆存储中。
- 在对话历史中进行向量搜索。
from langchain.memory.chat_message_histories import ZepChatMessageHistory
from langchain.schema import HumanMessage, AIMessage
from uuid import uuid4
# 将此设置为您的 Zep 服务器 URL
ZEP_API_URL = "http://localhost:8000"
初始化 Zep 聊天消息历史类,并将聊天消息历史添加到记忆存储中
**注意:**与其他检索器不同,Zep 检索器返回的内容是会话/用户特定的。在实例化检索器时需要提供 session_id
。
session_id = str(uuid4()) # 这是用户/会话的唯一标识符
# 设置 Zep 聊天历史。我们将使用它将聊天历史添加到记忆存储中
zep_chat_history = ZepChatMessageHistory(
session_id=session_id,
url=ZEP_API_URL,
)
# 预加载一些消息到记忆存储中。默认的消息窗口为 12 条消息。我们想要超过这个数量以演示自动摘要功能。
test_history = [
{"role": "human", "content": "Who was Octavia Butler?"},
{
"role": "ai",
"content": (
"Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American"
" science fiction author."
),
},
{"role": "human", "content": "Which books of hers were made into movies?"},
{
"role": "ai",
"content": (
"The most well-known adaptation of Octavia Butler's work is the FX series"
" Kindred, based on her novel of the same name."
),
},
{"role": "human", "content": "Who were her contemporaries?"},
{
"role": "ai",
"content": (
"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R."
" Delany, and Joanna Russ."
),
},
{"role": "human", "content": "What awards did she win?"},
{
"role": "ai",
"content": (
"Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur"
" Fellowship."
),
},
{
"role": "human",
"content": "Which other women sci-fi writers might I want to read?",
},
{
"role": "ai",
"content": "You might want to read Ursula K. Le Guin or Joanna Russ.",
},
{
"role": "human",
"content": (
"Write a short synopsis of Butler's book, Parable of the Sower. What is it"
" about?"
),
},
{
"role": "ai",
"content": (
"Parable of the Sower is a science fiction novel by Octavia Butler,"
" published in 1993. It follows the story of Lauren Olamina, a young woman"
" living in a dystopian future where society has collapsed due to"
" environmental disasters, poverty, and violence."
),
},
]
for msg in test_history:
zep_chat_history.append(
HumanMessage(content=msg["content"])
if msg["role"] == "human"
else AIMessage(content=msg["content"])
)
使用 Zep 检索器对 Zep 记忆进行向量搜索
Zep 可以在历史对话记忆中进行原生的向量搜索。嵌入会自动进行。
注意:消息的嵌入是异步进行的,因此第一次查询可能不会返回结果。随着嵌入生成,后续查询将返回结果。
from langchain.retrievers import ZepRetriever
zep_retriever = ZepRetriever(
session_id=session_id, # Ensure that you provide the session_id when instantiating the Retriever
url=ZEP_API_URL,
top_k=5,
)
await zep_retriever.aget_relevant_documents("Who wrote Parable of the Sower?")
[Document(page_content='Who was Octavia Butler?', metadata={'score': 0.7759001673780126, 'uuid': '3a82a02f-056e-4c6a-b960-67ebdf3b2b93', 'created_at': '2023-05-25T15:03:30.2041Z', 'role': 'human', 'token_count': 8}),
Document(page_content="Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.", metadata={'score': 0.7602262941130749, 'uuid': 'a2fc9c21-0897-46c8-bef7-6f5c0f71b04a', 'created_at': '2023-05-25T15:03:30.248065Z', 'role': 'ai', 'token_count': 27}),
Document(page_content='Who were her contemporaries?', metadata={'score': 0.757553366415519, 'uuid': '41f9c41a-a205-41e1-b48b-a0a4cd943fc8', 'created_at': '2023-05-25T15:03:30.243995Z', 'role': 'human', 'token_count': 8}),
Document(page_content='Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American science fiction author.', metadata={'score': 0.7546211059317948, 'uuid': '34678311-0098-4f1a-8fd4-5615ac692deb', 'created_at': '2023-05-25T15:03:30.231427Z', 'role': 'ai', 'token_count': 31}),
Document(page_content='Which books of hers were made into movies?', metadata={'score': 0.7496714959247069, 'uuid': '18046c3a-9666-4d3e-b4f0-43d1394732b7', 'created_at': '2023-05-25T15:03:30.236837Z', 'role': 'human', 'token_count': 11})]
我们还可以使用 Zep 同步 API 来获取结果:
zep_retriever.get_relevant_documents("Who wrote Parable of the Sower?")
[Document(page_content='Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.', metadata={'score': 0.8897321402776546, 'uuid': '1c09603a-52c1-40d7-9d69-29f26256029c', 'created_at': '2023-05-25T15:03:30.268257Z', 'role': 'ai', 'token_count': 56}),
Document(page_content="Write a short synopsis of Butler's book, Parable of the Sower. What is it about?", metadata={'score': 0.8857628682610436, 'uuid': 'f6706e8c-6c91-452f-8c1b-9559fd924657', 'created_at': '2023-05-25T15:03:30.265302Z', 'role': 'human', 'token_count': 23}),
Document(page_content='Who was Octavia Butler?', metadata={'score': 0.7759670375149477, 'uuid': '3a82a02f-056e-4c6a-b960-67ebdf3b2b93', 'created_at': '2023-05-25T15:03:30.2041Z', 'role': 'human', 'token_count': 8}),
Document(page_content="Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.", metadata={'score': 0.7602854653476563, 'uuid': 'a2fc9c21-0897-46c8-bef7-6f5c0f71b04a', 'created_at': '2023-05-25T15:03:30.248065Z', 'role': 'ai', 'token_count': 27}),
Document(page_content='You might want to read Ursula K. Le Guin or Joanna Russ.', metadata={'score': 0.7595293992240313, 'uuid': 'f22f2498-6118-4c74-8718-aa89ccd7e3d6', 'created_at': '2023-05-25T15:03:30.261198Z', 'role': 'ai', 'token_count': 18})]