IntentAgent: A Modular Approach to Multi-Level Intent Classification with LLMs and MongoDB
- Subhagato Adak
- Feb 24
- 4 min read
🚀 What is IntentAgent?
IntentAgent is an advanced, modular library designed to classify user queries into hierarchical intents using MongoDB for storage and Large Language Models (LLMs) for classification. It allows dynamic method generation for different intents, making it easy to execute actions based on the classified intent.
Key Capabilities:
Multi-Level Intent Classification: Supports a deep intent hierarchy (e.g., domain → intent → subintent → sub-subintent).
Flexible Storage with MongoDB: Efficiently stores and manages hierarchical intent data.
Integration with LLMs: Uses OpenAI GPT, Anthropic Claude, Google Gemini, and Hugging Face modelsfor intelligent classification.
Auto-Generated Python Methods: Dynamically creates functions for each intent, allowing for automated execution flows.
Optimized Query Processing: Reduces unnecessary calls to expensive models by intelligently routing requests.
🛠️ Key Features of IntentAgent
1️⃣ Multi-Level Intent Handling
Unlike simple keyword-based intent classifiers, IntentAgent can handle deep hierarchies.
Example:domain: ecommerce ├── intent: buy_product │ ├── subintent: electronics │ │ ├── sub-subintent: phones │ │ └── sub-subintent: laptops │ └── subintent: clothing │ ├── sub-subintent: shirts │ ├── sub-subintent: shoes │ └── sub-subintent: accessories
Why is this important?
Enables highly context-aware AI models.
Improves classification accuracy.
Facilitates better query routing.
2️⃣ Uses MongoDB for Efficient Intent Storage
Instead of rigid database schemas, IntentAgent uses MongoDB to flexibly store intents.
Each domain-intent-subintent-query relationship is stored in an efficient NoSQL format.
MongoDB enables:
Fast querying of intents.
Scalability to handle large datasets.
Easy modifications without breaking schema constraints.
3️⃣ LLM-Powered Intent Classification
IntentAgent supports multiple LLM providers, allowing flexibility in selecting the best AI model for intent detection:
🔹 OpenAI (GPT-4, GPT-3.5, etc.)
🔹 Anthropic Claude
🔹 Google Gemini
🔹 Hugging Face Transformers
How it works:
The user query is processed and classified by an LLM.
The LLM returns the best-matched intent.
The system routes the query accordingly.
4️⃣ Dynamic Python Method Generation
One of IntentAgent's unique capabilities is automatically generating Python methods for detected intents.
Example:def buy_product_electronics_phones(): print("Executing buy_product_electronics_phones method.")
This enables custom business logic execution for each detected intent.
5️⃣ Intelligent Query Routing
Not all queries need a full LLM classification.
IntentAgent intelligently routes queries to either:
Existing intents stored in MongoDB (low-cost, fast)
LLM models for classification (high-accuracy, slightly slower)
Benefit: Reduces API costs and optimizes response time.
🏗️ How IntentAgent Works: Step-by-Step
📌 1. User Query Processing
A user submits a query, e.g., "I want to buy an iPhone".
The system first checks if a pre-existing intent exists in MongoDB.
If the intent is not found, the system forwards it to an LLM for classification.
📌 2. LLM-Based Intent Matching
The LLM determines the most relevant intent based on the stored hierarchy.
Example LLM response:{ "domain": "ecommerce", "intent": "buy_product", "subintent": "electronics", "sub-subintent": "phones" }
The classified intent is stored in MongoDB for future retrieval.
📌 3. Automated Execution Flow
If an intent method exists, the system executes it automatically.
If no method exists, IntentAgent generates a new Python method dynamically.
Example:agent.create_intent_methods("ecommerce")
This auto-generates:def buy_product_electronics_phones(): print("Executing buy_product_electronics_phones method.")
This allows for customized responses or actions based on intent.
⚙️ Architecture Overview
User Query → MongoDB Check → LLM Classification → Intent Storage → Auto Method Execution
🛠️ Core Components:
Component | Description |
MongoDBManager | Manages MongoDB interactions (insert, update, delete, query). |
IntentLibrary | Stores and retrieves intents in a hierarchical format. |
LLMManager | Connects to OpenAI, Hugging Face, Google Gemini, and Anthropic Claude for classification. |
IntentAgent | Manages intent execution, creates dynamic methods, and handles LLM interactions. |
📥 Installation Guide
🔹 Step 1: Clone the Repository
git clone https://github.com/<your-username>/intentagent-library.git
cd intentagent-library
🔹 Step 2: Install Dependencies
Using pip:
pip install -r requirements.txt
Or using Conda:
conda env create -f environment.yml
conda activate intent-classification
🔹 Step 3: Install as a Package (Optional)
python setup.py install
Or:
pip install .
📝 Example Usage
🔹 Initialize MongoDB and Library Classes
from mongodb_manager import MongoDBManager
from intent_library import IntentLibrary
from llm_manager import LLMManager
from intent_agent import IntentAgent
# 1. Initialize MongoDB
db_manager = MongoDBManager(db_name="IntentDB", uri="mongodb://localhost:27017")
# 2. Create the IntentLibrary
intent_lib = IntentLibrary(db_manager)
# 3. Choose your LLM provider and API key
llm_manager = LLMManager(api_key="YOUR_API_KEY", model_provider="openai")
# 4. Create the IntentAgent
agent = IntentAgent(intent_library=intent_lib, llm_manager=llm_manager)
🔹 Add a New Intent
intent_lib.add_intent(
domain="ecommerce",
intent_hierarchy=["buy_product", "electronics", "phones"],
queries=["I want to buy a phone", "Purchase a mobile"]
)
🔹 Generate a Response
response = agent.get_llm_response("ecommerce")
print("LLM Response:", response)
# Expected output: {"intent": "buy_product", ...}
🔹 Auto-Generate Intent Methods
agent.create_intent_methods("ecommerce")
This dynamically creates a file: intent_methods.py
Example generated function:
def buy_product_electronics_phones():
print("Executing buy_product_electronics_phones method.")
🔍 Key Use Cases
IntentAgent can be applied to various domains:
✔️ Chatbots & Virtual Assistants – Enhance AI-driven chat interfaces with context-aware responses.✔️ Customer Support Routing – Auto-classify customer inquiries into hierarchical categories.✔️ Voice Assistants (Alexa, Siri, Google Assistant) – Enable multi-level intent handling for voice interactions.✔️ Search & Recommendation Systems – Improve search query interpretation for better results.✔️ Automated Workflows – Dynamically trigger task execution based on user intent.
📌 Final Thoughts
IntentAgent is a powerful AI-driven intent classification system that combines MongoDB, LLMs, and dynamic function generation to create a highly scalable and intelligent intent detection pipeline.
If you're building a smart chatbot, customer support system, or intelligent workflow automation, IntentAgent is an essential tool.
👉 Check out the GitHub repository here: IntentAgent GitHub 🚀
Comments