Neo4j
Setup
Install the dependencies needed for Neo4j:
- npm
- Yarn
- pnpm
npm install neo4j-driver
yarn add neo4j-driver
pnpm add neo4j-driver
Usage
This walkthrough uses Neo4j to demonstrate a graph database integration.
Instantiate a graph and retrieve information the the graph by generating Cypher query language statements using GraphCypherQAChain.
import { Neo4jGraph } from "langchain/graphs/neo4j_graph";
import { OpenAI } from "langchain/llms/openai";
import { GraphCypherQAChain } from "langchain/chains/graph_qa/cypher";
/**
* This example uses Neo4j database, which is native graph database.
* To set it up follow the instructions on https://neo4j.com/docs/operations-manual/current/installation/.
*/
const url = "bolt://localhost:7687";
const username = "neo4j";
const password = "pleaseletmein";
const graph = await Neo4jGraph.initialize({ url, username, password });
const model = new OpenAI({ temperature: 0 });
// Populate the database with two nodes and a relationship
await graph.query(
"CREATE (a:Actor {name:'Bruce Willis'})" +
"-[:ACTED_IN]->(:Movie {title: 'Pulp Fiction'})"
);
// Refresh schema
await graph.refreshSchema();
const chain = GraphCypherQAChain.fromLLM({
llm: model,
graph,
});
const res = await chain.run("Who played in Pulp Fiction?");
console.log(res);
// Bruce Willis played in Pulp Fiction.
API Reference:
- Neo4jGraph from
langchain/graphs/neo4j_graph
- OpenAI from
langchain/llms/openai
- GraphCypherQAChain from
langchain/chains/graph_qa/cypher
Disclaimer ⚠️
Security note: Make sure that the database connection uses credentials that are narrowly-scoped to only include necessary permissions. Failure to do so may result in data corruption or loss, since the calling code may attempt commands that would result in deletion, mutation of data if appropriately prompted or reading sensitive data if such data is present in the database. The best way to guard against such negative outcomes is to (as appropriate) limit the permissions granted to the credentials used with this tool. For example, creating read only users for the database is a good way to ensure that the calling code cannot mutate or delete data. See the security page for more information.