From e840f347bb4d443b777fd4791e2e8ff76cabc122 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Sun, 8 Oct 2023 09:35:59 +0200 Subject: [PATCH 1/4] feat(LlamaIndex): Add Integrate with Neo4j --- .../LlamaIndex_Integrate_with_Neo4j.ipynb | 274 ++++++++++++++++++ 1 file changed, 274 insertions(+) create mode 100644 LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb diff --git a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb new file mode 100644 index 0000000000..293e9c88b8 --- /dev/null +++ b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb @@ -0,0 +1,274 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4f259ab0-d2ba-48bf-8974-a4213cd19d0f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "a934428e-1e3b-41a2-85d1-59806c2cc42d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# LlamaIndex - Integrate with Neo4j" + ] + }, + { + "cell_type": "markdown", + "id": "af41a9b9-06f7-46cb-baa6-6b45956e2492", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #llamaindex #neo4j #integration #graphstore #request #response" + ] + }, + { + "cell_type": "markdown", + "id": "be16c2bc-5c21-47d1-a78e-1a5b51fea065", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Firstname Lastname]()" + ] + }, + { + "cell_type": "markdown", + "id": "8f81429e-1c1a-401c-8456-bcd2812d7277", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-10-08 (Created: 2023-10-08)" + ] + }, + { + "cell_type": "markdown", + "id": "a12e4b96-ac57-4e66-83af-76f4f72f4213", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook demonstrates how to use Llamaindex and Neo4j as graph store to perform request and response model for the custom data. It is usefull for organizations to quickly integrate Neo4j with Llamaindex." + ] + }, + { + "cell_type": "markdown", + "id": "eb76efa2-6db0-4b8b-8065-74723b2ecffe", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n- [Llamaindex Documentation](https://docs.llamaindex.ai/en/stable/examples/index_structs/knowledge_graph/Neo4jKGIndexDemo.html)\n- [Neo4j Documentation](https://neo4j.com/docs/)" + ] + }, + { + "cell_type": "markdown", + "id": "00263711-51eb-4e16-bcec-97d1c303f5d8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "0779560c-1df7-40a2-8dcb-ea0bb03aa963", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "547886eb-f870-4b13-919e-b8736c5631b5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "import llamaindex\nimport neo4j", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "f25d1634-3d65-439d-b7f9-f6d3f4fe9bd8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n- `neo4j_url`: URL of the Neo4j instance\n- `neo4j_username`: Username of the Neo4j instance\n- `neo4j_password`: Password of the Neo4j instance\n- `neo4j_graph`: Graph name of the Neo4j instance" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ac2ad64-70c4-4c60-ad40-233666a0f618", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "neo4j_url = \"http://localhost:7474\"\nneo4j_username = \"neo4j\"\nneo4j_password = \"password\"\nneo4j_graph = \"graph\"", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "c64696e5-5b1d-4910-8316-6e83b68be7ec", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "6bf916ef-5226-4f5e-bad4-a120accd85b8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Create Neo4j instance" + ] + }, + { + "cell_type": "markdown", + "id": "3f12da2f-6471-42a8-8dda-a2b181e2df96", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "Long description of the function without break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fce4f2d9-7d36-4f26-b5a4-444a3c24fd38", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "# Create Neo4j instance\nneo4j_instance = neo4j.GraphDatabase.driver(\n neo4j_url, auth=(neo4j_username, neo4j_password)\n)", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "e7f50dec-241b-41bf-bc79-f5bfe6f577fe", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Create Llamaindex instance" + ] + }, + { + "cell_type": "markdown", + "id": "100f49f3-8c75-45df-ae99-7448c09f1919", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "Long description of the function without break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1b9e0fb0-55b1-40ef-a1f7-b57d7fb9db62", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "# Create Llamaindex instance\nllamaindex_instance = llamaindex.LlamaIndex(neo4j_instance, neo4j_graph)", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "e5907070-0e84-43b9-86aa-2542d22edb90", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "31ecde86-9879-4c25-8df3-a7227d9c57c7", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "799585a2-3e21-4391-99a6-161f9efecc46", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "# Display result\nprint(llamaindex_instance)", + "outputs": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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", + "version": "3.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From 671bf63f2ca98b3023d7077d7303f72494e32adc Mon Sep 17 00:00:00 2001 From: Mahimai Raja J Date: Tue, 17 Oct 2023 14:53:00 +0530 Subject: [PATCH 2/4] feat: Added LlamaIndex integration with Neo4j --- .../LlamaIndex_Integrate_with_Neo4j.ipynb | 176 +++++++++++++++--- 1 file changed, 151 insertions(+), 25 deletions(-) diff --git a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb index 293e9c88b8..feb1fbf2fc 100644 --- a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb +++ b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb @@ -19,7 +19,13 @@ "tags": [] }, "source": [ - "# LlamaIndex - Integrate with Neo4j" + "# LlamaIndex - Integrate with Neo4j\n", + "\n", + "\n", + "\n", + "

\n", + "\n", + "Give Feedback | Bug report" ] }, { @@ -41,7 +47,7 @@ "tags": [] }, "source": [ - "**Author:** [Firstname Lastname]()" + "**Author:** [Mahimai Raja J](http://github.com/mahimairaja)" ] }, { @@ -52,7 +58,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-10-08 (Created: 2023-10-08)" + "**Last update:** 2023-10-17 (Created: 2023-10-08)" ] }, { @@ -74,7 +80,12 @@ "tags": [] }, "source": [ - "**References:**\n- [Llamaindex Documentation](https://docs.llamaindex.ai/en/stable/examples/index_structs/knowledge_graph/Neo4jKGIndexDemo.html)\n- [Neo4j Documentation](https://neo4j.com/docs/)" + "**References:**\n", + "- [Llamaindex Documentation](https://docs.llamaindex.ai/en/stable/examples/index_structs/knowledge_graph/Neo4jKGIndexDemo.html)\n", + "- [Neo4j Documentation](https://neo4j.com/docs/)\n", + "- [data connectors](https://docs.llamaindex.ai/en/stable/core_modules/data_modules/connector/modules.html)\n", + "- [Neo4j Sandbox](https://sandbox.neo4j.com/)\n", + "- [AuroDS instance](https://console.neo4j.io/?product=aura-ds) \n" ] }, { @@ -107,8 +118,39 @@ "papermill": {}, "tags": [] }, - "source": "import llamaindex\nimport neo4j", - "outputs": [] + "outputs": [], + "source": [ + "try:\n", + " import llama_index\n", + "except ModuleNotFoundError:\n", + " !pip install -q llama-index\n", + " import llama_index\n", + "try:\n", + " import neo4j\n", + "except ModuleNotFoundError:\n", + " !pip install neo4j --user\n", + " import neo4j\n", + "\n", + "import os\n", + "import logging\n", + "import sys\n", + "from llama_index.llms import OpenAI\n", + "from llama_index import ServiceContext\n", + "from llama_index import (\n", + " KnowledgeGraphIndex,\n", + " LLMPredictor,\n", + " ServiceContext,\n", + " SimpleDirectoryReader,\n", + ")\n", + "from llama_index.storage.storage_context import StorageContext\n", + "from llama_index.graph_stores import Neo4jGraphStore\n", + "\n", + "\n", + "from llama_index.llms import OpenAI\n", + "from IPython.display import Markdown, display\n", + "\n", + "logging.basicConfig(stream=sys.stdout, level=logging.INFO)" + ] }, { "cell_type": "markdown", @@ -118,7 +160,11 @@ "tags": [] }, "source": [ - "### Setup variables\n- `neo4j_url`: URL of the Neo4j instance\n- `neo4j_username`: Username of the Neo4j instance\n- `neo4j_password`: Password of the Neo4j instance\n- `neo4j_graph`: Graph name of the Neo4j instance" + "### Setup variables\n", + "- `neo4j_url`: URL of the Neo4j instance\n", + "- `neo4j_username`: Username of the Neo4j instance\n", + "- `neo4j_password`: Password of the Neo4j instance\n", + "- `neo4j_graph`: Graph name of the Neo4j instance" ] }, { @@ -129,8 +175,16 @@ "papermill": {}, "tags": [] }, - "source": "neo4j_url = \"http://localhost:7474\"\nneo4j_username = \"neo4j\"\nneo4j_password = \"password\"\nneo4j_graph = \"graph\"", - "outputs": [] + "outputs": [], + "source": [ + "neo4j_url = \"http://localhost:7474\"\n", + "neo4j_username = \"neo4j\"\n", + "neo4j_password = \"password\"\n", + "neo4j_graph = \"graph\"\n", + "\n", + "# Add your api key here\n", + "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY_HERE\"" + ] }, { "cell_type": "markdown", @@ -143,6 +197,37 @@ "## Model" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b661990", + "metadata": {}, + "outputs": [], + "source": [ + "input_file_path = os.path.join(os.getcwd(), 'input.txt')\n", + "\n", + "if not os.path.exists(input_file_path):\n", + " data_url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'\n", + " with open(input_file_path, 'w') as f:\n", + " f.write(requests.get(data_url).text)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84cfaad7", + "metadata": {}, + "outputs": [], + "source": [ + "llm = OpenAI(temperature=0, model=\"gpt-3.5-turbo\")\n", + "\n", + "service_context = ServiceContext.from_defaults(llm=llm, chunk_size=512)\n", + "\n", + "documents = SimpleDirectoryReader(\n", + " input_file_path\n", + ").load_data()" + ] + }, { "cell_type": "markdown", "id": "6bf916ef-5226-4f5e-bad4-a120accd85b8", @@ -162,7 +247,7 @@ "tags": [] }, "source": [ - "Long description of the function without break" + "In Neo4j, graphs are a data modeling paradigm that uses nodes to represent entities and relationships to depict connections between them. These nodes and relationships can store properties, allowing for rich and flexible data representation. Neo4j's graph database engine provides efficient traversal and querying capabilities, making it a powerful tool for working with highly interconnected data." ] }, { @@ -173,8 +258,17 @@ "papermill": {}, "tags": [] }, - "source": "# Create Neo4j instance\nneo4j_instance = neo4j.GraphDatabase.driver(\n neo4j_url, auth=(neo4j_username, neo4j_password)\n)", - "outputs": [] + "outputs": [], + "source": [ + "# Create Neo4j instance\n", + "\n", + "neo4j_instance = Neo4jGraphStore(\n", + " username=neo4j_username,\n", + " password=neo4j_password,\n", + " url=neo4j_url,\n", + " database=neo4j_graph,\n", + ")" + ] }, { "cell_type": "markdown", @@ -195,19 +289,48 @@ "tags": [] }, "source": [ - "Long description of the function without break" + "A knowledge graph in Neo4j is a structured, interconnected data model that represents real-world information as nodes and relationships. It enables the storage and retrieval of complex relationships and semantic connections between data entities. Neo4j's graph database technology is well-suited for building, querying, and navigating knowledge graphs, making it an ideal choice for applications involving data with intricate interdependencies." ] }, { "cell_type": "code", "execution_count": null, - "id": "1b9e0fb0-55b1-40ef-a1f7-b57d7fb9db62", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": "# Create Llamaindex instance\nllamaindex_instance = llamaindex.LlamaIndex(neo4j_instance, neo4j_graph)", - "outputs": [] + "id": "b47adb68", + "metadata": {}, + "outputs": [], + "source": [ + "storage_context = StorageContext.from_defaults(graph_store=neo4j_instance)\n", + "\n", + "# NOTE: can take a while!\n", + "index = KnowledgeGraphIndex.from_documents(\n", + " documents,\n", + " storage_context=storage_context,\n", + " max_triplets_per_chunk=2,\n", + " service_context=service_context,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b13d6c2f", + "metadata": {}, + "outputs": [], + "source": [ + "input_query = input(\"Enter the query to perform RAG\\n\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5a7c726b", + "metadata": {}, + "outputs": [], + "source": [ + "llamaindex_instance = index.as_query_engine(include_text=True, response_mode=\"tree_summarize\")\n", + "\n", + "response = llamaindex_instance.query(input_query)" + ] }, { "cell_type": "markdown", @@ -239,13 +362,16 @@ "papermill": {}, "tags": [] }, - "source": "# Display result\nprint(llamaindex_instance)", - "outputs": [] + "outputs": [], + "source": [ + "# Display result\n", + "display(Markdown(f\"{response}\"))" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -259,7 +385,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.6" + "version": "3.7.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { @@ -271,4 +397,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From 7ef9cfaebe4df9acaa7287145b18d38ccda0b422 Mon Sep 17 00:00:00 2001 From: Mahimai Raja J Date: Sun, 22 Oct 2023 17:14:37 +0530 Subject: [PATCH 3/4] feat: Added LlamaIndex integration with Neo4j --- .../LlamaIndex_Integrate_with_Neo4j.ipynb | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb index feb1fbf2fc..80be9160c3 100644 --- a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb +++ b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb @@ -58,7 +58,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-10-17 (Created: 2023-10-08)" + "**Last update:** 2023-10-22 (Created: 2023-10-08)" ] }, { @@ -85,7 +85,8 @@ "- [Neo4j Documentation](https://neo4j.com/docs/)\n", "- [data connectors](https://docs.llamaindex.ai/en/stable/core_modules/data_modules/connector/modules.html)\n", "- [Neo4j Sandbox](https://sandbox.neo4j.com/)\n", - "- [AuroDS instance](https://console.neo4j.io/?product=aura-ds) \n" + "- [AuroDS instance](https://console.neo4j.io/?product=aura-ds) \n", + "- [Paged Attention - VLLM](https://arxiv.org/abs/2309.06180)" ] }, { @@ -128,8 +129,14 @@ "try:\n", " import neo4j\n", "except ModuleNotFoundError:\n", - " !pip install neo4j --user\n", + " !pip install -q neo4j-driver\n", " import neo4j\n", + "try:\n", + " import pypdf\n", + "except ModuleNotFoundError:\n", + " !pip install -q pypdf\n", + " import pypdf\n", + "\n", "\n", "import os\n", "import logging\n", @@ -177,13 +184,26 @@ }, "outputs": [], "source": [ - "neo4j_url = \"http://localhost:7474\"\n", + "neo4j_url = \"bolt://3.220.232.45:7687\"\n", "neo4j_username = \"neo4j\"\n", - "neo4j_password = \"password\"\n", - "neo4j_graph = \"graph\"\n", + "neo4j_password = \"papers-food-agents\"\n", + "neo4j_graph = \"neo4j\"\n", "\n", "# Add your api key here\n", - "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY_HERE\"" + "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "034a61ae", + "metadata": {}, + "outputs": [], + "source": [ + "input_file_path = os.path.join(os.getcwd(), \"data\")\n", + "\n", + "!mkdir data\n", + "!wget -O data/vllm.pdf --user-agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36\" https://arxiv.org/pdf/2309.06180.pdf" ] }, { @@ -197,21 +217,6 @@ "## Model" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "4b661990", - "metadata": {}, - "outputs": [], - "source": [ - "input_file_path = os.path.join(os.getcwd(), 'input.txt')\n", - "\n", - "if not os.path.exists(input_file_path):\n", - " data_url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'\n", - " with open(input_file_path, 'w') as f:\n", - " f.write(requests.get(data_url).text)" - ] - }, { "cell_type": "code", "execution_count": null, From 7f3700c9415b87ec58332e4af41810f6d3208b05 Mon Sep 17 00:00:00 2001 From: Mahimai Raja J Date: Wed, 25 Oct 2023 10:24:28 +0530 Subject: [PATCH 4/4] feat: Added LlamaIndex integration with Neo4j --- LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb index 80be9160c3..7d64bdaa08 100644 --- a/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb +++ b/LlamaIndex/LlamaIndex_Integrate_with_Neo4j.ipynb @@ -86,7 +86,16 @@ "- [data connectors](https://docs.llamaindex.ai/en/stable/core_modules/data_modules/connector/modules.html)\n", "- [Neo4j Sandbox](https://sandbox.neo4j.com/)\n", "- [AuroDS instance](https://console.neo4j.io/?product=aura-ds) \n", - "- [Paged Attention - VLLM](https://arxiv.org/abs/2309.06180)" + "- [Paged Attention - VLLM](https://arxiv.org/abs/2309.06180)\n", + "\n", + "\n", + "## Step to create Neo4j instance\n", + "\n", + "1. Create a Neo4j Sandbox instance from [here](https://sandbox.neo4j.com/) or Create a Neo4j Aura DS instance from [here](https://console.neo4j.io/?product=aura-ds).\n", + "\n", + "2. Login to the Neo4j instance and create a new graph.\n", + "\n", + "3. Copy the `neo4j_url`, `neo4j_username`, `neo4j_password` and `neo4j_graph` from the Neo4j instance and replace in the below code." ] }, { @@ -252,7 +261,7 @@ "tags": [] }, "source": [ - "In Neo4j, graphs are a data modeling paradigm that uses nodes to represent entities and relationships to depict connections between them. These nodes and relationships can store properties, allowing for rich and flexible data representation. Neo4j's graph database engine provides efficient traversal and querying capabilities, making it a powerful tool for working with highly interconnected data." + "In Neo4j, graphs are a data modeling paradigm that uses nodes to represent entities and relationships to depict connections between them. These nodes and relationships can store properties, allowing for rich and flexible data representation. Neo4j's graph database engine provides efficient traversal and querying capabilities, making it a powerful tool for working with highly interconnected data.\n" ] }, {