Storage Operations

Neo4j

Learn how to save your knowledge graph to a Neo4j database.

See a working Neo4j implementation example on GitHub.

The Perseus SDK provides a convenient method to save a KnowledgeGraph object directly to a Neo4j database instance. This allows you to leverage the power of Neo4j for graph visualization, complex queries, and further analysis.

Configuration

Before you can save a graph to Neo4j, you need to configure the connection settings. The SDK uses environment variables or a .env file to manage these settings. For more details, refer to the Configuration guide.

The following parameters are required:

ParameterTypeDefault ValueDescription
NEO4J_URIstrbolt://localhost:7687The URI for your Neo4j database connection.
NEO4J_USERstrneo4jThe username for authenticating with Neo4j.
NEO4J_PASSWORDstrpasswordThe password for authenticating with Neo4j.

Saving the Graph

Once your configuration is set up, you can use the save_to_neo4j() method on a KnowledgeGraph object to save it to your Neo4j instance.

The save_to_neo4j() method saves the graph synchronously.

For more details on the save_to_neo4j method, refer to the Python API.

import perseus_client

knowledge_graphs = perseus_client.build_graph(
    file_paths=["assets/sample.txt"],
)

for kg in knowledge_graphs:
    kg.save_to_neo4j()

The save_to_neo4j_async() method saves the graph asynchronously.

For more details on the save_to_neo4j_async method, refer to the Python API.

import asyncio
import perseus_client

async def main():
    knowledge_graphs = perseus_client.build_graph(
        file_paths=["assets/sample.txt"],
    )

    for kg in knowledge_graphs:
        await kg.save_to_neo4j_async()

if __name__ == "__main__":
    asyncio.run(main())