Skip to content

Commit 4d49525

Browse files
authored
Use the neo4j_database parameter everywhere (#216)
* Use self.neo4j_database for all queries in Neo4jWriter * Make sure all execute_query can be run against a custom database * Update CHANGELOG * Update docstring + update examples not to use undocumented feature for neo4j driver * Expose neo4j_database in SimpleKGBuilder * Update CHANGELOG * Simplify changelog
1 parent 8285d56 commit 4d49525

29 files changed

+158
-59
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
## Next
44

55
### Added
6-
- Introduced optional lexical graph configuration for SimpleKGPipeline, enhancing flexibility in customizing node labels and relationship types in the lexical graph.
7-
- Ability to provide description and list of properties for entities and relations in the SimpleKGPipeline constructor.
6+
- Introduced optional lexical graph configuration for `SimpleKGPipeline`, enhancing flexibility in customizing node labels and relationship types in the lexical graph.
7+
- Introduced optional `neo4j_database` parameter for `SimpleKGPipeline`, `Neo4jChunkReader`and `Text2CypherRetriever`.
8+
- Ability to provide description and list of properties for entities and relations in the `SimpleKGPipeline` constructor.
9+
10+
### Fixed
11+
- `neo4j_database` parameter is now used for all queries in the `Neo4jWriter`.
12+
13+
### Changed
14+
- Updated all examples to use `neo4j_database` parameter instead of an undocumented neo4j driver constructor.
815

916
## 1.2.0
1017

examples/build_graph/simple_kg_builder_from_pdf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ async def define_and_run_pipeline(
5050
entities=ENTITIES,
5151
relations=RELATIONS,
5252
potential_schema=POTENTIAL_SCHEMA,
53+
neo4j_database=DATABASE,
5354
)
5455
return await kg_builder.run_async(file_path=str(file_path))
5556

@@ -62,7 +63,7 @@ async def main() -> PipelineResult:
6263
"response_format": {"type": "json_object"},
6364
},
6465
)
65-
with neo4j.GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver:
66+
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
6667
res = await define_and_run_pipeline(driver, llm)
6768
await llm.async_client.close()
6869
return res

examples/build_graph/simple_kg_builder_from_text.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Neo4j db infos
2222
URI = "neo4j://localhost:7687"
2323
AUTH = ("neo4j", "password")
24-
DATABASE = "neo4j"
24+
DATABASE = "newdb"
2525

2626
# Text to process
2727
TEXT = """The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House Atreides,
@@ -67,6 +67,7 @@ async def define_and_run_pipeline(
6767
relations=RELATIONS,
6868
potential_schema=POTENTIAL_SCHEMA,
6969
from_pdf=False,
70+
neo4j_database=DATABASE,
7071
)
7172
return await kg_builder.run_async(text=TEXT)
7273

@@ -79,7 +80,7 @@ async def main() -> PipelineResult:
7980
"response_format": {"type": "json_object"},
8081
},
8182
)
82-
with neo4j.GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver:
83+
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
8384
res = await define_and_run_pipeline(driver, llm)
8485
await llm.async_client.close()
8586
return res

examples/customize/answer/custom_prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
driver = neo4j.GraphDatabase.driver(
2424
URI,
2525
auth=AUTH,
26-
database=DATABASE,
2726
)
2827

2928
embedder = OpenAIEmbeddings()
@@ -33,6 +32,7 @@
3332
index_name=INDEX,
3433
retrieval_query="WITH node, score RETURN node.title as title, node.plot as plot",
3534
embedder=embedder,
35+
neo4j_database=DATABASE,
3636
)
3737

3838
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})

examples/customize/answer/langchain_compatiblity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
driver = neo4j.GraphDatabase.driver(
2222
URI,
2323
auth=AUTH,
24-
database=DATABASE,
2524
)
2625

2726
embedder = OpenAIEmbeddings(model="text-embedding-ada-002")
@@ -31,6 +30,7 @@
3130
index_name=INDEX,
3231
retrieval_query="WITH node, score RETURN node.title as title, node.plot as plot",
3332
embedder=embedder, # type: ignore[arg-type, unused-ignore]
33+
neo4j_database=DATABASE,
3434
)
3535

3636
llm = ChatOpenAI(model="gpt-4o", temperature=0)

examples/customize/retrievers/result_formatter_vector_cypher_retriever.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def my_result_formatter(record: neo4j.Record) -> RetrieverResultItem:
3838
)
3939

4040

41-
with neo4j.GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver:
41+
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
4242
# Initialize the retriever
4343
retriever = VectorCypherRetriever(
4444
driver=driver,
@@ -48,7 +48,7 @@ def my_result_formatter(record: neo4j.Record) -> RetrieverResultItem:
4848
retrieval_query=RETRIEVAL_QUERY,
4949
result_formatter=my_result_formatter,
5050
# optionally, set neo4j database
51-
# neo4j_database="neo4j",
51+
neo4j_database=DATABASE,
5252
)
5353

5454
# Perform the similarity search for a text query

examples/customize/retrievers/result_formatter_vector_retriever.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737
# Connect to Neo4j database
38-
driver = neo4j.GraphDatabase.driver(URI, auth=AUTH, database=DATABASE)
38+
driver = neo4j.GraphDatabase.driver(URI, auth=AUTH)
3939

4040

4141
query_text = "Find a movie about astronauts"
@@ -52,6 +52,7 @@
5252
index_name=INDEX_NAME,
5353
embedder=OpenAIEmbeddings(),
5454
return_properties=["title", "plot"],
55+
neo4j_database=DATABASE,
5556
)
5657
print(retriever.search(query_text=query_text, top_k=top_k_results))
5758
print()

examples/question_answering/graphrag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def formatter(record: neo4j.Record) -> RetrieverResultItem:
3535
driver = neo4j.GraphDatabase.driver(
3636
URI,
3737
auth=AUTH,
38-
database=DATABASE,
3938
)
4039

4140
embedder = OpenAIEmbeddings()
@@ -46,6 +45,7 @@ def formatter(record: neo4j.Record) -> RetrieverResultItem:
4645
retrieval_query="with node, score return node.title as title, node.plot as plot",
4746
result_formatter=formatter,
4847
embedder=embedder,
48+
neo4j_database=DATABASE,
4949
)
5050

5151
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})

examples/retrieve/hybrid_cypher_retriever.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# the name of all actors starring in that movie
2525
RETRIEVAL_QUERY = " MATCH (node)<-[:ACTED_IN]-(p:Person) RETURN node.title as movieTitle, node.plot as moviePlot, collect(p.name) as actors, score as similarityScore"
2626

27-
with neo4j.GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver:
27+
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
2828
# Initialize the retriever
2929
retriever = HybridCypherRetriever(
3030
driver=driver,
@@ -37,7 +37,7 @@
3737
# (see corresponding example in 'customize' directory)
3838
# result_formatter=None,
3939
# optionally, set neo4j database
40-
# neo4j_database="neo4j",
40+
neo4j_database=DATABASE,
4141
)
4242

4343
# Perform the similarity search for a text query

examples/retrieve/hybrid_retriever.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
FULLTEXT_INDEX_NAME = "movieFulltext"
1919

2020

21-
with neo4j.GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver:
21+
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
2222
# Initialize the retriever
2323
retriever = HybridRetriever(
2424
driver=driver,
@@ -31,7 +31,7 @@
3131
# (see corresponding example in 'customize' directory)
3232
# result_formatter=None,
3333
# optionally, set neo4j database
34-
# neo4j_database="neo4j",
34+
neo4j_database=DATABASE,
3535
)
3636

3737
# Perform the similarity search for a text query

0 commit comments

Comments
 (0)