Custom classifier
This guide explains how to create a custom classifier for the Agent Squad by extending the abstract Classifier class. Custom classifiers allow you to implement your own logic for intent classification and agent selection.
Overview
Section titled “Overview”To create a custom classifier, you need to:
- Extend the abstract
Classifierclass - Implement the required
process_requestmethod - Optionally override other methods for additional customization
Step-by-Step Guide
Section titled “Step-by-Step Guide”1. Extend the Classifier Class
Section titled “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 agent_squad.classifiers import Classifierfrom agent_squad.types import ClassifierResult, ConversationMessagefrom typing import List
class MyCustomClassifier(Classifier): # Implementation will go here pass2. Implement the process_request Method
Section titled “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
Section titled “Using Your Custom Classifier”To use your custom classifier with the Agent Squad:
import { AgentSquad } from './path-to-agent-squad';import { MyCustomClassifier } from './path-to-my-custom-classifier';
const customClassifier = new MyCustomClassifier();const orchestrator = new AgentSquad({ classifier: customClassifier });from agent_squad.orchestrator import AgentSquadfrom path_to_my_custom_classifier import MyCustomClassifier
custom_classifier = MyCustomClassifier()orchestrator = AgentSquad(classifier=custom_classifier)Best Practices
Section titled “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_requestmethod 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
Section titled “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 AgentSquad({ classifier: keywordClassifier });from agent_squad.classifiers import Classifierfrom agent_squad.types import ClassifierResult, ConversationMessagefrom agent_squad.orchestrator import AgentSquadfrom 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 = AgentSquad(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
Section titled “Conclusion”Creating a custom classifier allows you to implement specialized logic for intent classification and agent selection in the Agent Squad. 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.