在AI助手应用中,能够有效地回忆过去的对话内容是提高个性化体验和减少幻觉现象的重要功能。Zep Cloud Memory是一项长期记忆服务,专为AI助手应用设计。通过Zep,AI助手不仅能够记住久远的对话,还能降低幻觉发生率、提高响应速度,并减少运行成本。
核心原理解析
Zep Cloud Memory利用会话历史记录和向量搜索技术来丰富和查询记忆。它通过自动化的消息存储和审查功能,使AI应用能够智能、快速地查找过去的聊天内容。
代码实现演示
接下来我们将展示如何在聊天机器人中使用Zep作为记忆存储机制。
初始化和配置
from uuid import uuid4
from langchain.agents import AgentType, Tool, initialize_agent
from langchain_community.memory.zep_cloud_memory import ZepCloudMemory
from langchain_community.retrievers import ZepCloudRetriever
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_core.messages import AIMessage, HumanMessage
from langchain_openai import OpenAI
session_id = str(uuid4()) # 为会话生成唯一标识符
# 设置搜索工具
search = WikipediaAPIWrapper()
tools = [
Tool(
name="Search",
func=search.run,
description="用于在线搜索答案的工具。应向其提问明确的问题。",
),
]
# 配置Zep聊天记录
memory = ZepCloudMemory(
session_id=session_id,
api_key='your-zep-api-key', # 替换为你的Zep API密钥
return_messages=True,
memory_key="chat_history",
)
# 初始化代理
llm = OpenAI(
temperature=0,
openai_api_key='your-openai-api-key' # 替换为你的OpenAI API密钥
)
agent_chain = initialize_agent(
tools,
llm,
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
verbose=True,
memory=memory,
)
添加历史数据
我们可以预先加载一些对话信息以展示Zep的自动化总结功能。
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."},
]
for msg in test_history:
memory.chat_memory.add_message(
HumanMessage(content=msg["content"]) if msg["role"] == "human" else AIMessage(content=msg["content"])
)
执行代理并存储信息
在执行代理任务时,输入和响应会自动添加到Zep的记忆库中。
agent_chain.invoke(input="What is the book's relevance to the challenges facing contemporary society?")
检查Zep记忆
可以查看消息的总结和详细的对话内容,包括令牌统计、UUID、时间戳等。
def print_messages(messages):
for m in messages:
print(m.type, ":\n", m.dict())
print(memory.chat_memory.zep_summary)
print("\n")
print("Conversation Facts: ")
facts = memory.chat_memory.zep_facts
for fact in facts:
print(fact + "\n")
print_messages(memory.chat_memory.messages)
Zep记忆的向量搜索功能
Zep的原生向量搜索功能可以大幅提高查询效率,利用ZepRetriever对象就可以实现这一点。
retriever = ZepCloudRetriever(
session_id=session_id,
api_key='your-zep-api-key', # 替换为你的Zep API密钥
)
search_results = memory.chat_memory.search("who are some famous women sci-fi authors?")
for r in search_results:
if r.score > 0.8: # 输出相似度高于0.8的结果
print(r.message, r.score)
应用场景分析
Zep Cloud Memory可以广泛应用于各种AI助手场景中,如客户服务聊天机器人、教育助手、医疗咨询等,提高对话的连续性和上下文理解。
实践建议
为了最大化利用Zep的能力,请确保在实际应用中妥善管理API密钥,并定期更新会话数据以保持最新状态。
如果遇到问题欢迎在评论区交流。
—END—