Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Multi-agent Systems With AutoGen and MongoDB

Richmond Alake, Han Heloir10 min read • Published Sep 18, 2024 • Updated Sep 18, 2024
AIAtlasPython
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Today, agentic AI is no longer a novel term for people interested in generative AI (gen AI). Agentic AI is an objective-driven artificial intelligence system that decomposes complex tasks, makes autonomous decisions, and executes actions using available tools or system components.  These systems are characterized as agents due to their ability to perceive context, reason about potential actions, and execute those actions to achieve specific goals.
There are several different types of agentic AI design patterns that are emerging, the notable ones being:
  1. Task-specific agents: These are designed to excel at a particular task or domain, such as language translation, image recognition, or financial analysis.
  2. Single-agent systems: These consist of a single AI agent with access to various tools and system components. The agent is responsible for understanding the task, planning the approach, and utilizing the appropriate tools to accomplish the goal.
  3. Hierarchical agents: These systems have a structure where higher-level agents delegate tasks to lower-level agents, mimicking organizational hierarchies.
What if you had not only one agent but multiple intelligent agents in one system? Like in a Kubernetes cluster, agents are orchestrated like containers to solve pronounced problems. Each agent, much like a Kubernetes pod, is designed for a specific task and collaborates to meet demand.
We're excited to announce that MongoDB has contributed to the AutoGen framework, enabling users to utilize MongoDB as a retrieval-augmented generation (RAG) agent within AutoGen. This integration empowers developers and data scientists to harness the power of MongoDB's scalable and flexible database capabilities in building generative AI multi-agent systems.
MongoDB AutoGen Integration workflow
What's covered in this article:
  • Integration of the multi-agent framework AutoGen with MongoDB
  • Creation of an innovative agentic system akin to a Kubernetes environment
  • Benefits of multi-agent systems in AI applications
  • Key capabilities and features of AutoGen
  • Advantages of MongoDB for robust data management in multi-agent systems
  • Practical insights on combining these technologies for enhanced AI solutions
All code is available in this GitHub repository.

Understanding multi-agentic AI systems

Multi-agent systems can be compared to human organization structure, such as a high-performing team collaborating on a complex product development project. In such a team, diverse roles come together, each contributing specialized expertise to ensure the product's success. This same principle of collaboration is mirrored in multi-agent AI systems, where distinct AI agents, each with unique capabilities, work together to achieve a common objective.
  • Product manager: Defines the product vision, strategy, and roadmap to ensure alignment with market needs and business objectives
  • Developer: Implements features, ensuring the application is robust and production-ready
  • Quality assurance engineer: Tests the product to ensure reliability and quality
Ensuring collaboration among agents enhances their functionality and broadens their range of applications. When agents work together, they can tackle complex tasks more efficiently than a single agent could on its own. This is true because collaboration among agents allows for specialization and division of labor, enabling each agent to focus on its strengths while compensating for others' limitations.
Before exploring AutoGen and the MongoDB integration, it's important to discuss the prominent frameworks used to build AI agents and agentic systems. This is particularly relevant because the agentic AI space can be considered to be in its infancy. Examining these frameworks will help highlight the key advantages of AutoGen for your specific use case.
Several frameworks have gained popularity for developing multi-agent systems, each offering unique features and benefits.

Microsoft AutoGen

Microsoft introduced the AutoGen framework to simplify the development of multi-agent applications. AutoGen is designed to orchestrate large language model (LLM) agents effectively. It provides a multi-agent conversation framework as a high-level abstraction, making it easier for next-generation LLM applications with multi-agent collaborations.
  • Simplified orchestration: AutoGen simplifies the process of managing workflows involving multiple agents.
  • High-level abstraction: It offers a structured approach to manage agent interactions and workflows.
  • Open source: It allows developers to customize and extend its capabilities to fit specific application needs.

The CrewAI framework

The CrewAI framework leverages the principles of teamwork, where:
  • Agents are initialized with specific attributes and capabilities.
  • Tasks have detailed descriptions of objectives and expected outcomes.
  • Tools provide agents with the ability to perform various actions effectively.
  • Processes dictate the strategic workflow, guiding how the crew approaches and completes tasks.

LangGraph

LangGraph employs a "divide-and-conquer" strategy for multi-agent collaboration. This strategy creates specialized agents for each task or domain and routes tasks to the appropriate "expert." This approach ensures that complex tasks are managed effectively, overcoming the limitations of a single agent, even powerful models like GPT-4.
LangGraph visualizes agent interactions and workflows, making it easier to optimize processes and identify improvements. This makes it an ideal framework for developing robust multi-agent systems that can efficiently tackle complex, multi-faceted tasks.
Having explored the frameworks that facilitate multi-agent collaborations, the next section will focus on integrating AutoGen with MongoDB. This integration promises to enhance the capabilities of multi-agent systems by leveraging MongoDB's vector store, NoSQL capabilities, scalability, flexibility, and real-time data handling, making it an ideal choice for supporting multi-agent frameworks.

Why AutoGen and MongoDB? 

AutoGen is an open-source framework designed to simplify the creation and management of AI agents, enabling them to collaborate effectively on complex tasks. By streamlining workflows, AutoGen optimizes the development of advanced LLM applications, integrating toolsets and automating code execution within its architecture.
  • One of AutoGen’s core strengths lies in its flexible conversation structures, supporting a variety of interaction modes such as two-agent dialogues, sequential chats, and group discussions. This adaptability allows developers to design agent interactions that fit the unique requirements of different tasks, providing customizable communication flows for more efficient task execution.
  • A key feature of AutoGen is its human-in-the-loop capability, which allows for real-time human oversight and intervention. This approach enhances decision-making, improves system accuracy, and increases the adaptability of the framework to handle evolving challenges, ensuring more reliable outcomes.
In multi-agent systems, MongoDB stands out as a powerful data management solution. Atlas Vector Search is particularly suited for the data-heavy demands of AI-driven environments. MongoDB allows vector embeddings to be stored alongside metadata and source data, with automatic synchronization to the vector index, ensuring the database remains up-to-date with minimal effort.
  • MongoDB’s Atlas Vector Search enables seamless querying of both vector data and traditional data through the MongoDB Query API, offering a unified and efficient querying experience. Its hybrid search capabilities combine traditional keyword searches with advanced semantic vector searches, delivering more contextually accurate results. This dual approach ensures that searches are not only relevant but also comprehensive, making it ideal for complex data environments.
  • The semantic caching layer of MongoDB further enhances performance, particularly in RAG applications. By caching query results based on their semantic meaning, MongoDB reduces redundant computations, speeding up response times while providing more relevant, context-aware interactions.
  • Additionally, MongoDB’s fully managed Atlas platform eliminates the need for manual infrastructure management, allowing developers to focus on building applications. Its dedicated search node infrastructure enhances resource efficiency, boosts performance, and ensures high availability, making MongoDB a scalable and reliable solution for handling the complex needs of multi-agent systems.
With these capabilities, MongoDB is an excellent choice for supporting the data-intensive demands of AI-powered, multi-agent systems, ensuring high performance, scalability, and relevance in complex applications.
AMP stack AutoGen MongoDB Python

Implementation guide: AutoGen MongoDB integration 

In this section, we'll explore how to integrate AutoGen with MongoDB to construct a powerful retrieval-augmented generation system using two distinct agents. 
We will guide you through setting up the necessary environment, configuring the agents, and illustrating how a question is processed by the retrieval agent, which fetches information from the MongoDB vector store. 
This data is then used to provide an augmented answer to the user via the user proxy agent. Here's a step-by-step explanation of the setup and implementation process:

Step 1: Installation and environment setup

Before coding, it is essential to set up the environment with all necessary dependencies. This involves creating a Conda environment and installing the required libraries.
1. Set up Conda environment:
Begin by creating a new Conda environment specifically for this project. This ensures a clean and isolated workspace.
1 conda create -n pyautogen python=3.12
2 conda activate pyautogen
Python 3.12: Ensure you have the correct Python version, as it supports the features needed for our implementation.
2. Install required libraries: Next, install the necessary libraries, including autogen for agent implementation and pymongo for MongoDB interaction.
1!pip install pyautogen pymongo sentence_transformers
  • pyautogen: This library provides the tools needed to create and manage agents within our framework.
  • pymongo: Essential for connecting and interacting with MongoDB databases.
  • Sentence_transformers: This package is crucial for MongoDB's vector usage, enabling efficient search and retrieval.
3. Install additional dependencies: Install additional packages required by AutoGen for enhanced functionality and compatibility.
1!pip install ipython markdownify pypdf chromadb
  • ipython: Provides an interactive Python environment, facilitating testing and debugging
  • markdownify: Converts HTML content into Markdown; useful for processing documents
  • pypdf: Enables PDF document handling, which is crucial for retrieving text from various file formats
  • chromadb: Needed package for AutoGen dependency

Step 2: Multi-agent implementation

With the environment prepared, we import the necessary libraries and modules for our implementation. The autogen package is central to this setup, alongside specific agents for retrieval tasks. Using environment variables, we also handle sensitive information like API keys and MongoDB connection strings.
Note: Follow MongoDB’s steps to get the connection string from the Atlas UI. Securely store the URI within your development environment after setting up the database and obtaining the Atlas cluster connection URI.
1import os
2import getpass
3
4os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
5os.environ["MONGODB_URI"] = getpass.getpass("MongoDB URI:")
Configuring AutoGen and MongoDB Next, we configure the model and MongoDB, ensuring secure access and compatibility with our vector database.
  • Autogen config list: We define the model to be used, specifying gpt-3.5-turbo-0125 with its API key. This configuration ensures that at least one model is set up for agent interaction.
  • MongoDB connection: The MongoDB URI is retrieved from environment variables, securing access to the database.
  • File formats: Accepted file formats for document retrieval are printed to confirm compatibility with the vector database.
1import json
2import os
3
4import autogen
5from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent
6from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent
7
8# Accepted file formats for that can be stored in
9# a vector database instance
10from autogen.retrieve_utils import TEXT_FORMATS
11
12config_list = [{"model": "gpt-3.5-turbo-0125", "api_key": os.environ["OPENAI_API_KEY"], "api_type": "openai"}]
13assert len(config_list) > 0
14print("models to use: ", [config_list[i]["model"] for i in range(len(config_list))])
15MONGODB_URI = os.environ["MONGODB_URI"]
16
17print("Accepted file formats for `docs_path`:")
18print(TEXT_FORMATS)

Step 3: Creating the agents

The RetrieveAssistantAgent is initialized as an "assistant," acting as the primary interface to assist users with information retrieval.
  • Assistant agent: This agent is configured to interact with users, utilizing the specified model configuration.
1assistant = RetrieveAssistantAgent(
2 name="assistant",
3 system_message="You are a helpful assistant.",
4 llm_config={
5 "timeout": 600,
6 "cache_seed": 42,
7 "config_list": config_list,
8 },
9)
The RetrieveUserProxyAgent is configured to facilitate communication between the user and the assistant. This agent is named MongoRAGagent and operates autonomously without requiring human input.
  • Human input mode: This is set to "NEVER," allowing the agent to function independently.
  • Vector database configuration: MongoDB settings, including connection string, database name, collection name, and index management, are defined. Wait times for index readiness are configured to optimize retrieval performance.
  • Document ingestion: Paths to documents are specified, with custom text types and chunk token sizes ensuring efficient processing.
1ragproxyagent = RetrieveUserProxyAgent(
2 name="MongoRAGagent",
3 human_input_mode="NEVER",
4 max_consecutive_auto_reply=3,
5 retrieve_config={
6 "task": "code",
7 "docs_path": [
8 "https://github.com/mongodb-partners/maap-framework/blob/main/README.md",
9 os.path.join(os.path.abspath(""), "..", "website", "docs"),
10 ],
11 "custom_text_types": ["non-existent-type"],
12 "chunk_token_size": 2000,
13 "model": config_list[0]["model"],
14 "vector_db": "mongodb", # MongoDB Atlas database
15 "collection_name": "demo_collection",
16 "db_config": {
17 "connection_string": os.environ["MONGODB_URI"], # MongoDB Atlas connection string
18 "database_name": "test_db", # MongoDB Atlas database
19 "index_name": "vector_index",
20 "wait_until_index_ready": 120.0, # Setting to wait 120 seconds or until index is constructed before querying
21 "wait_until_document_ready": 120.0, # Setting to wait 120 seconds or until document is properly indexed after insertion/update
22 },
23 "get_or_create": True, # set to False if you don't want to reuse an existing collection
24 "overwrite": False, # set to True if you want to overwrite an existing collection, each overwrite will force a index creation and reupload of documents
25 },
26 code_execution_config=False, # set to False if you don't want to execute the code
27)

Step 4: Initiating and managing conversations

The final step involves initiating and managing the conversation flow between the agents and the user.
  • Reset the assistant: Before starting a new conversation, the assistant is reset to clear any previous context and ensure accurate responses.
  • Problem statement: A code problem is defined — “How does the MAAP framework handle data ingestion?”
  • Initiate chat: The ragproxyagent generates a prompt and engages the assistant. The conversation continues until a termination condition is met, such as the absence of code blocks or user exit.
1assistant.reset()
2
3code_problem = "How does the MAAP framework handle data ingestion?"
4chat_result = ragproxyagent.initiate_chat(assistant, message=ragproxyagent.message_generator, problem=code_problem)
The results of the operations are as follows:
1--------------------------------------------------------------------------------
2assistant (to MongoRAGagent):
3
4The MAAP framework handles data ingestion by providing support for multiple types of data loaders tailored to handle specific data sources such as web pages, PDF files, sitemaps, DOCX documents, and Confluence spaces. These data loaders are configured with parameters like source paths and chunking details and are added to a data loaders array for processing. The process of ingesting data involves configuring the application, running the server, and starting the UI client application to interact with the data through a web interface.
5
6--------------------------------------------------------------------------------

Conclusion

The integration of AutoGen with MongoDB enables the creation of advanced multi-agent systems by combining flexible agent orchestration with powerful data management and vector search capabilities. Developers can create sophisticated, scalable, and efficient agentic AI solutions by leveraging AutoGen's flexible agent orchestration capabilities and MongoDB's robust data management features.
This powerful combination allows for:
  • Enhanced collaboration among AI agents, mimicking high-performing human teams.
  • Efficient handling of complex, multi-faceted tasks through specialized agent roles.
  • Efficient retrieval and utilization of relevant information via MongoDB's vector search capabilities.
  • Improved context awareness and response relevance in AI interactions.
As the agentic AI space continues to evolve, frameworks like AutoGen, coupled with MongoDB's advanced data storage and retrieval systems, enable the development of more intelligent, adaptable, and capable multi-agent systems. This integration empowers developers to build AI solutions that can tackle increasingly complex challenges across various domains.
Following this implementation guide, you can integrate these tools to build intelligent agents capable of handling intricate tasks. All code presented in this article is available in this GitHub repository.

Frequently asked questions (FAQ)

1. What is the main benefit of integrating AutoGen with MongoDB?
The integration combines AutoGen’s multi-agent orchestration with MongoDB’s powerful data management and vector search capabilities. This enhances the performance of retrieval-augmented generation (RAG) systems by enabling agents to efficiently retrieve and process large amounts of data in real-time, improving decision-making and task execution.
2. How does MongoDB’s Atlas Vector Search enhance agent performance?
MongoDB’s Atlas Vector Search enables agents to perform both traditional keyword-based searches and semantic vector searches, ensuring more contextually accurate and relevant results. This dual-search capability allows agents to better handle complex queries, making interactions more efficient and effective.
3. Where can I find the official documentation for AutoGen and MongoDB integration?
You can find the official AutoGen documentation on Microsoft’s GitHub page
4. Where can I learn more about MongoDB Atlas and its vector search capabilities?
For detailed information about MongoDB Atlas and its vector search features, you can visit the official MongoDB documentation, which covers its powerful data management tools and hybrid search functionalities.
5. What types of applications can benefit from the AutoGen and MongoDB integration?
This integration can benefit applications that require data processing automation, retrieval-augmented generation, and multi-agent collaboration, such as AI-driven customer support systems, research assistants, and intelligent data analysis platforms. The framework is particularly suited for environments with high data demands and the need for real-time decision-making.
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

AWS Glue Visual ETL for Your Data in MongoDB Atlas


Nov 22, 2024 | 3 min read
Article

Atlas Search Playground: Easy Experimentation


Jun 03, 2024 | 7 min read
Article

Atlas Search is a Game Changer!


Sep 09, 2024 | 2 min read
Article

Atlas Search Relevancy Explained


Aug 14, 2024 | 13 min read
Table of Contents