š« CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: knowledge graph agent (#566)
Wendong-Fan3a04c946de5d
diff --git a/camel/agents/__init__.py b/camel/agents/__init__.pyindex 8da9d2449..a94c98c09 100644--- a/camel/agents/__init__.py+++ b/camel/agents/__init__.py@@ -15,6 +15,7 @@ from .base import BaseAgentfrom .chat_agent import ChatAgentfrom .critic_agent import CriticAgentfrom .embodied_agent import EmbodiedAgent+from .knowledge_graph_agent import KnowledgeGraphAgentfrom .role_assignment_agent import RoleAssignmentAgentfrom .task_agent import (TaskCreationAgent,@@ -37,4 +38,5 @@ __all__ = ['HuggingFaceToolAgent','EmbodiedAgent','RoleAssignmentAgent',+ 'KnowledgeGraphAgent',]diff --git a/camel/agents/knowledge_graph_agent.py b/camel/agents/knowledge_graph_agent.pynew file mode 100644index 000000000..f6222a3e9--- /dev/null+++ b/camel/agents/knowledge_graph_agent.py@@ -0,0 +1,221 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from typing import Any, Optional, Union++from unstructured.documents.elements import Element++from camel.agents import ChatAgent+from camel.messages import BaseMessage+from camel.prompts import TextPrompt+from camel.storages.graph_storages.graph_element import (+ GraphElement,+ Node,+ Relationship,+)+from camel.types import ModelType, RoleType++text_prompt = """+You are tasked with extracting nodes and relationships from given content and structures them into Node and Relationship objects. Here's the outline of what you needs to do:++Content Extraction:+You should be able to process input content and identify entities mentioned within it.+Entities can be any noun phrases or concepts that represent distinct entities in the context of the given content.++Node Extraction:+For each identified entity, you should create a Node object.+Each Node object should have a unique identifier (id) and a type (type).+Additional properties associated with the node can also be extracted and stored.++Relationship Extraction:+You should identify relationships between entities mentioned in the content.+For each relationship, create a Relationship object.+A Relationship object should have a subject (subj) and an object (obj) which are Node objects representing the entities involved in the relationship.+Each relationship should also have a type (type), and additional properties if applicable.++Output Formatting:+The extracted nodes and relationships should be formatted as instances of the provided Node and Relationship classes.+Ensure that the extracted data adheres to the structure defined by the classes.+Output the structured data in a format that can be easily validated against the provided code.++Instructions for you:+Read the provided content thoroughly.+Identify distinct entities mentioned in the content and categorize them as nodes.+Determine relationships between these entities and represent them as directed relationships.+Provide the extracted nodes and relationships in the specified format below.+Example for you:++Example Content:+"John works at XYZ Corporation. He is a software engineer. The company is located in New York City."++Expected Output:++Nodes:++Node(id='John', type='Person', properties={'agent_generated'})+Node(id='XYZ Corporation', type='Organization', properties={'agent_generated'})+Node(id='New York City', type='Location', properties={'agent_generated'})++Relationships:++Relationship(subj=Node(id='John', type='Person'), obj=Node(id='XYZ Corporation', type='Organization'), type='WorksAt', properties={'agent_generated'})+Relationship(subj=Node(id='John', type='Person'), obj=Node(id='New York City', type='Location'), type='ResidesIn', properties={'agent_generated'})++===== TASK =====+Please extracts nodes and relationships from given content and structures them into Node and Relationship objects.++{task}+"""+++class KnowledgeGraphAgent(ChatAgent):+ r"""An agent that can extract node and relationship information for different entities from given `Element` content.++ Attributes:+ task_prompt (TextPrompt): A prompt for the agent to extract node and+ relationship information for different entities.+ """++ def __init__(+ self,+ model_type: ModelType = ModelType.GPT_3_5_TURBO,+ model_config: Optional[Any] = None,+ ) -> None:+ r"""Initialize the `KnowledgeGraphAgent`.++ Args:+ model_type (ModelType, optional): The type of model to use for the+ agent. Defaults to `ModelType.GPT_3_5_TURBO`.+ model_config (Any, optional): The configuration for the model.+ Defaults to `None`.+ """+ system_message = BaseMessage(+ role_name="Graphify",+ role_type=RoleType.ASSISTANT,+ meta_dict=None,+ content="Your mission is to transform unstructured content "+ "intostructured graph data. Extract nodes and relationships with "+ "precision, and let the connections unfold. Your graphs will "+ "illuminate the hidden connections within the chaos of information.",+ )+ super().__init__(system_message, model_type, model_config)++ def run(+ self,+ element: Union[str, Element],+ parse_graph_elements: bool = False,+ ) -> Union[str, GraphElement]:+ r"""Run the agent to extract node and relationship information.++ Args:+ element (Union[str, Element]): The input element or string.+ parse_graph_elements (bool, optional): Whether to parse into+ `GraphElement`. Defaults to `False`.++ Returns:+ Union[str, GraphElement]: The extracted node and relationship+ information. If `parse_graph_elements` is `True` then return `GraphElement`, else return `str`.+ """+ self.reset()+ self.element = element++ knowledge_graph_prompt = TextPrompt(text_prompt)+ knowledge_graph_generation = knowledge_graph_prompt.format(+ task=str(element)+ )++ knowledge_graph_generation_msg = BaseMessage.make_user_message(+ role_name="Graphify", content=knowledge_graph_generation+ )++ response = self.step(input_message=knowledge_graph_generation_msg)++ content = response.msg.content++ if parse_graph_elements:+ content = self._parse_graph_elements(content)++ return content++ def _validate_node(self, node: Node) -> bool:+ r"""Validate if the object is a valid Node.++ Args:+ node (Node): Object to be validated.++ Returns:+ bool: True if the object is a valid Node, False otherwise.+ """+ return (+ isinstance(node, Node)+ and isinstance(node.id, (str, int))+ and isinstance(node.type, str)+ )++ def _validate_relationship(self, relationship: Relationship) -> bool:+ r"""Validate if the object is a valid Relationship.++ Args:+ relationship (Relationship): Object to be validated.++ Returns:+ bool: True if the object is a valid Relationship, False otherwise.+ """+ return (+ isinstance(relationship, Relationship)+ and self._validate_node(relationship.subj)+ and self._validate_node(relationship.obj)+ and isinstance(relationship.type, str)+ )++ def _parse_graph_elements(self, input_string: str) -> GraphElement:+ r"""Parses graph elements from given content.++ Args:+ input_string (str): The input content.++ Returns:+ GraphElement: The parsed graph elements.+ """+ import re++ # Regular expressions to extract nodes and relationships+ node_pattern = r"Node\(id='(.*?)', type='(.*?)', properties=(.*?)\)"+ rel_pattern = r"Relationship\(subj=Node\(id='(.*?)', type='(.*?)'\), obj=Node\(id='(.*?)', type='(.*?)'\), type='(.*?)', properties=\{(.*?)\}\)"++ nodes = {}+ relationships = []++ # Extract nodes+ for match in re.finditer(node_pattern, input_string):+ id, type, properties = match.groups()+ properties = eval(properties)+ if id not in nodes:+ node = Node(id, type, properties)+ if self._validate_node(node):+ nodes[id] = node++ # Extract relationships+ for match in re.finditer(rel_pattern, input_string):+ subj_id, subj_type, obj_id, obj_type, rel_type, properties_str = (+ match.groups()+ )+ properties = eval(properties_str)+ if subj_id in nodes and obj_id in nodes:+ subj = nodes[subj_id]+ obj = nodes[obj_id]+ relationship = Relationship(subj, obj, rel_type, properties)+ if self._validate_relationship(relationship):+ relationships.append(relationship)++ return GraphElement(list(nodes.values()), relationships, self.element)diff --git a/examples/knowledge_graph/knowledge_graph_agent.py b/examples/knowledge_graph/knowledge_graph_agent.pynew file mode 100644index 000000000..fb04c5544--- /dev/null+++ b/examples/knowledge_graph/knowledge_graph_agent.py@@ -0,0 +1,61 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from camel.agents import KnowledgeGraphAgent+from camel.loaders import UnstructuredIO++# Set instance+uio = UnstructuredIO()+kg_agent = KnowledgeGraphAgent()++# Set example text input+text_example = """CAMEL-AI.org is an open-source community dedicated to the study of autonomous and communicative agents.+"""++# Create an element from given text+element_example = uio.create_element_from_text(text=text_example)++# Let KnowledgeGraph Agent extract node and relationship information+ans_str = kg_agent.run(element_example, parse_graph_elements=False)+ans_GraphElement = kg_agent.run(element_example, parse_graph_elements=True)++# Get str output+print(ans_str)++# Get GraphElement output+print(ans_GraphElement)++"""+===============================================================================+Nodes:++Node(id='CAMEL-AI.org', type='Organization', properties={'agent_generated'})+Node(id='community', type='Concept', properties={'agent_generated'})+Node(id='study', type='Concept', properties={'agent_generated'})+Node(id='autonomous agents', type='Concept', properties={'agent_generated'})+Node(id='communicative agents', type='Concept', properties={'agent_generated'})++Relationships:++Relationship(subj=Node(id='CAMEL-AI.org', type='Organization'), obj=Node(id='community', type='Concept'), type='FocusOn', properties={'agent_generated'})+Relationship(subj=Node(id='CAMEL-AI.org', type='Organization'), obj=Node(id='study', type='Concept'), type='FocusOn', properties={'agent_generated'})+Relationship(subj=Node(id='CAMEL-AI.org', type='Organization'), obj=Node(id='autonomous agents', type='Concept'), type='FocusOn', properties={'agent_generated'})+Relationship(subj=Node(id='CAMEL-AI.org', type='Organization'), obj=Node(id='communicative agents', type='Concept'), type='FocusOn', properties={'agent_generated'})+===============================================================================+"""++"""+===============================================================================+GraphElement(nodes=[Node(id='CAMEL-AI.org', type='Organization', properties={'agent_generated'}), Node(id='community', type='Concept', properties={'agent_generated'}), Node(id='study', type='Concept', properties={'agent_generated'}), Node(id='autonomous agents', type='Concept', properties={'agent_generated'}), Node(id='communicative agents', type='Concept', properties={'agent_generated'})], relationships=[Relationship(subj=Node(id='CAMEL-AI.org', type='Organization', properties={'agent_generated'}), obj=Node(id='community', type='Concept', properties={'agent_generated'}), type='FocusOn', properties={"'agent_generated'"}), Relationship(subj=Node(id='CAMEL-AI.org', type='Organization', properties={'agent_generated'}), obj=Node(id='study', type='Concept', properties={'agent_generated'}), type='FocusOn', properties={"'agent_generated'"}), Relationship(subj=Node(id='CAMEL-AI.org', type='Organization', properties={'agent_generated'}), obj=Node(id='autonomous agents', type='Concept', properties={'agent_generated'}), type='FocusOn', properties={"'agent_generated'"}), Relationship(subj=Node(id='CAMEL-AI.org', type='Organization', properties={'agent_generated'}), obj=Node(id='communicative agents', type='Concept', properties={'agent_generated'}), type='FocusOn', properties={"'agent_generated'"})], source=<unstructured.documents.elements.Text object at 0x7fd050e7bd90>)+===============================================================================+"""diff --git a/examples/knowledge_graph/neo4j.py b/examples/knowledge_graph/neo4j.pynew file mode 100644index 000000000..0740d1d97--- /dev/null+++ b/examples/knowledge_graph/neo4j.py@@ -0,0 +1,73 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from camel.storages import Neo4jGraph++# Set Neo4j instance+n4j = Neo4jGraph(+ url="Your Url", username="Your Username", password="Your Password"+)++# Add triplet into database+n4j.add_triplet(subj="CAMEL", obj="multi-agent framework", rel="belongs to")++# Run a Cypher query+print(n4j.query("""MATCH (n) RETURN n AS node"""))++"""+===============================================================================+[{'node': {}}, {'node': {}}, {'node': {'id': 'CAMEL'}}, {'node': {'id': 'multi-agent framework'}}]+===============================================================================+"""++# Get schema from database+print(n4j.get_schema())++"""+===============================================================================+Node properties are the following:+Entity {id: STRING}+Relationship properties are the following:++The relationships are the following:+(:Entity)-[:BELONGS_TO]->(:Entity)+===============================================================================+"""++# Get structured schema from database+print(n4j.get_structured_schema())++"""+===============================================================================+{'node_props': {'Entity': [{'property': 'id', 'type': 'STRING'}]},+ 'rel_props': {}, 'relationships': [{'start': 'Entity', 'type': 'BELONGS_TO',+ 'end': 'Entity'}], 'metadata': {'constraint': [], 'index': [{'id': 0, 'name':+ 'index_343aff4e', 'state': 'ONLINE', 'populationPercent': 100.0, 'type':+ 'LOOKUP', 'entityType': 'NODE', 'labelsOrTypes': None, 'properties': None,+ 'indexProvider': 'token-lookup-1.0', 'owningConstraint': None, 'lastRead':+ neo4j.time.DateTime(2024, 5, 22, 15, 12, 27, 452000000, tzinfo=UTC),+ 'readCount': 675297, 'trackedSince': neo4j.time.DateTime(2024, 3, 17, 6, 31,+ 29, 925000000, tzinfo=UTC), 'options': {'indexProvider': 'token-lookup-1.0',+ 'indexConfig': {}}, 'failureMessage': '', 'createStatement': 'CREATE LOOKUP+ INDEX index_343aff4e FOR (n) ON EACH labels(n)'}, {'id': 1, 'name':+ 'index_f7700477', 'state': 'ONLINE', 'populationPercent': 100.0, 'type':+ 'LOOKUP', 'entityType': 'RELATIONSHIP', 'labelsOrTypes': None, 'properties'+ None, 'indexProvider': 'token-lookup-1.0', 'owningConstraint': None,+ 'lastRead': neo4j.time.DateTime(2024, 5, 22, 15, 9, 41, 917000000,+ tzinfo=UTC), 'readCount': 16, 'trackedSince': neo4j.time.DateTime(2024, 3,+ 17, 6, 31, 29, 939000000, tzinfo=UTC), 'options': {'indexProvider':+ 'token-lookup-1.0', 'indexConfig': {}}, 'failureMessage': '',+ 'createStatement': 'CREATE LOOKUP INDEX index_f7700477 FOR ()-[r]-() ON EACH+ type(r)'}]}}+ ==============================================================================+"""diff --git a/test/agents/test_knowledge_agent.py b/test/agents/test_knowledge_agent.pynew file mode 100644index 000000000..b4d306859--- /dev/null+++ b/test/agents/test_knowledge_agent.py@@ -0,0 +1,60 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from unittest.mock import Mock++from camel.agents import KnowledgeGraphAgent+from camel.storages.graph_storages.graph_element import Node, Relationship++agent = KnowledgeGraphAgent()+++def test_validate_node_valid():+ valid_node = Node(id='test_id', type='test_type', properties={})+ assert agent._validate_node(valid_node)+++def test_validate_node_invalid():+ invalid_node = "not a Node object"+ assert not agent._validate_node(invalid_node)+++def test_validate_relationship_valid():+ valid_relationship = Relationship(+ Node(id='subj_id', type='subj_type', properties={}),+ Node(id='obj_id', type='obj_type', properties={}),+ 'test_type',+ {},+ )+ assert agent._validate_relationship(valid_relationship)+++def test_validate_relationship_invalid():+ invalid_relationship = "not a Relationship object"+ assert not agent._validate_relationship(invalid_relationship)+++def test_parse_graph_elements():+ agent.element = Mock()+ input_string = """+ Node(id='node_id', type='node_type', properties={})+ Relationship(subj=Node(id='subj_id', type='subj_type', properties={}), obj=Node(id='obj_id', type='obj_type', properties={}), type='test_type', properties={})+ """+ expected_nodes = [+ Node(id='node_id', type='node_type', properties={}),+ Node(id='subj_id', type='subj_type', properties={}),+ Node(id='obj_id', type='obj_type', properties={}),+ ]++ result = agent._parse_graph_elements(input_string)+ assert result.nodes == expected_nodes