Custom classifier
This guide explains how to create a custom classifier for the Multi-Agent Orchestrator by extending the abstract Classifier
class. Custom classifiers allow you to implement your own logic for intent classification and agent selection.
Overview
To create a custom classifier, you need to:
- Extend the abstract
Classifier
class - Implement the required
process_request
method - Optionally override other methods for additional customization
Step-by-Step Guide
1. Extend the Classifier Class
Create a new class that extends the abstract Classifier
class:
import { Classifier } from './path-to-classifier';import { ClassifierResult, ConversationMessage } from './path-to-types';
export class MyCustomClassifier extends Classifier { // Implementation will go here}
from multi_agent_orchestrator.classifiers import Classifierfrom multi_agent_orchestrator.types import ClassifierResult, ConversationMessagefrom typing import List
class MyCustomClassifier(Classifier): # Implementation will go here pass
2. Implement the process_request Method
The process_request
method is the core of your custom classifier. It should analyze the input and return a ClassifierResult
:
export class MyCustomClassifier extends Classifier { async processRequest( inputText: string, chatHistory: ConversationMessage[] ): Promise<ClassifierResult> { // Your custom classification logic goes here
return { selectedAgent: firstAgent, confidence: 1.0 }; }}
class MyCustomClassifier(Classifier): async def process_request( self, input_text: str, chat_history: List[ConversationMessage] ) -> ClassifierResult: # Your custom classification logic goes here
first_agent = next(iter(self.agents.values())) return ClassifierResult( selected_agent=first_agent, confidence=1.0 )
Using Your Custom Classifier
To use your custom classifier with the Multi-Agent Orchestrator:
import { MultiAgentOrchestrator } from './path-to-multi-agent-orchestrator';import { MyCustomClassifier } from './path-to-my-custom-classifier';
const customClassifier = new MyCustomClassifier();const orchestrator = new MultiAgentOrchestrator({ classifier: customClassifier });
from multi_agent_orchestrator.orchestrator import MultiAgentOrchestratorfrom path_to_my_custom_classifier import MyCustomClassifier
custom_classifier = MyCustomClassifier()orchestrator = MultiAgentOrchestrator(classifier=custom_classifier)
Best Practices
- Robust Analysis: Implement thorough analysis of the input text and chat history to make informed classification decisions.
- Error Handling: Include proper error handling in your
process_request
method to gracefully handle unexpected inputs or processing errors. - Extensibility: Design your custom classifier to be easily extensible for future improvements or adaptations.
- Performance: Consider the performance implications of your classification logic, especially for high-volume applications.
Example: Keyword-Based Classifier
Here’s an example of a simple keyword-based classifier:
import { Classifier } from './path-to-classifier';import { ClassifierResult, ConversationMessage, Agent } from './path-to-types';
export class KeywordClassifier extends Classifier { private keywordMap: { [keyword: string]: string };
constructor(keywordMap: { [keyword: string]: string }) { super(); this.keywordMap = keywordMap; }
async processRequest( inputText: string, chatHistory: ConversationMessage[] ): Promise<ClassifierResult> { const lowercaseInput = inputText.toLowerCase();
for (const [keyword, agentId] of Object.entries(this.keywordMap)) { if (lowercaseInput.includes(keyword)) { const selectedAgent = this.getAgentById(agentId); return { selectedAgent, confidence: 0.8 // Simple fixed confidence }; } }
// Default to the first agent if no keyword matches const defaultAgent = Object.values(this.agents)[0]; return { selectedAgent: defaultAgent, confidence: 0.5 }; }}
// Usageconst keywordMap = { 'technical': 'tech-support-agent', 'billing': 'billing-agent', 'sales': 'sales-agent'};const keywordClassifier = new KeywordClassifier(keywordMap);const orchestrator = new MultiAgentOrchestrator({ classifier: keywordClassifier });
from multi_agent_orchestrator.classifiers import Classifierfrom multi_agent_orchestrator.types import ClassifierResult, ConversationMessagefrom multi_agent_orchestrator.orchestrator import MultiAgentOrchestratorfrom typing import List, Dict
class KeywordClassifier(Classifier): def __init__(self, keyword_map: Dict[str, str]): super().__init__() self.keyword_map = keyword_map
async def process_request( self, input_text: str, chat_history: List[ConversationMessage] ) -> ClassifierResult: lowercase_input = input_text.lower()
for keyword, agent_id in self.keyword_map.items(): if keyword in lowercase_input: selected_agent = self.get_agent_by_id(agent_id) return ClassifierResult( selected_agent=selected_agent, confidence=0.8 # Simple fixed confidence )
# Default to the first agent if no keyword matches default_agent = next(iter(self.agents.values())) return ClassifierResult( selected_agent=default_agent, confidence=0.5 )
# Usagekeyword_map = { 'technical': 'tech-support-agent', 'billing': 'billing-agent', 'sales': 'sales-agent'}keyword_classifier = KeywordClassifier(keyword_map)orchestrator = MultiAgentOrchestrator(classifier=keyword_classifier)
This example demonstrates a basic keyword-based classification strategy. You can expand on this concept to create more sophisticated custom classifiers based on your specific needs.
Conclusion
Creating a custom classifier allows you to implement specialized logic for intent classification and agent selection in the Multi-Agent Orchestrator. By extending the Classifier
class and implementing the process_request
method, you can tailor the classification process to your specific use case and requirements.
Remember to thoroughly test your custom classifier to ensure it performs well across a wide range of inputs and scenarios.