Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Falkordb vectorstore Integration #16047

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Python
__pycache__/
*.py[cod]
*$py.class

# Virtual Environment
venv/
env/
.env

# IDE files
.vscode/
.idea/

# Logs
*.log

# Build files
build/
dist/
*.egg-info/

# Test files
.pytest_cache/
.coverage

# OS generated files
.DS_Store
Thumbs.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "falkordb",
srcs = ["falkordb.py"],
deps = [
"//llama_index/core:base",
"//llama_index/core/vector_stores:types",
],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: all format lint test clean

all: format lint test

format:
black .
isort .

lint:
flake8 .
mypy .

test:
pytest tests/

clean:
find . -type f -name "*.pyc" -delete
find . -type d -name "__pycache__" -delete
rm -rf .pytest_cache
rm -rf .mypy_cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# LlamaIndex Vector_Stores Integration: Falkordb
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# FalkorDB Vector Store Integration for LlamaIndex

## Overview

The FalkorDB Vector Store integration for LlamaIndex allows you to use FalkorDB as a vector store backend for your LlamaIndex applications. This integration supports efficient storage and retrieval of vector embeddings, enabling fast similarity searches and other vector operations.

## Installation

To use the FalktorDB vector store with LlamaIndex, you need to install the necessary package:

```bash
pip install llama-index-vector-stores-falkordb
```

## Usage

### Initializing the FalkorDB Vector Store

To use the FalkorDB vector store, you first need to initialize it with your FalkorDB connection details:

```python
from llama_index.vector_stores.falkordb import FalkorDBVectorStore

vector_store = FalkorDBVectorStore(
url="bolt://localhost:7687",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to falkor://

database="your_database_name",
index_name="your_index_name",
node_label="YourNodeLabel",
embedding_node_property="embedding",
text_node_property="text",
distance_strategy="cosine",
embedding_dimension=1536
)
```

### Adding Documents

You can add documents to the vector store using the `add` method:

```python
from llama_index.core.schema import Document

documents = [
Document("This is the first document."),
Document("This is the second document.")
]

vector_store.add(documents)
```

### Querying the Vector Store

To perform a similarity search, use the `query` method:

```python
from llama_index.core.vector_stores.types import VectorStoreQuery

query_embedding = [0.1, 0.2, 0.3, ...] # Your query embedding
query = VectorStoreQuery(query_embedding=query_embedding, similarity_top_k=5)
results = vector_store.query(query)

for node, score in zip(results.nodes, results.similarities):
print(f"Text: {node.text}, Score: {score}")
```

## Advanced Features

### Creating a New Index

If you need to create a new vector index, you can use the `create_new_index` method:

```python
vector_store.create_new_index()
```

This method will create a new vector index in FalkorDB based on the parameters you provided when initializing the `FalkorDBVectorStore`.

### Retrieving Existing Index

To check if an index already exists and retrieve its information:

```python
exists = vector_store.retrieve_existing_index()
if exists:
print("Index exists with the following properties:")
print(f"Node Label: {vector_store.node_label}")
print(f"Embedding Property: {vector_store.embedding_node_property}")
print(f"Embedding Dimension: {vector_store.embedding_dimension}")
print(f"Distance Strategy: {vector_store.distance_strategy}")
else:
print("Index does not exist")
```

### Deleting Documents

To delete documents from the vector store:

```python
ref_doc_id = "your_document_id"
vector_store.delete(ref_doc_id)
```

### Using Metadata Filters

You can use metadata filters when querying the vector store:

```python
from llama_index.core.vector_stores.types import MetadataFilters, MetadataFilter

filters = MetadataFilters(
filters=[
MetadataFilter(key="category", value="science", operator=FilterOperator.EQ)
]
)

query = VectorStoreQuery(
query_embedding=query_embedding,
similarity_top_k=5,
filters=filters
)

results = vector_store.query(query)
```

## Best Practices

1. **Connection Management**: Ensure that you properly manage your FalkorDB connections, especially in production environments.
2. **Index Naming**: Use descriptive names for your indexes to easily identify them in your FalkorDB instance.
3. **Error Handling**: Implement proper error handling to manage potential issues with connections or queries.
4. **Performance Tuning**: Adjust the `embedding_dimension` and `distance_strategy` parameters based on your specific use case and performance requirements.

## Troubleshooting

If you encounter issues:

1. Check your FalkorDB connection details (URL, database name).
2. Ensure that the FalkorDB server is running and accessible.
3. Verify that the index exists and has the correct properties.
4. Check the FalkorDB logs for any error messages.

For more information on FalkorDB and its capabilities, refer to the [official FalkorDB documentation](https://falkordb.com/docs/).
Loading