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

feat: 2525 neo4j init graph notebooks #2526

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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": [
"<img width=\"8%\" alt=\"LangChain.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/LangChain.png\" style=\"border-radius: 15%\">"
]
},
{
"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
}
Loading
Loading