ZepCloudChatMessageHistory
回忆、理解并从聊天记录中提取数据。提供个性化的 AI 体验。
Zep 是一个用于 AI 助手应用的长期记忆服务。使用 Zep,您可以让 AI 助手回忆过去的对话,无论多远,同时还能减少幻觉、延迟和成本。
示例
本 notebook 演示了如何使用 Zep 来持久化聊天历史并将其与您的链结合使用。
from uuid import uuid4
from langchain_community.chat_message_histories import ZepCloudChatMessageHistory
from langchain_community.memory.zep_cloud_memory import ZepCloudMemory
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import (
RunnableParallel,
)
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
session_id = str(uuid4()) # This is a unique identifier for the session
API 参考:ZepCloudChatMessageHistory | ZepCloudMemory | AIMessage | HumanMessage | StrOutputParser | ChatPromptTemplate | MessagesPlaceholder | RunnableParallel | RunnableWithMessageHistory | ChatOpenAI
提供您的 OpenAI 密钥
import getpass
openai_key = getpass.getpass()
提供您的 Zep API 密钥。请参阅 https://help.getzep.com/projects#api-keys
zep_api_key = getpass.getpass()
预加载一些消息到内存中。默认消息窗口是 4 条消息。我们希望超越这个限制来演示自动摘要功能。
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"},
},
]
zep_memory = ZepCloudMemory(
session_id=session_id,
api_key=zep_api_key,
)
for msg in test_history:
zep_memory.chat_memory.add_message(
HumanMessage(content=msg["content"])
if msg["role"] == "human"
else AIMessage(content=msg["content"])
)
import time
time.sleep(
10
) # Wait for the messages to be embedded and summarized, this happens asynchronously.
MessagesPlaceholder - 我们这里使用变量名 chat_history。这将把聊天历史整合到提示中。重要的是,此变量名应与 RunnableWithMessageHistory 链中的 history_messages_key 对齐,以实现无缝集成。
question 必须与 `RunnableWithMessageHistory` 链中的 input_messages_key 匹配。
template = """Be helpful and answer the question below using the provided context:
"""
answer_prompt = ChatPromptTemplate.from_messages(
[
("system", template),
MessagesPlaceholder(variable_name="chat_history"),
("user", "{question}"),
]
)
我们使用 RunnableWithMessageHistory 将 Zep 的聊天历史整合到我们的链中。当您激活链时,此类需要一个 session_id 作为参数。
inputs = RunnableParallel(
{
"question": lambda x: x["question"],
"chat_history": lambda x: x["chat_history"],
},
)
chain = RunnableWithMessageHistory(
inputs | answer_prompt | ChatOpenAI(openai_api_key=openai_key) | StrOutputParser(),
lambda s_id: ZepCloudChatMessageHistory(
session_id=s_id, # This uniquely identifies the conversation, note that we are getting session id as chain configurable field
api_key=zep_api_key,
memory_type="perpetual",
),
input_messages_key="question",
history_messages_key="chat_history",
)
chain.invoke(
{
"question": "What is the book's relevance to the challenges facing contemporary society?"
},
config={"configurable": {"session_id": session_id}},
)
Parent run 622c6f75-3e4a-413d-ba20-558c1fea0d50 not found for run af12a4b1-e882-432d-834f-e9147465faf6. Treating as a root run.
'"Parable of the Sower" is relevant to the challenges facing contemporary society as it explores themes of environmental degradation, economic inequality, social unrest, and the search for hope and community in the face of chaos. The novel\'s depiction of a dystopian future where society has collapsed due to environmental and economic crises serves as a cautionary tale about the potential consequences of our current societal and environmental challenges. By addressing issues such as climate change, social injustice, and the impact of technology on humanity, Octavia Butler\'s work prompts readers to reflect on the pressing issues of our time and the importance of resilience, empathy, and collective action in building a better future.'