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
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4eb50795-d6a9-4339-a083-643f51a50911",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import AzureChatOpenAI\n",
"from dotenv import load_dotenv\n",
"import os\n",
"import openai\n",
"from langchain.chains import GraphCypherQAChain\n",
"from langchain.graphs import Neo4jGraph\n",
"from langchain.callbacks import get_openai_callback\n",
"import pandas as pd\n",
"from tqdm import tqdm\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "63905a82-6569-4e69-b757-04b187c33686",
"metadata": {},
"outputs": [],
"source": [
"LLM_MODEL = 'gpt-4'\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "678addb6-d6b9-4447-893c-fb15099c7bf6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/karthiksoman/anaconda3/envs/cypher_rag/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The class `AzureChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import AzureChatOpenAI`.\n",
" warn_deprecated(\n"
]
}
],
"source": [
"\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 = AzureChatOpenAI(openai_api_key=API_KEY, openai_api_version=API_VERSION, azure_deployment=chat_deployment_id, azure_endpoint=RESOURCE_ENDPOINT, temperature=temperature)\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_URL')\n",
"database = os.environ.get('NEO4J_DB')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "30a068c7-42d6-4f41-bce1-5fe2a38ace5a",
"metadata": {},
"outputs": [],
"source": [
"graph = Neo4jGraph(url=url, username=username, password=password, database = database)\n",
"chain = GraphCypherQAChain.from_llm(chat_model, graph=graph, verbose=True, validate_cypher=True,return_intermediate_steps=True)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5b844002-f139-4dcf-9ddb-ebf42fcee2f5",
"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: \"hypochondrogenesis\"})-[:ASSOCIATES_DaG]->(g:Gene) RETURN g.name\u001b[0m\n",
"Full Context:\n",
"\u001b[32;1m\u001b[1;3m[{'g.name': 'COL2A1'}]\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
}
],
"source": [
"question = \"What are the genes associated with hypochondrogenesis?\"\n",
"\n",
"cypher_rag_answer = chain.invoke({\"query\":question})\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "142b0c6d-b5b5-4c98-b5e3-c6f5f39bd30a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'query': 'What are the genes associated with hypochondrogenesis?',\n",
" 'result': 'The gene associated with hypochondrogenesis is COL2A1.',\n",
" 'intermediate_steps': [{'query': 'MATCH (d:Disease {name: \"hypochondrogenesis\"})-[:ASSOCIATES_DaG]->(g:Gene) RETURN g.name'},\n",
" {'context': [{'g.name': 'COL2A1'}]}]}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cypher_rag_answer"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4f99bfef-bcc2-462a-87b9-3c553978f8a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'query': 'MATCH (d:Disease {name: \"hypochondrogenesis\"})-[:ASSOCIATES_DaG]->(g:Gene) RETURN g.name'},\n",
" {'context': [{'g.name': 'COL2A1'}]}]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cypher_rag_answer[\"intermediate_steps\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2bd57161-2d8c-4c7e-99f5-09890b13fe27",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (cypher_rag)",
"language": "python",
"name": "cypher_rag"
},
"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
}
|