Back to Llama Index

FalkorDB Graph Store

docs/examples/index_structs/knowledge_graph/FalkorDBGraphDemo.ipynb

0.14.212.8 KB
Original Source

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/index_structs/knowledge_graph/FalkorDBGraphDemo.ipynb" target="_parent"></a>

FalkorDB Graph Store

This notebook walks through configuring FalkorDB to be the backend for graph storage in LlamaIndex.

python
%pip install llama-index-llms-openai
%pip install llama-index-graph-stores-falkordb
python
# My OpenAI Key
import os

os.environ["OPENAI_API_KEY"] = "API_KEY_HERE"
python
import logging
import sys

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

Using Knowledge Graph with FalkorDBGraphStore

Start FalkorDB

The easiest way to start FalkorDB as a Graph database is using the falkordb docker image.

To follow every step of this tutorial, launch the image as follows:

bash
docker run -p 6379:6379 -it --rm falkordb/falkordb:edge
python
from llama_index.graph_stores.falkordb import FalkorDBGraphStore

graph_store = FalkorDBGraphStore(
    "redis://localhost:6379", decode_responses=True
)

Building the Knowledge Graph

python
from llama_index.core import SimpleDirectoryReader, KnowledgeGraphIndex

from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
from IPython.display import Markdown, display
python
documents = SimpleDirectoryReader(
    "../../../../examples/paul_graham_essay/data"
).load_data()
python
# define LLM

llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
Settings.llm = llm
Settings.chunk_size = 512
python
from llama_index.core import StorageContext

storage_context = StorageContext.from_defaults(graph_store=graph_store)

# NOTE: can take a while!
index = KnowledgeGraphIndex.from_documents(
    documents,
    max_triplets_per_chunk=2,
    storage_context=storage_context,
)

Querying the Knowledge Graph

First, we can query and send only the triplets to the LLM.

python
query_engine = index.as_query_engine(
    include_text=False, response_mode="tree_summarize"
)
response = query_engine.query(
    "Tell me more about Interleaf",
)
python
display(Markdown(f"<b>{response}</b>"))

For more detailed answers, we can also send the text from where the retrieved tripets were extracted.

python
query_engine = index.as_query_engine(
    include_text=True, response_mode="tree_summarize"
)
response = query_engine.query(
    "Tell me more about Interleaf",
)
python
display(Markdown(f"<b>{response}</b>"))

Visualizing the Graph

python
%pip install pyvis
python
## create graph
from pyvis.network import Network

g = index.get_networkx_graph()
net = Network(notebook=True, cdn_resources="in_line", directed=True)
net.from_nx(g)
net.show("falkordbgraph_draw.html")