HugeGraph-AI Quick Start

1 HugeGraph-AI Overview

hugegraph-ai aims to explore the integration of HugeGraph and artificial intelligence (AI), including applications combined with large models, integration with graph machine learning components, etc., to provide comprehensive support for developers to use HugeGraph’s AI capabilities in projects.

2 Environment Requirements

  • python 3.8+
  • hugegraph 1.0.0+

3 Preparation

  • Start the HugeGraph database, you can achieve this through Docker. Please refer to this link for guidance.
  • Start the gradio interactive demo, you can start with the following command, and open after starting
# ${PROJECT_ROOT_DIR} is the root directory of hugegraph-ai, which needs to be configured by yourself
export PYTHONPATH=${PROJECT_ROOT_DIR}/hugegraph-llm/src:${PROJECT_ROOT_DIR}/hugegraph-python-client/src
python3 ./hugegraph-llm/src/hugegraph_llm/utils/
  • Configure HugeGraph database connection information and LLM information, which can be configured in two ways:
    1. Configure the ./hugegraph-llm/src/config/config.ini file
    2. In gradio, after completing the configurations for LLM and HugeGraph, click on Initialize configs, the complete and initialized configuration file will be outputted. gradio-config
  • offline download NLTK stopwords
python3 ./hugegraph_llm/operators/common_op/

4 How to use

4.1 Build a knowledge graph in HugeGraph through LLM

4.1.1 Build a knowledge graph through the gradio interactive interface
  • Parameter description:
    • Text: The input text.
    • Schema: Accepts the following two types of text:
      • User-defined JSON format schema.
      • Specify the name of the HugeGraph graph instance, which will automatically extract the schema of the graph.
    • Disambiguate word sense: Whether to disambiguate word sense.
    • Commit to hugegraph: Whether to submit the constructed knowledge graph to the HugeGraph server


4.1.2 Build a knowledge graph through code
  • Complete code
from hugegraph_llm.llms.init_llm import LLMs
from hugegraph_llm.operators.kg_construction_task import KgBuilder

llm = LLMs().get_llm()
builder = KgBuilder(llm)
  • Sequence Diagram gradio-config
  1. Initialize: Initialize the LLMs instance, get the LLM, and then create a task instance KgBuilder for graph construction. KgBuilder defines multiple operators, and users can freely combine them according to their needs. (tip: print_result() can print the result of each step in the console, without affecting the overall execution logic)
llm = LLMs().get_llm()
builder = KgBuilder(llm)
  1. Import Schema: Import using the import_schema method, which supports three modes:
    • Import from a HugeGraph instance, specify the name of the HugeGraph graph instance, and it will automatically extract the schema of the graph.
    • Import from a user-defined schema, accept user-defined JSON format schema.
    • Import from the extraction result (release soon)
# Import schema from a HugeGraph instance
# Import schema from user-defined schema
# Import schema from an extraction result
  1. Extract triples: Use the extract_triples method to extract triples from the text.
TEXT = "Meet Sarah, a 30-year-old attorney, and her roommate, James, whom she's shared a home with since 2010."
  1. Disambiguate word sense: Use the disambiguate_word_sense method to disambiguate word sense.
  1. Commit to HugeGraph: Use the commit_to_hugegraph method to submit the constructed knowledge graph to the HugeGraph instance.
  1. Run: Use the run method to execute the above operations.

4.2 Retrieval augmented generation (RAG) based on HugeGraph

4.1.1 Interactive Q&A through gradio
  1. First click the Initialize HugeGraph test data button to initialize the HugeGraph data. gradio-config
  2. Then click the Retrieval augmented generation button to generate the answer to the question. gradio-config
4.1.2 Build Graph RAG through code
  • code
graph_rag = GraphRAG()
result = (
    graph_rag.extract_keyword(text="Tell me about Al Pacino.").print_result()
  1. extract_keyword: Extract keywords and expand synonyms.
graph_rag.extract_keyword(text="Tell me about Al Pacino.").print_result()
  1. query_graph_for_rag: Retrieve the corresponding keywords and their multi-degree associated relationships from HugeGraph.
    • max_deep: The maximum depth of hugegraph retrieval.
    • max_items: The maximum number of results returned by hugegraph.
  1. synthesize_answer: Summarize the results and organize the language to answer the question.
  1. run: Execute the above operations.