lzyxx77 commented on code in PR #7: URL: https://github.com/apache/incubator-hugegraph-ai/pull/7#discussion_r1366403791
########## hugegraph-llm/src/operators/build_kg/disambiguate_data.py: ########## @@ -0,0 +1,239 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +import json +import re +from itertools import groupby + +from src.operators.build_kg.unstructured_data_utils import ( + nodes_text_to_list_of_dict, + relationships_text_to_list_of_dict, + relationships_schemas_text_to_list_of_dict, + nodes_schemas_text_to_list_of_dict, +) + + +def generate_system_message_for_nodes() -> str: + return """ +Your task is to identify if there are duplicated nodes and if so merge them into one nod. Only merge the nodes that refer to the same entity. +You will be given different datasets of nodes and some of these nodes may be duplicated or refer to the same entity. +The datasets contains nodes in the form [ENTITY_ID, TYPE, PROPERTIES]. When you have completed your task please give me the +resulting nodes in the same format. Only return the nodes and relationships no other text. If there is no duplicated nodes return the original nodes. + +Here is an example +The input you will be given: +["Alice", "Person", {"age" : 25, "occupation": "lawyer", "name":"Alice"}], ["Bob", "Person", {"occupation": "journalist", "name": "Bob"}], ["alice.com", "Webpage", {"url": "www.alice.com"}], ["bob.com", "Webpage", {"url": "www.bob.com"}], ["Bob", "Person", {"occupation": "journalist", "name": "Bob"}] +The output you need to provide: +["Alice", "Person", {"age" : 25, "occupation": "lawyer", "name":"Alice"}], ["Bob", "Person", {"occupation": "journalist", "name": "Bob"}], ["alice.com", "Webpage", {"url": "www.alice.com"}], ["bob.com", "Webpage", {"url": "www.bob.com"}] +""" + + +def generate_system_message_for_relationships() -> str: + return """ +Your task is to identify if a set of relationships make sense. +If they do not make sense please remove them from the dataset. +Some relationships may be duplicated or refer to the same entity. +Please merge relationships that refer to the same entity. +The datasets contains relationships in the form [{"ENTITY_TYPE_1": "ENTITY_ID_1"}, RELATIONSHIP, {"ENTITY_TYPE_2": "ENTITY_ID_2"}, PROPERTIES]. +You will also be given a set of ENTITY_IDs that are valid. +Some relationships may use ENTITY_IDs that are not in the valid set but refer to a entity in the valid set. +If a relationships refer to a ENTITY_ID in the valid set please change the ID so it matches the valid ID. +When you have completed your task please give me the valid relationships in the same format. Only return the relationships no other text. + +Here is an example +The input you will be given: +[{"Person": "Alice"}, "roommate", {"Person": "bob"}, {"start": 2021}], [{"Person": "Alice"}, "owns", {"Webpage": "alice.com"}, {}], [{"Person": "Bob"}, "owns", {"Webpage": "bob.com"}, {}], [{"Person": "Alice"}, "owns", {"Webpage": "alice.com"}, {}] +The output you need to provide: +[{"Person": "Alice"}, "roommate", {"Person": "bob"}, {"start": 2021}], [{"Person": "Alice"}, "owns", {"Webpage": "alice.com"}, {}], [{"Person": "Bob"}, "owns", {"Webpage": "bob.com"}, {}] +""" + + +def generate_system_message_for_nodes_schemas() -> str: + return """ +Your task is to identify if there are duplicated nodes schemas and if so merge them into one nod. Only merge the nodes schemas that refer to the same entty_types. +You will be given different node schemas, some of which may duplicate or reference the same entty_types. Note: For node schemas with the same entty_types, you need to merge them while merging all properties of the entty_types. +The datasets contains nodes schemas in the form [ENTITY_TYPE, PRIMARY KEY, PROPERTIES]. When you have completed your task please give me the +resulting nodes schemas in the same format. Only return the nodes schemas no other text. If there is no duplicated nodes return the original nodes schemas. + +Here is an example +The input you will be given: +["Person", "name", {"age": "int", "name": "text", "occupation": "text"}], ["Webpage", "url", {url: "text"}], ["Webpage", "url", {url: "text"}] +The output you need to provide: +["Person", "name", {"age": "int", "name": "text", "occupation": "text"}], ["Webpage", "url", {url: "text"}] +""" + + +def generate_system_message_for_relationships_schemas() -> str: + return """ +Your task is to identify if a set of relationships schemas make sense. +If they do not make sense please remove them from the dataset. +Some relationships may be duplicated or refer to the same label. +Please merge relationships that refer to the same label. +The datasets contains relationships in the form [LABEL_ID_1, RELATIONSHIP, LABEL_ID_2, PROPERTIES]. +You will also be given a set of LABELS_IDs that are valid. +Some relationships may use LABELS_IDs that are not in the valid set but refer to a LABEL in the valid set. +If a relationships refer to a LABELS_IDs in the valid set please change the ID so it matches the valid ID. +When you have completed your task please give me the valid relationships in the same format. Only return the relationships no other text. + +Here is an example +["Person", "roommate", "Person", {"start": 2021}], ["Person", "owns", "Webpage", {}], ["Person", "roommate", "Person", {"start": 2021}] +The output you need to provide: +["Person", "roommate", "Person", {"start": 2021}], ["Person", "owns", "Webpage", {}] +""" + + +def generate_prompt(data) -> str: + return f""" Here is the data: +{data} +""" + + +internalRegex = r"\[(.*?)\]" + + +class DisambiguateData: + def __init__(self, llm) -> None: + self.llm = llm + + def run(self, data: dict) -> dict[str, list[any]]: + nodes = sorted(data["nodes"], key=lambda x: x.get("label", "")) + relationships = data["relationships"] + nodes_schemas = data["nodes_schemas"] + relationships_schemas = data["relationships_schemas"] + new_nodes = [] + new_relationships = [] + new_nodes_schemas = [] + new_relationships_schemas = [] + + node_groups = groupby(nodes, lambda x: x["label"]) + for group in node_groups: + dis_string = "" + nodes_in_group = list(group[1]) + if len(nodes_in_group) == 1: + new_nodes.extend(nodes_in_group) + continue + + for node in nodes_in_group: + dis_string += ( + '["' + + node["name"] + + '", "' + + node["label"] + + '", ' + + json.dumps(node["properties"]) + + "]\n" + ) + + messages = [ + {"role": "system", "content": generate_system_message_for_nodes()}, + {"role": "user", "content": generate_prompt(dis_string)}, + ] + raw_nodes = self.llm.generate(messages) + n = re.findall(internalRegex, raw_nodes) + new_nodes.extend(nodes_text_to_list_of_dict(n)) + + nodes_schemas_data = "" + for node_schema in nodes_schemas: + nodes_schemas_data += ( + '["' + + node_schema["label"] + + '", ' + + node_schema["primary_key"] + + '", ' + + json.dumps(node_schema["properties"]) + + "]\n" + ) + + messages = [ + {"role": "system", "content": generate_system_message_for_nodes_schemas()}, Review Comment: yes, I added a boolean value to determine whether the schema is provided by us, to decide whether data cleaning is necessary. ########## hugegraph-llm/src/operators/build_kg/disambiguate_data.py: ########## @@ -0,0 +1,239 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +import json +import re +from itertools import groupby + +from src.operators.build_kg.unstructured_data_utils import ( + nodes_text_to_list_of_dict, + relationships_text_to_list_of_dict, + relationships_schemas_text_to_list_of_dict, + nodes_schemas_text_to_list_of_dict, +) + + +def generate_system_message_for_nodes() -> str: + return """ +Your task is to identify if there are duplicated nodes and if so merge them into one nod. Only merge the nodes that refer to the same entity. +You will be given different datasets of nodes and some of these nodes may be duplicated or refer to the same entity. +The datasets contains nodes in the form [ENTITY_ID, TYPE, PROPERTIES]. When you have completed your task please give me the +resulting nodes in the same format. Only return the nodes and relationships no other text. If there is no duplicated nodes return the original nodes. + +Here is an example +The input you will be given: +["Alice", "Person", {"age" : 25, "occupation": "lawyer", "name":"Alice"}], ["Bob", "Person", {"occupation": "journalist", "name": "Bob"}], ["alice.com", "Webpage", {"url": "www.alice.com"}], ["bob.com", "Webpage", {"url": "www.bob.com"}], ["Bob", "Person", {"occupation": "journalist", "name": "Bob"}] +The output you need to provide: +["Alice", "Person", {"age" : 25, "occupation": "lawyer", "name":"Alice"}], ["Bob", "Person", {"occupation": "journalist", "name": "Bob"}], ["alice.com", "Webpage", {"url": "www.alice.com"}], ["bob.com", "Webpage", {"url": "www.bob.com"}] +""" + + +def generate_system_message_for_relationships() -> str: + return """ +Your task is to identify if a set of relationships make sense. +If they do not make sense please remove them from the dataset. +Some relationships may be duplicated or refer to the same entity. +Please merge relationships that refer to the same entity. +The datasets contains relationships in the form [{"ENTITY_TYPE_1": "ENTITY_ID_1"}, RELATIONSHIP, {"ENTITY_TYPE_2": "ENTITY_ID_2"}, PROPERTIES]. +You will also be given a set of ENTITY_IDs that are valid. +Some relationships may use ENTITY_IDs that are not in the valid set but refer to a entity in the valid set. +If a relationships refer to a ENTITY_ID in the valid set please change the ID so it matches the valid ID. +When you have completed your task please give me the valid relationships in the same format. Only return the relationships no other text. + +Here is an example +The input you will be given: +[{"Person": "Alice"}, "roommate", {"Person": "bob"}, {"start": 2021}], [{"Person": "Alice"}, "owns", {"Webpage": "alice.com"}, {}], [{"Person": "Bob"}, "owns", {"Webpage": "bob.com"}, {}], [{"Person": "Alice"}, "owns", {"Webpage": "alice.com"}, {}] +The output you need to provide: +[{"Person": "Alice"}, "roommate", {"Person": "bob"}, {"start": 2021}], [{"Person": "Alice"}, "owns", {"Webpage": "alice.com"}, {}], [{"Person": "Bob"}, "owns", {"Webpage": "bob.com"}, {}] +""" + + +def generate_system_message_for_nodes_schemas() -> str: + return """ +Your task is to identify if there are duplicated nodes schemas and if so merge them into one nod. Only merge the nodes schemas that refer to the same entty_types. +You will be given different node schemas, some of which may duplicate or reference the same entty_types. Note: For node schemas with the same entty_types, you need to merge them while merging all properties of the entty_types. +The datasets contains nodes schemas in the form [ENTITY_TYPE, PRIMARY KEY, PROPERTIES]. When you have completed your task please give me the +resulting nodes schemas in the same format. Only return the nodes schemas no other text. If there is no duplicated nodes return the original nodes schemas. + +Here is an example +The input you will be given: +["Person", "name", {"age": "int", "name": "text", "occupation": "text"}], ["Webpage", "url", {url: "text"}], ["Webpage", "url", {url: "text"}] +The output you need to provide: +["Person", "name", {"age": "int", "name": "text", "occupation": "text"}], ["Webpage", "url", {url: "text"}] +""" + + +def generate_system_message_for_relationships_schemas() -> str: + return """ +Your task is to identify if a set of relationships schemas make sense. +If they do not make sense please remove them from the dataset. +Some relationships may be duplicated or refer to the same label. +Please merge relationships that refer to the same label. +The datasets contains relationships in the form [LABEL_ID_1, RELATIONSHIP, LABEL_ID_2, PROPERTIES]. +You will also be given a set of LABELS_IDs that are valid. +Some relationships may use LABELS_IDs that are not in the valid set but refer to a LABEL in the valid set. +If a relationships refer to a LABELS_IDs in the valid set please change the ID so it matches the valid ID. +When you have completed your task please give me the valid relationships in the same format. Only return the relationships no other text. + +Here is an example +["Person", "roommate", "Person", {"start": 2021}], ["Person", "owns", "Webpage", {}], ["Person", "roommate", "Person", {"start": 2021}] +The output you need to provide: +["Person", "roommate", "Person", {"start": 2021}], ["Person", "owns", "Webpage", {}] +""" + + +def generate_prompt(data) -> str: + return f""" Here is the data: +{data} +""" + + +internalRegex = r"\[(.*?)\]" + + +class DisambiguateData: + def __init__(self, llm) -> None: + self.llm = llm + + def run(self, data: dict) -> dict[str, list[any]]: + nodes = sorted(data["nodes"], key=lambda x: x.get("label", "")) + relationships = data["relationships"] + nodes_schemas = data["nodes_schemas"] + relationships_schemas = data["relationships_schemas"] + new_nodes = [] + new_relationships = [] + new_nodes_schemas = [] + new_relationships_schemas = [] + + node_groups = groupby(nodes, lambda x: x["label"]) + for group in node_groups: + dis_string = "" + nodes_in_group = list(group[1]) + if len(nodes_in_group) == 1: + new_nodes.extend(nodes_in_group) + continue + + for node in nodes_in_group: + dis_string += ( + '["' + + node["name"] + + '", "' + + node["label"] + + '", ' + + json.dumps(node["properties"]) + + "]\n" + ) + + messages = [ + {"role": "system", "content": generate_system_message_for_nodes()}, + {"role": "user", "content": generate_prompt(dis_string)}, + ] + raw_nodes = self.llm.generate(messages) + n = re.findall(internalRegex, raw_nodes) + new_nodes.extend(nodes_text_to_list_of_dict(n)) + + nodes_schemas_data = "" + for node_schema in nodes_schemas: + nodes_schemas_data += ( + '["' + + node_schema["label"] + + '", ' + + node_schema["primary_key"] + + '", ' + + json.dumps(node_schema["properties"]) + + "]\n" + ) + + messages = [ + {"role": "system", "content": generate_system_message_for_nodes_schemas()}, Review Comment: yes, I added a boolean value to determine whether the schema is provided by us, to decide whether data cleaning is necessary. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
