> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ag2.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# autogen.agentchat.contrib.graph_rag.neo4j_graph_query_engine.Neo4jGraphQueryEngine

<h2 id="autogen.agentchat.contrib.graph_rag.neo4j_graph_query_engine.Neo4jGraphQueryEngine" class="doc doc-heading">
  <code class="doc-symbol doc-symbol-heading doc-symbol-class" />

  <span class="doc doc-object-name doc-class-name">Neo4jGraphQueryEngine</span>
</h2>

```python theme={null}
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.<br />It builds a property graph Index from input documents,
storing and retrieving data from the property graph in the Neo4j database.<br />It extracts triplets, i.e., \[entity] -> \[relationship] -> \[entity] sets,
from the input documents using llamIndex extractors.<br />Users can provide custom entities, relationships, and schema to guide the extraction process.<br />If strict is True, the engine will extract triplets following the schema
of allowed relationships for each entity specified in the schema.<br />It also leverages LlamaIndex's chat engine which has a conversation history internally to provide context-aware responses.<br />For usage, please refer to example notebook/agentchat\_graph\_rag\_neo4j.ipynb

Initialize a Neo4j Property graph.<br />Please also refer to [https://docs.llamaindex.ai/en/stable/examples/property\_graph/graph\_store/](https://docs.llamaindex.ai/en/stable/examples/property_graph/graph_store/)

<b>Parameters:</b>

| Name        | Description                                                                       |
| ----------- | --------------------------------------------------------------------------------- |
| `host`      | **Type:** str<br /><br />**Default:** 'bolt                                       |
| `port`      | **Type:** int<br /><br />**Default:** 7687                                        |
| `database`  | **Type:** str<br /><br />**Default:** 'neo4j'                                     |
| `username`  | **Type:** str<br /><br />**Default:** 'neo4j'                                     |
| `password`  | **Type:** str<br /><br />**Default:** 'neo4j'                                     |
| `llm`       | **Type:** ForwardRef('LLM') \| None<br /><br />**Default:** None                  |
| `embedding` | **Type:** ForwardRef('BaseEmbedding') \| None<br /><br />**Default:** None        |
| `entities`  | **Type:** ForwardRef('TypeAlias') \| None<br /><br />**Default:** None            |
| `relations` | **Type:** ForwardRef('TypeAlias') \| None<br /><br />**Default:** None            |
| `schema`    | **Type:** dict\[str, str] \| list\['Triple'] \| None<br /><br />**Default:** None |
| `strict`    | **Type:** bool \| None<br /><br />**Default:** False                              |

### Instance Methods

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### add\_records

```python theme={null}
add_records(self, new_records: list[Document]) -> bool
```

Add new records to the knowledge graph. Must be local files.<br />

<b>Parameters:</b>

| Name          | Description                                                                                                                                     |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `new_records` | List of new documents to add.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/graph_rag/document/Document)] |

<b>Returns:</b>

| Type | Description                                |
| ---- | ------------------------------------------ |
| bool | bool: True if successful, False otherwise. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### connect\_db

```python theme={null}
connect_db(self) -> None
```

Connect to an existing knowledge graph database.

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### init\_db

```python theme={null}
init_db(self, input_doc: list[Document] | None = None) -> None
```

Build the knowledge graph with input documents.

<b>Parameters:</b>

| Name        | Description                                                                                                                                 |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `input_doc` | **Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/graph_rag/document/Document)] \| None<br /><br />**Default:** None |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### query

```python theme={null}
query(
    self,
    question: str,
    n_results: int = 1,
    **kwargs: Any
) -> GraphStoreQueryResult
```

Query the property graph with a question using LlamaIndex chat engine.<br />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.<br />

<b>Parameters:</b>

| Name        | Description                                                                     |
| ----------- | ------------------------------------------------------------------------------- |
| `question`  | a human input question.<br /><br />**Type:** str                                |
| `n_results` | number of results to return.<br /><br />**Type:** int<br /><br />**Default:** 1 |
| `**kwargs`  | additional keyword arguments.<br /><br />**Type:** Any                          |

<b>Returns:</b>

| Type                                                                                                                      | Description                                                               |
| ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| [GraphStoreQueryResult](/docs/api-reference/autogen/agentchat/contrib/graph_rag/graph_query_engine/GraphStoreQueryResult) | A GrapStoreQueryResult object containing the answer and related triplets. |

<br />
