Skip to content

Commit

Permalink
Running pre-commit checks manually
Browse files Browse the repository at this point in the history
  • Loading branch information
ajosh0504 committed Feb 5, 2025
1 parent 4d8e8b6 commit b5f53ef
Show file tree
Hide file tree
Showing 82 changed files with 5,928 additions and 4,804 deletions.
1 change: 0 additions & 1 deletion apps/graph_rag_demo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ node_modules
#Ignore PDF Files
PDF_KG
.sass-cache

22 changes: 11 additions & 11 deletions apps/graph_rag_demo/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# kg_graph__rag_mongo
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.
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.


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

2. Install Required Libraries:
For Python:
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)
For Python:
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)
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:

```bash
conda env create -f environment.yaml
conda activate kg-demo
```
For Node.js:
For Node.js:
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

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
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
OPENAI_API_KEY1=
ATLAS_CONNECTION_STRING=
PDF =
PDF =

4. Create the MongoDB Atlas Database:
Please note that ATLAS_CONNECTION_STRING and OPENAI_API_KEY1 should already be created in the environment file.
Expand All @@ -32,7 +32,7 @@ This guide explores how to leverage MongoDB's capabilities to create and manipul
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/).

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>
Sample:
Sample:
```json
{
"fields": [ {
Expand All @@ -44,8 +44,8 @@ This guide explores how to leverage MongoDB's capabilities to create and manipul
}
```
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>




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

**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 -----------
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.**
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.**
6 changes: 2 additions & 4 deletions apps/graph_rag_demo/addEmbeddings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async function run() {
// Ensure that the collection is empty
await collection.deleteMany({});
const pdfArray = [
'./PDF_KG/Diet, Stress and Mental Healt.pdf',
'./PDF_KG/Effect of Stress Management Interventions on Job Stress among nurses working in critical care units.pdf',
'./PDF_KG/Diet, Stress and Mental Healt.pdf',
'./PDF_KG/Effect of Stress Management Interventions on Job Stress among nurses working in critical care units.pdf',
'./PDF_KG/Factors contributing to stress among parents of children with autism.pdf',
'./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',
'./PDF_KG/Stress and Blood Pr ess and Blood Pressure During Pr e During Pregnancy Racial Diff egnancy Racial Differences.pdf',
Expand All @@ -52,5 +52,3 @@ async function run() {
}
}
run().catch(console.dir);


4 changes: 2 additions & 2 deletions apps/graph_rag_demo/addTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ async function findAllDocuments(dbName, collectionName1, collectionName2) {
},
{
$merge: {
into: collectionName2,
whenMatched: "merge",
into: collectionName2,
whenMatched: "merge",
whenNotMatched: "discard"
}
}
Expand Down
47 changes: 25 additions & 22 deletions apps/graph_rag_demo/build_graph.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
from collections import defaultdict
def addEdge(graph,u,v):
graph[u].append(v)
def generate_edges(graph):
edges = []

# for each node in graph
for node in graph:

# for each neighbour node of a single node
for neighbour in graph[node]:

# if edge exists then append
edges.append((node, neighbour))
from collections import defaultdict


def addEdge(graph, u, v):
graph[u].append(v)


def generate_edges(graph):
edges = []

# for each node in graph
for node in graph:
# for each neighbour node of a single node
for neighbour in graph[node]:
# if edge exists then append
edges.append((node, neighbour))
return edges


def build_graph(level_dict):
graph = defaultdict(list)
relationship_names = defaultdict(set)
for level in level_dict.keys():
#print(f'Level is {level}')
for source,targets in level_dict[level].items():
#print(f'Source is {source} and targets are {targets}')
for _,relationships in targets.items():
#print(f'Target Node {target_node} and relationships {relationships}')
# print(f'Level is {level}')
for source, targets in level_dict[level].items():
# print(f'Source is {source} and targets are {targets}')
for _, relationships in targets.items():
# print(f'Target Node {target_node} and relationships {relationships}')
for target_node, edges in relationships.items():
addEdge(graph,source,target_node)
relationship_key = (source,target_node)
addEdge(graph, source, target_node)
relationship_key = (source, target_node)
for edge in edges:
relationship_names[relationship_key].add(edge)
return graph,relationship_names
return graph, relationship_names
62 changes: 36 additions & 26 deletions apps/graph_rag_demo/depth_first_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def depth_first_search(graph,start,relationship_names,path):
def depth_first_search(graph, start, relationship_names, path):
visited = []
stack = []
parent = []
Expand All @@ -8,43 +8,53 @@ def depth_first_search(graph,start,relationship_names,path):
visited.append(start)
i = 0
while stack:
s=stack.pop()
print(s,end=" ")
node = {'id':s,'group':0,'level':1,'label':s.split(":")[0]+' from '+s.split(":")[1]}
s = stack.pop()
print(s, end=" ")
node = {
"id": s,
"group": 0,
"level": 1,
"label": s.split(":")[0] + " from " + s.split(":")[1],
}
nodes.append(node)
if i!=0:
if i != 0:
parent_node = parent[-1]
parent_name=''
current_child_count=0
for x,y in parent_node.items():
parent_name=x
current_child_count=y
relationships = relationship_names[(parent_name,s)]
#print(f'{relationships}')
rel_names=''
j=0
parent_name = ""
current_child_count = 0
for x, y in parent_node.items():
parent_name = x
current_child_count = y
relationships = relationship_names[(parent_name, s)]
# print(f'{relationships}')
rel_names = ""
j = 0
if relationships:
for rel in relationships:
if j!=len(relationships)-1:
rel_names+=rel+" / "
if j != len(relationships) - 1:
rel_names += rel + " / "
else:
rel_names+=rel
j+=1
path+= f'{parent_name.split(":")[0]} ({rel_names}) {s.split(":")[0]}, '
link = {'source':parent_name,'target':s,'strength':0.7,'linkName':rel_names}
rel_names += rel
j += 1
path += f'{parent_name.split(":")[0]} ({rel_names}) {s.split(":")[0]}, '
link = {
"source": parent_name,
"target": s,
"strength": 0.7,
"linkName": rel_names,
}
links.append(link)
current_child_count -= 1
if current_child_count == 0:
parent.pop()
else:
parent[-1][parent_name]=current_child_count
k=0
parent[-1][parent_name] = current_child_count
k = 0
for x in graph[s][::-1]:
if x not in visited:
visited.append(x)
stack.append(x)
k+=1
k += 1
if k > 0:
parent.append({s:k})
i+=1
return path,nodes,links
parent.append({s: k})
i += 1
return path, nodes, links
37 changes: 18 additions & 19 deletions apps/graph_rag_demo/do_graphlookup.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
from pymongo import MongoClient
from pprint import pprint
import os
from pprint import pprint

from dotenv import load_dotenv
from pymongo import MongoClient

load_dotenv()
def graph_lookup(node_name,max_depth):


def graph_lookup(node_name, max_depth):
graph_lookup_docs = []
try:
uri = os.getenv("ATLAS_CONNECTION_STRING")
client = MongoClient(uri, appname="devrel.showcase.apps.graph_rag_demo")
database = client["langchain_db"]
collection = database["nodes_relationships"]
pipeline = [
{
'$match': {
'_id': node_name
}
}, {
'$graphLookup': {
'from': 'nodes_relationships',
'startWith': '$_id',
'connectFromField': 'relationships',
'connectToField': '_id',
'as': 'relates_to',
'maxDepth': max_depth,
'depthField': 'distance',
'restrictSearchWithMatch': {}
}
}
{"$match": {"_id": node_name}},
{
"$graphLookup": {
"from": "nodes_relationships",
"startWith": "$_id",
"connectFromField": "relationships",
"connectToField": "_id",
"as": "relates_to",
"maxDepth": max_depth,
"depthField": "distance",
"restrictSearchWithMatch": {},
}
},
]
cursor = collection.aggregate(pipeline)
for doc in cursor:
Expand Down
Loading

0 comments on commit b5f53ef

Please sign in to comment.