Skip to content

Commit c415e30

Browse files
doc: update llm readme (#43)
* docs: update llm readme * Update hugegraph-llm/README.md Co-authored-by: imbajin <jin@apache.org> * Update hugegraph-llm/README.md --------- Co-authored-by: imbajin <jin@apache.org>
1 parent 25711ba commit c415e30

File tree

1 file changed

+92
-41
lines changed

1 file changed

+92
-41
lines changed

hugegraph-llm/README.md

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,120 @@ graph systems and large language models.
1515
2. Use natural language to operate graph databases (gremlin)
1616
3. Knowledge graph supplements answer context (RAG)
1717

18+
## Environment Requirements
19+
20+
- python 3.8+
21+
- hugegraph 1.0.0+
22+
23+
## Preparation
24+
25+
- Start the HugeGraph database, you can do it via Docker. Refer to [docker-link](https://hub.docker.com/r/hugegraph/hugegraph) & [deploy-doc](https://hugegraph.apache.org/docs/quickstart/hugegraph-server/#31-use-docker-container-convenient-for-testdev) for guidance
26+
- Start the gradio interactive demo, you can start with the following command, and open http://127.0.0.1:8001 after starting
27+
```bash
28+
# ${PROJECT_ROOT_DIR} is the root directory of hugegraph-ai, which needs to be configured by yourself
29+
export PYTHONPATH=${PROJECT_ROOT_DIR}/hugegraph-llm/src:${PROJECT_ROOT_DIR}/hugegraph-python-client/src
30+
python3 ./hugegraph-llm/src/hugegraph_llm/utils/gradio_demo.py
31+
```
32+
- Configure HugeGraph database connection information and LLM information, which can be configured in two ways:
33+
1. Configure the `./hugegraph-llm/src/config/config.ini` file
34+
2. In gradio, after completing the configurations for LLM and HugeGraph, click on `Initialize configs`, the complete and initialized configuration file will be outputted.
35+
- offline download NLTK stopwords
36+
```bash
37+
python3 ./hugegraph_llm/operators/common_op/nltk_helper.py
38+
```
39+
1840
## Examples
1941

20-
### Examples (knowledge graph construction by llm)
42+
### 1.Build a knowledge graph in HugeGraph through LLM
2143

22-
1. Start the HugeGraph database, you can do it via Docker. Refer to this [link](https://hub.docker.com/r/hugegraph/hugegraph) for guidance
23-
2. Run example like `python hugegraph-llm/examples/build_kg_test.py`
24-
25-
> Note: If you need a proxy to access OpenAI's API, please set your HTTP proxy in `build_kg_test.py`.
44+
Run example like `python3 ./hugegraph-llm/examples/build_kg_test.py`
2645

2746
The `KgBuilder` class is used to construct a knowledge graph. Here is a brief usage guide:
2847

2948
1. **Initialization**: The `KgBuilder` class is initialized with an instance of a language model. This can be obtained from the `LLMs` class.
3049

31-
```python
32-
from hugegraph_llm.llms.init_llm import LLMs
33-
from hugegraph_llm.operators.kg_construction_task import KgBuilder
34-
35-
TEXT = ""
36-
builder = KgBuilder(LLMs().get_llm())
37-
(
38-
builder
39-
.import_schema(from_hugegraph="talent_graph").print_result()
40-
.extract_triples(TEXT).print_result()
41-
.disambiguate_word_sense().print_result()
42-
.commit_to_hugegraph()
43-
.run()
44-
)
45-
```
50+
```python
51+
from hugegraph_llm.llms.init_llm import LLMs
52+
from hugegraph_llm.operators.kg_construction_task import KgBuilder
53+
54+
TEXT = ""
55+
builder = KgBuilder(LLMs().get_llm())
56+
(
57+
builder
58+
.import_schema(from_hugegraph="talent_graph").print_result()
59+
.extract_triples(TEXT).print_result()
60+
.disambiguate_word_sense().print_result()
61+
.commit_to_hugegraph()
62+
.run()
63+
)
64+
```
4665

4766
2. **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 method `print_result` can be chained to print the result.
4867

49-
```python
50-
# Import schema from a HugeGraph instance
51-
import_schema(from_hugegraph="xxx").print_result()
52-
# Import schema from an extraction result
53-
import_schema(from_extraction="xxx").print_result()
54-
# Import schema from user-defined schema
55-
import_schema(from_user_defined="xxx").print_result()
56-
```
68+
```python
69+
# Import schema from a HugeGraph instance
70+
import_schema(from_hugegraph="xxx").print_result()
71+
# Import schema from an extraction result
72+
import_schema(from_extraction="xxx").print_result()
73+
# Import schema from user-defined schema
74+
import_schema(from_user_defined="xxx").print_result()
75+
```
5776

5877
3. **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.
5978

60-
```python
61-
TEXT = "Meet Sarah, a 30-year-old attorney, and her roommate, James, whom she's shared a home with since 2010."
62-
extract_triples(TEXT).print_result()
63-
```
79+
```python
80+
TEXT = "Meet Sarah, a 30-year-old attorney, and her roommate, James, whom she's shared a home with since 2010."
81+
extract_triples(TEXT).print_result()
82+
```
6483

6584
4. **Disambiguate Word Sense**: The `disambiguate_word_sense` method is used to disambiguate the sense of words in the extracted triples.
6685

67-
```python
68-
disambiguate_word_sense().print_result()
69-
```
86+
```python
87+
disambiguate_word_sense().print_result()
88+
```
7089

7190
5. **Commit to HugeGraph**: The `commit_to_hugegraph` method is used to commit the constructed knowledge graph to a HugeGraph instance.
7291

73-
```python
74-
commit_to_hugegraph().print_result()
75-
```
92+
```python
93+
commit_to_hugegraph().print_result()
94+
```
7695

7796
6. **Run**: The `run` method is used to execute the chained operations.
7897

79-
```python
80-
run()
81-
```
98+
```python
99+
run()
100+
```
82101

83102
The methods of the `KgBuilder` class can be chained together to perform a sequence of operations.
103+
104+
### 2. Retrieval augmented generation (RAG) based on HugeGraph
105+
106+
Run example like `python3 ./hugegraph-llm/examples/graph_rag_test.py`
107+
108+
The `GraphRAG` class is used to integrate HugeGraph with large language models to provide retrieval-augmented generation capabilities. Here is a brief usage guide:
109+
110+
1. **Extract Keyword:**: Extract keywords and expand synonyms.
111+
112+
```python
113+
graph_rag.extract_keyword(text="Tell me about Al Pacino.").print_result()
114+
```
115+
116+
2. **Query Graph for Rag**: Retrieve the corresponding keywords and their multi-degree associated relationships from HugeGraph.
117+
118+
```python
119+
graph_rag.query_graph_for_rag(
120+
max_deep=2,
121+
max_items=30
122+
).print_result()
123+
```
124+
3. **Synthesize Answer**: Summarize the results and organize the language to answer the question.
125+
126+
```python
127+
graph_rag.synthesize_answer().print_result()
128+
```
129+
130+
4. **Run**: The `run` method is used to execute the above operations.
131+
132+
```python
133+
graph_rag.run(verbose=True)
134+
```

0 commit comments

Comments
 (0)