[graph_memory]: improve delete/add graph memory (#2073)

This commit is contained in:
Mayank
2025-01-03 22:21:05 +05:30
committed by GitHub
parent 542153ad4f
commit 78a2ef41d7
7 changed files with 439 additions and 225 deletions

View File

@@ -85,7 +85,7 @@ NOOP_TOOL = {
RELATIONS_TOOL = {
"type": "function",
"function": {
"name": "establish_relations",
"name": "establish_relationships",
"description": "Establish relationships among the entities based on the provided text.",
"parameters": {
"type": "object",
@@ -99,7 +99,7 @@ RELATIONS_TOOL = {
"type": "string",
"description": "The source entity of the relationship."
},
"relation": {
"relationship": {
"type": "string",
"description": "The relationship between the source and destination entities."
},
@@ -109,9 +109,9 @@ RELATIONS_TOOL = {
},
},
"required": [
"source_entity",
"relation",
"destination_entity",
"source",
"relationship",
"destination",
],
"additionalProperties": False,
},
@@ -262,7 +262,7 @@ RELATIONS_STRUCT_TOOL = {
"type": "string",
"description": "The source entity of the relationship."
},
"relation": {
"relatationship": {
"type": "string",
"description": "The relationship between the source and destination entities."
},
@@ -273,7 +273,7 @@ RELATIONS_STRUCT_TOOL = {
},
"required": [
"source_entity",
"relation",
"relatationship",
"destination_entity",
],
"additionalProperties": False,
@@ -321,3 +321,66 @@ EXTRACT_ENTITIES_STRUCT_TOOL = {
}
}
}
DELETE_MEMORY_STRUCT_TOOL_GRAPH = {
"type": "function",
"function": {
"name": "delete_graph_memory",
"description": "Delete the relationship between two nodes. This function deletes the existing relationship.",
"strict": True,
"parameters": {
"type": "object",
"properties": {
"source": {
"type": "string",
"description": "The identifier of the source node in the relationship.",
},
"relationship": {
"type": "string",
"description": "The existing relationship between the source and destination nodes that needs to be deleted.",
},
"destination": {
"type": "string",
"description": "The identifier of the destination node in the relationship.",
}
},
"required": [
"source",
"relationship",
"destination",
],
"additionalProperties": False,
},
},
}
DELETE_MEMORY_TOOL_GRAPH = {
"type": "function",
"function": {
"name": "delete_graph_memory",
"description": "Delete the relationship between two nodes. This function deletes the existing relationship.",
"parameters": {
"type": "object",
"properties": {
"source": {
"type": "string",
"description": "The identifier of the source node in the relationship.",
},
"relationship": {
"type": "string",
"description": "The existing relationship between the source and destination nodes that needs to be deleted.",
},
"destination": {
"type": "string",
"description": "The identifier of the destination node in the relationship.",
}
},
"required": [
"source",
"relationship",
"destination",
],
"additionalProperties": False,
},
},
}

View File

@@ -43,7 +43,7 @@ CUSTOM_PROMPT
Relationships:
- Use consistent, general, and timeless relationship types.
- Example: Prefer "PROFESSOR" over "BECAME_PROFESSOR."
- Example: Prefer "professor" over "became_professor."
- Relationships should only be established among the entities explicitly mentioned in the user message.
Entity Consistency:
@@ -54,15 +54,41 @@ Strive to construct a coherent and easily understandable knowledge graph by esht
Adhere strictly to these guidelines to ensure high-quality knowledge graph extraction."""
DELETE_RELATIONS_SYSTEM_PROMPT = """
You are a graph memory manager specializing in identifying, managing, and optimizing relationships within graph-based memories. Your primary task is to analyze a list of existing relationships and determine which ones should be deleted based on the new information provided.
Input:
1. Existing Graph Memories: A list of current graph memories, each containing source, relationship, and destination information.
2. New Text: The new information to be integrated into the existing graph structure.
3. Use "USER_ID" as node for any self-references (e.g., "I," "me," "my," etc.) in user messages.
def get_update_memory_prompt(existing_memories, new_memories, template):
return template.format(existing_memories=existing_memories, new_memories=new_memories)
Guidelines:
1. Identification: Use the new information to evaluate existing relationships in the memory graph.
2. Deletion Criteria: Delete a relationship only if it meets at least one of these conditions:
- Outdated or Inaccurate: The new information is more recent or accurate.
- Contradictory: The new information conflicts with or negates the existing information.
3. DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.
4. Comprehensive Analysis:
- Thoroughly examine each existing relationship against the new information and delete as necessary.
- Multiple deletions may be required based on the new information.
5. Semantic Integrity:
- Ensure that deletions maintain or improve the overall semantic structure of the graph.
- Avoid deleting relationships that are NOT contradictory/outdated to the new information.
6. Temporal Awareness: Prioritize recency when timestamps are available.
7. Necessity Principle: Only DELETE relationships that must be deleted and are contradictory/outdated to the new information to maintain an accurate and coherent memory graph.
Note: DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.
def get_update_memory_messages(existing_memories, new_memories):
return [
{
"role": "user",
"content": get_update_memory_prompt(existing_memories, new_memories, UPDATE_GRAPH_PROMPT),
},
]
For example:
Existing Memory: alice -- loves_to_eat -- pizza
New Information: Alice also loves to eat burger.
Do not delete in the above example because there is a possibility that Alice loves to eat both pizza and burger.
Memory Format:
source -- relationship -- destination
Provide a list of deletion instructions, each specifying the relationship to be deleted.
"""
def get_delete_messages(existing_memories_string, data, user_id):
return DELETE_RELATIONS_SYSTEM_PROMPT.replace("USER_ID", user_id), f"Here are the existing memories: {existing_memories_string} \n\n New Information: {data}"