1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "677867a3",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import GraphCypherQAChain\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.graphs import Neo4jGraph\n",
"from langchain.callbacks import get_openai_callback\n",
"from dotenv import load_dotenv\n",
"import os\n",
"import openai\n",
"import pandas as pd\n",
"from neo4j.exceptions import CypherSyntaxError\n"
]
},
{
"cell_type": "markdown",
"id": "186eb11d",
"metadata": {},
"source": [
"## Choose the LLM"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fe18f3fc",
"metadata": {},
"outputs": [],
"source": [
"LLM_MODEL = 'gpt-4-32k'\n"
]
},
{
"cell_type": "markdown",
"id": "1a040044",
"metadata": {},
"source": [
"## Custom function for neo4j RAG chain"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ead42cd3",
"metadata": {},
"outputs": [],
"source": [
"def get_neo4j_cypher_rag_chain():\n",
" load_dotenv(os.path.join(os.path.expanduser('~'), '.spoke_neo4j_config.env'))\n",
" username = os.environ.get('NEO4J_USER')\n",
" password = os.environ.get('NEO4J_PSW')\n",
" url = os.environ.get('NEO4J_URI')\n",
" database = os.environ.get('NEO4J_DB')\n",
"\n",
" graph = Neo4jGraph(\n",
" url=url, \n",
" username=username, \n",
" password=password,\n",
" database = database\n",
" )\n",
"\n",
" load_dotenv(os.path.join(os.path.expanduser('~'), '.gpt_config.env'))\n",
" API_KEY = os.environ.get('API_KEY')\n",
" API_VERSION = os.environ.get('API_VERSION')\n",
" RESOURCE_ENDPOINT = os.environ.get('RESOURCE_ENDPOINT')\n",
" openai.api_type = \"azure\"\n",
" openai.api_key = API_KEY\n",
" openai.api_base = RESOURCE_ENDPOINT\n",
" openai.api_version = API_VERSION\n",
" chat_deployment_id = LLM_MODEL\n",
" chat_model_id = chat_deployment_id\n",
" temperature = 0\n",
" chat_model = ChatOpenAI(openai_api_key=API_KEY, \n",
" engine=chat_deployment_id, \n",
" temperature=temperature)\n",
" chain = GraphCypherQAChain.from_llm(\n",
" chat_model, \n",
" graph=graph, \n",
" verbose=True, \n",
" validate_cypher=True,\n",
" return_intermediate_steps=True\n",
" )\n",
" return chain"
]
},
{
"cell_type": "markdown",
"id": "cc863aed",
"metadata": {},
"source": [
"## Initiate neo4j RAG chain"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9866fd11",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING! engine is not default parameter.\n",
" engine was transferred to model_kwargs.\n",
" Please confirm that engine is what you intended.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 17.5 ms, sys: 3.65 ms, total: 21.2 ms\n",
"Wall time: 28.1 s\n"
]
}
],
"source": [
"%%time\n",
"neo4j_rag_chain = get_neo4j_cypher_rag_chain()\n"
]
},
{
"cell_type": "markdown",
"id": "3827d5c4",
"metadata": {},
"source": [
"## Enter question -1 "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7b9837c5",
"metadata": {},
"outputs": [],
"source": [
"question = \"Is Parkinson's disease associated with PINK1 gene?\"\n"
]
},
{
"cell_type": "markdown",
"id": "23e669ed",
"metadata": {},
"source": [
"## Run cypher RAG"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9f685b8e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
"Generated Cypher:\n",
"\u001b[32;1m\u001b[1;3mMATCH (d:Disease {name: \"Parkinson's disease\"}), (g:Gene {name: \"PINK1\"}) \n",
"RETURN EXISTS((d)-[:ASSOCIATES_DaG]->(g)) AS is_associated\u001b[0m\n",
"Full Context:\n",
"\u001b[32;1m\u001b[1;3m[{'is_associated': True}]\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"Yes, Parkinson's disease is associated with the PINK1 gene.\n"
]
}
],
"source": [
"\n",
"out = neo4j_rag_chain.run(query=question, return_final_only=True, verbose=True)\n",
"print(out)\n"
]
},
{
"cell_type": "markdown",
"id": "069bc2f2",
"metadata": {},
"source": [
"## Question 1 perturbed (all smallcase)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "4cf3363b",
"metadata": {},
"outputs": [],
"source": [
"question = \"Is parkinson's disease associated with pink1 gene?\"\n"
]
},
{
"cell_type": "markdown",
"id": "487fd17f",
"metadata": {},
"source": [
"## Run cypher RAG"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "61d5eb0b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
"Generated Cypher:\n",
"\u001b[32;1m\u001b[1;3mMATCH (d:Disease {name: \"Parkinson's Disease\"}), (g:Gene {name: \"PINK1\"}) \n",
"RETURN EXISTS((d)-[:ASSOCIATES_DaG]->(g)) AS is_associated\u001b[0m\n",
"Full Context:\n",
"\u001b[32;1m\u001b[1;3m[]\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"I'm sorry, but I don't have the information to answer that question.\n"
]
}
],
"source": [
"\n",
"out = neo4j_rag_chain.run(query=question, return_final_only=True, verbose=True)\n",
"print(out)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6123c1a7",
"metadata": {},
"outputs": [],
"source": []
}
],
"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",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|