跳转到内容

聊天摘要记忆缓冲区

注意:此内存示例已弃用,推荐使用更新且更灵活的 Memory 类。请参阅最新文档

ChatSummaryMemoryBuffer 是一个记忆缓冲区,用于存储符合令牌限制的最后X条消息。它还会将聊天记录总结为单条消息。

%pip install llama-index-core
from llama_index.core.memory import ChatSummaryMemoryBuffer
memory = ChatSummaryMemoryBuffer.from_defaults(
token_limit=40000,
# optional set the summary prompt, here's the default:
# summarize_prompt=(
# "The following is a conversation between the user and assistant. "
# "Write a concise summary about the contents of this conversation."
# )
)
from llama_index.core.llms import ChatMessage
chat_history = [
ChatMessage(role="user", content="Hello, how are you?"),
ChatMessage(role="assistant", content="I'm doing well, thank you!"),
]
# put a list of messages
memory.put_messages(chat_history)
# put one message at a time
# memory.put_message(chat_history[0])
# Get the last X messages that fit into a token limit
history = memory.get()
# Get all messages
all_history = memory.get_all()
# clear the memory
memory.reset()

你可以在 .run() 方法中为任何智能体设置记忆。

import os
os.environ["OPENAI_API_KEY"] = "sk-proj-..."
from llama_index.core.agent.workflow import ReActAgent, FunctionAgent
from llama_index.core.workflow import Context
from llama_index.llms.openai import OpenAI
memory = ChatMemoryBuffer.from_defaults(token_limit=40000)
agent = FunctionAgent(tools=[], llm=OpenAI(model="gpt-4o-mini"))
# context to hold the chat history/state
ctx = Context(agent)
resp = await agent.run("Hello, how are you?", ctx=ctx, memory=memory)
print(memory.get_all())
[ChatMessage(role=<MessageRole.USER: 'user'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text='Hello, how are you?')]), ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text="Hello! I'm just a program, so I don't have feelings, but I'm here and ready to help you. How can I assist you today?")])]