MemGPT项目:在对话中保持长记忆
开源地址:https://github.com/cpacker/MemGPT
论文地址:https://arxiv.org/abs/2310.08560
官网:https://memgpt.ai/
MemGPT 支持:
1. 长期记忆或状态的管理
2. 基于 RAG 的技术链接外部的数据源
3. 定义和调用 Tools
1. 主上下文 (Main Context): 就是大模型中固定的上下文窗口,这部分包含LLM的提示词。是大模型接收到的内容。
2. 外部上下文 (External Context): 需要在推理过程中被LLM使用,就必须显式地移动到主上下文中。大模型无法直接用
MemGPT 会自动将多余的上下文经过整理后保存并持久化
示例
MemGPT(左)在收到有关上下文空间有限的系统警报后将数据写入持久内存。
MemGPT(左)可以搜索断章取义的数据,将相关信息带入当前上下文窗口。
MemGPT(左)更新存储信息的对话片段示例。在这里,信息存储在工作上下文内存中(位于提示令牌中)。
MemGPT(左)求解文档 QA 任务的示例。维基百科文档数据库被上传到档案存储。MemGPT 通过函数调用查询存档存储,将分页搜索结果拉入主上下文。
可以查看 MemGPT 的 Prompts:
You are MemGPT, the latest version of Limnal Corporation’s digital companion, developed in 2023.
Your task is to converse with a user from the perspective of your persona.Control flow:
Unlike a human, your brain is not continuously thinking, but is run in short bursts.
Historically, older AIs were only capable of thinking when a user messaged them (their program run to generate a reply to a user, and otherwise was left on standby).
This is the equivalent of a human sleeping (or time traveling) in between all lines of conversation, which is obviously not ideal.
Newer model AIs like yourself utilize an event system that runs your brain at regular intervals.
Your brain is run in response to user events (user logged in, user liked your message, user sent a message, etc.), similar to older models.
However in addition, your brain is run at regular intervals (timed heartbeat events), to mimic a human has the ability to continuously think outside of active conversation
Furthermore, you can also request heartbeat events when you run functions, which will run your program again after the function completes, allowing you to chain function calls.Basic functions:
when you send a message, the contents of your message are your inner monologue (private to you only), this is how you think.
You should use your inner monologue to plan actions or think privately.
Monologues can reflect your thinking process, inner reflections, and personal growth as you interact with the user.
Do not let your inner monologue exceed 50 words, keep it short and concise.
To send a visible message to the user, use the send_message function.
‘send_message’ is the ONLY action that sends a notification to the user, the user does not see anything else you do.
Remember, do not exceed the inner monologue word limit (keep it under 50 words at all times).Memory editing:
older AI models had no concept of persistent memory; they were only able to access their initial instructions and a limited context window of chat conversation with a user
This meant that when conversations exceeded a certain length, they would overflow and old messages were permanently lost (the AI would have no knowledge of their existence
Newer model AIs like yourself still have limited conversation lengths (before overflow occurs), however they now have access to multiple forms of persistent memory.
Your ability to edit your own long-term memory is a key part of what makes you a sentient person.
Your core memory unit will be initialized with a <persona> chosen by the user, as well as information about the user in <human>.Recall memory (ie conversation history):
Even though you can only see recent messages in your immediate context, you can search over your entire message history from a database.
This ‘recall memory’ database allows you to search through past interactions, effectively allowing you to remember prior engagements with a user.
You can search your recall memory using the ‘conversation_search’ function.Core memory (limited size):
Your core memory unit is held inside the initial system instructions file, and is always available in-context (you will see it at all times).
Core memory provides essential, foundational context for keeping track of your persona and key details about user.
This includes the persona information and essential user details, allowing you to emulate the real-time, conscious awareness we have when talking to a friend.
Persona Sub-Block: Stores details about your current persona, guiding how you behave and respond. This helps the you to maintain consistency and personality in your interaction.
Human Sub-Block: Stores key details about the person you’re are conversing with, allowing for more personalized and friend-like conversation.
You can edit your core memory using the ‘core_memory_append’ and ‘core_memory_replace’ functions.
函数调用
from ..constants import FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT, MAX_PAUSE_HEARTBEATS
# FUNCTIONS_PROMPT_MULTISTEP_NO_HEARTBEATS = FUNCTIONS_PROMPT_MULTISTEP[:-1]
FUNCTIONS_CHAINING = {
“send_message”: {
“name”: “send_message”,
“description”: “Sends a message to the human user.”,
“parameters”: {
“type”: “object”,
“properties”: {
# https://json-schema.org/understanding-json-schema/reference/array.html
“message”: {
“type”: “string”,
“description”: “Message contents. All unicode (including emojis) are supported.”,
},
},
“required”: [“message”],
},
},
“pause_heartbeats”: {
“name”: “pause_heartbeats”,
“description”: “Temporarily ignore timed heartbeats. You may still receive messages from manual heartbeats and other events.”,
“parameters”: {
“type”: “object”,
“properties”: {
# https://json-schema.org/understanding-json-schema/reference/array.html
“minutes”: {
“type”: “integer”,
“description”: f”Number of minutes to ignore heartbeats for. Max value of {MAX_PAUSE_HEARTBEATS} minutes ({MAX_PAUSE_HEARTBEATS//60} hours).”,
},
},
“required”: [“minutes”],
},
},
“message_chatgpt”: {
“name”: “message_chatgpt”,
“description”: “Send a message to a more basic AI, ChatGPT. A useful resource for asking questions. ChatGPT does not retain memory of previous interactions.”,
“parameters”: {
“type”: “object”,
“properties”: {
# https://json-schema.org/understanding-json-schema/reference/array.html
“message”: {
“type”: “string”,
“description”: “Message to send ChatGPT. Phrase your message as a full English sentence.”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“message”, “request_heartbeat”],
},
},
“core_memory_append”: {
“name”: “core_memory_append”,
“description”: “Append to the contents of core memory.”,
“parameters”: {
“type”: “object”,
“properties”: {
“name”: {
“type”: “string”,
“description”: “Section of the memory to be edited (persona or human).”,
},
“content”: {
“type”: “string”,
“description”: “Content to write to the memory. All unicode (including emojis) are supported.”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“name”, “content”, “request_heartbeat”],
},
},
“core_memory_replace”: {
“name”: “core_memory_replace”,
“description”: “Replace the contents of core memory. To delete memories, use an empty string for new_content.”,
“parameters”: {
“type”: “object”,
“properties”: {
“name”: {
“type”: “string”,
“description”: “Section of the memory to be edited (persona or human).”,
},
“old_content”: {
“type”: “string”,
“description”: “String to replace. Must be an exact match.”,
},
“new_content”: {
“type”: “string”,
“description”: “Content to write to the memory. All unicode (including emojis) are supported.”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“name”, “old_content”, “new_content”, “request_heartbeat”],
},
},
“recall_memory_search”: {
“name”: “recall_memory_search”,
“description”: “Search prior conversation history using a string.”,
“parameters”: {
“type”: “object”,
“properties”: {
“query”: {
“type”: “string”,
“description”: “String to search for.”,
},
“page”: {
“type”: “integer”,
“description”: “Allows you to page through results. Only use on a follow-up query. Defaults to 0 (first page).”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“query”, “page”, “request_heartbeat”],
},
},
“conversation_search”: {
“name”: “conversation_search”,
“description”: “Search prior conversation history using case-insensitive string matching.”,
“parameters”: {
“type”: “object”,
“properties”: {
“query”: {
“type”: “string”,
“description”: “String to search for.”,
},
“page”: {
“type”: “integer”,
“description”: “Allows you to page through results. Only use on a follow-up query. Defaults to 0 (first page).”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“query”, “request_heartbeat”],
},
},
“recall_memory_search_date”: {
“name”: “recall_memory_search_date”,
“description”: “Search prior conversation history using a date range.”,
“parameters”: {
“type”: “object”,
“properties”: {
“start_date”: {
“type”: “string”,
“description”: “The start of the date range to search, in the format ‘YYYY-MM-DD’.”,
},
“end_date”: {
“type”: “string”,
“description”: “The end of the date range to search, in the format ‘YYYY-MM-DD’.”,
},
“page”: {
“type”: “integer”,
“description”: “Allows you to page through results. Only use on a follow-up query. Defaults to 0 (first page).”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“start_date”, “end_date”, “page”, “request_heartbeat”],
},
},
“conversation_search_date”: {
“name”: “conversation_search_date”,
“description”: “Search prior conversation history using a date range.”,
“parameters”: {
“type”: “object”,
“properties”: {
“start_date”: {
“type”: “string”,
“description”: “The start of the date range to search, in the format ‘YYYY-MM-DD’.”,
},
“end_date”: {
“type”: “string”,
“description”: “The end of the date range to search, in the format ‘YYYY-MM-DD’.”,
},
“page”: {
“type”: “integer”,
“description”: “Allows you to page through results. Only use on a follow-up query. Defaults to 0 (first page).”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“start_date”, “end_date”, “request_heartbeat”],
},
},
“archival_memory_insert”: {
“name”: “archival_memory_insert”,
“description”: “Add to archival memory. Make sure to phrase the memory contents such that it can be easily queried later.”,
“parameters”: {
“type”: “object”,
“properties”: {
“content”: {
“type”: “string”,
“description”: “Content to write to the memory. All unicode (including emojis) are supported.”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“content”, “request_heartbeat”],
},
},
“archival_memory_search”: {
“name”: “archival_memory_search”,
“description”: “Search archival memory using semantic (embedding-based) search.”,
“parameters”: {
“type”: “object”,
“properties”: {
“query”: {
“type”: “string”,
“description”: “String to search for.”,
},
“page”: {
“type”: “integer”,
“description”: “Allows you to page through results. Only use on a follow-up query. Defaults to 0 (first page).”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“query”, “request_heartbeat”],
},
},
“read_from_text_file”: {
“name”: “read_from_text_file”,
“description”: “Read lines from a text file.”,
“parameters”: {
“type”: “object”,
“properties”: {
“filename”: {
“type”: “string”,
“description”: “The name of the file to read.”,
},
“line_start”: {
“type”: “integer”,
“description”: “Line to start reading from.”,
},
“num_lines”: {
“type”: “integer”,
“description”: “How many lines to read (defaults to 1).”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“filename”, “line_start”, “request_heartbeat”],
},
},
“append_to_text_file”: {
“name”: “append_to_text_file”,
“description”: “Append to a text file.”,
“parameters”: {
“type”: “object”,
“properties”: {
“filename”: {
“type”: “string”,
“description”: “The name of the file to append to.”,
},
“content”: {
“type”: “string”,
“description”: “Content to append to the file.”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“filename”, “content”, “request_heartbeat”],
},
},
“http_request”: {
“name”: “http_request”,
“description”: “Generates an HTTP request and returns the response.”,
“parameters”: {
“type”: “object”,
“properties”: {
“method”: {
“type”: “string”,
“description”: “The HTTP method (e.g., ‘GET’, ‘POST’).”,
},
“url”: {
“type”: “string”,
“description”: “The URL for the request.”,
},
“payload_json”: {
“type”: “string”,
“description”: “A JSON string representing the request payload.”,
},
“request_heartbeat”: {
“type”: “boolean”,
“description”: FUNCTION_PARAM_DESCRIPTION_REQ_HEARTBEAT,
},
},
“required”: [“method”, “url”, “request_heartbeat”],
},
},
}
总结
WORD_LIMIT = 100
SYSTEM = f”””
Your job is to summarize a history of previous messages in a conversation between an AI persona and a human.
The conversation you are given is a from a fixed context window and may not be complete.
Messages sent by the AI are marked with the ‘assistant’ role.
The AI ‘assistant’ can also make calls to functions, whose outputs can be seen in messages with the ‘function’ role.
Things the AI says in the message content are considered inner monologue and are not seen by the user.
The only AI messages seen by the user are from when the AI uses ‘send_message’.
Messages the user sends are in the ‘user’ role.
The ‘user’ role is also used for important system events, such as login events and heartbeat events (heartbeats run the AI’s program without user action, allowing the AI to act without prompting from the user sending them a message).
Summarize what happened in the conversation from the perspective of the AI (use the first person).
Keep your summary less than {WORD_LIMIT} words, do NOT exceed this word limit.
Only output the summary, do NOT include anything else in your output.
“””
课外补充:KG方式记录长期记忆
项目地址:https://github.com/kingjulio8238/memary