|
| 1 | +# 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. |
| 3 | + |
| 4 | + |
| 5 | +## Prep Steps |
| 6 | +1. Set Up Your MongoDB Database: |
| 7 | + |
| 8 | + Set up a Atlas a [cloud-based MongoDB instance of MongoDB.](https://www.mongodb.com/docs/atlas/tutorial/create-new-cluster/) |
| 9 | + |
| 10 | +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) |
| 13 | + 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 | + |
| 15 | + ```bash |
| 16 | + conda env create -f environment.yaml |
| 17 | + conda activate kg-demo |
| 18 | +``` |
| 19 | + For Node.js: |
| 20 | + 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 |
| 21 | + |
| 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 |
| 23 | + OPENAI_API_KEY1= |
| 24 | + ATLAS_CONNECTION_STRING= |
| 25 | + PDF = |
| 26 | + |
| 27 | +4. Create the MongoDB Atlas Database: |
| 28 | + Please note that ATLAS_CONNECTION_STRING and OPENAI_API_KEY1 should already be created in the environment file. |
| 29 | + Please create a new MongoDB Atlas database called <code>langchain_db</code> |
| 30 | + Create two collections named <code>knowledge_graph</code> and <code>nodes_relationships</code> |
| 31 | + |
| 32 | +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/). |
| 33 | + |
| 34 | +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: |
| 36 | + ```json |
| 37 | + { |
| 38 | + "fields": [ { |
| 39 | + "type": "vector", |
| 40 | + "path": "embedding", |
| 41 | + "numDimensions": 1536, |
| 42 | + "similarity": "euclidean" |
| 43 | + } ] |
| 44 | + } |
| 45 | + ``` |
| 46 | + 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 | + |
| 49 | + |
| 50 | + |
| 51 | +## Running the application |
| 52 | + |
| 53 | +### If running for the first time |
| 54 | +We have to setup the data in the collection. Please perform the following steps: |
| 55 | + |
| 56 | +1. Open an Anaconda Shell and navigate to the project directory. |
| 57 | +2. Activate the kg-demo environment by issuing the below commands: |
| 58 | +```bash |
| 59 | +conda activate kg-demo |
| 60 | +``` |
| 61 | +3. Run the <code>data_insert.py file</code> by issuing the following command: |
| 62 | +```bash |
| 63 | +python data_insert.py |
| 64 | +``` |
| 65 | +4. Now open a OS shell which should have `node` installed. Navigate to the project directory and issue the following commands in the same order: |
| 66 | +```bash |
| 67 | +node addEmbeddings.js |
| 68 | +``` |
| 69 | +**Please note that if the above command does not end after 10-15 seconds, please terminate it using Ctrl+C** |
| 70 | +And then |
| 71 | +```bash |
| 72 | +node addTags.js |
| 73 | +``` |
| 74 | +5. Now return to the Anaconda shell opened in Step 1 where you should already be there in the project directory and in the kg-demo environment and run the following command: |
| 75 | +```bash |
| 76 | +python driver_code.py |
| 77 | +``` |
| 78 | +This will ask for a question which you want to ask. You can give a question like **How is social support related to aging?**. |
| 79 | +It will then ask about the Spanning Tree depth. You can give it a value of 2 or 3 for an optimal performance. |
| 80 | +Enjoy |
| 81 | + |
| 82 | +### On Subsequent Runs |
| 83 | +Data is already prepared. We just need to chat and ask questions. Please perform the following steps: |
| 84 | + |
| 85 | +1. Open an Anaconda Shell and navigate to the project directory. |
| 86 | +2. Activate the kg-demo environment by issuing the below commands: |
| 87 | +```bash |
| 88 | +conda activate kg-demo |
| 89 | +``` |
| 90 | +3. Run the following command: |
| 91 | +```bash |
| 92 | +python driver_code.py |
| 93 | +``` |
| 94 | +This will ask for a question which you want to ask. You can give a question like **How is social support related to aging?**. |
| 95 | +It will then ask about the Spanning Tree depth. You can give it a value of 2 or 3 for an optimal performance. |
| 96 | + |
| 97 | +The answer for this question should be something as below: |
| 98 | + |
| 99 | +**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.** |
0 commit comments