-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
332 lines (276 loc) · 12.1 KB
/
Copy pathmain.py
File metadata and controls
332 lines (276 loc) · 12.1 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
import tempfile
import streamlit as st
from dotenv import load_dotenv
from neo4j import GraphDatabase
import streamlit.components.v1 as components
from pyvis.network import Network
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import Document
from langchain.prompts import PromptTemplate
from langchain_community.vectorstores import Neo4jVector
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.graphs import Neo4jGraph
from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_community.chains.graph_qa.cypher import GraphCypherQAChain
def render_graph(graph):
query = """
MATCH (n)
OPTIONAL MATCH (n)-[r]->(m)
RETURN n, r, m
LIMIT 100
"""
results = graph.query(query)
net = Network(height="600px", width="100%", directed=True)
net.barnes_hut()
net.set_options("""
{
"nodes": {
"shape": "dot",
"size": 16,
"font": { "size": 14 }
},
"edges": {
"arrows": "to",
"font": { "size": 12 }
},
"physics": {
"enabled": true
}
}
""")
added_nodes = set()
added_edges = set()
for row in results:
# Handle tuple OR dict
if isinstance(row, tuple):
n, r, m = row
else:
n = row.get("n")
r = row.get("r")
m = row.get("m")
# ---------- NODE N ----------
if n:
if isinstance(n, dict):
n_id = str(n.get("id", n.get("elementId", "node")))
n_labels = n.get("labels", ["Node"])
n_props = n.get("properties", {})
else:
n_id = str(getattr(n, "id", getattr(n, "element_id", "node")))
n_labels = list(getattr(n, "labels", ["Node"]))
n_props = dict(n)
n_label = n_labels[0] if n_labels else "Node"
n_name = n_props.get("id") or n_props.get("name") or n_props.get("text") or n_id
if n_id not in added_nodes:
net.add_node(n_id, label=str(n_name), title=str(n_label))
added_nodes.add(n_id)
# ---------- NODE M ----------
if m:
if isinstance(m, dict):
m_id = str(m.get("id", m.get("elementId", "node")))
m_labels = m.get("labels", ["Node"])
m_props = m.get("properties", {})
else:
m_id = str(getattr(m, "id", getattr(m, "element_id", "node")))
m_labels = list(getattr(m, "labels", ["Node"]))
m_props = dict(m)
m_label = m_labels[0] if m_labels else "Node"
m_name = m_props.get("id") or m_props.get("name") or m_props.get("text") or m_id
if m_id not in added_nodes:
net.add_node(m_id, label=str(m_name), title=str(m_label))
added_nodes.add(m_id)
# ---------- EDGE ----------
if n and r and m:
if isinstance(r, dict):
rel_type = r.get("type", "RELATED_TO")
else:
rel_type = getattr(r, "type", "RELATED_TO")
source_id = str(n_id)
target_id = str(m_id)
edge_key = (source_id, target_id, rel_type)
if edge_key not in added_edges:
net.add_edge(source_id, target_id, label=rel_type)
added_edges.add(edge_key)
net.save_graph("graph.html")
with open("graph.html", "r", encoding="utf-8") as f:
html = f.read()
components.html(html, height=620, scrolling=True)
def main():
st.set_page_config(
layout="wide",
page_title="GraphMind",
page_icon=":graph:"
)
st.sidebar.image('logo.png', width='content')
with st.sidebar.expander("Expand Me"):
st.markdown("""
This application allows you to upload a PDF file, extract its content into a Neo4j graph database, and perform queries using natural language.
It leverages LangChain and OpenAI's GPT models to generate Cypher queries that interact with the Neo4j database in real-time.
""")
st.title("GraphMind: Real-Time GraphRAG App")
load_dotenv()
if "chat_history" not in st.session_state:
st.session_state["chat_history"] = []
# Set OpenAI API key
if 'OPENAI_API_KEY' not in st.session_state:
st.sidebar.subheader("OpenAI API Key")
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type='password')
if openai_api_key:
os.environ['OPENAI_API_KEY'] = openai_api_key
st.session_state['OPENAI_API_KEY'] = openai_api_key
st.sidebar.success("OpenAI API Key set successfully.")
embeddings = OpenAIEmbeddings()
llm = ChatOpenAI(model_name="gpt-4o") # Use model that supports function calling
st.session_state['embeddings'] = embeddings
st.session_state['llm'] = llm
else:
embeddings = st.session_state['embeddings']
llm = st.session_state['llm']
# Initialize variables
neo4j_url = None
neo4j_username = None
neo4j_password = None
graph = None
# Set Neo4j connection details
neo4j_database = st.sidebar.text_input("Neo4j Database:", value="406f38bf")
if 'neo4j_connected' not in st.session_state:
st.sidebar.subheader("Connect to Neo4j Database")
neo4j_url = st.sidebar.text_input("Neo4j URL:", value="neo4j+s://<your-neo4j-url>")
neo4j_username = st.sidebar.text_input("Neo4j Username:", value="neo4j")
neo4j_password = st.sidebar.text_input("Neo4j Password:", type='password')
connect_button = st.sidebar.button("Connect")
if connect_button and neo4j_password:
try:
graph = Neo4jGraph(
url=neo4j_url,
username=neo4j_username,
password=neo4j_password,
database=neo4j_database
)
st.session_state['graph'] = graph
st.session_state['neo4j_connected'] = True
# Store connection parameters for later use
st.session_state['neo4j_url'] = neo4j_url
st.session_state['neo4j_username'] = neo4j_username
st.session_state['neo4j_password'] = neo4j_password
st.session_state['neo4j_database'] = neo4j_database
st.sidebar.success("Connected to Neo4j database.")
except Exception as e:
st.error(f"Failed to connect to Neo4j: {e}")
else:
graph = st.session_state['graph']
neo4j_url = st.session_state['neo4j_url']
neo4j_username = st.session_state['neo4j_username']
neo4j_password = st.session_state['neo4j_password']
neo4j_database = st.session_state['neo4j_database']
# Ensure that the Neo4j connection is established before proceeding
if graph is not None:
# File uploader
uploaded_file = st.file_uploader("Please select a PDF file.", type="pdf")
if uploaded_file is not None:
with st.spinner("Processing the PDF..."):
st.session_state["chat_history"] = []
# Save uploaded file to temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
tmp_file.write(uploaded_file.read())
tmp_file_path = tmp_file.name
# Load and split the PDF
loader = PyPDFLoader(tmp_file_path)
pages = loader.load_and_split()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=40)
docs = text_splitter.split_documents(pages)
lc_docs = []
for doc in docs:
lc_docs.append(Document(page_content=doc.page_content.replace("\n", ""),
metadata={'source': uploaded_file.name}))
# Clear the graph database
cypher = """
MATCH (n)
DETACH DELETE n;
"""
graph.query(cypher)
# Define allowed nodes and relationships
allowed_nodes = ["Patient", "Disease", "Medication", "Test", "Symptom", "Doctor"]
allowed_relationships = ["HAS_DISEASE", "TAKES_MEDICATION", "UNDERWENT_TEST", "HAS_SYMPTOM", "TREATED_BY"]
# Transform documents into graph documents
transformer = LLMGraphTransformer(
llm=llm,
allowed_nodes=allowed_nodes,
allowed_relationships=allowed_relationships,
node_properties=False,
relationship_properties=False
)
graph_documents = transformer.convert_to_graph_documents(lc_docs)
graph.add_graph_documents(graph_documents, include_source=True)
# Use the stored connection parameters
index = Neo4jVector.from_existing_graph(
embedding=embeddings,
url=neo4j_url,
username=neo4j_username,
password=neo4j_password,
database=neo4j_database,
node_label="Patient",
text_node_properties=["id", "text"],
embedding_node_property="embedding",
index_name="vector_index",
keyword_index_name="entity_index",
search_type="hybrid"
)
st.success(f"{uploaded_file.name} preparation is complete.")
st.subheader("Knowledge Graph Visualization")
render_graph(graph)
# Retrieve the graph schema
schema = graph.get_schema
# Set up the QA chain
template = """
Task: Generate a Cypher statement to query the graph database.
Instructions:
Use only relationship types and properties provided in schema.
Do not use other relationship types or properties that are not provided.
schema:
{schema}
Note: Do not include explanations or apologies in your answers.
Do not answer questions that ask anything other than creating Cypher statements.
Do not include any text other than generated Cypher statements.
Question: {question}"""
question_prompt = PromptTemplate(
template=template,
input_variables=["schema", "question"]
)
qa = GraphCypherQAChain.from_llm(
llm=llm,
graph=graph,
cypher_prompt=question_prompt,
verbose=True,
allow_dangerous_requests=True
)
st.session_state['qa'] = qa
else:
st.warning("Please connect to the Neo4j database before you can upload a PDF.")
if 'qa' in st.session_state:
st.subheader("Ask Questions")
question = st.text_input("Enter your question:", key="question_input")
col1, col2 = st.columns([1, 1])
with col1:
ask_button = st.button("Ask")
with col2:
clear_button = st.button("Clear Chat History")
if ask_button and question:
with st.spinner("Generating answer..."):
res = st.session_state['qa'].invoke({"query": question})
answer = res["result"]
st.session_state["chat_history"].append({
"question": question,
"answer": answer
})
if clear_button:
st.session_state["chat_history"] = []
if st.session_state["chat_history"]:
st.markdown("### Conversation")
for i, chat in enumerate(st.session_state["chat_history"], start=1):
st.markdown(f"**Q{i}:** {chat['question']}")
st.markdown(f"**A{i}:** {chat['answer']}")
st.markdown("---")
if __name__ == "__main__":
main()