Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB agent fixes #14362

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