|
9 | 9 | "\n", |
10 | 10 | "This notebook is a companion to the [GraphRAG with MongoDB and LangChain](https://www.mongodb.com/docs/atlas/ai-integrations/langchain/graph-rag/) tutorial. Refer to the page for set-up instructions and detailed explanations.\n", |
11 | 11 | "\n", |
12 | | - "This notebook demonstrates a GraphRAG implementation using MongoDB Atlas and LangChain. Compared to vector-based RAG, which structures your data as vector embeddings, GraphRAG structures data as a knowledge graph with entities and their relationships. This enables relationship-aware retrieval and multi-hop reasoning.\n", |
| 12 | + "This notebook demonstrates a GraphRAG implementation using MongoDB and LangChain. Compared to vector-based RAG, which structures your data as vector embeddings, GraphRAG structures data as a knowledge graph with entities and their relationships. This enables relationship-aware retrieval and multi-hop reasoning.\n", |
13 | 13 | "\n", |
14 | 14 | "<a target=\"_blank\" href=\"https://colab.research.google.com/github/mongodb/docs-notebooks/blob/main/ai-integrations/langchain-graphrag.ipynb\">\n", |
15 | 15 | " <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n", |
|
37 | 37 | "\n", |
38 | 38 | "Before you begin, make sure you have the following:\n", |
39 | 39 | "\n", |
40 | | - "- An Atlas cluster up and running (you'll need the [connection string](https://www.mongodb.com/docs/guides/atlas/connection-string/))\n", |
| 40 | + "- A MongoDB cluster up and running (you'll need the [connection string](https://www.mongodb.com/docs/manual/reference/connection-string/))\n", |
41 | 41 | "- An API key to access an LLM (This tutorial uses a model from OpenAI, but you can use any model [supported by LangChain](https://python.langchain.com/docs/integrations/chat/))" |
42 | 42 | ] |
43 | 43 | }, |
|
51 | 51 | "import os\n", |
52 | 52 | "\n", |
53 | 53 | "os.environ[\"OPENAI_API_KEY\"] = \"<api-key>\"\n", |
54 | | - "ATLAS_CONNECTION_STRING = \"<connection-string>\"\n", |
55 | | - "ATLAS_DB_NAME = \"langchain_db\" # MongoDB database to store the knowledge graph\n", |
56 | | - "ATLAS_COLLECTION = \"wikipedia\" # MongoDB collection to store the knowledge graph" |
| 54 | + "MONGODB_URI = \"<connection-string>\"\n", |
| 55 | + "DB_NAME = \"langchain_db\" # MongoDB database to store the knowledge graph\n", |
| 56 | + "COLLECTION = \"wikipedia\" # MongoDB collection to store the knowledge graph" |
57 | 57 | ] |
58 | 58 | }, |
59 | 59 | { |
60 | 60 | "cell_type": "markdown", |
61 | 61 | "id": "0adf66a8", |
62 | 62 | "metadata": {}, |
63 | 63 | "source": [ |
64 | | - "## Use MongoDB Atlas as a knowledge graph\n", |
| 64 | + "## Use MongoDB as a knowledge graph\n", |
65 | 65 | "\n", |
66 | 66 | "Use the `MongoDBGraphStore` component to store your data as a knowledge graph. This component allows you to implement GraphRAG by storing entities (nodes) and their relationships (edges) in a MongoDB collection. It stores each entity as a document with relationship fields that reference other documents in your collection." |
67 | 67 | ] |
|
111 | 111 | "from langchain_mongodb.graphrag.graph import MongoDBGraphStore\n", |
112 | 112 | "\n", |
113 | 113 | "graph_store = MongoDBGraphStore(\n", |
114 | | - " connection_string = ATLAS_CONNECTION_STRING,\n", |
115 | | - " database_name = ATLAS_DB_NAME,\n", |
116 | | - " collection_name = ATLAS_COLLECTION,\n", |
| 114 | + " connection_string = MONGODB_URI,\n", |
| 115 | + " database_name = DB_NAME,\n", |
| 116 | + " collection_name = COLLECTION,\n", |
117 | 117 | " entity_extraction_model = chat_model\n", |
118 | 118 | ")" |
119 | 119 | ] |
|
125 | 125 | "metadata": {}, |
126 | 126 | "outputs": [], |
127 | 127 | "source": [ |
128 | | - "# Extract entities and create knowledge graph in Atlas\n", |
| 128 | + "# Extract entities and create knowledge graph in MongoDB\n", |
129 | 129 | "# This might take a few minutes; you can ignore any warnings\n", |
130 | 130 | "graph_store.add_documents(wikipedia_docs)" |
131 | 131 | ] |
|
222 | 222 | "from IPython.display import HTML, display\n", |
223 | 223 | "from pymongo import MongoClient\n", |
224 | 224 | "\n", |
225 | | - "client = MongoClient(ATLAS_CONNECTION_STRING)\n", |
| 225 | + "client = MongoClient(MONGODB_URI)\n", |
226 | 226 | "\n", |
227 | | - "collection = client[ATLAS_DB_NAME][ATLAS_COLLECTION]\n", |
| 227 | + "collection = client[DB_NAME][COLLECTION]\n", |
228 | 228 | "html = visualize_graph(collection)\n", |
229 | 229 | "\n", |
230 | 230 | "display(HTML(html))" |
|
0 commit comments