|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "f9696293", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Query Atlas with Natural Language Using LangChain and LangGraph" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "e696dea0", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "This notebook is a companion to the [Query Atlas with Natural Language Using LangChain and LangGraph](https://www.mongodb.com/docs/atlas/atlas-vector-search/ai-integrations/langchain/natural-language-to-mql/) tutorial. Refer to the page for set-up instructions and detailed explanations.\n", |
| 17 | + "\n", |
| 18 | + "This notebook demonstrates how to query an Atlas cluster with a natural language prompt using an AI agent built with the [LangChain MongoDB Toolkit](https://langchain-mongodb.readthedocs.io/en/latest/langchain_mongodb/agent_toolkit/langchain_mongodb.agent_toolkit.toolkit.MongoDBDatabaseToolkit.html#langchain_mongodb.agent_toolkit.toolkit.MongoDBDatabaseToolkit) and the [LangGraph ReAct Agent Framework](https://langchain-ai.github.io/langgraph/agents/agents/).\n", |
| 19 | + "\n", |
| 20 | + "<a target=\"_blank\" href=\"https://colab.research.google.com/github/mongodb/docs-notebooks/blob/main/ai-integrations/langchain-natural-language.ipynb\">\n", |
| 21 | + " <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n", |
| 22 | + "</a>" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": null, |
| 28 | + "id": "f106dda9", |
| 29 | + "metadata": { |
| 30 | + "vscode": { |
| 31 | + "languageId": "shellscript" |
| 32 | + } |
| 33 | + }, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "pip install --quiet --upgrade langchain-mongodb langchain-openai langgraph" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "id": "998157e0", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "## Set up your environment\n", |
| 45 | + "\n", |
| 46 | + "Before you begin, make sure you have the following:\n", |
| 47 | + "\n", |
| 48 | + "- An Atlas cluster up and running (you'll need the [connection string](https://www.mongodb.com/docs/guides/atlas/connection-string/))\n", |
| 49 | + "- 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/))" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": null, |
| 55 | + "id": "694ccd64", |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "os.environ[\"OPENAI_API_KEY\"] = '<api-key>'\n", |
| 60 | + "ATLAS_CONNECTION_STRING = '<atlas-connection-string>'\n", |
| 61 | + "ATLAS_DB_NAME = 'sample_restaurants'\n", |
| 62 | + "NATURAL_LANGUAGE_QUERY = 'Find all restaurants that serve hamburgers.'" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "id": "c764c565", |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "import os\n", |
| 73 | + "from langchain_openai import ChatOpenAI\n", |
| 74 | + "from langgraph.prebuilt import create_react_agent\n", |
| 75 | + "from langchain_mongodb.agent_toolkit import (\n", |
| 76 | + " MONGODB_AGENT_SYSTEM_PROMPT,\n", |
| 77 | + " MongoDBDatabase,\n", |
| 78 | + " MongoDBDatabaseToolkit,\n", |
| 79 | + ")" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "id": "5a6b006c", |
| 85 | + "metadata": {}, |
| 86 | + "source": [ |
| 87 | + "## Build the agent\n", |
| 88 | + "\n", |
| 89 | + "Next, define the `NaturalLanguageToMQL` Python class.\n", |
| 90 | + "\n", |
| 91 | + "#### Key Points\n", |
| 92 | + "\n", |
| 93 | + "- `self.toolkit`, the tools that the agent can use, is an instance of the [MongoDB Toolkit](https://langchain-mongodb.readthedocs.io/en/latest/langchain_mongodb/agent_toolkit/langchain_mongodb.agent_toolkit.toolkit.MongoDBDatabaseToolkit.html#langchain_mongodb.agent_toolkit.toolkit.MongoDBDatabaseToolkit). \n", |
| 94 | + "\n", |
| 95 | + "- `self.agent`, the agent itself, is an instance of the [ReAct Agent framework](https://langchain-ai.github.io/langgraph/agents/agents/), which takes `self.toolkit` as a parameter." |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "id": "b45185db", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "class NaturalLanguageToMQL:\n", |
| 106 | + " def __init__(self):\n", |
| 107 | + " self.llm = ChatOpenAI(model=\"gpt-4o-mini\", timeout=60)\n", |
| 108 | + " self.system_message = MONGODB_AGENT_SYSTEM_PROMPT.format(top_k=5)\n", |
| 109 | + " self.db_wrapper = MongoDBDatabase.from_connection_string(\n", |
| 110 | + " ATLAS_CONNECTION_STRING, \n", |
| 111 | + " database=ATLAS_DB_NAME)\n", |
| 112 | + " self.toolkit = MongoDBDatabaseToolkit(db=self.db_wrapper, llm=self.llm)\n", |
| 113 | + " self.agent = create_react_agent(\n", |
| 114 | + " self.llm, \n", |
| 115 | + " self.toolkit.get_tools(), \n", |
| 116 | + " state_modifier=self.system_message)\n", |
| 117 | + " self.messages = []\n", |
| 118 | + "\n", |
| 119 | + " def convert_to_mql_and_execute_query(self, query):\n", |
| 120 | + " # Start the agent with the agent.stream() method\n", |
| 121 | + " events = self.agent.stream(\n", |
| 122 | + " {'messages': [('user', query)]},\n", |
| 123 | + " stream_mode='values',\n", |
| 124 | + " )\n", |
| 125 | + " # Add output (events) from the agent to the self.messages list\n", |
| 126 | + " for event in events:\n", |
| 127 | + " self.messages.extend(event['messages'])\n", |
| 128 | + " \n", |
| 129 | + " def print_results(self):\n", |
| 130 | + " # Print the the end-user's expected output from \n", |
| 131 | + " # the final message produced by the agent.\n", |
| 132 | + " print(self.messages[-1].content)" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "id": "c90825eb", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "## Run a sample query\n", |
| 141 | + "\n", |
| 142 | + "And finally, instantiate the `NaturalLanguageToMQL` class and run a sample query." |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "id": "b7284c63", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "def main():\n", |
| 153 | + " converter = NaturalLanguageToMQL()\n", |
| 154 | + " converter.convert_to_mql_and_execute_query(NATURAL_LANGUAGE_QUERY)\n", |
| 155 | + " converter.print_results()\n", |
| 156 | + "\n", |
| 157 | + "main()" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | +"metadata": { |
| 162 | + "kernelspec": { |
| 163 | + "display_name": "Python 3", |
| 164 | + "language": "python", |
| 165 | + "name": "python3" |
| 166 | + }, |
| 167 | + "language_info": { |
| 168 | + "codemirror_mode": { |
| 169 | + "name": "ipython", |
| 170 | + "version": 3 |
| 171 | + }, |
| 172 | + "file_extension": ".py", |
| 173 | + "mimetype": "text/x-python", |
| 174 | + "name": "python", |
| 175 | + "nbconvert_exporter": "python", |
| 176 | + "pygments_lexer": "ipython3", |
| 177 | + "version": "3.10.12" |
| 178 | + } |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 2 |
| 182 | + } |
0 commit comments