|
| 1 | +"""This example demonstrates how to use event callback to receive notifications |
| 2 | +about the pipeline progress. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import logging |
| 9 | + |
| 10 | +import neo4j |
| 11 | +from neo4j_graphrag.experimental.components.kg_writer import Neo4jWriter |
| 12 | +from neo4j_graphrag.experimental.components.lexical_graph import LexicalGraphBuilder |
| 13 | +from neo4j_graphrag.experimental.components.text_splitters.fixed_size_splitter import ( |
| 14 | + FixedSizeSplitter, |
| 15 | +) |
| 16 | +from neo4j_graphrag.experimental.pipeline import Pipeline |
| 17 | +from neo4j_graphrag.experimental.pipeline.pipeline import PipelineResult |
| 18 | +from neo4j_graphrag.experimental.pipeline.types import Event |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | +logging.basicConfig() |
| 22 | +logger.setLevel(logging.WARNING) |
| 23 | + |
| 24 | + |
| 25 | +async def event_handler(event: Event) -> None: |
| 26 | + """Function can do anything about the event, |
| 27 | + here we're just logging it if it's a pipeline-level event. |
| 28 | + """ |
| 29 | + if event.event_type.is_pipeline_event: |
| 30 | + logger.warning(event) |
| 31 | + |
| 32 | + |
| 33 | +async def main(neo4j_driver: neo4j.Driver) -> PipelineResult: |
| 34 | + """This is where we define and run the Lexical Graph builder pipeline, instantiating |
| 35 | + a few components: |
| 36 | +
|
| 37 | + - Text Splitter: to split the text into manageable chunks of fixed size |
| 38 | + - Chunk Embedder: to embed the chunks' text |
| 39 | + - Lexical Graph Builder: to build the lexical graph, ie creating the chunk nodes and relationships between them |
| 40 | + - KG writer: save the lexical graph to Neo4j |
| 41 | + """ |
| 42 | + pipe = Pipeline( |
| 43 | + callback=event_handler, |
| 44 | + ) |
| 45 | + # define the components |
| 46 | + pipe.add_component( |
| 47 | + FixedSizeSplitter(chunk_size=300, chunk_overlap=10, approximate=False), |
| 48 | + "splitter", |
| 49 | + ) |
| 50 | + pipe.add_component( |
| 51 | + LexicalGraphBuilder(), |
| 52 | + "lexical_graph_builder", |
| 53 | + ) |
| 54 | + pipe.add_component(Neo4jWriter(neo4j_driver), "writer") |
| 55 | + # define the execution order of component |
| 56 | + # and how the output of previous components must be used |
| 57 | + pipe.connect( |
| 58 | + "splitter", "lexical_graph_builder", input_config={"text_chunks": "splitter"} |
| 59 | + ) |
| 60 | + pipe.connect( |
| 61 | + "lexical_graph_builder", |
| 62 | + "writer", |
| 63 | + input_config={ |
| 64 | + "graph": "lexical_graph_builder.graph", |
| 65 | + "lexical_graph_config": "lexical_graph_builder.config", |
| 66 | + }, |
| 67 | + ) |
| 68 | + # user input: |
| 69 | + # the initial text |
| 70 | + # and the list of entities and relations we are looking for |
| 71 | + pipe_inputs = { |
| 72 | + "splitter": { |
| 73 | + "text": """Albert Einstein was a German physicist born in 1879 who |
| 74 | + wrote many groundbreaking papers especially about general relativity |
| 75 | + and quantum mechanics. He worked for many different institutions, including |
| 76 | + the University of Bern in Switzerland and the University of Oxford.""" |
| 77 | + }, |
| 78 | + "lexical_graph_builder": { |
| 79 | + "document_info": { |
| 80 | + # 'path' can be anything |
| 81 | + "path": "example/pipeline_with_notifications" |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + # run the pipeline |
| 86 | + return await pipe.run(pipe_inputs) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + with neo4j.GraphDatabase.driver( |
| 91 | + "bolt://localhost:7687", auth=("neo4j", "password") |
| 92 | + ) as driver: |
| 93 | + print(asyncio.run(main(driver))) |
0 commit comments