Git Product home page Git Product logo

neo4j-genai-python's Introduction

Neo4j GenAI package for Python

This repository contains the official Neo4j GenAI features for Python.

The purpose of this package is to provide a first party package to developers, where Neo4j can guarantee long term commitment and maintenance as well as being fast to ship new features and high performing patterns and methods.

Documentation: https://neo4j.com/docs/neo4j-genai-python/

Python versions supported:

  • Python 3.12 supported.
  • Python 3.11 supported.
  • Python 3.10 supported.
  • Python 3.9 supported.
  • Python 3.8 supported.

Usage

Installation

This package requires Python (>=3.8.1).

To install the latest stable version, use:

pip install neo4j-genai

Examples

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 the embeddings have.

Assumption: Neo4j running

from neo4j import GraphDatabase
from neo4j_genai.indexes import create_vector_index

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")

INDEX_NAME = "vector-index-name"

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)

# Creating the index
create_vector_index(
    driver,
    INDEX_NAME,
    label="Document",
    embedding_property="vectorProperty",
    dimensions=1536,
    similarity_fn="euclidean",
)

Populating the Neo4j Vector Index

Note that the below example is not the only way you can upsert data into your Neo4j database. For example, you could also leverage the Neo4j Python driver.

Assumption: Neo4j running with a defined vector index

from neo4j import GraphDatabase
from neo4j_genai.indexes import upsert_vector

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)

# Upsert the vector
vector = ...
upsert_vector(
    driver,
    node_id=1,
    embedding_property="vectorProperty",
    vector=vector,
)

Performing a similarity search

Assumption: Neo4j running with populated vector index in place.

Limitation: The query over the vector index is an approximate nearest neighbor search and may not give exact results. See this reference for more details.

While the library has more retrievers than shown here, the following examples should be able to get you started.

In the following example, we use a simple vector search as retriever, that will perform a similarity search over the index-name vector index in Neo4j.

from neo4j import GraphDatabase
from neo4j_genai.retrievers import VectorRetriever
from neo4j_genai.llm import OpenAILLM
from neo4j_genai.generation import GraphRAG
from neo4j_genai.embeddings.openai import OpenAIEmbeddings

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")

INDEX_NAME = "vector-index-name"

# Connect to Neo4j database
driver = GraphDatabase.driver(URI, auth=AUTH)

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

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

# Initialize the LLM
# Note: An OPENAI_API_KEY environment variable is required here
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})

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

# Query the graph
query_text = "How do I do similarity search in Neo4j?"
response = rag.search(query_text=query_text, retriever_config={"top_k": 5})
print(response.answer)

Development

Install dependencies

poetry install

Getting started

Issues

If you have a bug to report or feature to request, first search to see if an issue already exists. If a related issue doesn't exist, please raise a new issue using the relevant issue form.

If you're a Neo4j Enterprise customer, you can also reach out to Customer Support.

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 and/or Discord.

Make changes

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

Pull request

When you're finished with your changes, create a pull request, also known as a PR.

  • Ensure that you have signed the CLA.
  • Ensure that the base of your PR is set to main.
  • Don't forget to link your PR to an issue if you are solving one.
  • Enable the checkbox to allow maintainer edits 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 or normal pull request comments. You can apply suggested changes directly through the UI, and 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.
  • 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. When opening a PR, you can generate an edit suggestion by commenting on the GitHub PR using CodiumAI:
@CodiumAI-Agent /update_changelog
  1. Use this as a suggestion and update the CHANGELOG.md content under 'Next'.
  2. Commit the changes.

Run tests

Unit tests

This should run out of the box once the dependencies are installed.

poetry run pytest tests/unit

E2E tests

To run e2e tests you'd need to have some services running locally:

  • neo4j
  • weaviate
  • weaviate-text2vec-transformers

The easiest way to get it up and running is via Docker compose:

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

(pro tip: if you suspect something in the databases are cached, run docker compose -f tests/e2e/docker-compose.yml down to remove them completely)

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

poetry run pytest tests/e2e

Further information

neo4j-genai-python's People

Contributors

willtai avatar oskarhane avatar jonbesga avatar leila-messallem avatar alexthomas93 avatar stellasia avatar team-gen-ai avatar jpryce-aklundh avatar a-s-g93 avatar

Stargazers

 avatar SeungEon Cha avatar Minju Kim avatar Zhazha_JiaYiZhen avatar  avatar  avatar WAHAHA avatar  avatar  avatar  avatar yihong avatar Tom Wanzek avatar Tonic avatar Axel Bellec avatar  avatar ma-raza avatar dorkmatter69 avatar Jesse Garza avatar zack avatar James Tarr avatar  avatar  avatar Matt Murray avatar Steve Bang avatar Kevin Armengol avatar Scott Joyner avatar Daniel Bukowski avatar Hung Nguyen avatar Alter-xyz avatar David Moore avatar  avatar Matthew Moon avatar  avatar 天马行空 avatar  avatar William E. H. Qvaale avatar Jefferson Cunha avatar Latin avatar Jannic Horst avatar Zinnia avatar AI in PM avatar Abul Kalam Faruk avatar Alexander Jarasch avatar Rodolfo Rodriguez Girbes avatar Straughter "BatmanOsama" Guthrie avatar Raman avatar zyk avatar Ding Jiatong avatar  avatar lewis avatar otluk avatar  avatar Andrey A. avatar  avatar Yacine Kheddache avatar Adu-pavi avatar Kunal Suri avatar Stephane Jourdan avatar Alex Tyrode avatar  avatar Flipped avatar Jonas Oppenlaender avatar Ebenezer Agbozo avatar Alex avatar Matt avatar Morgan Senechal avatar  avatar Luca avatar Michael Hunger avatar Kushal avatar  avatar mnemonyx avatar Robsdedude avatar Michael Cheah avatar  avatar  avatar

Watchers

Mattias Finné avatar  avatar Nacho Cordón avatar  avatar  avatar  avatar Kostas Georgiou avatar  avatar

neo4j-genai-python's Issues

Uncaught Neo4j Exception

Always get this uncaught exception when running the VectorRetriever().search() function.

This occurs regardless if vector index was previously created or hasn't yet been added. Wrapping in following a try-except does not gracefully catch this either:

    try:
        driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))
        embedder = OpenAIEmbeddings(model="text-embedding-3-large")
        retriever = VectorRetriever(driver, INDEX_NAME, embedder)
        response = retriever.search(query_text=query, top_k=5)
    except Exception as e:
        logging.ERROR(f"Error: {e}")
        # Never caught

Console output:

DEBUG:neo4j.io:[#C339]  _: <CONNECTION> server state: READY > TX_READY_OR_TX_STREAMING
DEBUG:neo4j.io:[#C339]  S: SUCCESS {'t_first': 167, 'fields': ['node', 'score'], 'qid': 0}
DEBUG:neo4j.io:[#C339]  S: FAILURE {'code': 'Neo.ClientError.Procedure.ProcedureCallFailed', 'message': 'Failed to invoke procedure `db.index.vector.queryNodes`: Caused by: java.lang.IllegalArgumentException: Index query vector has 3072 dimensions, but indexed vectors have 384.'}
DEBUG:neo4j.io:[#C339]  C: RESET
DEBUG:neo4j.io:[#C339]  _: <CONNECTION> client state: TX_READY_OR_TX_STREAMING > READY
DEBUG:neo4j.io:[#C339]  S: SUCCESS {}
DEBUG:neo4j.io:[#C339]  _: <CONNECTION> server state: FAILED > READY
DEBUG:neo4j.pool:[#C339]  _: <POOL> released bolt-155989
ERROR:root:Neo4j Uncaught Exception: 'int' object is not callable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.