Memgraph
Setup
Install LangChain dependencies
Install the dependencies needed for Memgraph:
- npm
- Yarn
- pnpm
npm install @langchain/openai neo4j-driver @langchain/community
yarn add @langchain/openai neo4j-driver @langchain/community
pnpm add @langchain/openai neo4j-driver @langchain/community
Install Memgraph
Memgraph bundles the database along with various analytical tools into distinct Docker images. If you're new to Memgraph or you're in a developing stage, we recommend running Memgraph Platform with Docker Compose. Besides the database, it also includes all the tools you might need to analyze your data, such as command-line interface mgconsole, web interface Memgraph Lab and a complete set of algorithms within a MAGE library.
With the Docker running in the background, run the following command in the console:
Linux/MacOS:
curl https://install.memgraph.com | sh
Windows:
iwr https://windows.memgraph.com | iex
For other options of installation, check the Getting started guide.
Usage
The example below shows how to instantiate Memgraph graph, create a small database and retrieve information from the graph by generating Cypher query language statements using GraphCypherQAChain
.
import { MemgraphGraph } from "@langchain/community/graphs/memgraph_graph";
import { OpenAI } from "@langchain/openai";
import { GraphCypherQAChain } from "langchain/chains/graph_qa/cypher";
/**
* This example uses Memgraph database, an in-memory graph database.
* To set it up follow the instructions on https://memgraph.com/docs/getting-started.
*/
const url = "bolt://localhost:7687";
const username = "";
const password = "";
const graph = await MemgraphGraph.initialize({ url, username, password });
const model = new OpenAI({ temperature: 0 });
// Populate the database with two nodes and a relationship
await graph.query(
"CREATE (c1:Character {name: 'Jon Snow'}), (c2: Character {name: 'Olly'}) CREATE (c2)-[:KILLED {count: 1, method: 'Knife'}]->(c1);"
);
// Refresh schema
await graph.refreshSchema();
const chain = GraphCypherQAChain.fromLLM({
llm: model,
graph,
});
const res = await chain.run("Who killed Jon Snow and how?");
console.log(res);
// Olly killed Jon Snow using a knife.
API Reference:
- MemgraphGraph from
@langchain/community/graphs/memgraph_graph
- OpenAI from
@langchain/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.