Neo4jGraphQueryEngine

Neo4jGraphQueryEngine(
    host: str = 'bolt://localhost',
    port: int = 7687,
    database: str = 'neo4j',
    username: str = 'neo4j',
    password: str = 'neo4j',
    llm: ForwardRef('LLM') | None = None,
    embedding: ForwardRef('BaseEmbedding') | None = None,
    entities: ForwardRef('TypeAlias') | None = None,
    relations: ForwardRef('TypeAlias') | None = None,
    schema: dict[str, str] | list['Triple'] | None = None,
    strict: bool | None = False
)

This class serves as a wrapper for a property graph query engine backed by LlamaIndex and Neo4j, facilitating the creating, connecting, updating, and querying of LlamaIndex property graphs.
It builds a property graph Index from input documents, storing and retrieving data from the property graph in the Neo4j database.
It extracts triplets, i.e., [entity] -> [relationship] -> [entity] sets, from the input documents using llamIndex extractors.
Users can provide custom entities, relationships, and schema to guide the extraction process.
If strict is True, the engine will extract triplets following the schema of allowed relationships for each entity specified in the schema.
It also leverages LlamaIndex’s chat engine which has a conversation history internally to provide context-aware responses.
For usage, please refer to example notebook/agentchat_graph_rag_neo4j.ipynb

Initialize a Neo4j Property graph.
Please also refer to https://docs.llamaindex.ai/en/stable/examples/property_graph/graph_store/

Parameters:
NameDescription
hostType: str

Default: ‘bolt
portType: int

Default: 7687
databaseType: str

Default: ‘neo4j’
usernameType: str

Default: ‘neo4j’
passwordType: str

Default: ‘neo4j’
llmType: ForwardRef(‘LLM’) | None

Default: None
embeddingType: ForwardRef(‘BaseEmbedding’) | None

Default: None
entitiesType: ForwardRef(‘TypeAlias’) | None

Default: None
relationsType: ForwardRef(‘TypeAlias’) | None

Default: None
schemaType: dict[str, str] | list[‘Triple’] | None

Default: None
strictType: bool | None

Default: False

Instance Methods

add_records

add_records(self, new_records: list[Document]) -> bool

Add new records to the knowledge graph. Must be local files.

Parameters:
NameDescription
new_recordsList of new documents to add.

Type: list[Document]
Returns:
TypeDescription
boolbool: True if successful, False otherwise.

connect_db

connect_db(self) -> None

Connect to an existing knowledge graph database.


init_db

init_db(self, input_doc: list[Document] | None = None) -> None

Build the knowledge graph with input documents.

Parameters:
NameDescription
input_docType: list[Document] | None

Default: None

query

query(
    self,
    question: str,
    n_results: int = 1,
    **kwargs: Any
) -> GraphStoreQueryResult

Query the property graph with a question using LlamaIndex chat engine.
We use the condense_plus_context chat mode which condenses the conversation history and the user query into a standalone question, and then build a context for the standadlone question from the property graph to generate a response.

Parameters:
NameDescription
questiona human input question.

Type: str
n_resultsnumber of results to return.

Type: int

Default: 1
**kwargsType: Any
Returns:
TypeDescription
GraphStoreQueryResultA GrapStoreQueryResult object containing the answer and related triplets.