Docs Menu
Docs Home
/ /

Integrar MongoDB con CrewAI

Puede integrar MongoDB con CrewAIPara crear agentes de IA autónomos y aplicaciones multiagente con roles, herramientas y tareas especializadas. En concreto, puede aprovechar la herramienta de búsqueda vectorial de MongoDB para CrewAI para que los agentes de IA de sus equipos puedan recuperar información relevante de sus datos y completar sus tareas.

Para completar un tutorial usando CrewAI y MongoDB, consulte Cree una aplicación Agentic RAG con CrewAI y MongoDB.

Para instalar la herramienta de búsqueda vectorial MongoDB para CrewAI, ejecute uno de los siguientes comandos según su administrador de paquetes de Python:

pip install 'crewai-tools[mongodb]'
uv add crewai-tools --extra mongodb

Nota

La compatibilidad de las versiones de Python puede variar con respecto a la documentación oficial de CrewAI. Al momento de escribir este artículo, crewai-tools El paquete depende de embedchain, que requiere una versión de Python entre 3.9 y 3.13.2 (inclusive).

Para usar la Herramienta de Búsqueda Vectorial de MongoDB, inicialícela y luego pásela a un agente.

Para inicializar la herramienta, debe especificar lo siguiente:

from crewai_tools import MongoDBVectorSearchTool
tool = MongoDBVectorSearchTool(
connection_string="<connection-string>",
database_name="<database-name>",
collection_name="<collection-name>",
# Other optional parameters...
)
# To test the tool
print(tool.run(query="<test-query>"))
# To use the tool in an agent
rag_agent = Agent(
name="rag_agent",
role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.",
goal="...",
backstory="...",
tools=[tool],
)

Opcionalmente, puede personalizar la consulta de búsqueda de vectores para la herramienta especificando una instancia de MongoDBVectorSearchConfig en el constructor de la herramienta.

Para obtener más información sobre las consultas de búsqueda de vectores, consulte Ejecutar consultas de búsqueda de vectores.

from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool
# Custom query configuration
query_config = MongoDBVectorSearchConfig(
limit = 10,
oversampling_factor = 2,
)
tool = MongoDBVectorSearchTool(
database_name="example_database",
collection_name="example_collection",
connection_string="<connection_string>",
query_config=query_config,
# Other optional parameters...
)
# To test the tool
print(tool.run(query="<test-query>"))
# To use the tool in an agent
rag_agent = Agent(
name="rag_agent",
role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.",
goal="...",
backstory="...",
tools=[tool],
)

La clase MongoDBVectorSearchTool proporciona los siguientes métodos:

  • add_texts():Agrega documentos de texto a la colección MongoDB especificada.

  • create_vector_search_index():Crea un índice de búsqueda vectorial en la colección.

  • run():Ejecuta una consulta de búsqueda vectorial en sus datos.

import os
from crewai_tools import MongoDBVectorSearchTool
tool = MongoDBVectorSearchTool(
connection_string="<connection-string>",
database_name="<database-name>",
collection_name="<collection-name>"
)
# Example of loading text content from a local folder
texts = []
for fname in os.listdir("knowledge"):
path = os.path.join("knowledge", fname)
if os.path.isfile(path):
with open(path, "r", encoding="utf-8") as f:
texts.append(f.read())
# Method to add documents to the vector store
tool.add_texts(texts)
# Method to create the vector search index
tool.create_vector_search_index(dimensions=<number-of-dimensions>)
# Method to test the tool by running a vector search query
tool.run(query="<search-query>")

Volver

Compila un agente de IA

En esta página