Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion genai-tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FalkorDB provides powerful tools and integrations for building intelligent GenAI

- [GraphRAG-SDK](./graphrag-sdk.md): Build intelligent GraphRAG applications with FalkorDB and LLMs.
- [AG2](./ag2.md): Build multi-agent AI systems with AG2 (formerly AutoGen) and FalkorDB GraphRAG.
- [LangChain](./langchain.md): Integration with LangChain for AI agents with memory.
- [LangChain](./langchain.md): Integration with LangChain for AI agents with memory (Python and JavaScript/TypeScript).
- [LangGraph](./langgraph.md): Build stateful, multi-actor agentic applications with LangGraph.
- [LlamaIndex](./llamaindex.md): Simplify development of LLM-powered applications with LlamaIndex.
- [GraphRAG Toolkit](./graphrag-toolkit.md): AWS GraphRAG Toolkit integration for building knowledge graph applications.
186 changes: 175 additions & 11 deletions genai-tools/langchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ parent: "GenAI Tools"

FalkorDB is integrated with [LangChain](https://www.langchain.com/), bringing powerful graph database capabilities to AI-driven applications. This integration enables the creation of AI agents with memory, enhancing their ability to retain state and context across interactions.

The FalkorDB LangChain integration is available for both **Python** and **JavaScript/TypeScript** environments, making it easy to build intelligent applications in your preferred language.

## Resources

- 🔗 [FalkorDBQAChain Documentation](https://python.langchain.com/docs/use_cases/more/graph/graph_falkordb_qa)
- 🔗 [FalkorDBQAChain Documentation (Python)](https://python.langchain.com/docs/use_cases/more/graph/graph_falkordb_qa)
- 📦 [@falkordb/langchain-ts Package (JavaScript/TypeScript)](https://www.npmjs.com/package/@falkordb/langchain-ts)
- 💻 [FalkorDB-Langchain-js Repository](https://github.com/FalkorDB/FalkorDB-Langchain-js)
- 📓 [Blog: Build AI Agents with Memory – LangChain + FalkorDB](https://www.falkordb.com/blog/building-ai-agents-with-memory-langchain/)

## Installation
---

## Python Integration

### Installation

Install LangChain with FalkorDB support:

```bash
pip install langchain langchain-community falkordb
```

## Quick Start
### Quick Start

### 1. Connect to FalkorDB
#### 1. Connect to FalkorDB

```python
from langchain_community.graphs import FalkorDBGraph
Expand All @@ -39,7 +47,7 @@ graph = FalkorDBGraph(
)
```

### 2. Create a Knowledge Graph from Text
#### 2. Create a Knowledge Graph from Text

```python
from langchain.chains import GraphCypherQAChain
Expand All @@ -56,7 +64,7 @@ chain = GraphCypherQAChain.from_llm(
)
```

### 3. Query the Graph
#### 3. Query the Graph

```python
# Ask natural language questions
Expand All @@ -68,9 +76,9 @@ response = chain.run("What other movies did they act in?")
print(response)
```

## Advanced Usage
### Advanced Usage

### Using Graph Memory for Conversational AI
#### Using Graph Memory for Conversational AI

```python
from langchain.memory import ConversationGraphMemory
Expand All @@ -96,7 +104,7 @@ conversation.predict(input="I work as a software engineer")
conversation.predict(input="What do you know about me?")
```

### Custom Cypher Generation
#### Custom Cypher Generation

```python
from langchain.chains.graph_qa.cypher import GraphCypherQAChain
Expand Down Expand Up @@ -131,7 +139,7 @@ chain = GraphCypherQAChain.from_llm(
response = chain.run("Find all products in the electronics category")
```

### Loading Data into the Graph
#### Loading Data into the Graph

```python
from langchain_community.document_loaders import TextLoader
Expand All @@ -156,7 +164,7 @@ vector_store = FalkorDBVector.from_documents(
)
```

### Graph RAG Pattern
#### Graph RAG Pattern

```python
from langchain.chains import RetrievalQA
Expand All @@ -183,6 +191,162 @@ response = qa_chain.run("What are the key features of our product?")
print(response)
```

---

## JavaScript/TypeScript Integration

FalkorDB also provides a JavaScript/TypeScript integration for LangChain applications through the [@falkordb/langchain-ts](https://www.npmjs.com/package/@falkordb/langchain-ts) package.

### Installation

```bash
npm install @falkordb/langchain-ts falkordb langchain @langchain/openai
```

### Quick Start (JS/TS)

```typescript
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"
});

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

// Create and populate the graph
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,
});

// 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.

await graph.close();
```

> **Alternative Connection:** You can also connect using a URL format:
> ```typescript
> const graph = await FalkorDBGraph.initialize({
> url: "falkor://localhost:6379",
> graph: "movies"
> });
> ```

### Key Features (JS/TS)

- **Natural Language Querying**: Convert questions to Cypher queries automatically
- **Schema Management**: Automatic schema refresh and retrieval
- **Type Safety**: Full TypeScript support with type definitions
- **Promise-based API**: Modern async/await patterns

### API Reference (JS/TS)

#### `FalkorDBGraph.initialize(config)`

Create and initialize a new FalkorDB connection.

**Config Options:**
- `host` (string): Database host (default: "localhost")
- `port` (number): Database port (default: 6379)
- `graph` (string): Graph name to use
- `url` (string): Alternative connection URL format: `falkor[s]://[[username][:password]@][host][:port][/db-number]`
- `enhancedSchema` (boolean): Enable enhanced schema details

**Example with URL:**
```typescript
// Connect using URL format
const graph = await FalkorDBGraph.initialize({
url: "falkor://localhost:6379",
graph: "myGraph"
});

// With authentication
const graph = await FalkorDBGraph.initialize({
url: "falkor://username:password@localhost:6379",
graph: "myGraph"
});
```

#### `query(query: string)`

Execute a Cypher query on the graph.

```typescript
const result = await graph.query(
"MATCH (n:Person) RETURN n.name LIMIT 10"
);
```

#### `refreshSchema()`

Update the graph schema information.

```typescript
await graph.refreshSchema();
console.log(graph.getSchema());
```

### Advanced Usage (JS/TS)

#### Custom Cypher Queries

```typescript
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);
```

#### Working with Schema (JS/TS)

```typescript
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);
```

For more examples and source code, see the [@falkordb/langchain-ts repository](https://github.com/FalkorDB/FalkorDB-Langchain-js).

---

## Use Cases

- **Conversational AI with Memory**: Build chatbots that remember user context across sessions
Expand Down
Loading