Danialebrat's picture
Deploying sentiment analysis project
9858829
"""
Base Agent class for all agents in the workflow
This provides a common interface and structure for extensibility
"""
from abc import ABC, abstractmethod
from typing import Dict, Any, Optional
import json
import logging
logger = logging.getLogger(__name__)
class BaseAgent(ABC):
"""
Abstract base class for all agents in the agentic workflow.
Provides common functionality and enforces consistent interface.
"""
def __init__(self, name: str, config: Dict[str, Any]):
"""
Initialize the base agent.
Args:
name: Name of the agent
config: Configuration dictionary for the agent
"""
self.name = name
self.config = config
self.model = config.get("model", "gpt-4o-mini")
self.temperature = config.get("temperature", 0.7)
self.max_retries = config.get("max_retries", 3)
logger.info(f"Initialized {self.name} with model {self.model}")
@abstractmethod
def process(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
"""
Process input data and return results.
This method must be implemented by all concrete agent classes.
Args:
input_data: Dictionary containing input data for processing
Returns:
Dictionary containing processing results
"""
pass
@abstractmethod
def validate_input(self, input_data: Dict[str, Any]) -> bool:
"""
Validate input data before processing.
Args:
input_data: Dictionary containing input data
Returns:
True if input is valid, False otherwise
"""
pass
def get_name(self) -> str:
"""Get the agent name."""
return self.name
def get_config(self) -> Dict[str, Any]:
"""Get the agent configuration."""
return self.config
def log_processing(self, message: str, level: str = "info"):
"""
Log processing information.
Args:
message: Log message
level: Log level (info, warning, error, debug)
"""
log_method = getattr(logger, level, logger.info)
log_method(f"[{self.name}] {message}")
def handle_error(self, error: Exception, context: str = "") -> Dict[str, Any]:
"""
Handle errors consistently across all agents.
Args:
error: The exception that occurred
context: Additional context about the error
Returns:
Error dictionary with details
"""
error_msg = f"Error in {self.name}"
if context:
error_msg += f" ({context})"
error_msg += f": {str(error)}"
logger.error(error_msg)
return {
"success": False,
"error": str(error),
"agent": self.name,
"context": context
}