Custom Behaviors
Create sophisticated custom behaviors to enhance your AI agents' capabilities. Learn how to implement personality traits, decision-making processes, and specialized interaction patterns.
Basic Custom Behaviors
Create and implement custom behaviors for specific use cases:
custom_behavior.py
12345678910111213141516171819202122
from voxin import Agent, Behavior, Contextclass CustomerSupportBehavior(Behavior): async def analyze_sentiment(self, message: str) -> float: sentiment = await self.agent.analyze(message) return sentiment.score async def handle_frustrated_customer(self, context: Context): await self.agent.adjust_tone(tone="empathetic") await self.agent.speak( "I understand your frustration. Let's solve this together." )# Apply custom behavioragent = Agent(name"Support Agent")agent.add_behavior(CustomerSupportBehavior())@agent.on_messageasync def handle_message(message): sentiment = await agent.behaviors.analyze_sentiment(message) if sentiment < 0.3: # Negative sentiment await agent.behaviors.handle_frustrated_customer(message)
Key Concepts
- ✓ Behavior inheritance
- ✓ Context handling
- ✓ Event processing
- ✓ State management
Personality & Emotional Intelligence
Implement personality traits and emotional responses:
personality.py
1234567891011121314151617181920212223
from voxin import PersonalityTraits, EmotionalStateclass FriendlyPersonality(Behavior): def __init__(self): self.traits = PersonalityTraits( friendliness=0.9, formality=0.3, enthusiasm=0.8 ) self.emotional_state = EmotionalState() async def modify_response(self, response: str) -> str: # Adjust response based on personality modified = await self.agent.apply_personality_traits( response, self.traits ) return modified async def update_emotional_state(self, context: Context): # Update emotional state based on interaction self.emotional_state.update(context.emotional_signals) await self.agent.adapt_behavior(self.emotional_state)
Personality Features
- ✓ Customizable personality traits
- ✓ Emotional state tracking
- ✓ Response modification
- ✓ Adaptive behavior
Decision Making
Create complex decision-making behaviors:
decision_making.py
12345678910111213141516171819202122232425262728
from voxin import DecisionTree, Ruleclass SupportDecisionMaking(Behavior): def __init__(self): self.decision_tree = DecisionTree([ Rule("technical_issue", condition=lambda ctx: "error" in ctx.message.lower(), action=self.handle_technical), Rule("billing_issue", condition=lambda ctx: "bill" in ctx.message.lower(), action=self.handle_billing), Rule("general_inquiry", condition=lambda ctx: True, action=self.handle_general) ]) async def decide_action(self, context: Context): action = await self.decision_tree.evaluate(context) return await action(context) async def handle_technical(self, context): return await self.agent.provide_technical_support(context) async def handle_billing(self, context): return await self.agent.handle_billing_inquiry(context) async def handle_general(self, context): return await self.agent.handle_general_inquiry(context)
Best Practices
- ✓ Keep behaviors focused and single-purpose
- ✓ Implement proper error handling
- ✓ Use dependency injection for flexibility
- ✓ Document behavior interfaces
- ✓ Test behaviors in isolation