Skip to content

User Group Agent

The User Group Agent module manages group interactions and user behavior in chat environments.

Overview

from teleAgent.models.agent_model.user_groupagent import UserGroupAgent

The UserGroupAgent class extends the base agent functionality to handle group-specific behaviors and interactions.

Core Components

State Management

class UserGroupAgent(ProactGroupAgent):
    def __init__(self, name: str, system_message: str, **kwargs):
        super().__init__(name=name, system_message=system_message, **kwargs)
        self.working_memory = {}
        self.long_term_memory = {}
        self.social_relations = {}

Event Handling

async def a_update_inner_state(self, messages, sender, **kwargs):
    """
    Update agent's inner state based on new messages

    Args:
        messages: List of message dictionaries
        sender: The agent or user who sent the messages
    """
    if self.working_memory.get('fast_reply', 0):
        self.working_memory.update({'response_decision': 0})
        return (False, None)

    message = self.transform_message_history(messages, sender)
    self.update_message_to_modules(message)
    reply = await self.a_initiate_chats(self.module_sequence)
    self.update_system_message()

Social Interaction Management

def update_social_relations(self, user_id: str, interaction_data: dict):
    """
    Update social relationship data for a specific user

    Args:
        user_id: Unique identifier for the user
        interaction_data: Dictionary containing interaction metrics
    """
    if user_id not in self.social_relations:
        self.social_relations[user_id] = {}

    self.social_relations[user_id].update(interaction_data)

Integration Examples

Basic Group Chat Setup

async def setup_group_chat():
    agent = UserGroupAgent(
        name="GroupManager",
        system_message="I am a helpful group chat manager",
        llm_config={"model": "gpt-4"}
    )

    # Add event handlers
    @agent.on_user_join
    async def welcome_user(user):
        await agent.send_message(
            f"Welcome {user.name} to the group!"
        )

    return agent

Advanced Usage

# Implementing complex group behaviors
async def manage_group_dynamics():
    agent = UserGroupAgent(...)

    # Monitor and moderate discussions
    @agent.on_message
    async def moderate_content(message):
        analysis = await agent.analyze_message(message)
        if analysis.requires_moderation:
            await agent.moderate_message(message)

    # Track user engagement
    @agent.on_interaction
    async def track_engagement(interaction):
        await agent.update_user_metrics(
            interaction.user_id,
            interaction.metrics
        )

Best Practices

  1. State Management
  2. Regularly persist important state data
  3. Implement state recovery mechanisms
  4. Clear temporary states periodically

  5. Memory Management

  6. Use working memory for short-term context
  7. Persist important information to long-term memory
  8. Implement memory cleanup routines

  9. Social Relations

  10. Track user interactions
  11. Update relationship metrics
  12. Use relationship data for personalized responses

  13. Error Handling

  14. Implement graceful fallbacks
  15. Log errors appropriately
  16. Maintain group stability during errors

Configuration

agent_config = {
    "name": "GroupManager",
    "personality": "helpful and friendly",
    "response_style": "casual",
    "memory_config": {
        "max_working_memory": 1000,
        "persistence_path": "/path/to/storage"
    },
    "moderation_rules": {
        "max_messages_per_minute": 10,
        "forbidden_words": ["spam", "abuse"],
        "action_thresholds": {
            "warning": 2,
            "mute": 3,
            "ban": 5
        }
    }
}