From 86f8a2b6a132a5a25dfd9afb15e35d788fe2dddb Mon Sep 17 00:00:00 2001 From: Tomaz Bratanic Date: Sat, 14 Oct 2023 00:58:14 +0200 Subject: [PATCH] Add neo4j vector index docs (#2895) * Add neo4j vector index docs * Run build, fix docs --------- Co-authored-by: jacoblee93 --- .../vectorstores/integrations/neo4jvector.mdx | 52 ++++++++++++++++ .../neo4j_vector/docker-compose.example.yml | 8 +++ .../neo4j_vector/neo4j_vector.ts | 36 +++++++++++ .../neo4j_vector_existinggraph.ts | 35 +++++++++++ .../neo4j_vector/neo4j_vector_retrieval.ts | 59 +++++++++++++++++++ langchain/.gitignore | 3 + langchain/package.json | 8 +++ langchain/src/load/import_constants.ts | 1 + langchain/src/load/import_type.d.ts | 3 + langchain/tsconfig.json | 1 + 10 files changed, 206 insertions(+) create mode 100644 docs/extras/modules/data_connection/vectorstores/integrations/neo4jvector.mdx create mode 100644 examples/src/indexes/vector_stores/neo4j_vector/docker-compose.example.yml create mode 100644 examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector.ts create mode 100644 examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_existinggraph.ts create mode 100644 examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_retrieval.ts diff --git a/docs/extras/modules/data_connection/vectorstores/integrations/neo4jvector.mdx b/docs/extras/modules/data_connection/vectorstores/integrations/neo4jvector.mdx new file mode 100644 index 000000000000..06986c4c1093 --- /dev/null +++ b/docs/extras/modules/data_connection/vectorstores/integrations/neo4jvector.mdx @@ -0,0 +1,52 @@ +# Neo4j Vector Index + +Neo4j is an open-source graph database with integrated support for vector similarity search. +It supports: + +- approximate nearest neighbor search +- Euclidean similarity and cosine similarity +- Hybrid search combining vector and keyword searches + +## Setup + +To work with Neo4j Vector Index, you need to install the `neo4j-driver` package: + +```bash npm2yarn +npm install neo4j-driver +``` + +### Setup a `Neo4j` self hosted instance with `docker-compose` + +`Neo4j` provides a prebuilt Docker image that can be used to quickly setup a self-hosted Neo4j database instance. +Create a file below named `docker-compose.yml`: + +import CodeBlock from "@theme/CodeBlock"; +import DockerExample from "@examples/indexes/vector_stores/neo4j_vector/docker-compose.example.yml"; + + + {DockerExample} + + +And then in the same directory, run `docker compose up` to start the container. + +You can find more information on how to setup `Neo4j` on their [website](https://neo4j.com/docs/operations-manual/current/installation/). + +## Usage + +import Example from "@examples/indexes/vector_stores/neo4j_vector/neo4j_vector.ts"; + +One complete example of using `Neo4jVectorStore` is the following: + +{Example} + +### Use retrievalQuery parameter to customize responses + +import RetrievalExample from "@examples/indexes/vector_stores/neo4j_vector/neo4j_vector_retrieval.ts"; + +{RetrievalExample} + +### Instantiate Neo4jVectorStore from existing graph + +import ExistingGraphExample from "@examples/indexes/vector_stores/neo4j_vector/neo4j_vector_existinggraph.ts"; + +{ExistingGraphExample} diff --git a/examples/src/indexes/vector_stores/neo4j_vector/docker-compose.example.yml b/examples/src/indexes/vector_stores/neo4j_vector/docker-compose.example.yml new file mode 100644 index 000000000000..0d56c951ac2f --- /dev/null +++ b/examples/src/indexes/vector_stores/neo4j_vector/docker-compose.example.yml @@ -0,0 +1,8 @@ +services: + database: + image: neo4j + ports: + - 7687:7687 + - 7474:7474 + environment: + - NEO4J_AUTH=neo4j/pleaseletmein diff --git a/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector.ts b/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector.ts new file mode 100644 index 000000000000..3e30bc7c2d56 --- /dev/null +++ b/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector.ts @@ -0,0 +1,36 @@ +import { OpenAIEmbeddings } from "langchain/embeddings/openai"; +import { Neo4jVectorStore } from "langchain/vectorstores/neo4j_vector"; + +// Configuration object for Neo4j connection and other related settings +const config = { + url: "bolt://localhost:7687", // URL for the Neo4j instance + username: "neo4j", // Username for Neo4j authentication + password: "pleaseletmein", // Password for Neo4j authentication + indexName: "vector", // Name of the vector index + keywordIndexName: "keyword", // Name of the keyword index if using hybrid search + searchType: "vector", // Type of search (e.g., vector, hybrid) + nodeLabel: "Chunk", // Label for the nodes in the graph + textNodeProperty: "text", // Property of the node containing text + embeddingNodeProperty: "embedding", // Property of the node containing embedding +}; + +const documents = [ + { pageContent: "what's this", metadata: { a: 2 } }, + { pageContent: "Cat drinks milk", metadata: { a: 1 } }, +]; + +const neo4jVectorIndex = await Neo4jVectorStore.fromDocuments( + documents, + new OpenAIEmbeddings(), + config +); + +const results = await neo4jVectorIndex.similaritySearch("water", 1); + +console.log(results); + +/* + [ Document { pageContent: 'Cat drinks milk', metadata: { a: 1 } } ] +*/ + +await neo4jVectorIndex.close(); diff --git a/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_existinggraph.ts b/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_existinggraph.ts new file mode 100644 index 000000000000..cdbcd7d2d475 --- /dev/null +++ b/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_existinggraph.ts @@ -0,0 +1,35 @@ +import { OpenAIEmbeddings } from "langchain/embeddings/openai"; +import { Neo4jVectorStore } from "langchain/vectorstores/neo4j_vector"; + +/** + * `fromExistingGraph` Method: + * + * Description: + * This method initializes a `Neo4jVectorStore` instance using an existing graph in the Neo4j database. + * It's designed to work with nodes that already have textual properties but might not have embeddings. + * The method will compute and store embeddings for nodes that lack them. + * + * Note: + * This method is particularly useful when you have a pre-existing graph with textual data and you want + * to enhance it with vector embeddings for similarity searches without altering the original data structure. + */ + +// Configuration object for Neo4j connection and other related settings +const config = { + url: "bolt://localhost:7687", // URL for the Neo4j instance + username: "neo4j", // Username for Neo4j authentication + password: "pleaseletmein", // Password for Neo4j authentication + indexName: "wikipedia", + nodeLabel: "Wikipedia", + textNodeProperties: ["title", "description"], + embeddingNodeProperty: "embedding", + searchType: "hybrid", +}; + +// You should have a populated Neo4j database to use this method +const neo4jVectorIndex = await Neo4jVectorStore.fromExistingGraph( + new OpenAIEmbeddings(), + config +); + +await neo4jVectorIndex.close(); diff --git a/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_retrieval.ts b/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_retrieval.ts new file mode 100644 index 000000000000..addbe6c173d9 --- /dev/null +++ b/examples/src/indexes/vector_stores/neo4j_vector/neo4j_vector_retrieval.ts @@ -0,0 +1,59 @@ +import { OpenAIEmbeddings } from "langchain/embeddings/openai"; +import { Neo4jVectorStore } from "langchain/vectorstores/neo4j_vector"; + +/* + * The retrievalQuery is a customizable Cypher query fragment used in the Neo4jVectorStore class to define how + * search results should be retrieved and presented from the Neo4j database. It allows developers to specify + * the format and structure of the data returned after a similarity search. + * Mandatory columns for `retrievalQuery`: + * + * 1. text: + * - Description: Represents the textual content of the node. + * - Type: String + * + * 2. score: + * - Description: Represents the similarity score of the node in relation to the search query. A + * higher score indicates a closer match. + * - Type: Float (ranging between 0 and 1, where 1 is a perfect match) + * + * 3. metadata: + * - Description: Contains additional properties and information about the node. This can include + * any other attributes of the node that might be relevant to the application. + * - Type: Object (key-value pairs) + * - Example: { "id": "12345", "category": "Books", "author": "John Doe" } + * + * Note: While you can customize the `retrievalQuery` to fetch additional columns or perform + * transformations, never omit the mandatory columns. The names of these columns (`text`, `score`, + * and `metadata`) should remain consistent. Renaming them might lead to errors or unexpected behavior. + */ + +// Configuration object for Neo4j connection and other related settings +const config = { + url: "bolt://localhost:7687", // URL for the Neo4j instance + username: "neo4j", // Username for Neo4j authentication + password: "pleaseletmein", // Password for Neo4j authentication + retrievalQuery: ` + RETURN node.text AS text, score, {a: node.a * 2} AS metadata + `, +}; + +const documents = [ + { pageContent: "what's this", metadata: { a: 2 } }, + { pageContent: "Cat drinks milk", metadata: { a: 1 } }, +]; + +const neo4jVectorIndex = await Neo4jVectorStore.fromDocuments( + documents, + new OpenAIEmbeddings(), + config +); + +const results = await neo4jVectorIndex.similaritySearch("water", 1); + +console.log(results); + +/* + [ Document { pageContent: 'Cat drinks milk', metadata: { a: 2 } } ] +*/ + +await neo4jVectorIndex.close(); diff --git a/langchain/.gitignore b/langchain/.gitignore index 47522eba7fbd..1e683beb4ad2 100644 --- a/langchain/.gitignore +++ b/langchain/.gitignore @@ -235,6 +235,9 @@ vectorstores/pgvector.d.ts vectorstores/milvus.cjs vectorstores/milvus.js vectorstores/milvus.d.ts +vectorstores/neo4j_vector.cjs +vectorstores/neo4j_vector.js +vectorstores/neo4j_vector.d.ts vectorstores/prisma.cjs vectorstores/prisma.js vectorstores/prisma.d.ts diff --git a/langchain/package.json b/langchain/package.json index 01d2f1726b5f..0dcc1d88e970 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -247,6 +247,9 @@ "vectorstores/milvus.cjs", "vectorstores/milvus.js", "vectorstores/milvus.d.ts", + "vectorstores/neo4j_vector.cjs", + "vectorstores/neo4j_vector.js", + "vectorstores/neo4j_vector.d.ts", "vectorstores/prisma.cjs", "vectorstores/prisma.js", "vectorstores/prisma.d.ts", @@ -1641,6 +1644,11 @@ "import": "./vectorstores/milvus.js", "require": "./vectorstores/milvus.cjs" }, + "./vectorstores/neo4j_vector": { + "types": "./vectorstores/neo4j_vector.d.ts", + "import": "./vectorstores/neo4j_vector.js", + "require": "./vectorstores/neo4j_vector.cjs" + }, "./vectorstores/prisma": { "types": "./vectorstores/prisma.d.ts", "import": "./vectorstores/prisma.js", diff --git a/langchain/src/load/import_constants.ts b/langchain/src/load/import_constants.ts index 0e68d31554b7..9b6c2e5868a4 100644 --- a/langchain/src/load/import_constants.ts +++ b/langchain/src/load/import_constants.ts @@ -56,6 +56,7 @@ export const optionalImportEntrypoints = [ "langchain/vectorstores/opensearch", "langchain/vectorstores/pgvector", "langchain/vectorstores/milvus", + "langchain/vectorstores/neo4j_vector", "langchain/vectorstores/typeorm", "langchain/vectorstores/myscale", "langchain/vectorstores/redis", diff --git a/langchain/src/load/import_type.d.ts b/langchain/src/load/import_type.d.ts index 0d8d72e9a982..e3cf0013c043 100644 --- a/langchain/src/load/import_type.d.ts +++ b/langchain/src/load/import_type.d.ts @@ -166,6 +166,9 @@ export interface OptionalImportMap { "langchain/vectorstores/milvus"?: | typeof import("../vectorstores/milvus.js") | Promise; + "langchain/vectorstores/neo4j_vector"?: + | typeof import("../vectorstores/neo4j_vector.js") + | Promise; "langchain/vectorstores/typeorm"?: | typeof import("../vectorstores/typeorm.js") | Promise; diff --git a/langchain/tsconfig.json b/langchain/tsconfig.json index 85cd35c1f45d..b00788e756f2 100644 --- a/langchain/tsconfig.json +++ b/langchain/tsconfig.json @@ -111,6 +111,7 @@ "src/vectorstores/opensearch.ts", "src/vectorstores/pgvector.ts", "src/vectorstores/milvus.ts", + "src/vectorstores/neo4j_vector.ts", "src/vectorstores/prisma.ts", "src/vectorstores/typeorm.ts", "src/vectorstores/myscale.ts",