{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "3f897989",
"metadata": {},
"source": [
"
"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "82f90261",
"metadata": {},
"source": [
"# FalkorDB Graph Store\n",
"\n",
"This notebook walks through configuring `FalkorDB` to be the backend for graph storage in LlamaIndex."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f4d0dbf",
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-llms-openai\n",
"%pip install llama-index-graph-stores-falkordb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1a9eb90-335c-4214-8bb6-fd1edbe3ccbd",
"metadata": {},
"outputs": [],
"source": [
"# My OpenAI Key\n",
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"API_KEY_HERE\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88a9f2e3-c729-455a-a338-2f83776c1d4c",
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"import sys\n",
"\n",
"logging.basicConfig(stream=sys.stdout, level=logging.INFO)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "be3f7baa-1c0a-430b-981b-83ddca9e71f2",
"metadata": {},
"source": [
"## Using Knowledge Graph with FalkorDBGraphStore"
]
},
{
"cell_type": "markdown",
"id": "554d96c3",
"metadata": {},
"source": [
"### Start FalkorDB\n",
"\n",
"The easiest way to start FalkorDB as a Graph database is using the [falkordb](https://hub.docker.com/r/falkordb/falkordb:edge) docker image.\n",
"\n",
"To follow every step of this tutorial, launch the image as follows:\n",
"\n",
"```bash\n",
"docker run -p 6379:6379 -it --rm falkordb/falkordb:edge\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71c8a77a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:numexpr.utils:NumExpr defaulting to 8 threads.\n"
]
}
],
"source": [
"from llama_index.graph_stores.falkordb import FalkorDBGraphStore\n",
"\n",
"graph_store = FalkorDBGraphStore(\n",
" \"redis://localhost:6379\", decode_responses=True\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "75f1d565-04e8-41bc-9165-166dc89b6b47",
"metadata": {},
"source": [
"#### Building the Knowledge Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d0b2364-4806-4656-81e7-3f6e4b910b5b",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import SimpleDirectoryReader, KnowledgeGraphIndex\n",
"\n",
"from llama_index.llms.openai import OpenAI\n",
"from llama_index.core import Settings\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c297fd3-3424-41d8-9d0d-25fe6310ab62",
"metadata": {},
"outputs": [],
"source": [
"documents = SimpleDirectoryReader(\n",
" \"../../../../examples/paul_graham_essay/data\"\n",
").load_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61679142-7595-492b-8792-26cbc439caf8",
"metadata": {},
"outputs": [],
"source": [
"# define LLM\n",
"\n",
"llm = OpenAI(temperature=0, model=\"gpt-3.5-turbo\")\n",
"Settings.llm = llm\n",
"Settings.chunk_size = 512"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "370fd08f-56ff-4c24-b0c4-c93116a6d482",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import StorageContext\n",
"\n",
"storage_context = StorageContext.from_defaults(graph_store=graph_store)\n",
"\n",
"# NOTE: can take a while!\n",
"index = KnowledgeGraphIndex.from_documents(\n",
" documents,\n",
" max_triplets_per_chunk=2,\n",
" storage_context=storage_context,\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c39a0eeb-ef16-4982-8ba8-b37c2c5f4437",
"metadata": {},
"source": [
"#### Querying the Knowledge Graph\n",
"\n",
"First, we can query and send only the triplets to the LLM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "670300d8-d0a8-4201-bbcd-4a74b199fcdd",
"metadata": {},
"outputs": [],
"source": [
"query_engine = index.as_query_engine(\n",
" include_text=False, response_mode=\"tree_summarize\"\n",
")\n",
"response = query_engine.query(\n",
" \"Tell me more about Interleaf\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eecf2d57-3efa-4b0d-941a-95438d42893c",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Interleaf is a software company that was founded in 1981. It specialized in developing and selling desktop publishing software. The company's flagship product was called Interleaf, which was a powerful tool for creating and publishing complex documents. Interleaf's software was widely used in industries such as aerospace, defense, and government, where there was a need for creating technical documentation and manuals. The company was acquired by BroadVision in 2000."
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(Markdown(f\"{response}\"))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9eeaba8a",
"metadata": {},
"source": [
"For more detailed answers, we can also send the text from where the retrieved tripets were extracted."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd14686d-1c53-4637-9340-3745f2121ae2",
"metadata": {},
"outputs": [],
"source": [
"query_engine = index.as_query_engine(\n",
" include_text=True, response_mode=\"tree_summarize\"\n",
")\n",
"response = query_engine.query(\n",
" \"Tell me more about Interleaf\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4c87d14-d2d8-4d80-89f6-1e5972973528",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Interleaf was a company that had smart people and built impressive technology. However, it faced challenges and eventually got crushed by Moore's Law. The exponential growth in the power of commodity processors, particularly Intel processors, in the 1990s led to the consolidation of high-end, special-purpose hardware and software companies. Interleaf was one of the casualties of this trend. While the company had talented individuals and advanced technology, it was unable to compete with the rapid advancements in processor power."
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(Markdown(f\"{response}\"))"
]
},
{
"cell_type": "markdown",
"id": "9225c416",
"metadata": {},
"source": [
"#### Visualizing the Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9505a3a0",
"metadata": {},
"outputs": [],
"source": [
"%pip install pyvis"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d2341a1",
"metadata": {},
"outputs": [],
"source": [
"## create graph\n",
"from pyvis.network import Network\n",
"\n",
"g = index.get_networkx_graph()\n",
"net = Network(notebook=True, cdn_resources=\"in_line\", directed=True)\n",
"net.from_nx(g)\n",
"net.show(\"falkordbgraph_draw.html\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}