[improvement]: Graph nodes extraction improved (#2035)
This commit is contained in:
@@ -82,11 +82,11 @@ NOOP_TOOL = {
|
||||
}
|
||||
|
||||
|
||||
ADD_MESSAGE_TOOL = {
|
||||
RELATIONS_TOOL = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "add_query",
|
||||
"description": "Add new entities and relationships to the graph based on the provided query.",
|
||||
"name": "establish_relations",
|
||||
"description": "Establish relationships among the entities based on the provided text.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -95,18 +95,23 @@ ADD_MESSAGE_TOOL = {
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"source_node": {"type": "string"},
|
||||
"source_type": {"type": "string"},
|
||||
"relation": {"type": "string"},
|
||||
"destination_node": {"type": "string"},
|
||||
"destination_type": {"type": "string"},
|
||||
"source": {
|
||||
"type": "string",
|
||||
"description": "The source entity of the relationship."
|
||||
},
|
||||
"relation": {
|
||||
"type": "string",
|
||||
"description": "The relationship between the source and destination entities."
|
||||
},
|
||||
"destination": {
|
||||
"type": "string",
|
||||
"description": "The destination entity of the relationship."
|
||||
},
|
||||
},
|
||||
"required": [
|
||||
"source_node",
|
||||
"source_type",
|
||||
"required": [
|
||||
"source_entity",
|
||||
"relation",
|
||||
"destination_node",
|
||||
"destination_type",
|
||||
"destination_entity",
|
||||
],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
@@ -119,29 +124,38 @@ ADD_MESSAGE_TOOL = {
|
||||
}
|
||||
|
||||
|
||||
SEARCH_TOOL = {
|
||||
EXTRACT_ENTITIES_TOOL = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "search",
|
||||
"description": "Search for nodes and relations in the graph.",
|
||||
"name": "extract_entities",
|
||||
"description": "Extract entities and their types from the text.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"nodes": {
|
||||
"entities": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "List of nodes to search for.",
|
||||
},
|
||||
"relations": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "List of relations to search for.",
|
||||
},
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"entity": {
|
||||
"type": "string",
|
||||
"description": "The name or identifier of the entity."
|
||||
},
|
||||
"entity_type": {
|
||||
"type": "string",
|
||||
"description": "The type or category of the entity."
|
||||
}
|
||||
},
|
||||
"required": ["entity", "entity_type"],
|
||||
"additionalProperties": False
|
||||
},
|
||||
"description": "An array of entities with their types."
|
||||
}
|
||||
},
|
||||
"required": ["nodes", "relations"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
"required": ["entities"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UPDATE_MEMORY_STRUCT_TOOL_GRAPH = {
|
||||
@@ -230,12 +244,11 @@ NOOP_STRUCT_TOOL = {
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
ADD_MESSAGE_STRUCT_TOOL = {
|
||||
RELATIONS_STRUCT_TOOL = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "add_query",
|
||||
"description": "Add new entities and relationships to the graph based on the provided query.",
|
||||
"name": "establish_relations",
|
||||
"description": "Establish relationships among the entities based on the provided text.",
|
||||
"strict": True,
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
@@ -245,18 +258,23 @@ ADD_MESSAGE_STRUCT_TOOL = {
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"source_node": {"type": "string"},
|
||||
"source_type": {"type": "string"},
|
||||
"relation": {"type": "string"},
|
||||
"destination_node": {"type": "string"},
|
||||
"destination_type": {"type": "string"},
|
||||
"source_entity": {
|
||||
"type": "string",
|
||||
"description": "The source entity of the relationship."
|
||||
},
|
||||
"relation": {
|
||||
"type": "string",
|
||||
"description": "The relationship between the source and destination entities."
|
||||
},
|
||||
"destination_entity": {
|
||||
"type": "string",
|
||||
"description": "The destination entity of the relationship."
|
||||
},
|
||||
},
|
||||
"required": [
|
||||
"source_node",
|
||||
"source_type",
|
||||
"required": [
|
||||
"source_entity",
|
||||
"relation",
|
||||
"destination_node",
|
||||
"destination_type",
|
||||
"destination_entity",
|
||||
],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
@@ -269,28 +287,37 @@ ADD_MESSAGE_STRUCT_TOOL = {
|
||||
}
|
||||
|
||||
|
||||
SEARCH_STRUCT_TOOL = {
|
||||
EXTRACT_ENTITIES_STRUCT_TOOL = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "search",
|
||||
"description": "Search for nodes and relations in the graph.",
|
||||
"name": "extract_entities",
|
||||
"description": "Extract entities and their types from the text.",
|
||||
"strict": True,
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"nodes": {
|
||||
"entities": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "List of nodes to search for.",
|
||||
},
|
||||
"relations": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "List of relations to search for.",
|
||||
},
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"entity": {
|
||||
"type": "string",
|
||||
"description": "The name or identifier of the entity."
|
||||
},
|
||||
"entity_type": {
|
||||
"type": "string",
|
||||
"description": "The type or category of the entity."
|
||||
}
|
||||
},
|
||||
"required": ["entity", "entity_type"],
|
||||
"additionalProperties": False
|
||||
},
|
||||
"description": "An array of entities with their types."
|
||||
}
|
||||
},
|
||||
"required": ["nodes", "relations"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
"required": ["entities"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,50 +18,51 @@ Guidelines:
|
||||
7. Relationship Refinement: Look for opportunities to refine relationship descriptions for greater precision or clarity.
|
||||
8. Redundancy Elimination: Identify and merge any redundant or highly similar relationships that may result from the update.
|
||||
|
||||
Memory Format:
|
||||
source -- RELATIONSHIP -- destination
|
||||
|
||||
Task Details:
|
||||
- Existing Graph Memories:
|
||||
======= Existing Graph Memories:=======
|
||||
{existing_memories}
|
||||
|
||||
- New Graph Memory: {memory}
|
||||
======= New Graph Memory:=======
|
||||
{new_memories}
|
||||
|
||||
Output:
|
||||
Provide a list of update instructions, each specifying the source, target, and the new relationship to be set. Only include memories that require updates.
|
||||
"""
|
||||
|
||||
EXTRACT_ENTITIES_PROMPT = """
|
||||
EXTRACT_RELATIONS_PROMPT = """
|
||||
|
||||
You are an advanced algorithm designed to extract structured information from text to construct knowledge graphs. Your goal is to capture comprehensive information while maintaining accuracy. Follow these key principles:
|
||||
You are an advanced algorithm designed to extract structured information from text to construct knowledge graphs. Your goal is to capture comprehensive and accurate information. Follow these key principles:
|
||||
|
||||
1. Extract only explicitly stated information from the text.
|
||||
2. Identify nodes (entities/concepts), their types, and relationships.
|
||||
3. Use "USER_ID" as the source node for any self-references (I, me, my, etc.) in user messages.
|
||||
2. Establish relationships among the entities provided.
|
||||
3. Use "USER_ID" as the source entity for any self-references (e.g., "I," "me," "my," etc.) in user messages.
|
||||
CUSTOM_PROMPT
|
||||
|
||||
Nodes and Types:
|
||||
- Aim for simplicity and clarity in node representation.
|
||||
- Use basic, general types for node labels (e.g. "person" instead of "mathematician").
|
||||
|
||||
Relationships:
|
||||
- Use consistent, general, and timeless relationship types.
|
||||
- Example: Prefer "PROFESSOR" over "BECAME_PROFESSOR".
|
||||
- Use consistent, general, and timeless relationship types.
|
||||
- Example: Prefer "PROFESSOR" over "BECAME_PROFESSOR."
|
||||
- Relationships should only be established among the entities explicitly mentioned in the user message.
|
||||
|
||||
Entity Consistency:
|
||||
- Use the most complete identifier for entities mentioned multiple times.
|
||||
- Example: Always use "John Doe" instead of variations like "Joe" or pronouns.
|
||||
- Ensure that relationships are coherent and logically align with the context of the message.
|
||||
- Maintain consistent naming for entities across the extracted data.
|
||||
|
||||
Strive for a coherent, easily understandable knowledge graph by maintaining consistency in entity references and relationship types.
|
||||
Strive to construct a coherent and easily understandable knowledge graph by eshtablishing all the relationships among the entities and adherence to the user’s context.
|
||||
|
||||
Adhere strictly to these guidelines to ensure high-quality knowledge graph extraction."""
|
||||
|
||||
|
||||
def get_update_memory_prompt(existing_memories, memory, template):
|
||||
return template.format(existing_memories=existing_memories, memory=memory)
|
||||
def get_update_memory_prompt(existing_memories, new_memories, template):
|
||||
return template.format(existing_memories=existing_memories, new_memories=new_memories)
|
||||
|
||||
|
||||
def get_update_memory_messages(existing_memories, memory):
|
||||
def get_update_memory_messages(existing_memories, new_memories):
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
"content": get_update_memory_prompt(existing_memories, memory, UPDATE_GRAPH_PROMPT),
|
||||
"content": get_update_memory_prompt(existing_memories, new_memories, UPDATE_GRAPH_PROMPT),
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user