Skip to content

Commit

Permalink
MongoDB agent fixes (#14362)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing to LangChain!

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes (if applicable),
  - **Dependencies:** any dependencies required for this change,
- **Tag maintainer:** for a quicker response, tag the relevant
maintainer (see below),
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc:

https://github.com/langchain-ai/langchain/blob/master/.github/CONTRIBUTING.md

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in `docs/extras`
directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
- **Description:** Moved the agent designed to interact with and query a
MongoDB database using PyMongo into experimental, and fixed the unit
test issues that came about using MongoDB Unix sockets.
- **Issue:** There were some some test workflows not running in
`Harrison/mongo-agent` due to using nonexistent sockets, and Mongo agent
should have been moved to experimental due to concerns mentioned in
#13991
  - **Dependencies:** `pymongo` in experimental, not optional
- Reason: Github workflows were throwing `pymongo ModuleNotFound` errors
when it was optional, even after adding it to extended testing, running
poetry lock, doing everything that the testing documentation said to do
  - **Related:** #14056, #13991
  - **Tag maintainers:** @hwchase17 @efriis

---------

Co-authored-by: Harrison Chase <[email protected]>
  • Loading branch information
amiaxys and hwchase17 authored Dec 6, 2023
1 parent ca21ef5 commit 66162dd
Show file tree
Hide file tree
Showing 30 changed files with 297 additions and 393 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/_compile_integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ jobs:
working-directory: ${{ inputs.working-directory }}
cache-key: compile-integration

- name: MongoDB in GitHub Actions
uses: supercharge/[email protected]
with:
mongodb-version: 7.0
mongodb-replica-set: test
mongodb-port: 27017

- name: Install integration dependencies
shell: bash
run: poetry install --with=test_integration
Expand Down
10 changes: 6 additions & 4 deletions docs/docs/integrations/toolkits/mongo_database.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"source": [
"# MongoDB Database\n",
"\n",
"This notebook showcases an agent designed to interact with and query a `MongoDB` database using PyMongo. \n",
"This notebook showcases an experimental agent designed to interact with and query a `MongoDB` database using PyMongo. \n",
"The agent is similar to [SQL Database](https://python.langchain.com/docs/integrations/toolkits/sql_database).\n",
"\n",
"As this agent is in development, currently it only supports one database per instance `MongoDatabase` class using URI. Additionally, all answers may not be correct, and it is not guaranteed that the agent won't perform destructive commands on your database (or in general) given certain questions."
Expand All @@ -25,11 +25,13 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import create_mongo_agent\n",
"from langchain.agents.agent_toolkits import MongoDatabaseToolkit\n",
"from langchain.agents.agent_types import AgentType\n",
"from langchain.llms.openai import OpenAI\n",
"from langchain.utilities.mongo_database import MongoDatabase\n",
"from langchain_experimental.agents.agent_toolkits import (\n",
" MongoDatabaseToolkit,\n",
" create_mongo_agent,\n",
")\n",
"from langchain_experimental.utilities import MongoDatabase\n",
"\n",
"db = MongoDatabase.from_uri(\"mongodb://localhost:27017/my_db\")\n",
"db._client[\"my_db\"][\"my_collection\"].insert_many(\n",
Expand Down
93 changes: 9 additions & 84 deletions docs/docs/integrations/toolkits/sql_database.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from langchain_experimental.agents.agent_toolkits.csv.base import create_csv_agent
from langchain_experimental.agents.agent_toolkits.mongo.base import (
MongoDatabaseToolkit,
create_mongo_agent,
)
from langchain_experimental.agents.agent_toolkits.pandas.base import (
create_pandas_dataframe_agent,
)
Expand All @@ -16,4 +20,6 @@
"create_spark_dataframe_agent",
"create_python_agent",
"create_csv_agent",
"create_mongo_agent",
"MongoDatabaseToolkit",
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
"""MongoDB agent."""
from typing import Any, Dict, List, Optional, Sequence

from langchain.agents.agent import AgentExecutor, BaseSingleActionAgent
from langchain.agents.agent_types import AgentType
from langchain.agents.mrkl.base import ZeroShotAgent
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain.callbacks.base import BaseCallbackManager
from langchain.chains.llm import LLMChain
from langchain.tools import BaseTool
from langchain_core.language_models import BaseLanguageModel
from langchain_core.messages import AIMessage, SystemMessage
from langchain_core.prompts.chat import (
Expand All @@ -9,20 +17,14 @@
MessagesPlaceholder,
)

from langchain.agents.agent import AgentExecutor, BaseSingleActionAgent
from langchain.agents.agent_toolkits.mongo.prompt import (
from langchain_experimental.agents.agent_toolkits.mongo.prompt import (
MONGO_FUNCTIONS_SUFFIX,
MONGO_PREFIX,
MONGO_SUFFIX,
)
from langchain.agents.agent_toolkits.mongo.toolkit import MongoDatabaseToolkit
from langchain.agents.agent_types import AgentType
from langchain.agents.mrkl.base import ZeroShotAgent
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain.callbacks.base import BaseCallbackManager
from langchain.chains.llm import LLMChain
from langchain.tools import BaseTool
from langchain_experimental.agents.agent_toolkits.mongo.toolkit import (
MongoDatabaseToolkit,
)


def create_mongo_agent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""Toolkit for interacting with a Mongo database."""
from typing import List

from langchain.agents.agent_toolkits.base import BaseToolkit
from langchain.tools import BaseTool
from langchain_core.language_models import BaseLanguageModel
from langchain_core.pydantic_v1 import Field

from langchain.agents.agent_toolkits.base import BaseToolkit
from langchain.tools import BaseTool
from langchain.tools.mongo_database.tool import (
from langchain_experimental.tools.mongo_database.tool import (
InfoMongoDBTool,
ListMongoDBTool,
QueryMongoDBCheckerTool,
QueryMongoDBTool,
)
from langchain.utilities.mongo_database import MongoDatabase
from langchain_experimental.utilities.mongo_database import MongoDatabase


class MongoDatabaseToolkit(BaseToolkit):
Expand Down
15 changes: 14 additions & 1 deletion libs/experimental/langchain_experimental/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from langchain_experimental.tools.mongo_database.tool import (
InfoMongoDBTool,
ListMongoDBTool,
QueryMongoDBCheckerTool,
QueryMongoDBTool,
)
from langchain_experimental.tools.python.tool import PythonAstREPLTool, PythonREPLTool

__all__ = ["PythonREPLTool", "PythonAstREPLTool"]
__all__ = [
"PythonREPLTool",
"PythonAstREPLTool",
"InfoMongoDBTool",
"ListMongoDBTool",
"QueryMongoDBCheckerTool",
"QueryMongoDBTool",
]
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
)
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate
from langchain.utilities.mongo_database import MongoDatabase
from langchain_experimental.utilities.mongo_database import MongoDatabase
from langchain.tools.base import BaseTool
from langchain.tools.mongo_database.prompt import QUERY_CHECKER
from langchain_experimental.tools.mongo_database.prompt import QUERY_CHECKER


class BaseMongoDBTool(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from langchain_experimental.utilities.mongo_database import MongoDatabase
from langchain_experimental.utilities.python import PythonREPL

__all__ = ["PythonREPL"]
__all__ = ["PythonREPL", "MongoDatabase"]
Loading

0 comments on commit 66162dd

Please sign in to comment.