-
Notifications
You must be signed in to change notification settings - Fork 28
Construct knowledge graph in hugegraph by LLM
Simon Cheung edited this page Jan 31, 2024
·
2 revisions
- Start the HugeGraph database, you can do it via Docker. Refer to this link for guidance
- Run example like
python hugegraph-llm/examples/build_kg_test.py
Note: If you need a proxy to access OpenAI's API, please set your HTTP proxy in
build_kg_test.py
.
-
Initialization: The
KgBuilder
class is initialized with an instance of a language model. This can be obtained from theLLMs
class.
from hugegraph_llm.llms.init_llm import LLMs
from hugegraph_llm.operators.kg_construction_task import KgBuilder
TEXT = ""
builder = KgBuilder(LLMs().get_llm())
(
builder
.import_schema(from_hugegraph="talent_graph").print_result()
.extract_triples(TEXT).print_result()
.disambiguate_word_sense().print_result()
.commit_to_hugegraph()
.run()
)
-
Import Schema: The
import_schema
method is used to import a schema from a source. The source can be a HugeGraph instance,a user-defined schema or an extraction result. The methodprint_result
can be chained to print the result.
# Import schema from a HugeGraph instance
import_schema(from_hugegraph="talent_graph").print_result()
# Import schema from an extraction result
import_schema(from_extraction="xxx").print_result()
# Import schema from user-defined schema
import_schema(from_user_defined="xxx").print_result()
-
Extract Triples: The
extract_triples
method is used to extract triples from a text. The text should be passed as a string argument to the method.
TEXT = "Meet Sarah, a 30-year-old attorney, and her roommate, James, whom she's shared a home with since 2010."
extract_triples(TEXT).print_result()
-
Disambiguate Word Sense: The
disambiguate_word_sense
method is used to disambiguate the sense of words in the extracted triples.
disambiguate_word_sense().print_result()
-
Commit to HugeGraph: The
commit_to_hugegraph
method is used to commit the constructed knowledge graph to a HugeGraph instance.
commit_to_hugegraph().print_result()
-
Run: The
run
method is used to execute the chained operations.
run()
The methods of the KgBuilder
class can be chained together to perform a sequence of operations.
Documentation license here.