from llama_index import PropertyGraphIndex
# Define your strict schema for node labels
schema = {
"Person": ["name", "age", "occupation"],
"Location": ["name", "coordinates"],
"Event": ["name", "date", "location"]
}
# Initialize the PropertyGraphIndex with the schema
graph_index = PropertyGraphIndex(schema=schema)
# Add documents to the index
documents = [
{"text": "Alice is a software engineer living in San Francisco."},
{"text": "Bob is a data scientist from New York."}
]
for doc in documents:
graph_index.add_document(doc)
# Optionally, connect to an existing Neo4j knowledge graph
graph_index.connect_to_neo4j(uri="bolt://localhost:7687", user="neo4j", password="password")
# Query the graph
results = graph_index.query("Find all software engineers in San Francisco.")
print(results)
def upsert_nodes(self, nodes: List[LabelledNode]) -> None: # Lists to hold separated types entity_dicts: List[dict] = [] chunk_dicts: List[dict] = []
# Sort by type
for item in nodes:
if isinstance(item, EntityNode):
entity_dicts.append({**item.dict(), "id": item.id})
elif isinstance(item, ChunkNode):
chunk_dicts.append({**item.dict(), "id": item.id})
else:
# Log that we do not support these types of nodes
# Or raise an error?
pass
if chunk_dicts:
self.structured_query(
"""
UNWIND $data AS row MERGE (c:Chunk {id: row.id}) SET c.text = row.text WITH c, row SET c += row.properties WITH c, row.embedding AS embedding WHERE embedding IS NOT NULL CALL db.create.setNodeVectorProperty(c, 'embedding', embedding) RETURN count(*) """, param_map={"data": chunk_dicts}, )
if entity_dicts:
self.structured_query(
"""
UNWIND $data AS row MERGE (e:Entity {id: row.id}) SET e.name = row.name WITH e, row SET e += row.properties RETURN count(*) """, param_map={"data": entity_dicts}, )
# Create tags for each LabelledNode
for node in nodes:
stmt = f'CREATE (n:Node__ {{id: "{node.id}", label: "{node.label}"}})'
self.structured_query(stmt)
6条答案
按热度按时间ss2ws0br1#
在构建Neo4j知识图谱时,要在PropertyGraphIndex中自定义节点标签,可以定义严格的模式、实现自定义提取模块或使用LLM进行提取。以下是一般的方法:
以下是定义严格模式的示例:
此外,
neo4j_property_graph.py
模块提供了诸如refresh_schema
之类的函数,以便您的模式保持最新,并提供预定义查询来管理节点和关系属性1(https://github.com/run-llama/llama_index/blob/main/docs/docs/module_guides/indexing/index_guide.md)2(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/graph_stores/llama-index-graph-stores-neo4j/llama_index/graph_stores/neo4j/neo4j_property_graph.py)。4ioopgfo2#
如果我想要为我提取的数据分配一个固定的标签,我应该怎么做?
4uqofj5v3#
要为使用
PropertyGraphIndex
构建Neo4j知识图谱时提取的数据分配固定标签,可以使用Node__
标签存储节点标签。以下是在Neo4jPropertyGraphStore
类的upsert_nodes
方法中如何执行此操作的示例:def upsert_nodes(self, nodes: List[LabelledNode]) -> None:
# Lists to hold separated types
entity_dicts: List[dict] = []
chunk_dicts: List[dict] = []
UNWIND $data AS row
MERGE (c:Chunk {id: row.id})
SET c.text = row.text
WITH c, row
SET c += row.properties
WITH c, row.embedding AS embedding
WHERE embedding IS NOT NULL
CALL db.create.setNodeVectorProperty(c, 'embedding', embedding)
RETURN count(*)
""",
param_map={"data": chunk_dicts},
)
UNWIND $data AS row
MERGE (e:Entity {id: row.id})
SET e.name = row.name
WITH e, row
SET e += row.properties
RETURN count(*)
""",
param_map={"data": entity_dicts},
)
class MockKGExtractor(TransformComponent):
"""A mock knowledge graph extractor that extracts a simple relation from a text."""
imzjd6km4#
我需要更多的帮助,这不能解决我的问题。我如何通过标签进行搜索?
rm5edbpk5#
要使用LlamaIndex库在您的Neo4j知识图谱中基于标签执行搜索,您可以使用具有关键字实体检索功能的
Neo4jQueryEnginePack
查询引擎类型。以下是如何初始化和使用它的示例:from llama_index.packs.neo4j_query_engine.base import Neo4jQueryEnginePack, Neo4jQueryEngineType
from llama_index.core.schema import Document
Define your Neo4j connection parameters
username = "your_neo4j_username"
password = "your_neo4j_password"
url = "your_neo4j_url"
database = "your_neo4j_database"
Define your documents
docs = [
Document(text="Your document text here"),
# Add more documents as needed
]
Initialize the Neo4jQueryEnginePack with KG_KEYWORD type
query_engine_pack = Neo4jQueryEnginePack(
username=username,
password=password,
url=url,
database=database,
docs=docs,
query_engine_type=Neo4jQueryEngineType.KG_KEYWORD
)
Perform a search query
query = "your search query based on labels"
results = query_engine_pack.run(query)
Print the results
for result in results:
print(result)
然后,您可以根据标签执行搜索查询并打印结果。
此外,如果您需要直接与Neo4j数据库交互以根据标签检索节点和关系,您可以使用
Neo4jGraphStore
类。以下是如何使用它的示例:from llama_index.legacy.graph_stores.neo4j import Neo4jGraphStore
Define your Neo4j connection parameters
username = "your_neo4j_username"
password = "your_neo4j_password"
url = "your_neo4j_url"
database = "your_neo4j_database"
Initialize the Neo4jGraphStore
graph_store = Neo4jGraphStore(
username=username,
password=password,
url=url,
database=database,
node_label="YourNodeLabel"
)
Example function to get relationships for a specific node
def get_relationships(node_id):
return graph_store.get(node_id)
Example function to get a relationship map
def get_relationship_map(node_ids, depth=2, limit=30):
return graph_store.get_rel_map(subjs=node_ids, depth=depth, limit=limit)
Retrieve relationships for a specific node
node_id = "your_node_id"
relationships = get_relationships(node_id)
print(relationships)
Retrieve a relationship map for a list of nodes
node_ids = ["node_id_1", "node_id_2"]
relationship_map = get_relationship_map(node_ids)
print(relationship_map)
这段代码展示了如何使用
Neo4jGraphStore
类根据节点标签检索关系和关系Map。mnowg1ta6#
你的回复对我来说没有用。