Zack Saadioui
8/24/2024
1
2
bash
%pip install --upgrade langchain langchain-community langchain-openai langchain-experimental neo4j
1
2
3
4
python
import getpass
import os
from langchain_community.graphs import Neo4jGraph
1
2
3
4
5
python
os.environ['NEO4J_URI'] = 'bolt://localhost:7687'
os.environ['NEO4J_USERNAME'] = 'neo4j'
os.environ['NEO4J_PASSWORD'] = 'password'
graph = Neo4jGraph()
1
LLMGraphTransformer
1
LLMGraphTransformer
1
LLMGraphTransformer
1
This allows you to feed text into the transformer & examine the results:
1
The output will show you the nodes & relationships identified:
1
2
3
4
5
6
7
8
9
python
llm_transformer_filtered = LLMGraphTransformer(
llm=llm,
allowed_nodes=["Person", "Country", "Organization"],
allowed_relationships=["NATIONALITY", "LOCATED_IN", "WORKED_AT", "SPOUSE"],
)
graph_documents_filtered = llm_transformer_filtered.convert_to_graph_documents(documents)
print(f"Filtered Nodes:{graph_documents_filtered[0].nodes}")
print(f"Filtered Relationships:{graph_documents_filtered[0].relationships}")
1
node_properties
1
2
3
4
5
6
7
8
9
10
python
llm_transformer_props = LLMGraphTransformer(
llm=llm,
allowed_nodes=["Person", "Country", "Organization"],
allowed_relationships=["NATIONALITY", "LOCATED_IN", "WORKED_AT", "SPOUSE"],
node_properties=["born_year"],
)
graph_documents_props = llm_transformer_props.convert_to_graph_documents(documents)
print(f"Nodes with Properties:{graph_documents_props[0].nodes}")
print(f"Relationships with Properties:{graph_documents_props[0].relationships}")
1
node_properties=True
1
add_graph_documents
1
2
python
graph.add_graph_documents(graph_documents_props)
1
2
3
4
python
query = "MATCH (n) RETURN n"
results = graph.query(query)
print(results)
Copyright © Arsturn 2024