技术背景介绍
在AI助手应用中,能够记住和回忆过去的对话是提升用户体验的重要特性之一。通过引入长时记忆,你的AI助手能够在后续对话中参考此前交互内容,从而提供更加智能和个性化的服务。
Zep 是一个长时记忆服务,它能有效地帮助AI助手记住并提升对话质量,减轻了传统方法中的幻觉现象、延迟和成本问题。
核心原理解析
Zep 通过存储和处理对话数据,使AI系统能够在需要时回忆起过去的交互。主要包括以下几个功能:
- 添加对话历史:能够动态地将对话消息添加到存储中。
- 消息自动归档:运行代理并自动将消息添加到存储中。
- 查看丰富化的消息:自动生成对历史对话的总结。
- 对话历史矢量搜索:基于矢量化的对话历史进行搜索,并返回相关结果。
代码实现演示
下面的代码示例演示了如何使用Zep来实现长时记忆。首先,我们需要安装并配置相关依赖,以及初始化Zep和OpenAI的API。
import openai
from uuid import uuid4
from langchain.agents import AgentType, initialize_agent
from langchain.memory import ZepMemory
from langchain_community.retrievers import ZepRetriever
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.tools import Tool
from langchain_openai import OpenAI
# 设置Zep服务器URL
ZEP_API_URL = "http://localhost:8000"
session_id = str(uuid4()) # 用户的唯一标识符
# 提供你的OpenAI Key
import getpass
openai_key = getpass.getpass()
# 提供你的Zep API Key (可选)
zep_api_key = getpass.getpass()
# 初始化Zep Chat Message History类和Agent
search = WikipediaAPIWrapper()
tools = [
Tool(
name="Search",
func=search.run,
description=(
"useful for when you need to search online for answers. You should ask"
" targeted questions"
),
),
]
# 设置Zep Chat History
memory = ZepMemory(
session_id=session_id,
url=ZEP_API_URL,
api_key=zep_api_key,
memory_key="chat_history",
)
# 初始化Agent
llm = OpenAI(temperature=0, openai_api_key=openai_key)
agent_chain = initialize_agent(
tools,
llm,
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
verbose=True,
memory=memory,
)
# 预加载一些消息到内存中
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.", "metadata": {"foo": "bar"}},
]
for msg in test_history:
memory.chat_memory.add_message(
(
HumanMessage(content=msg["content"])
if msg["role"] == "human"
else AIMessage(content=msg["content"])
),
metadata=msg.get("metadata", {}),
)
# 运行agent,将输入和响应自动添加到Zep内存
agent_chain.run(
input="What is the book's relevance to the challenges facing contemporary society?",
)
# 查看内存中的摘要和消息
def print_messages(messages):
for m in messages:
print(m.type, ":\n", m.dict())
print(memory.chat_memory.zep_summary)
print("\n")
print_messages(memory.chat_memory.messages)
# 使用ZepRetriever进行矢量搜索
retriever = ZepRetriever(
session_id=session_id,
url=ZEP_API_URL,
api_key=zep_api_key,
)
search_results = memory.chat_memory.search("who are some famous women sci-fi authors?")
for r in search_results:
if r.dist > 0.8: # 仅打印相似度大于0.8的结果
print(r.message, r.dist)
应用场景分析
- 客户支持系统:利用Zep的长时记忆功能,客户支持系统能够回忆并结合先前对话,提供个性化的服务,提升用户满意度。
- 教育辅导助手:教育领域的AI助手可以记住学生的学习内容、问题和进度,提供更有针对性的辅导。
- 智能助手:个人智能助手可以记住用户的偏好和习惯,提供更加贴心的服务。
实践建议
- 数据安全和隐私:在实际应用中,确保对用户对话数据的安全存储和隐私保护。
- 优化存储结构:根据实际需求,优化Zep的内存和存储配置,以平衡访问速度和存储成本。
- 持续训练模型:随着对话内容的增多,定期更新和训练你的AI模型,确保其在新数据上的准确性和高效性。
如果遇到问题欢迎在评论区交流。
—END—
423




被折叠的 条评论
为什么被折叠?



