Skip to main content

FalkorDB LangChain JS/TS Integration

The @falkordb/langchain-ts package enables developers to use FalkorDB’s ultra-fast graph database alongside their LangChain applications. What this means in practice is that your application can now take natural language questions, automatically generate the corresponding Cypher queries, pull the relevant context from your graph database, and return responses in plain language. The entire translation layer gets handled for you. The package fits into existing LangChain workflows, so if you are already using that framework for other AI capabilities, this becomes another tool in your stack. FalkorDB’s low latency characteristics combine with LangChain’s language model integration to provide quick responses.

About FalkorDB

An ultra-fast, multi-tenant graph database powering Generative AI, Agent Memory, Cloud Security, and Fraud Detection. FalkorDB is the first queryable Property Graph Database to leverage sparse matrices for representing the adjacency matrix in graphs and linear algebra for querying.

Installation

npm2yarn
npm install @falkordb/langchain-ts falkordb
You’ll also need LangChain and a language model:
npm2yarn
npm install langchain @langchain/openai

Quick start

1. Start FalkorDB

The easiest way to run FalkorDB is with Docker:
docker run -p 6379:6379 -it --rm falkordb/falkordb:latest

2. Basic usage

import { FalkorDBGraph } from "@falkordb/langchain-ts";
import { ChatOpenAI } from "@langchain/openai";
import { GraphCypherQAChain } from "@langchain/community/chains/graph_qa/cypher";

// Initialize FalkorDB connection
const graph = await FalkorDBGraph.initialize({
  host: "localhost",
  port: 6379,
  graph: "movies" // Your graph name
});

// Set up the language model
const model = new ChatOpenAI({ temperature: 0 });

// Create and populate the graph with some data
await graph.query(
  "CREATE (a:Actor {name:'Bruce Willis'})" +
  "-[:ACTED_IN]->(:Movie {title: 'Pulp Fiction'})"
);

// Refresh the graph schema
await graph.refreshSchema();

// Create a graph QA chain
const chain = GraphCypherQAChain.fromLLM({
  llm: model,
  graph: graph as any, // Type assertion for LangChain compatibility
});

// Ask questions about your graph
const response = await chain.run("Who played in Pulp Fiction?");
console.log(response);
// Output: Bruce Willis played in Pulp Fiction.

// Clean up
await graph.close();

API Reference

FalkorDBGraph

initialize(config: FalkorDBGraphConfig): Promise<FalkorDBGraph>

Creates and initializes a new FalkorDB connection. Config Options:
  • host (string, optional): Database host. Default: "localhost"
  • port (number, optional): Database port. Default: 6379
  • graph (string, optional): Graph name to use
  • url (string, optional): Alternative connection URL format
  • enhancedSchema (boolean, optional): Enable enhanced schema details. Default: false
Example:
const graph = await FalkorDBGraph.initialize({
  host: "localhost",
  port: 6379,
  graph: "myGraph",
  enhancedSchema: true
});

query(query: string): Promise<any>

Executes a Cypher query on the graph.
const result = await graph.query(
  "MATCH (n:Person) RETURN n.name LIMIT 10"
);

refreshSchema(): Promise<void>

Updates the graph schema information.
await graph.refreshSchema();
console.log(graph.getSchema());

getSchema(): string

Returns the current graph schema as a formatted string.

getStructuredSchema(): StructuredSchema

Returns the structured schema object containing node properties, relationship properties, and relationships.

close(): Promise<void>

Closes the database connection.
await graph.close();

Advanced usage

Custom Cypher queries

const graph = await FalkorDBGraph.initialize({
  host: "localhost",
  port: 6379,
  graph: "movies"
});

// Complex query
const result = await graph.query(`
  MATCH (a:Actor)-[:ACTED_IN]->(m:Movie)
  WHERE m.year > 2000
  RETURN a.name, m.title, m.year
  ORDER BY m.year DESC
  LIMIT 10
`);

console.log(result.data);

Multiple queries

await graph.executeQueries([
  "CREATE (p:Person {name: 'Alice'})",
  "CREATE (p:Person {name: 'Bob'})",
  "MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'}) CREATE (a)-[:KNOWS]->(b)"
]);

Working with schema

await graph.refreshSchema();

// Get formatted schema
const schema = graph.getSchema();
console.log(schema);

// Get structured schema
const structuredSchema = graph.getStructuredSchema();
console.log(structuredSchema.nodeProps);
console.log(structuredSchema.relationships);

Requirements

  • Node.js >= 18
  • FalkorDB server running (Redis-compatible)
  • LangChain >= 0.1.0

Examples

For more examples, see the @falkordb/langchain-ts repository on GitHub.

License

MIT

Support


Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.