Skip to content

Commit b5f53ef

Browse files
author
ajosh0504
committed
Running pre-commit checks manually
1 parent 4d8e8b6 commit b5f53ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+5928
-4804
lines changed

apps/graph_rag_demo/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ node_modules
1616
#Ignore PDF Files
1717
PDF_KG
1818
.sass-cache
19-

apps/graph_rag_demo/README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# kg_graph__rag_mongo
2-
This guide explores how to leverage MongoDB's capabilities to create and manipulate graph representations using both Python and Node.js. By utilizing these two popular programming languages, we can demonstrate the versatility of MongoDB in different development environments, showcasing how to perform essential Graph RAG to represent and analyze graphs can provide valuable insights into complex relationships and interactions within data combining the Graph and RAG data in MongoDB.
2+
This guide explores how to leverage MongoDB's capabilities to create and manipulate graph representations using both Python and Node.js. By utilizing these two popular programming languages, we can demonstrate the versatility of MongoDB in different development environments, showcasing how to perform essential Graph RAG to represent and analyze graphs can provide valuable insights into complex relationships and interactions within data combining the Graph and RAG data in MongoDB.
33

44

55
## Prep Steps
@@ -8,21 +8,21 @@ This guide explores how to leverage MongoDB's capabilities to create and manipul
88
Set up a Atlas a [cloud-based MongoDB instance of MongoDB.](https://www.mongodb.com/docs/atlas/tutorial/create-new-cluster/)
99

1010
2. Install Required Libraries:
11-
For Python:
12-
Please do a regular installation of Anaconda for your Operating System using the [doc](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html)
11+
For Python:
12+
Please do a regular installation of Anaconda for your Operating System using the [doc](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html)
1313
Once conda is installed, open up a shell with the Conda CLI, change to this directory and create a new environment with the packages listed in the requirements.txt by executing the following statement:
14-
14+
1515
```bash
1616
conda env create -f environment.yaml
1717
conda activate kg-demo
1818
```
19-
For Node.js:
19+
For Node.js:
2020
For the Node JS files we have a package.json and you will be able to use npm install to install all the packages needed. Our Code was tested on Node version v20.15.0 and npm version 10.7.0
2121

22-
3. Now open the .env file in the directory and populate the following three variables. Please note that the .env file may be empty when you clone this repository
22+
3. Now open the .env file in the directory and populate the following three variables. Please note that the .env file may be empty when you clone this repository
2323
OPENAI_API_KEY1=
2424
ATLAS_CONNECTION_STRING=
25-
PDF =
25+
PDF =
2626

2727
4. Create the MongoDB Atlas Database:
2828
Please note that ATLAS_CONNECTION_STRING and OPENAI_API_KEY1 should already be created in the environment file.
@@ -32,7 +32,7 @@ This guide explores how to leverage MongoDB's capabilities to create and manipul
3232
5. Download and install **MongoDB Compass** for your platform by following the steps mentioned [here](https://www.mongodb.com/docs/compass/current/install/). Please ensure you install a compass version 1.40.0 or higher. Once installed, connect to your Atlas cluster by following the link [here](https://www.mongodb.com/docs/compass/current/connect/).
3333

3434
6. Create vector index on the Compass UI for the <code>embedding</code> field for <code>knowledge_graph</code> collection. [Please refer to this document](https://www.mongodb.com/docs/compass/current/indexes/create-vector-search-index/). You can use the following json document for the index defination. Please name the vector index as <code>vector_index</code>
35-
Sample:
35+
Sample:
3636
```json
3737
{
3838
"fields": [ {
@@ -44,8 +44,8 @@ This guide explores how to leverage MongoDB's capabilities to create and manipul
4444
}
4545
```
4646
After this create an Atlas Search Index on the <code>knowledge_graph</code> collection. [Please refer this document](https://www.mongodb.com/docs/compass/current/indexes/create-search-index/). Please name the Atlas Search Index as <code>default</code>
47-
48-
47+
48+
4949

5050

5151
## Running the application
@@ -97,4 +97,4 @@ It will then ask about the Spanning Tree depth. You can give it a value of 2 or
9797
The answer for this question should be something as below:
9898

9999
**Social Support:Social factor Stress:Condition Diet:Lifestyle factor Physical Health:Condition Work Environment:Environment Job Satisfaction:Emotional state Aging:Condition Job Satisfaction:Condition Productivity:Condition Social Support:Activity Immune System:Biological system Cortisol:Hormone Cognitive Function:Function Immune System:System Diet:Activity Burnout:Condition Work Performance:Condition Heart Disease:Disease Cognitive Function:Condition Sleep Quality:Condition Inflammation:Biological process Social Support:Condition Productivity:Outcome Employee Turnover:Outcome Work Environment:Factor Diabetes:Disease Genetics:Biological factor Physical Activity:Activity Diet:Condition Obesity:Condition Heart Disease:Condition Social Support:Factor Social Relationships:Condition Sleep Quality:Health aspect Inflammation:Condition Diet:Factor Memory:Condition Blood Pressure:Condition Exercise:Activity Depression:Condition Anxiety:Condition Mental Health:Aspect Learning:Condition Sleep Quality:Aspect Stress:Concept Diet:Behavior Physical Health:Health aspect Anxiety:Emotional state Anxiety:Mental condition Depression:Mental condition Depression:Emotional state Physical Activity:Behavior Job Satisfaction:Psychological factor Mental Health:Health aspect Mental Health:Condition -----------
100-
Social support is related to aging through its impact on stress. Social support reduces stress, and since stress accelerates aging, having social support can indirectly slow down the aging process by reducing the level of stress experienced by individuals.**
100+
Social support is related to aging through its impact on stress. Social support reduces stress, and since stress accelerates aging, having social support can indirectly slow down the aging process by reducing the level of stress experienced by individuals.**

apps/graph_rag_demo/addEmbeddings.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ async function run() {
2424
// Ensure that the collection is empty
2525
await collection.deleteMany({});
2626
const pdfArray = [
27-
'./PDF_KG/Diet, Stress and Mental Healt.pdf',
28-
'./PDF_KG/Effect of Stress Management Interventions on Job Stress among nurses working in critical care units.pdf',
27+
'./PDF_KG/Diet, Stress and Mental Healt.pdf',
28+
'./PDF_KG/Effect of Stress Management Interventions on Job Stress among nurses working in critical care units.pdf',
2929
'./PDF_KG/Factors contributing to stress among parents of children with autism.pdf',
3030
'./PDF_KG/Level of physical activity, well-being, stress and self rated health in persons with migraine and co existing tension-type headache and neck pain.pdf',
3131
'./PDF_KG/Stress and Blood Pr ess and Blood Pressure During Pr e During Pregnancy Racial Diff egnancy Racial Differences.pdf',
@@ -52,5 +52,3 @@ async function run() {
5252
}
5353
}
5454
run().catch(console.dir);
55-
56-

apps/graph_rag_demo/addTags.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ async function findAllDocuments(dbName, collectionName1, collectionName2) {
4747
},
4848
{
4949
$merge: {
50-
into: collectionName2,
51-
whenMatched: "merge",
50+
into: collectionName2,
51+
whenMatched: "merge",
5252
whenNotMatched: "discard"
5353
}
5454
}

apps/graph_rag_demo/build_graph.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
from collections import defaultdict
2-
def addEdge(graph,u,v):
3-
graph[u].append(v)
4-
def generate_edges(graph):
5-
edges = []
6-
7-
# for each node in graph
8-
for node in graph:
9-
10-
# for each neighbour node of a single node
11-
for neighbour in graph[node]:
12-
13-
# if edge exists then append
14-
edges.append((node, neighbour))
1+
from collections import defaultdict
2+
3+
4+
def addEdge(graph, u, v):
5+
graph[u].append(v)
6+
7+
8+
def generate_edges(graph):
9+
edges = []
10+
11+
# for each node in graph
12+
for node in graph:
13+
# for each neighbour node of a single node
14+
for neighbour in graph[node]:
15+
# if edge exists then append
16+
edges.append((node, neighbour))
1517
return edges
1618

19+
1720
def build_graph(level_dict):
1821
graph = defaultdict(list)
1922
relationship_names = defaultdict(set)
2023
for level in level_dict.keys():
21-
#print(f'Level is {level}')
22-
for source,targets in level_dict[level].items():
23-
#print(f'Source is {source} and targets are {targets}')
24-
for _,relationships in targets.items():
25-
#print(f'Target Node {target_node} and relationships {relationships}')
24+
# print(f'Level is {level}')
25+
for source, targets in level_dict[level].items():
26+
# print(f'Source is {source} and targets are {targets}')
27+
for _, relationships in targets.items():
28+
# print(f'Target Node {target_node} and relationships {relationships}')
2629
for target_node, edges in relationships.items():
27-
addEdge(graph,source,target_node)
28-
relationship_key = (source,target_node)
30+
addEdge(graph, source, target_node)
31+
relationship_key = (source, target_node)
2932
for edge in edges:
3033
relationship_names[relationship_key].add(edge)
31-
return graph,relationship_names
34+
return graph, relationship_names
+36-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def depth_first_search(graph,start,relationship_names,path):
1+
def depth_first_search(graph, start, relationship_names, path):
22
visited = []
33
stack = []
44
parent = []
@@ -8,43 +8,53 @@ def depth_first_search(graph,start,relationship_names,path):
88
visited.append(start)
99
i = 0
1010
while stack:
11-
s=stack.pop()
12-
print(s,end=" ")
13-
node = {'id':s,'group':0,'level':1,'label':s.split(":")[0]+' from '+s.split(":")[1]}
11+
s = stack.pop()
12+
print(s, end=" ")
13+
node = {
14+
"id": s,
15+
"group": 0,
16+
"level": 1,
17+
"label": s.split(":")[0] + " from " + s.split(":")[1],
18+
}
1419
nodes.append(node)
15-
if i!=0:
20+
if i != 0:
1621
parent_node = parent[-1]
17-
parent_name=''
18-
current_child_count=0
19-
for x,y in parent_node.items():
20-
parent_name=x
21-
current_child_count=y
22-
relationships = relationship_names[(parent_name,s)]
23-
#print(f'{relationships}')
24-
rel_names=''
25-
j=0
22+
parent_name = ""
23+
current_child_count = 0
24+
for x, y in parent_node.items():
25+
parent_name = x
26+
current_child_count = y
27+
relationships = relationship_names[(parent_name, s)]
28+
# print(f'{relationships}')
29+
rel_names = ""
30+
j = 0
2631
if relationships:
2732
for rel in relationships:
28-
if j!=len(relationships)-1:
29-
rel_names+=rel+" / "
33+
if j != len(relationships) - 1:
34+
rel_names += rel + " / "
3035
else:
31-
rel_names+=rel
32-
j+=1
33-
path+= f'{parent_name.split(":")[0]} ({rel_names}) {s.split(":")[0]}, '
34-
link = {'source':parent_name,'target':s,'strength':0.7,'linkName':rel_names}
36+
rel_names += rel
37+
j += 1
38+
path += f'{parent_name.split(":")[0]} ({rel_names}) {s.split(":")[0]}, '
39+
link = {
40+
"source": parent_name,
41+
"target": s,
42+
"strength": 0.7,
43+
"linkName": rel_names,
44+
}
3545
links.append(link)
3646
current_child_count -= 1
3747
if current_child_count == 0:
3848
parent.pop()
3949
else:
40-
parent[-1][parent_name]=current_child_count
41-
k=0
50+
parent[-1][parent_name] = current_child_count
51+
k = 0
4252
for x in graph[s][::-1]:
4353
if x not in visited:
4454
visited.append(x)
4555
stack.append(x)
46-
k+=1
56+
k += 1
4757
if k > 0:
48-
parent.append({s:k})
49-
i+=1
50-
return path,nodes,links
58+
parent.append({s: k})
59+
i += 1
60+
return path, nodes, links

apps/graph_rag_demo/do_graphlookup.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
from pymongo import MongoClient
2-
from pprint import pprint
31
import os
2+
from pprint import pprint
3+
44
from dotenv import load_dotenv
5+
from pymongo import MongoClient
56

67
load_dotenv()
7-
def graph_lookup(node_name,max_depth):
88

9+
10+
def graph_lookup(node_name, max_depth):
911
graph_lookup_docs = []
1012
try:
1113
uri = os.getenv("ATLAS_CONNECTION_STRING")
1214
client = MongoClient(uri, appname="devrel.showcase.apps.graph_rag_demo")
1315
database = client["langchain_db"]
1416
collection = database["nodes_relationships"]
1517
pipeline = [
16-
{
17-
'$match': {
18-
'_id': node_name
19-
}
20-
}, {
21-
'$graphLookup': {
22-
'from': 'nodes_relationships',
23-
'startWith': '$_id',
24-
'connectFromField': 'relationships',
25-
'connectToField': '_id',
26-
'as': 'relates_to',
27-
'maxDepth': max_depth,
28-
'depthField': 'distance',
29-
'restrictSearchWithMatch': {}
30-
}
31-
}
18+
{"$match": {"_id": node_name}},
19+
{
20+
"$graphLookup": {
21+
"from": "nodes_relationships",
22+
"startWith": "$_id",
23+
"connectFromField": "relationships",
24+
"connectToField": "_id",
25+
"as": "relates_to",
26+
"maxDepth": max_depth,
27+
"depthField": "distance",
28+
"restrictSearchWithMatch": {},
29+
}
30+
},
3231
]
3332
cursor = collection.aggregate(pipeline)
3433
for doc in cursor:

0 commit comments

Comments
 (0)