From a77692b8e534a0e5818ed30d3d7e950772d180a5 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Thu, 23 May 2024 15:56:28 +0200 Subject: [PATCH 1/2] feat: langchain import cypher --- ...her_Query_and_Use_GraphCypherQAChain.ipynb | 306 ++++++++++++++++++ ...n_Import_Cypher_Query_from_Text_File.ipynb | 266 +++++++++++++++ 2 files changed, 572 insertions(+) create mode 100644 LangChain/LangChain_Import_Cypher_Query_and_Use_GraphCypherQAChain.ipynb create mode 100644 LangChain/LangChain_Import_Cypher_Query_from_Text_File.ipynb diff --git a/LangChain/LangChain_Import_Cypher_Query_and_Use_GraphCypherQAChain.ipynb b/LangChain/LangChain_Import_Cypher_Query_and_Use_GraphCypherQAChain.ipynb new file mode 100644 index 0000000000..21be949593 --- /dev/null +++ b/LangChain/LangChain_Import_Cypher_Query_and_Use_GraphCypherQAChain.ipynb @@ -0,0 +1,306 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d57803c7-fcf3-4521-8073-d246ba9206f3", + "metadata": { + "execution": { + "iopub.execute_input": "2021-02-23T14:22:16.610471Z", + "iopub.status.busy": "2021-02-23T14:22:16.610129Z", + "iopub.status.idle": "2021-02-23T14:22:16.627784Z", + "shell.execute_reply": "2021-02-23T14:22:16.626866Z", + "shell.execute_reply.started": "2021-02-23T14:22:16.610384Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "\"LangChain.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "bafb077c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# LangChain - Import Cypher Query and Use GraphCypherQAChain" + ] + }, + { + "cell_type": "markdown", + "id": "cb407ea8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #langchain #neo4j #cypher #txt #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "3fa65002", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "c7851585", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2024-05-22 (Created: 2024-05-22)" + ] + }, + { + "cell_type": "markdown", + "id": "188a2da0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook imports a Cypher query into Neo4j and asks a question using GraphCypherQAChain." + ] + }, + { + "cell_type": "markdown", + "id": "498e71dd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "d2366b9a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "try:\n", + " from langchain.graphs import Neo4jGraph\n", + " from langchain.chains import GraphCypherQAChain\n", + "except:\n", + " !pip install langchain==0.1.13 --user\n", + " from langchain.graphs import Neo4jGraph\n", + " from langchain.chains import GraphCypherQAChain\n", + "try:\n", + " from langchain_openai import ChatOpenAI\n", + "except:\n", + " !pip install langchain-openai=0.1.1 --user\n", + " from langchain_openai import ChatOpenAI\n", + "import requests\n", + "import os" + ] + }, + { + "cell_type": "markdown", + "id": "4acf67a5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `url`: URL to connect to the Neo4j database\n", + "- `user`: Username for the Neo4j database\n", + "- `password`: Password for the Neo4j database\n", + "- `os.environ['OPENAI_API_KEY']`: Environment variable for the OpenAI API key\n", + "- `graph_source_url`: URL to the json file containing the graph database structure\n", + "- `question`: The query question to be asked in the Cypher language" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e98a53eb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "url = \"bolt://localhost:7687\"\n", + "user = \"neo4j\"\n", + "password = \"password\"\n", + "os.environ['OPENAI_API_KEY'] = \"YOUR_OPENAI_API_KEY\"\n", + "graph_source_url = \"https://gist.githubusercontent.com/tomasonjo/08dc8ba0e19d592c4c3cde40dd6abcc3/raw/e90b0c9386bf8be15b199e8ac8f83fc265a2ac57/microservices.json\"\n", + "question = \"How will recommendation service be updated?\"" + ] + }, + { + "cell_type": "markdown", + "id": "f91368ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "34fbede8-2b03-41ae-8b5b-0c011aa390f0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Get graph dataset" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78e78369-2cf4-4798-85bf-3dd609f54314", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "import_query = requests.get(graph_source_url).json().get('query')\n", + "import_query" + ] + }, + { + "cell_type": "markdown", + "id": "46939e8a-fe8d-4a85-b4dc-9b9bdf35d180", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import graph into Neo4j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24da5209-79f5-4341-8bd5-5b6da47a5a87", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "graph = Neo4jGraph(\n", + " url=url, \n", + " username=username, \n", + " password=password\n", + ")\n", + "graph.query(\n", + " import_query\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "5bc82b85-83b0-4fda-b9e6-d23ddd64d834", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "8bd9b44c-bc05-442c-803f-f4ff74e18717", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### GraphCypherQAChain" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42800eeb-c86b-4484-9147-ac9244f879cd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "graph.refresh_schema()\n", + "\n", + "cypher_chain = GraphCypherQAChain.from_llm(\n", + " cypher_llm=ChatOpenAI(temperature=0, model_name='gpt-4'),\n", + " qa_llm=ChatOpenAI(temperature=0), graph=graph, verbose=True,\n", + ")\n", + "\n", + "cypher_chain.invoke(\n", + " question\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7ca70c6-7329-4020-aaa3-26a76524746d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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" + }, + "naas": { + "notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a", + "notebook_path": "LangChain/LangChain_Create_Agent.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/LangChain/LangChain_Import_Cypher_Query_from_Text_File.ipynb b/LangChain/LangChain_Import_Cypher_Query_from_Text_File.ipynb new file mode 100644 index 0000000000..ff61830468 --- /dev/null +++ b/LangChain/LangChain_Import_Cypher_Query_from_Text_File.ipynb @@ -0,0 +1,266 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d57803c7-fcf3-4521-8073-d246ba9206f3", + "metadata": { + "execution": { + "iopub.execute_input": "2021-02-23T14:22:16.610471Z", + "iopub.status.busy": "2021-02-23T14:22:16.610129Z", + "iopub.status.idle": "2021-02-23T14:22:16.627784Z", + "shell.execute_reply": "2021-02-23T14:22:16.626866Z", + "shell.execute_reply.started": "2021-02-23T14:22:16.610384Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "\"LangChain.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "bafb077c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# LangChain - Import Cypher Query from Text File" + ] + }, + { + "cell_type": "markdown", + "id": "cb407ea8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #langchain #neo4j #cypher #txt #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "3fa65002", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "c7851585", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2024-05-23 (Created: 2024-05-23)" + ] + }, + { + "cell_type": "markdown", + "id": "188a2da0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook imports a Cypher query into Neo4j from a text file." + ] + }, + { + "cell_type": "markdown", + "id": "498e71dd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "d2366b9a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "try:\n", + " from langchain.graphs import Neo4jGraph\n", + " from langchain.chains import GraphCypherQAChain\n", + "except:\n", + " !pip install langchain==0.1.13 --user\n", + " from langchain.graphs import Neo4jGraph\n", + " from langchain.chains import GraphCypherQAChain\n", + "try:\n", + " from langchain_openai import ChatOpenAI\n", + "except:\n", + " !pip install langchain-openai=0.1.1 --user\n", + " from langchain_openai import ChatOpenAI\n", + "import requests\n", + "import os" + ] + }, + { + "cell_type": "markdown", + "id": "4acf67a5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `url`: URL to connect to the Neo4j database\n", + "- `user`: Username for the Neo4j database\n", + "- `password`: Password for the Neo4j database\n", + "- `os.environ['OPENAI_API_KEY']`: Environment variable for the OpenAI API key\n", + "- `input_file_path`: Path of the input file containing the Cypher query." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e98a53eb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "url = \"bolt://localhost:7687\"\n", + "user = \"neo4j\"\n", + "password = \"password\"\n", + "os.environ['OPENAI_API_KEY'] = \"YOUR_OPENAI_API_KEY\"\n", + "input_file_path = \"cypher_query.txt\"" + ] + }, + { + "cell_type": "markdown", + "id": "f91368ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "34fbede8-2b03-41ae-8b5b-0c011aa390f0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Open and read a text file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78e78369-2cf4-4798-85bf-3dd609f54314", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "def read_txt_file(file_path):\n", + " with open(file_path, 'r') as f:\n", + " data = f.read()\n", + " return data\n", + "\n", + "import_query = read_txt_file(input_file_path)\n", + "import_query" + ] + }, + { + "cell_type": "markdown", + "id": "5bc82b85-83b0-4fda-b9e6-d23ddd64d834", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "46939e8a-fe8d-4a85-b4dc-9b9bdf35d180", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import graph into Neo4j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24da5209-79f5-4341-8bd5-5b6da47a5a87", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "graph = Neo4jGraph(\n", + " url=url, \n", + " username=username, \n", + " password=password\n", + ")\n", + "graph.query(import_query)" + ] + } + ], + "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" + }, + "naas": { + "notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a", + "notebook_path": "LangChain/LangChain_Create_Agent.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 39bf5a755067c02eddeeb5f0d6c86c2391e9cd13 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Thu, 23 May 2024 17:33:27 +0200 Subject: [PATCH 2/2] feat: init neo4j templates --- Neo4j/Neo4j_Create_Node.ipynb | 312 ++++++++++++++++++ Neo4j/Neo4j_Create_Relationship.ipynb | 279 ++++++++++++++++ ...Neo4j_Get_Properties_Types_from_Node.ipynb | 272 +++++++++++++++ 3 files changed, 863 insertions(+) create mode 100644 Neo4j/Neo4j_Create_Node.ipynb create mode 100644 Neo4j/Neo4j_Create_Relationship.ipynb create mode 100644 Neo4j/Neo4j_Get_Properties_Types_from_Node.ipynb diff --git a/Neo4j/Neo4j_Create_Node.ipynb b/Neo4j/Neo4j_Create_Node.ipynb new file mode 100644 index 0000000000..a87f4a978a --- /dev/null +++ b/Neo4j/Neo4j_Create_Node.ipynb @@ -0,0 +1,312 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "143c505b", + "metadata": { + "execution": { + "iopub.execute_input": "2021-02-23T14:22:16.610471Z", + "iopub.status.busy": "2021-02-23T14:22:16.610129Z", + "iopub.status.idle": "2021-02-23T14:22:16.627784Z", + "shell.execute_reply": "2021-02-23T14:22:16.626866Z", + "shell.execute_reply.started": "2021-02-23T14:22:16.610384Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Neo4j.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "bafb077c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Neo4j - Create Node" + ] + }, + { + "cell_type": "markdown", + "id": "cb407ea8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #neo4j #knowledgegraph #node #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "3fa65002", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "c7851585", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2024-05-22 (Created: 2024-05-22)" + ] + }, + { + "cell_type": "markdown", + "id": "188a2da0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook creates a node with properties in Neo4j using a Cypher query." + ] + }, + { + "cell_type": "markdown", + "id": "498e71dd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "d2366b9a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from neo4j import GraphDatabase\n", + "from datetime import datetime" + ] + }, + { + "cell_type": "markdown", + "id": "4acf67a5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `url`: Database connection URL\n", + "- `user`: Database username\n", + "- `password`: Database password\n", + "- `node_type`: Node type to be updated\n", + "- `node_id`: Identifier of the node to be updated\n", + "- `node_id_label`: Label for the node identifier\n", + "- `properties`: Custom properties for the node\n", + "- `output_file_path`: Path for the output file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e98a53eb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "url = \"bolt://localhost:7687\"\n", + "user = \"neo4j\"\n", + "password = \"password\"\n", + "node_type = \"Person\" # Type of node to update\n", + "node_id = \"John\" # Identifier of node to update\n", + "node_id_label = \"id\"\n", + "properties = {\"name\": \"John\", \"age\": 30, \"city\": \"New York\", \"birthdate\": datetime(1990, 5, 17).strftime(\"%Y-%m-%dT%H:%M:%S\")} # Custom properties for node\n", + "output_file_path = 'query_output.txt' # Output file path" + ] + }, + { + "cell_type": "markdown", + "id": "f91368ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "f1fa2ed4-5944-41e1-99f9-0fb2288e0683", + "metadata": {}, + "source": [ + "### Connect to GraphDatabase" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4f3bf28-1661-4a17-98c0-02140aead632", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "driver = GraphDatabase.driver(url, auth=(username, password))" + ] + }, + { + "cell_type": "markdown", + "id": "6223fb35", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Create a node with custom type and properties in Neo4j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c164f0e3-3c2d-4c5d-ae5c-71df38cb4223", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def create_node(\n", + " driver,\n", + " node_type,\n", + " node_id,\n", + " node_id_label=\"id\",\n", + " properties=[],\n", + " output_file_path='cypher_query.txt'\n", + "):\n", + " # Function to create a node with custom type and properties in Neo4j\n", + " def create_custom_node(tx, node_type, node_id, node_id_label, properties):\n", + " # Prepare properties string for Cypher query\n", + " properties_list = []\n", + " for key, value in properties.items():\n", + " if 'date' in key:\n", + " properties_list.append(f'n.{key} = datetime(\"{value}\")')\n", + " elif isinstance(value, int) or isinstance(value, float):\n", + " properties_list.append(f'n.{key} = {value}')\n", + " else:\n", + " properties_list.append(f'n.{key} = \"{value}\"')\n", + " properties_str = ', '.join(properties_list)\n", + " \n", + " # Create cypher query\n", + " cypher_query = f\"MERGE (n:{node_type} {{{node_id_label}: '{node_id}'}}) SET {properties_str}\"\n", + " tx.run(cypher_query)\n", + " with open(output_file_path, 'w') as f: # Open the output file in write mode\n", + " f.write(cypher_query) # Write the query to the output file\n", + "\n", + " ### Use the driver to create a session and run the function\n", + " with driver.session() as session:\n", + " session.execute_write(create_custom_node, node_type, node_id, node_id_label, properties)\n", + "\n", + " ## Output\n", + " ### Print success message\n", + " print(\"Node created successfully.\")\n", + " print(f\"Cypher query stored in {output_file_path}.\")\n", + " \n", + "create_node(\n", + " driver,\n", + " node_type,\n", + " node_id,\n", + " node_id_label,\n", + " properties,\n", + " output_file_path\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "8dfea0ee", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "cde6062b-7b01-473e-a2e7-2c76937e257f", + "metadata": {}, + "source": [ + "### Display cypher query" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ca6b668-9c69-4d00-bc17-fbe47c3dfd22", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "with open(output_file_path, 'r') as f:\n", + " data = f.read()\n", + "print(f\"Cypher query: {data}\")" + ] + } + ], + "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" + }, + "naas": { + "notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a", + "notebook_path": "LangChain/LangChain_Create_Agent.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Neo4j/Neo4j_Create_Relationship.ipynb b/Neo4j/Neo4j_Create_Relationship.ipynb new file mode 100644 index 0000000000..04c7e43fee --- /dev/null +++ b/Neo4j/Neo4j_Create_Relationship.ipynb @@ -0,0 +1,279 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "143c505b", + "metadata": { + "execution": { + "iopub.execute_input": "2021-02-23T14:22:16.610471Z", + "iopub.status.busy": "2021-02-23T14:22:16.610129Z", + "iopub.status.idle": "2021-02-23T14:22:16.627784Z", + "shell.execute_reply": "2021-02-23T14:22:16.626866Z", + "shell.execute_reply.started": "2021-02-23T14:22:16.610384Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Neo4j.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "bafb077c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Neo4j - Create Relationship" + ] + }, + { + "cell_type": "markdown", + "id": "cb407ea8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #neo4j #knowledgegraph #node #relationship #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "3fa65002", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "c7851585", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2024-05-22 (Created: 2024-05-22)" + ] + }, + { + "cell_type": "markdown", + "id": "188a2da0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook create a relationship between two nodes in Neo4j." + ] + }, + { + "cell_type": "markdown", + "id": "498e71dd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "d2366b9a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from neo4j import GraphDatabase" + ] + }, + { + "cell_type": "markdown", + "id": "4acf67a5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `url`: URL of the Neo4j database server.\n", + "- `user`: Username for the Neo4j database.\n", + "- `password`: Password for the Neo4j database.\n", + "- `node_type_1`: type of the first node for the relationship.\n", + "- `node_identifier_1`: Identifier of the first node for the relationship.\n", + "- `node_type_2`: Type of the second node for the relationship.\n", + "- `node_identifier_2`: Identifier of the second node for the relationship.\n", + "- `relationship_type`: Type of the relationship to be created between the nodes.\n", + "- `output_file_path`: Path of the output file where the query will be stored." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e98a53eb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "url = \"bolt://localhost:7687\"\n", + "user = \"neo4j\"\n", + "password = \"password\"\n", + "node_type_1 = \"Person\" # Type of the first node\n", + "node_identifier_1 = \"John\" # Identifier of the first node\n", + "node_type_2 = \"City\" # Type of the second node\n", + "node_identifier_2 = \"New York\" # Identifier of the second node\n", + "relationship_type = \"LIVES_IN\" # Type of the relationship\n", + "output_file_path = 'query_output.txt' # Output file path" + ] + }, + { + "cell_type": "markdown", + "id": "f91368ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "f1fa2ed4-5944-41e1-99f9-0fb2288e0683", + "metadata": {}, + "source": [ + "### Connect to GraphDatabase" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4f3bf28-1661-4a17-98c0-02140aead632", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "driver = GraphDatabase.driver(url, auth=(username, password))" + ] + }, + { + "cell_type": "markdown", + "id": "6223fb35", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Create a relationship between two nodes in Neo4j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c164f0e3-3c2d-4c5d-ae5c-71df38cb4223", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "### Function to create a relationship between two nodes in Neo4j\n", + "def create_relationship(tx, node_type_1, node_identifier_1, node_type_2, node_identifier_2, relationship_type, output_file_path):\n", + " cypher_query = f\"MERGE (a:{node_type_1} {{name: '{node_identifier_1}'}}) MERGE (b:{node_type_2} {{name: '{node_identifier_2}'}}) MERGE (a)-[r:{relationship_type}]->(b)\"\n", + " tx.run(cypher_query)\n", + " with open(output_file_path, 'w') as f: # Open the output file in write mode\n", + " f.write(cypher_query.replace(\"\\n\", \"\").strip()) # Write the query to the output file\n", + "\n", + "### Use the driver to create a session and run the function\n", + "with driver.session() as session:\n", + " session.execute_write(create_relationship, node_type_1, node_identifier_1, node_type_2, node_identifier_2, relationship_type, output_file_path)" + ] + }, + { + "cell_type": "markdown", + "id": "8dfea0ee", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "cde6062b-7b01-473e-a2e7-2c76937e257f", + "metadata": {}, + "source": [ + "### Display cypher query" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ca6b668-9c69-4d00-bc17-fbe47c3dfd22", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "with open(output_file_path, 'r') as f:\n", + " data = f.read()\n", + "print(f\"Cypher query: {data}\")" + ] + } + ], + "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" + }, + "naas": { + "notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a", + "notebook_path": "LangChain/LangChain_Create_Agent.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Neo4j/Neo4j_Get_Properties_Types_from_Node.ipynb b/Neo4j/Neo4j_Get_Properties_Types_from_Node.ipynb new file mode 100644 index 0000000000..646c595822 --- /dev/null +++ b/Neo4j/Neo4j_Get_Properties_Types_from_Node.ipynb @@ -0,0 +1,272 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "143c505b", + "metadata": { + "execution": { + "iopub.execute_input": "2021-02-23T14:22:16.610471Z", + "iopub.status.busy": "2021-02-23T14:22:16.610129Z", + "iopub.status.idle": "2021-02-23T14:22:16.627784Z", + "shell.execute_reply": "2021-02-23T14:22:16.626866Z", + "shell.execute_reply.started": "2021-02-23T14:22:16.610384Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Neo4j.png\"" + ] + }, + { + "cell_type": "markdown", + "id": "bafb077c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Neo4j - Get Properties Types from Node" + ] + }, + { + "cell_type": "markdown", + "id": "cb407ea8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #neo4j #knowledgegraph #node #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "3fa65002", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "c7851585", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2024-05-22 (Created: 2024-05-22)" + ] + }, + { + "cell_type": "markdown", + "id": "188a2da0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook retrieve the types of properties from a given node in Neo4j." + ] + }, + { + "cell_type": "markdown", + "id": "498e71dd", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "d2366b9a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from neo4j import GraphDatabase" + ] + }, + { + "cell_type": "markdown", + "id": "4acf67a5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- `url`: URL of the database\n", + "- `user`: Username for the database\n", + "- `password`: Password for the database\n", + "- `node_type`: Type of nodes to retrieve properties from" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e98a53eb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "url = \"bolt://localhost:7687\"\n", + "user = \"neo4j\"\n", + "password = \"password\"\n", + "node_type = \"Person\" # The type of nodes to retrieve properties from" + ] + }, + { + "cell_type": "markdown", + "id": "f91368ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "f1fa2ed4-5944-41e1-99f9-0fb2288e0683", + "metadata": {}, + "source": [ + "### Connect to GraphDatabase" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4f3bf28-1661-4a17-98c0-02140aead632", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "driver = GraphDatabase.driver(url, auth=(username, password))" + ] + }, + { + "cell_type": "markdown", + "id": "6223fb35", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Retrieve the types of properties from given nodes in Neo4j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c164f0e3-3c2d-4c5d-ae5c-71df38cb4223", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Function to retrieve the types of properties from given nodes in Neo4j\n", + "def get_property_types(tx, node_type):\n", + " cypher_query = f\"MATCH (n:{node_type}) RETURN properties(n) AS properties LIMIT 1\"\n", + " result = tx.run(cypher_query)\n", + " # Get the first record\n", + " record = result.single()\n", + " # Get the properties from the record\n", + " properties = record['properties']\n", + " # Get the types of the properties\n", + " property_types = {key: type(value).__name__ for key, value in properties.items()}\n", + " return property_types\n", + "\n", + "# Use the driver to create a session and run the function\n", + "with driver.session() as session:\n", + " property_types = session.execute_read(get_property_types, node_type)" + ] + }, + { + "cell_type": "markdown", + "id": "8dfea0ee", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "a4fbaf59-fb42-4814-a176-e968ea7a4c94", + "metadata": {}, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f2697ee-8545-48e3-b611-70d5969718d1", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "print(property_types)" + ] + } + ], + "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" + }, + "naas": { + "notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a", + "notebook_path": "LangChain/LangChain_Create_Agent.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}