The hugegraph-llm
is a tool for the implementation and research related to large language models.
This project includes runnable demos, it can also be used as a third-party library.
As we know, graph systems can help large models address challenges like timeliness and hallucination, while large models can help graph systems with cost-related issues.
With this project, we aim to reduce the cost of using graph systems, and decrease the complexity of building knowledge graphs. This project will offer more applications and integration solutions for graph systems and large language models.
- Construct knowledge graph by LLM + HugeGraph
- Use natural language to operate graph databases (Gremlin/Cypher)
- Knowledge graph supplements answer context (GraphRAG)
- python 3.9+
- hugegraph-server 1.2+
-
Start the HugeGraph database, you can run it via Docker/Binary Package. Refer to detailed doc for more guidance (PS: Graph visualization in step8)
-
Clone this project
git clone https://github.com/apache/incubator-hugegraph-ai.git
-
Install hugegraph-python-client and hugegraph_llm
cd ./incubator-hugegraph-ai # better to use virtualenv (source venv/bin/activate) pip install ./hugegraph-python-client pip install -r ./hugegraph-llm/requirements.txt
-
Enter the project directory
cd ./hugegraph-llm/src
-
Start the gradio interactive demo of Graph RAG, you can run with the following command, and open http://127.0.0.1:8001 after starting
python3 -m hugegraph_llm.demo.rag_demo.app
The default host is
0.0.0.0
and the port is8001
. You can change them by passing command line arguments--host
and--port
.python3 -m hugegraph_llm.demo.rag_demo.app --host 127.0.0.1 --port 18001
-
Or start the gradio interactive demo of Text2Gremlin, you can run with the following command, and open http://127.0.0.1:8002 after starting. You can also change the default host
0.0.0.0
and port8002
as above. (🚧ing)python3 -m hugegraph_llm.demo.gremlin_generate_web_demo
-
After running the web demo, the config file
.env
will be automatically generated. You can modify its content on the web page. Or modify the file directly and restart the web application.(Optional)To regenerate the config file, you can use
config.generate
with-u
or--update
.python3 -m hugegraph_llm.config.generate --update
-
(Optional) You could use hugegraph-hubble to visit the graph data, could run it via Docker/Docker-Compose for guidance. (Hubble is a graph-analysis dashboard include data loading/schema management/graph traverser/display).
Run example like python3 ./hugegraph_llm/examples/build_kg_test.py
The KgBuilder
class is used to construct a knowledge graph. Here is a brief usage guide:
-
Initialization: The
KgBuilder
class is initialized with an instance of a language model. This can be obtained from theLLMs
class.from hugegraph_llm.models.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="xxx").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.
Run example like python3 ./hugegraph_llm/examples/graph_rag_test.py
The RAGPipeline
class is used to integrate HugeGraph with large language models to provide retrieval-augmented generation capabilities.
Here is a brief usage guide:
-
Extract Keyword:: Extract keywords and expand synonyms.
graph_rag.extract_keywords(text="Tell me about Al Pacino.").print_result()
-
Query Graph for Rag: Retrieve the corresponding keywords and their multi-degree associated relationships from HugeGraph.
graph_rag.query_graphdb(max_deep=2, max_items=30).print_result()
-
Synthesize Answer: Summarize the results and organize the language to answer the question.
graph_rag.synthesize_answer().print_result()
-
Run: The
run
method is used to execute the above operations.graph_rag.run(verbose=True)