|
| 1 | +"""In this example, we set up a single pipeline with two Neo4j writers: |
| 2 | +one for creating the lexical graph (Document and Chunks) |
| 3 | +and another for creating the entity graph (entities and relations derived from the text). |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import asyncio |
| 9 | + |
| 10 | +import neo4j |
| 11 | +from neo4j_graphrag.embeddings.openai import OpenAIEmbeddings |
| 12 | +from neo4j_graphrag.experimental.components.embedder import TextChunkEmbedder |
| 13 | +from neo4j_graphrag.experimental.components.entity_relation_extractor import ( |
| 14 | + LLMEntityRelationExtractor, |
| 15 | +) |
| 16 | +from neo4j_graphrag.experimental.components.kg_writer import Neo4jWriter |
| 17 | +from neo4j_graphrag.experimental.components.lexical_graph import LexicalGraphBuilder |
| 18 | +from neo4j_graphrag.experimental.components.schema import ( |
| 19 | + SchemaBuilder, |
| 20 | + SchemaEntity, |
| 21 | + SchemaProperty, |
| 22 | + SchemaRelation, |
| 23 | +) |
| 24 | +from neo4j_graphrag.experimental.components.text_splitters.fixed_size_splitter import ( |
| 25 | + FixedSizeSplitter, |
| 26 | +) |
| 27 | +from neo4j_graphrag.experimental.components.types import LexicalGraphConfig |
| 28 | +from neo4j_graphrag.experimental.pipeline import Pipeline |
| 29 | +from neo4j_graphrag.experimental.pipeline.pipeline import PipelineResult |
| 30 | +from neo4j_graphrag.llm import LLMInterface, OpenAILLM |
| 31 | + |
| 32 | + |
| 33 | +async def define_and_run_pipeline( |
| 34 | + neo4j_driver: neo4j.Driver, |
| 35 | + llm: LLMInterface, |
| 36 | + lexical_graph_config: LexicalGraphConfig, |
| 37 | + text: str, |
| 38 | +) -> PipelineResult: |
| 39 | + """Define and run the pipeline with the following components: |
| 40 | +
|
| 41 | + - Text Splitter: to split the text into manageable chunks of fixed size |
| 42 | + - Chunk Embedder: to embed the chunks' text |
| 43 | + - Lexical Graph Builder: to build the lexical graph, ie creating the chunk nodes and relationships between them |
| 44 | + - LG KG writer: save the lexical graph to Neo4j |
| 45 | +
|
| 46 | + - Schema Builder: this component takes a list of entities, relationships and |
| 47 | + possible triplets as inputs, validate them and return a schema ready to use |
| 48 | + for the rest of the pipeline |
| 49 | + - LLM Entity Relation Extractor is an LLM-based entity and relation extractor: |
| 50 | + based on the provided schema, the LLM will do its best to identity these |
| 51 | + entities and their relations within the provided text |
| 52 | + - EG KG writer: once entities and relations are extracted, they can be writen |
| 53 | + to a Neo4j database |
| 54 | +
|
| 55 | + """ |
| 56 | + pipe = Pipeline() |
| 57 | + # define the components |
| 58 | + pipe.add_component( |
| 59 | + FixedSizeSplitter(chunk_size=200, chunk_overlap=50), |
| 60 | + "splitter", |
| 61 | + ) |
| 62 | + pipe.add_component(TextChunkEmbedder(embedder=OpenAIEmbeddings()), "chunk_embedder") |
| 63 | + pipe.add_component( |
| 64 | + LexicalGraphBuilder(lexical_graph_config), |
| 65 | + "lexical_graph_builder", |
| 66 | + ) |
| 67 | + pipe.add_component(Neo4jWriter(neo4j_driver), "lg_writer") |
| 68 | + pipe.add_component(SchemaBuilder(), "schema") |
| 69 | + pipe.add_component( |
| 70 | + LLMEntityRelationExtractor( |
| 71 | + llm=llm, |
| 72 | + create_lexical_graph=False, |
| 73 | + ), |
| 74 | + "extractor", |
| 75 | + ) |
| 76 | + pipe.add_component(Neo4jWriter(neo4j_driver), "eg_writer") |
| 77 | + # define the execution order of component |
| 78 | + # and how the output of previous components must be used |
| 79 | + pipe.connect("splitter", "chunk_embedder", input_config={"text_chunks": "splitter"}) |
| 80 | + pipe.connect( |
| 81 | + "chunk_embedder", |
| 82 | + "lexical_graph_builder", |
| 83 | + input_config={"text_chunks": "chunk_embedder"}, |
| 84 | + ) |
| 85 | + pipe.connect( |
| 86 | + "lexical_graph_builder", |
| 87 | + "lg_writer", |
| 88 | + input_config={ |
| 89 | + "graph": "lexical_graph_builder.graph", |
| 90 | + "lexical_graph_config": "lexical_graph_builder.config", |
| 91 | + }, |
| 92 | + ) |
| 93 | + # define the execution order of component |
| 94 | + # and how the output of previous components must be used |
| 95 | + pipe.connect( |
| 96 | + "chunk_embedder", "extractor", input_config={"chunks": "chunk_embedder"} |
| 97 | + ) |
| 98 | + pipe.connect("schema", "extractor", input_config={"schema": "schema"}) |
| 99 | + pipe.connect( |
| 100 | + "extractor", |
| 101 | + "eg_writer", |
| 102 | + input_config={"graph": "extractor"}, |
| 103 | + ) |
| 104 | + # make sure the lexical graph is created before creating the entity graph: |
| 105 | + pipe.connect("lg_writer", "eg_writer", {}) |
| 106 | + # user input: |
| 107 | + # the initial text |
| 108 | + # and the list of entities and relations we are looking for |
| 109 | + pipe_inputs = { |
| 110 | + "splitter": { |
| 111 | + "text": text, |
| 112 | + }, |
| 113 | + "lexical_graph_builder": { |
| 114 | + "document_info": { |
| 115 | + # 'path' can be anything |
| 116 | + "path": "example/lexical_graph_from_text.py" |
| 117 | + }, |
| 118 | + }, |
| 119 | + "schema": { |
| 120 | + "entities": [ |
| 121 | + SchemaEntity( |
| 122 | + label="Person", |
| 123 | + properties=[ |
| 124 | + SchemaProperty(name="name", type="STRING"), |
| 125 | + SchemaProperty(name="place_of_birth", type="STRING"), |
| 126 | + SchemaProperty(name="date_of_birth", type="DATE"), |
| 127 | + ], |
| 128 | + ), |
| 129 | + SchemaEntity( |
| 130 | + label="Organization", |
| 131 | + properties=[ |
| 132 | + SchemaProperty(name="name", type="STRING"), |
| 133 | + SchemaProperty(name="country", type="STRING"), |
| 134 | + ], |
| 135 | + ), |
| 136 | + SchemaEntity( |
| 137 | + label="Field", |
| 138 | + properties=[ |
| 139 | + SchemaProperty(name="name", type="STRING"), |
| 140 | + ], |
| 141 | + ), |
| 142 | + ], |
| 143 | + "relations": [ |
| 144 | + SchemaRelation( |
| 145 | + label="WORKED_ON", |
| 146 | + ), |
| 147 | + SchemaRelation( |
| 148 | + label="WORKED_FOR", |
| 149 | + ), |
| 150 | + ], |
| 151 | + "potential_schema": [ |
| 152 | + ("Person", "WORKED_ON", "Field"), |
| 153 | + ("Person", "WORKED_FOR", "Organization"), |
| 154 | + ], |
| 155 | + }, |
| 156 | + "extractor": { |
| 157 | + "lexical_graph_config": lexical_graph_config, |
| 158 | + }, |
| 159 | + } |
| 160 | + # run the pipeline |
| 161 | + return await pipe.run(pipe_inputs) |
| 162 | + |
| 163 | + |
| 164 | +async def main(driver: neo4j.Driver) -> PipelineResult: |
| 165 | + # optional: define some custom node labels for the lexical graph: |
| 166 | + lexical_graph_config = LexicalGraphConfig( |
| 167 | + id_prefix="example", |
| 168 | + chunk_node_label="TextPart", |
| 169 | + document_node_label="Text", |
| 170 | + ) |
| 171 | + text = """Albert Einstein was a German physicist born in 1879 who |
| 172 | + wrote many groundbreaking papers especially about general relativity |
| 173 | + and quantum mechanics. He worked for many different institutions, including |
| 174 | + the University of Bern in Switzerland and the University of Oxford.""" |
| 175 | + llm = OpenAILLM( |
| 176 | + model_name="gpt-4o", |
| 177 | + model_params={ |
| 178 | + "max_tokens": 1000, |
| 179 | + "response_format": {"type": "json_object"}, |
| 180 | + }, |
| 181 | + ) |
| 182 | + res = await define_and_run_pipeline( |
| 183 | + driver, |
| 184 | + llm, |
| 185 | + lexical_graph_config, |
| 186 | + text, |
| 187 | + ) |
| 188 | + await llm.async_client.close() |
| 189 | + return res |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + with neo4j.GraphDatabase.driver( |
| 194 | + "bolt://localhost:7687", auth=("neo4j", "password") |
| 195 | + ) as driver: |
| 196 | + print(asyncio.run(main(driver))) |
0 commit comments