neo4j-graphrag


Nameneo4j-graphrag JSON
Version 1.4.2 PyPI version JSON
download
home_pageNone
SummaryPython package to allow easy integration to Neo4j's GraphRAG features
upload_time2025-01-15 08:49:12
maintainerNone
docs_urlNone
authorNeo4j, Inc
requires_python<4.0.0,>=3.9.0
licenseApache License, Version 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Neo4j GraphRAG Package for Python

The official Neo4j GraphRAG package for Python enables developers to build [graph retrieval augmented generation (GraphRAG)](https://neo4j.com/blog/graphrag-manifesto/) applications using the power of Neo4j and Python.
As a first-party library, it offers a robust, feature-rich, and high-performance solution, with the added assurance of long-term support and maintenance directly from Neo4j.

## ๐Ÿ“„ Documentation

Documentation can be found [here](https://neo4j.com/docs/neo4j-graphrag-python/)

### Resources

A series of blog posts demonstrating how to use this package:

- Build a Knowledge Graph and use GenAI to answer questions:
  - [GraphRAG Python Package: Accelerating GenAI With Knowledge Graphs](https://neo4j.com/blog/graphrag-python-package/)
- Retrievers: when the Neo4j graph is already populated:
  - [Getting Started With the Neo4j GraphRAG Python Package](https://neo4j.com/developer-blog/get-started-graphrag-python-package/)
  - [Enriching Vector Search With Graph Traversal Using the GraphRAG Python Package](https://neo4j.com/developer-blog/graph-traversal-graphrag-python-package/)
  - [Hybrid Retrieval for GraphRAG Applications Using the GraphRAG Python Package](https://neo4j.com/developer-blog/hybrid-retrieval-graphrag-python-package/)
  - [Enhancing Hybrid Retrieval With Graph Traversal Using the GraphRAG Python Package](https://neo4j.com/developer-blog/enhancing-hybrid-retrieval-graphrag-python-package/)
  - [Effortless RAG With Text2CypherRetriever](https://medium.com/neo4j/effortless-rag-with-text2cypherretriever-cb1a781ca53c)

A list of Neo4j GenAI-related features can also be found at [Neo4j GenAI Ecosystem](https://neo4j.com/labs/genai-ecosystem/).


## ๐Ÿ Python Version Support

| Version | Supported? |
| ------- | ---------: |
| 3.12    | &check;    |
| 3.11    | &check;    |
| 3.10    | &check;    |
| 3.9     | &check;    |
| 3.8     | &cross;    |

## ๐Ÿ“ฆ Installation

To install the latest stable version, run:

```shell
pip install neo4j-graphrag
```

### Optional Dependencies

This package has some optional features that can be enabled using
the extra dependencies described below:

- LLM providers (at least one is required for RAG and KG Builder Pipeline):
    - **ollama**: LLMs from Ollama
    - **openai**: LLMs from OpenAI (including AzureOpenAI)
    - **google**: LLMs from Vertex AI
    - **cohere**: LLMs from Cohere
    - **anthropic**: LLMs from Anthropic
    - **mistralai**: LLMs from MistralAI
- **sentence-transformers** : to use embeddings from the `sentence-transformers` Python package
- Vector database (to use :ref:`External Retrievers`):
    - **weaviate**: store vectors in Weaviate
    - **pinecone**: store vectors in Pinecone
    - **qdrant**: store vectors in Qdrant
- **experimental**: experimental features mainly related to the Knowledge Graph creation pipelines.
    - Warning: this dependency group requires `pygraphviz`. See below for installation instructions.


Install package with optional dependencies with (for instance):

```shell
pip install "neo4j-graphrag[openai]"
```

#### pygraphviz

`pygraphviz` is used for visualizing pipelines.
Installation instructions can be found [here](https://pygraphviz.github.io/documentation/stable/install.html).

## ๐Ÿ’ป Example Usage

The scripts below demonstrate how to get started with the package and make use of its key features.
To run these examples, ensure that you have a Neo4j instance up and running and update the `NEO4J_URI`, `NEO4J_USERNAME`, and `NEO4J_PASSWORD` variables in each script with the details of your Neo4j instance.
For the examples, make sure to export your OpenAI key as an environment variable named `OPENAI_API_KEY`.
Additional examples are available in the `examples` folder.

### Knowledge Graph Construction

**NOTE: The [APOC core library](https://neo4j.com/labs/apoc/) must be installed in your Neo4j instance in order to use this feature**

This package offers two methods for constructing a knowledge graph.

The `Pipeline` class provides extensive customization options, making it ideal for advanced use cases.
See the `examples/pipeline` folder for examples of how to use this class.

For a more streamlined approach, the `SimpleKGPipeline` class offers a simplified abstraction layer over the `Pipeline`, making it easier to build knowledge graphs.
Both classes support working directly with text and PDFs.

```python
import asyncio

from neo4j import GraphDatabase
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
from neo4j_graphrag.llm.openai_llm import OpenAILLM

NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"

# Connect to the Neo4j database
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))

# List the entities and relations the LLM should look for in the text
entities = ["Person", "House", "Planet"]
relations = ["PARENT_OF", "HEIR_OF", "RULES"]
potential_schema = [
    ("Person", "PARENT_OF", "Person"),
    ("Person", "HEIR_OF", "House"),
    ("House", "RULES", "Planet"),
]

# Create an Embedder object
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

# Instantiate the LLM
llm = OpenAILLM(
    model_name="gpt-4o",
    model_params={
        "max_tokens": 2000,
        "response_format": {"type": "json_object"},
        "temperature": 0,
    },
)

# Instantiate the SimpleKGPipeline
kg_builder = SimpleKGPipeline(
    llm=llm,
    driver=driver,
    embedder=embedder,
    entities=entities,
    relations=relations,
    on_error="IGNORE",
    from_pdf=False,
)

# Run the pipeline on a piece of text
text = (
    "The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House "
    "Atreides, an aristocratic family that rules the planet Caladan."
)
asyncio.run(kg_builder.run_async(text=text))
driver.close()
```

Example knowledge graph created using the above script:

![Example knowledge graph](https://raw.githubusercontent.com/neo4j/neo4j-graphrag-python/fd276af0069e4dd1769255d358793cc96e299bf3/images/kg_construction.svg)

### Creating a Vector Index

When creating a vector index, make sure you match the number of dimensions in the index with the number of dimensions your embeddings have.

```python
from neo4j import GraphDatabase
from neo4j_graphrag.indexes import create_vector_index

NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"
INDEX_NAME = "vector-index-name"

# Connect to the Neo4j database
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))

# Create the index
create_vector_index(
    driver,
    INDEX_NAME,
    label="Chunk",
    embedding_property="embedding",
    dimensions=3072,
    similarity_fn="euclidean",
)
driver.close()
```

### Populating a Vector Index

This example demonstrates one method for upserting data in your Neo4j database.
It's important to note that there are alternative approaches, such as using the [Neo4j Python driver](https://github.com/neo4j/neo4j-python-driver).

Ensure that your vector index is created prior to executing this example.

```python
from neo4j import GraphDatabase
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.indexes import upsert_vector

NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"

# Connect to the Neo4j database
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))

# Create an Embedder object
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

# Generate an embedding for some text
text = (
    "The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House "
    "Atreides, an aristocratic family that rules the planet Caladan."
)
vector = embedder.embed_query(text)

# Upsert the vector
upsert_vector(
    driver,
    node_id=0,
    embedding_property="embedding",
    vector=vector,
)
driver.close()
```

### Performing a Similarity Search

Please note that when querying a Neo4j vector index _approximate_ nearest neighbor search is used, which may not always deliver exact results.
For more information, refer to the Neo4j documentation on [limitations and issues of vector indexes](https://neo4j.com/docs/cypher-manual/current/indexes/semantic-indexes/vector-indexes/#limitations-and-issues).

In the example below, we perform a simple vector search using a retriever that conducts a similarity search over the `vector-index-name` vector index.

This library provides more retrievers beyond just the `VectorRetriever`.
See the `examples` folder for examples of how to use these retrievers.

Before running this example, make sure your vector index has been created and populated.

```python
from neo4j import GraphDatabase
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.generation import GraphRAG
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.retrievers import VectorRetriever

NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"
INDEX_NAME = "vector-index-name"

# Connect to the Neo4j database
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))

# Create an Embedder object
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

# Initialize the retriever
retriever = VectorRetriever(driver, INDEX_NAME, embedder)

# Instantiate the LLM
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})

# Instantiate the RAG pipeline
rag = GraphRAG(retriever=retriever, llm=llm)

# Query the graph
query_text = "Who is Paul Atreides?"
response = rag.search(query_text=query_text, retriever_config={"top_k": 5})
print(response.answer)
driver.close()
```

## ๐Ÿค Contributing

You must sign the [contributors license agreement](https://neo4j.com/developer/contributing-code/#sign-cla) in order to make contributions to this project.

### Install Dependencies

Our Python dependencies are managed using Poetry.
If Poetry is not yet installed on your system, you can follow the instructions [here](https://python-poetry.org/) to set it up.
To begin development on this project, start by cloning the repository and then install all necessary dependencies, including the development dependencies, with the following command:

```bash
poetry install --with dev
```

### Reporting Issues

If you have a bug to report or feature to request, first
[search to see if an issue already exists](https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-issues-and-pull-requests#search-by-the-title-body-or-comments).
If a related issue doesn't exist, please raise a new issue using the [issue form](https://github.com/neo4j/neo4j-graphrag-python/issues/new/choose).

If you're a Neo4j Enterprise customer, you can also reach out to [Customer Support](http://support.neo4j.com/).

If you don't have a bug to report or feature request, but you need a hand with
the library; community support is available via [Neo4j Online Community](https://community.neo4j.com/)
and/or [Discord](https://discord.gg/neo4j).

### Workflow for Contributions

1. Fork the repository.
2. Install Python and Poetry.
3. Create a working branch from `main` and start with your changes!

### Code Formatting and Linting

Our codebase follows strict formatting and linting standards using [Ruff](https://docs.astral.sh/ruff/) for code quality checks and [Mypy](https://github.com/python/mypy) for type checking.
Before contributing, ensure that all code is properly formatted, free of linting issues, and includes accurate type annotations.

- To install Ruff, follow the instructions [here](https://docs.astral.sh/ruff/installation/).
- To set up Mypy, follow the steps outlined [here](https://mypy.readthedocs.io/en/stable/getting_started.html#installing-and-running-mypy).

Adherence to these standards is required for contributions to be accepted.

#### Using Pre-commit

We recommend setting up [pre-commit](https://pre-commit.com/) to automate code quality checks.
This ensures your changes meet our guidelines before committing.

1. Install pre-commit by following the [installation guide](https://pre-commit.com/#install).
2. Set up the pre-commit hooks by running:

   ```bash
   pre-commit install
   ```

3. To manually check if a file meets the quality requirements, run:

   ```bash
   pre-commit run --file path/to/file
   ```

### Pull Requests

When you're finished with your changes, create a pull request (PR) using the following workflow.

- Ensure you have formatted and linted your code.
- Ensure that you have [signed the CLA](https://neo4j.com/developer/contributing-code/#sign-cla).
- Ensure that the base of your PR is set to `main`.
- Don't forget to [link your PR to an issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
    if you are solving one.
- Check the checkbox to [allow maintainer edits](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)
    so that maintainers can make any necessary tweaks and update your branch for merge.
- Reviewers may ask for changes to be made before a PR can be merged, either using
    [suggested changes](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/incorporating-feedback-in-your-pull-request)
    or normal pull request comments. You can apply suggested changes directly through
    the UI. Any other changes can be made in your fork and committed to the PR branch.
- As you update your PR and apply changes, mark each conversation as [resolved](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request#resolving-conversations).
- Update the `CHANGELOG.md` if you have made significant changes to the project, these include:
  - Major changes:
    - New features
    - Bug fixes with high impact
    - Breaking changes
  - Minor changes:
    - Documentation improvements
    - Code refactoring without functional impact
    - Minor bug fixes
- Keep `CHANGELOG.md` changes brief and focus on the most important changes.

### Updating the `CHANGELOG.md`

1. You can automatically generate a changelog suggestion for your PR by commenting on it [using CodiumAI](https://github.com/CodiumAI-Agent):

```
@CodiumAI-Agent /update_changelog
```

2. Edit the suggestion if necessary and update the appropriate subsection in the `CHANGELOG.md` file under 'Next'.
3. Commit the changes.

## ๐Ÿงช Tests

### Unit Tests

Install the project dependencies then run the following command to run the unit tests locally:

```bash
poetry run pytest tests/unit
```

### E2E tests

To execute end-to-end (e2e) tests, you need the following services to be running locally:

- neo4j
- weaviate
- weaviate-text2vec-transformers

The simplest way to set these up is by using Docker Compose:

```bash
docker compose -f tests/e2e/docker-compose.yml up
```

_(tip: If you encounter any caching issues within the databases, you can completely remove them by running `docker compose -f tests/e2e/docker-compose.yml down`)_

Once all the services are running, execute the following command to run the e2e tests:

```bash
poetry run pytest tests/e2e
```

## โ„น๏ธ Additional Information

- [The official Neo4j Python driver](https://github.com/neo4j/neo4j-python-driver)
- [Neo4j GenAI integrations](https://neo4j.com/docs/cypher-manual/current/genai-integrations/)


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "neo4j-graphrag",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0.0,>=3.9.0",
    "maintainer_email": null,
    "keywords": null,
    "author": "Neo4j, Inc",
    "author_email": "team-gen-ai@neo4j.com",
    "download_url": "https://files.pythonhosted.org/packages/52/0a/2b5c3d42811492813d5601fe65b53fc20ff364daa10abbc7128fdc27730e/neo4j_graphrag-1.4.2.tar.gz",
    "platform": null,
    "description": "# Neo4j GraphRAG Package for Python\n\nThe official Neo4j GraphRAG package for Python enables developers to build [graph retrieval augmented generation (GraphRAG)](https://neo4j.com/blog/graphrag-manifesto/) applications using the power of Neo4j and Python.\nAs a first-party library, it offers a robust, feature-rich, and high-performance solution, with the added assurance of long-term support and maintenance directly from Neo4j.\n\n## \ud83d\udcc4 Documentation\n\nDocumentation can be found [here](https://neo4j.com/docs/neo4j-graphrag-python/)\n\n### Resources\n\nA series of blog posts demonstrating how to use this package:\n\n- Build a Knowledge Graph and use GenAI to answer questions:\n  - [GraphRAG Python Package: Accelerating GenAI With Knowledge Graphs](https://neo4j.com/blog/graphrag-python-package/)\n- Retrievers: when the Neo4j graph is already populated:\n  - [Getting Started With the Neo4j GraphRAG Python Package](https://neo4j.com/developer-blog/get-started-graphrag-python-package/)\n  - [Enriching Vector Search With Graph Traversal Using the GraphRAG Python Package](https://neo4j.com/developer-blog/graph-traversal-graphrag-python-package/)\n  - [Hybrid Retrieval for GraphRAG Applications Using the GraphRAG Python Package](https://neo4j.com/developer-blog/hybrid-retrieval-graphrag-python-package/)\n  - [Enhancing Hybrid Retrieval With Graph Traversal Using the GraphRAG Python Package](https://neo4j.com/developer-blog/enhancing-hybrid-retrieval-graphrag-python-package/)\n  - [Effortless RAG With Text2CypherRetriever](https://medium.com/neo4j/effortless-rag-with-text2cypherretriever-cb1a781ca53c)\n\nA list of Neo4j GenAI-related features can also be found at [Neo4j GenAI Ecosystem](https://neo4j.com/labs/genai-ecosystem/).\n\n\n## \ud83d\udc0d Python Version Support\n\n| Version | Supported? |\n| ------- | ---------: |\n| 3.12    | &check;    |\n| 3.11    | &check;    |\n| 3.10    | &check;    |\n| 3.9     | &check;    |\n| 3.8     | &cross;    |\n\n## \ud83d\udce6 Installation\n\nTo install the latest stable version, run:\n\n```shell\npip install neo4j-graphrag\n```\n\n### Optional Dependencies\n\nThis package has some optional features that can be enabled using\nthe extra dependencies described below:\n\n- LLM providers (at least one is required for RAG and KG Builder Pipeline):\n    - **ollama**: LLMs from Ollama\n    - **openai**: LLMs from OpenAI (including AzureOpenAI)\n    - **google**: LLMs from Vertex AI\n    - **cohere**: LLMs from Cohere\n    - **anthropic**: LLMs from Anthropic\n    - **mistralai**: LLMs from MistralAI\n- **sentence-transformers** : to use embeddings from the `sentence-transformers` Python package\n- Vector database (to use :ref:`External Retrievers`):\n    - **weaviate**: store vectors in Weaviate\n    - **pinecone**: store vectors in Pinecone\n    - **qdrant**: store vectors in Qdrant\n- **experimental**: experimental features mainly related to the Knowledge Graph creation pipelines.\n    - Warning: this dependency group requires `pygraphviz`. See below for installation instructions.\n\n\nInstall package with optional dependencies with (for instance):\n\n```shell\npip install \"neo4j-graphrag[openai]\"\n```\n\n#### pygraphviz\n\n`pygraphviz` is used for visualizing pipelines.\nInstallation instructions can be found [here](https://pygraphviz.github.io/documentation/stable/install.html).\n\n## \ud83d\udcbb Example Usage\n\nThe scripts below demonstrate how to get started with the package and make use of its key features.\nTo run these examples, ensure that you have a Neo4j instance up and running and update the `NEO4J_URI`, `NEO4J_USERNAME`, and `NEO4J_PASSWORD` variables in each script with the details of your Neo4j instance.\nFor the examples, make sure to export your OpenAI key as an environment variable named `OPENAI_API_KEY`.\nAdditional examples are available in the `examples` folder.\n\n### Knowledge Graph Construction\n\n**NOTE: The [APOC core library](https://neo4j.com/labs/apoc/) must be installed in your Neo4j instance in order to use this feature**\n\nThis package offers two methods for constructing a knowledge graph.\n\nThe `Pipeline` class provides extensive customization options, making it ideal for advanced use cases.\nSee the `examples/pipeline` folder for examples of how to use this class.\n\nFor a more streamlined approach, the `SimpleKGPipeline` class offers a simplified abstraction layer over the `Pipeline`, making it easier to build knowledge graphs.\nBoth classes support working directly with text and PDFs.\n\n```python\nimport asyncio\n\nfrom neo4j import GraphDatabase\nfrom neo4j_graphrag.embeddings import OpenAIEmbeddings\nfrom neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline\nfrom neo4j_graphrag.llm.openai_llm import OpenAILLM\n\nNEO4J_URI = \"neo4j://localhost:7687\"\nNEO4J_USERNAME = \"neo4j\"\nNEO4J_PASSWORD = \"password\"\n\n# Connect to the Neo4j database\ndriver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))\n\n# List the entities and relations the LLM should look for in the text\nentities = [\"Person\", \"House\", \"Planet\"]\nrelations = [\"PARENT_OF\", \"HEIR_OF\", \"RULES\"]\npotential_schema = [\n    (\"Person\", \"PARENT_OF\", \"Person\"),\n    (\"Person\", \"HEIR_OF\", \"House\"),\n    (\"House\", \"RULES\", \"Planet\"),\n]\n\n# Create an Embedder object\nembedder = OpenAIEmbeddings(model=\"text-embedding-3-large\")\n\n# Instantiate the LLM\nllm = OpenAILLM(\n    model_name=\"gpt-4o\",\n    model_params={\n        \"max_tokens\": 2000,\n        \"response_format\": {\"type\": \"json_object\"},\n        \"temperature\": 0,\n    },\n)\n\n# Instantiate the SimpleKGPipeline\nkg_builder = SimpleKGPipeline(\n    llm=llm,\n    driver=driver,\n    embedder=embedder,\n    entities=entities,\n    relations=relations,\n    on_error=\"IGNORE\",\n    from_pdf=False,\n)\n\n# Run the pipeline on a piece of text\ntext = (\n    \"The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House \"\n    \"Atreides, an aristocratic family that rules the planet Caladan.\"\n)\nasyncio.run(kg_builder.run_async(text=text))\ndriver.close()\n```\n\nExample knowledge graph created using the above script:\n\n![Example knowledge graph](https://raw.githubusercontent.com/neo4j/neo4j-graphrag-python/fd276af0069e4dd1769255d358793cc96e299bf3/images/kg_construction.svg)\n\n### Creating a Vector Index\n\nWhen creating a vector index, make sure you match the number of dimensions in the index with the number of dimensions your embeddings have.\n\n```python\nfrom neo4j import GraphDatabase\nfrom neo4j_graphrag.indexes import create_vector_index\n\nNEO4J_URI = \"neo4j://localhost:7687\"\nNEO4J_USERNAME = \"neo4j\"\nNEO4J_PASSWORD = \"password\"\nINDEX_NAME = \"vector-index-name\"\n\n# Connect to the Neo4j database\ndriver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))\n\n# Create the index\ncreate_vector_index(\n    driver,\n    INDEX_NAME,\n    label=\"Chunk\",\n    embedding_property=\"embedding\",\n    dimensions=3072,\n    similarity_fn=\"euclidean\",\n)\ndriver.close()\n```\n\n### Populating a Vector Index\n\nThis example demonstrates one method for upserting data in your Neo4j database.\nIt's important to note that there are alternative approaches, such as using the [Neo4j Python driver](https://github.com/neo4j/neo4j-python-driver).\n\nEnsure that your vector index is created prior to executing this example.\n\n```python\nfrom neo4j import GraphDatabase\nfrom neo4j_graphrag.embeddings import OpenAIEmbeddings\nfrom neo4j_graphrag.indexes import upsert_vector\n\nNEO4J_URI = \"neo4j://localhost:7687\"\nNEO4J_USERNAME = \"neo4j\"\nNEO4J_PASSWORD = \"password\"\n\n# Connect to the Neo4j database\ndriver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))\n\n# Create an Embedder object\nembedder = OpenAIEmbeddings(model=\"text-embedding-3-large\")\n\n# Generate an embedding for some text\ntext = (\n    \"The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House \"\n    \"Atreides, an aristocratic family that rules the planet Caladan.\"\n)\nvector = embedder.embed_query(text)\n\n# Upsert the vector\nupsert_vector(\n    driver,\n    node_id=0,\n    embedding_property=\"embedding\",\n    vector=vector,\n)\ndriver.close()\n```\n\n### Performing a Similarity Search\n\nPlease note that when querying a Neo4j vector index _approximate_ nearest neighbor search is used, which may not always deliver exact results.\nFor more information, refer to the Neo4j documentation on [limitations and issues of vector indexes](https://neo4j.com/docs/cypher-manual/current/indexes/semantic-indexes/vector-indexes/#limitations-and-issues).\n\nIn the example below, we perform a simple vector search using a retriever that conducts a similarity search over the `vector-index-name` vector index.\n\nThis library provides more retrievers beyond just the `VectorRetriever`.\nSee the `examples` folder for examples of how to use these retrievers.\n\nBefore running this example, make sure your vector index has been created and populated.\n\n```python\nfrom neo4j import GraphDatabase\nfrom neo4j_graphrag.embeddings import OpenAIEmbeddings\nfrom neo4j_graphrag.generation import GraphRAG\nfrom neo4j_graphrag.llm import OpenAILLM\nfrom neo4j_graphrag.retrievers import VectorRetriever\n\nNEO4J_URI = \"neo4j://localhost:7687\"\nNEO4J_USERNAME = \"neo4j\"\nNEO4J_PASSWORD = \"password\"\nINDEX_NAME = \"vector-index-name\"\n\n# Connect to the Neo4j database\ndriver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))\n\n# Create an Embedder object\nembedder = OpenAIEmbeddings(model=\"text-embedding-3-large\")\n\n# Initialize the retriever\nretriever = VectorRetriever(driver, INDEX_NAME, embedder)\n\n# Instantiate the LLM\nllm = OpenAILLM(model_name=\"gpt-4o\", model_params={\"temperature\": 0})\n\n# Instantiate the RAG pipeline\nrag = GraphRAG(retriever=retriever, llm=llm)\n\n# Query the graph\nquery_text = \"Who is Paul Atreides?\"\nresponse = rag.search(query_text=query_text, retriever_config={\"top_k\": 5})\nprint(response.answer)\ndriver.close()\n```\n\n## \ud83e\udd1d Contributing\n\nYou must sign the [contributors license agreement](https://neo4j.com/developer/contributing-code/#sign-cla) in order to make contributions to this project.\n\n### Install Dependencies\n\nOur Python dependencies are managed using Poetry.\nIf Poetry is not yet installed on your system, you can follow the instructions [here](https://python-poetry.org/) to set it up.\nTo begin development on this project, start by cloning the repository and then install all necessary dependencies, including the development dependencies, with the following command:\n\n```bash\npoetry install --with dev\n```\n\n### Reporting Issues\n\nIf you have a bug to report or feature to request, first\n[search to see if an issue already exists](https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-issues-and-pull-requests#search-by-the-title-body-or-comments).\nIf a related issue doesn't exist, please raise a new issue using the [issue form](https://github.com/neo4j/neo4j-graphrag-python/issues/new/choose).\n\nIf you're a Neo4j Enterprise customer, you can also reach out to [Customer Support](http://support.neo4j.com/).\n\nIf you don't have a bug to report or feature request, but you need a hand with\nthe library; community support is available via [Neo4j Online Community](https://community.neo4j.com/)\nand/or [Discord](https://discord.gg/neo4j).\n\n### Workflow for Contributions\n\n1. Fork the repository.\n2. Install Python and Poetry.\n3. Create a working branch from `main` and start with your changes!\n\n### Code Formatting and Linting\n\nOur codebase follows strict formatting and linting standards using [Ruff](https://docs.astral.sh/ruff/) for code quality checks and [Mypy](https://github.com/python/mypy) for type checking.\nBefore contributing, ensure that all code is properly formatted, free of linting issues, and includes accurate type annotations.\n\n- To install Ruff, follow the instructions [here](https://docs.astral.sh/ruff/installation/).\n- To set up Mypy, follow the steps outlined [here](https://mypy.readthedocs.io/en/stable/getting_started.html#installing-and-running-mypy).\n\nAdherence to these standards is required for contributions to be accepted.\n\n#### Using Pre-commit\n\nWe recommend setting up [pre-commit](https://pre-commit.com/) to automate code quality checks.\nThis ensures your changes meet our guidelines before committing.\n\n1. Install pre-commit by following the [installation guide](https://pre-commit.com/#install).\n2. Set up the pre-commit hooks by running:\n\n   ```bash\n   pre-commit install\n   ```\n\n3. To manually check if a file meets the quality requirements, run:\n\n   ```bash\n   pre-commit run --file path/to/file\n   ```\n\n### Pull Requests\n\nWhen you're finished with your changes, create a pull request (PR) using the following workflow.\n\n- Ensure you have formatted and linted your code.\n- Ensure that you have [signed the CLA](https://neo4j.com/developer/contributing-code/#sign-cla).\n- Ensure that the base of your PR is set to `main`.\n- Don't forget to [link your PR to an issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)\n    if you are solving one.\n- Check the checkbox to [allow maintainer edits](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)\n    so that maintainers can make any necessary tweaks and update your branch for merge.\n- Reviewers may ask for changes to be made before a PR can be merged, either using\n    [suggested changes](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/incorporating-feedback-in-your-pull-request)\n    or normal pull request comments. You can apply suggested changes directly through\n    the UI. Any other changes can be made in your fork and committed to the PR branch.\n- As you update your PR and apply changes, mark each conversation as [resolved](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request#resolving-conversations).\n- Update the `CHANGELOG.md` if you have made significant changes to the project, these include:\n  - Major changes:\n    - New features\n    - Bug fixes with high impact\n    - Breaking changes\n  - Minor changes:\n    - Documentation improvements\n    - Code refactoring without functional impact\n    - Minor bug fixes\n- Keep `CHANGELOG.md` changes brief and focus on the most important changes.\n\n### Updating the `CHANGELOG.md`\n\n1. You can automatically generate a changelog suggestion for your PR by commenting on it [using CodiumAI](https://github.com/CodiumAI-Agent):\n\n```\n@CodiumAI-Agent /update_changelog\n```\n\n2. Edit the suggestion if necessary and update the appropriate subsection in the `CHANGELOG.md` file under 'Next'.\n3. Commit the changes.\n\n## \ud83e\uddea Tests\n\n### Unit Tests\n\nInstall the project dependencies then run the following command to run the unit tests locally:\n\n```bash\npoetry run pytest tests/unit\n```\n\n### E2E tests\n\nTo execute end-to-end (e2e) tests, you need the following services to be running locally:\n\n- neo4j\n- weaviate\n- weaviate-text2vec-transformers\n\nThe simplest way to set these up is by using Docker Compose:\n\n```bash\ndocker compose -f tests/e2e/docker-compose.yml up\n```\n\n_(tip: If you encounter any caching issues within the databases, you can completely remove them by running `docker compose -f tests/e2e/docker-compose.yml down`)_\n\nOnce all the services are running, execute the following command to run the e2e tests:\n\n```bash\npoetry run pytest tests/e2e\n```\n\n## \u2139\ufe0f Additional Information\n\n- [The official Neo4j Python driver](https://github.com/neo4j/neo4j-python-driver)\n- [Neo4j GenAI integrations](https://neo4j.com/docs/cypher-manual/current/genai-integrations/)\n\n",
    "bugtrack_url": null,
    "license": "Apache License, Version 2.0",
    "summary": "Python package to allow easy integration to Neo4j's GraphRAG features",
    "version": "1.4.2",
    "project_urls": {
        "Homepage": "https://neo4j.com/",
        "Repository": "https://github.com/neo4j/neo4j-graphrag-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b5c263023b5a637e6459bd75d8c1c3cdf8c8b6a4f865fec907144f61c135c92a",
                "md5": "9f9bd65a35d1e4c6748c391f6f4ad1ac",
                "sha256": "e4949bcf14c4d6a8f9c77ae4cb92c2b6372df609adc5b859ba401873353dcde9"
            },
            "downloads": -1,
            "filename": "neo4j_graphrag-1.4.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9f9bd65a35d1e4c6748c391f6f4ad1ac",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0.0,>=3.9.0",
            "size": 147319,
            "upload_time": "2025-01-15T08:49:08",
            "upload_time_iso_8601": "2025-01-15T08:49:08.291675Z",
            "url": "https://files.pythonhosted.org/packages/b5/c2/63023b5a637e6459bd75d8c1c3cdf8c8b6a4f865fec907144f61c135c92a/neo4j_graphrag-1.4.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "520a2b5c3d42811492813d5601fe65b53fc20ff364daa10abbc7128fdc27730e",
                "md5": "672cae5d1413a0574ae52ab156f87b21",
                "sha256": "32706e224827cbefa4de759f5faf1e47774f6cb9ea43846225df3edc5816b84b"
            },
            "downloads": -1,
            "filename": "neo4j_graphrag-1.4.2.tar.gz",
            "has_sig": false,
            "md5_digest": "672cae5d1413a0574ae52ab156f87b21",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0.0,>=3.9.0",
            "size": 79569,
            "upload_time": "2025-01-15T08:49:12",
            "upload_time_iso_8601": "2025-01-15T08:49:12.341155Z",
            "url": "https://files.pythonhosted.org/packages/52/0a/2b5c3d42811492813d5601fe65b53fc20ff364daa10abbc7128fdc27730e/neo4j_graphrag-1.4.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-15 08:49:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "neo4j",
    "github_project": "neo4j-graphrag-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "neo4j-graphrag"
}
        
Elapsed time: 1.59353s