Crate bep_neo4j

Source
Expand description

A Bep vector store for Neo4j.

This crate is a companion crate to the bep-core crate. It provides a vector store implementation that uses Neo4j as the underlying datastore.

See the README for more information.

§Prerequisites

§GenAI Plugin

The GenAI plugin is enabled by default in Neo4j Aura.

The plugin needs to be installed on self-managed instances. This is done by moving the neo4j-genai.jar file from /products to /plugins in the Neo4j home directory, or, if you are using Docker, by starting the Docker container with the extra parameter --env NEO4J_PLUGINS='["genai"]'.

For more information, see Operations Manual → Configure plugins.

§Pre-existing Vector Index

The Neo4jVectorStoreIndex struct is designed to work with a pre-existing Neo4j vector index. You can create the index using the Neo4j browser, a raw Cypher query, or the Neo4jClient::create_vector_index method. See the Neo4j documentation for more information.

The index name must be unique among both indexes and constraints. ❗A newly created index is not immediately available but is created in the background.

CREATE VECTOR INDEX moviePlots
    FOR (m:Movie)
    ON m.embedding
    OPTIONS {indexConfig: {
        `vector.dimensions`: 1536,
        `vector.similarity_function`: 'cosine'
    }}

§Simple example:

More examples can be found in the /examples folder.

use bep_neo4j::{vector_index::*, Neo4jClient};
use neo4rs::ConfigBuilder;
use bep::{providers::openai::*, vector_store::VectorStoreIndex};
use serde::Deserialize;
use std::env;

#[tokio::main]
async fn main() {
    let openai_api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
    let openai_client = Client::new(&openai_api_key);
    let model = openai_client.embedding_model(TEXT_EMBEDDING_ADA_002);


    const NEO4J_URI: &str = "neo4j+s://demo.neo4jlabs.com:7687";
    const NEO4J_DB: &str = "recommendations";
    const NEO4J_USERNAME: &str = "recommendations";
    const NEO4J_PASSWORD: &str = "recommendations";

    let client = Neo4jClient::from_config(
        ConfigBuilder::default()
            .uri(NEO4J_URI)
            .db(NEO4J_DB)
            .user(NEO4J_USERNAME)
            .password(NEO4J_PASSWORD)
            .build()
            .unwrap(),
    )
   .await
   .unwrap();

    let index = client.get_index(
        model,
        "moviePlotsEmbedding",
        SearchParams::default()
    ).await.unwrap();

    #[derive(Debug, Deserialize)]
    struct Movie {
        title: String,
        plot: String,
    }
    let results = index.top_n::<Movie>("Batman", 3).await.unwrap();
    println!("{:#?}", results);
}

Modules§

vector_index
A vector index for a Neo4j graph DB.

Structs§

Neo4jClient

Traits§

ToBoltType