🛡️ ArtSpark Chat Agent - Guard Rails & Input Validation

Guard-Rails-Input-Validation-Implementation.md Last updated: June 04, 2025 at 1:05 PM

🛡️ ArtSpark Chat Agent - Guard Rails & Input Validation

Overview

The ArtSpark Chat Agent now includes a comprehensive guard rails and input validation system to ensure that conversations remain appropriate, on-topic, and aligned with the educational mission of the platform. This system acts as a content filter and conversation guide, helping maintain high-quality interactions between users and AI personas.

Key Features

Content Validation

  • Message Format Validation: Ensures proper length, structure, and formatting
  • Inappropriate Content Detection: Filters out offensive, spam, or harmful content
  • Art-Topic Enforcement: Ensures conversations stay focused on art and cultural topics
  • Persona Alignment: Validates that questions are appropriate for the selected persona

🔍 Validation Categories

1. Format Validation

  • Minimum message length: 2 characters
  • Maximum message length: 500 characters
  • Prevents excessive repeating characters (e.g., "aaaaaa")
  • Blocks excessive punctuation (e.g., "!!!!!!!")
  • Detects potential spam patterns

2. Content Appropriateness

  • Filters inappropriate keywords and phrases
  • Detects potential spam or promotional content
  • Prevents excessive capitalization (shouting)
  • Blocks URLs and promotional language

3. Topic Relevance

  • Ensures messages are art-related or contextually appropriate
  • Allows greetings and polite conversation starters
  • Validates questions are about artworks, techniques, or cultural topics
  • Permits conversation continuations and follow-up questions

4. Persona Alignment

  • Artwork Persona: Questions about feelings, experience, and personal story
  • Artist Persona: Questions about creation process, techniques, and inspiration
  • Curator Persona: Questions about research, context, and collection significance
  • Historian Persona: Questions about historical context, periods, and cultural movements

Implementation

Core Components

1. IChatInputValidator Interface

public interface IChatInputValidator
{
    ChatValidationResult ValidateRequest(ChatRequest request);
    bool IsPersonaAppropriate(string message, ChatPersona persona);
    bool IsArtRelated(string message);
    bool ContainsInappropriateContent(string message);
    bool IsValidFormat(string message);
}

2. ChatValidationResult Model

public class ChatValidationResult
{
    public bool IsValid { get; set; }
    public string? ErrorMessage { get; set; }
    public ChatValidationError ErrorCode { get; set; }
    public string? Suggestion { get; set; }
    public Dictionary<string, object> Metadata { get; set; }
}

3. Validation Error Types

  • None - Validation passed
  • EmptyMessage - Message is empty or too short
  • MessageTooLong - Exceeds maximum length
  • InappropriateContent - Contains offensive or inappropriate material
  • OffTopic - Not related to art or museum context
  • PersonaMismatch - Not appropriate for selected persona
  • SpamDetected - Contains spam patterns
  • HarmfulContent - Potentially dangerous content
  • RateLimitExceeded - Too many requests
  • ValidationError - General validation failure

Integration in Chat Flow

The validation system is integrated at the beginning of the chat processing pipeline:

public async Task<ChatResponse> ChatAsync(ChatRequest request, CancellationToken cancellationToken = default)
{
    // Step 1: Validate input and check guard rails
    var validationResult = _inputValidator.ValidateRequest(request);
    if (!validationResult.IsValid)
    {
        return ChatResponse.FromValidationError(validationResult);
    }
    
    // Step 2: Continue with normal chat processing...
}

Usage Examples

Valid Messages

"What colors do you see in this painting?"
"How did the artist create those textures?"
"What is the historical significance of this piece?"
"Tell me about your cultural background."

Persona-Appropriate Questions

// Artwork Persona
"How do you feel about being displayed here?"
"What emotions do you evoke in viewers?"

// Artist Persona  
"What inspired you to create this piece?"
"What techniques did you use?"

// Curator Persona
"How did this artwork come to the museum?"
"What research has been done on this piece?"

// Historian Persona
"What period does this artwork represent?"
"How does this fit into art history?"

Invalid Messages

Off-Topic Content

"What's the weather like today?"
"Can you help me with my math homework?"
"What's your favorite restaurant?"

Inappropriate Content

"This is spam! Buy now!"
"HELP ME WITH MY BUSINESS!!!"
[Excessive capitalization or promotional content]

Format Issues

"" // Empty message
"hi" // Too short
[600+ character message] // Too long
"aaaaaaaaa" // Excessive repetition

Error Handling & User Guidance

When validation fails, the system provides:

1. Clear Error Messages

  • Explains why the message was rejected
  • Uses friendly, educational language
  • Avoids technical jargon

2. Helpful Suggestions

  • Provides alternative question ideas
  • Suggests persona-appropriate topics
  • Offers conversation starters

3. Contextual Guidance

  • Different suggestions based on error type
  • Persona-specific recommendations
  • Progressive guidance for repeat issues

Example Error Response

{
  "success": false,
  "error": "I'm here to discuss art and this artwork specifically. Your question doesn't seem to be related to art or this piece.",
  "suggestedQuestions": [
    "Try asking about the artwork's visual elements or historical context.",
    "What draws your eye in this artwork?",
    "How does this piece make you feel?"
  ],
  "metadata": {
    "validationError": "OffTopic",
    "suggestion": "Try asking about the artwork's visual elements, historical context, or cultural significance."
  }
}

Configuration & Customization

Keyword Lists

The system uses configurable keyword lists for content detection:

  • Art-Related Keywords: Expanded list of art, culture, and museum terms
  • Inappropriate Keywords: Configurable list of filtered content
  • Persona Keywords: Specific terms that align with each persona's expertise

Validation Rules

  • Message Length: Configurable min/max limits
  • Spam Detection: Adjustable patterns and thresholds
  • Content Filtering: Customizable inappropriate content rules
  • Topic Enforcement: Flexible art-related content detection

Service Registration

The validation service is automatically registered with the DI container:

services.AddScoped<IChatInputValidator, ChatInputValidator>();

Testing & Quality Assurance

Automated Testing

  • Comprehensive test suite covering all validation scenarios
  • Edge case testing for borderline content
  • Performance testing for validation speed
  • False positive/negative detection

Example Test Cases

// Valid cases
TestValidation("What colors are in this painting?", true);
TestValidation("Tell me about the brushstrokes", true);

// Invalid cases  
TestValidation("What's for lunch today?", false);
TestValidation("BUY NOW! CLICK HERE!", false);

// Borderline cases
TestValidation("Hello! Tell me about yourself.", true); // Greeting
TestValidation("Thank you for that explanation.", true); // Continuation

Benefits

🎯 Improved User Experience

  • Guides users toward meaningful art discussions
  • Provides helpful suggestions when messages are inappropriate
  • Maintains conversation quality and educational value

🛡️ Content Safety

  • Prevents inappropriate or offensive content
  • Blocks spam and promotional messages
  • Maintains family-friendly environment

🎭 Persona Consistency

  • Ensures questions align with persona capabilities
  • Helps users understand different perspectives
  • Maintains character authenticity

📊 Analytics & Insights

  • Tracks validation patterns and common issues
  • Identifies areas for improvement
  • Monitors system effectiveness

Future Enhancements

🔮 Planned Features

  • Machine Learning Integration: AI-powered content classification
  • Dynamic Keyword Lists: Learning from user interactions
  • Contextual Validation: Conversation history awareness
  • Multi-Language Support: Validation in multiple languages
  • Rate Limiting: Prevent abuse and spam
  • User Feedback Integration: Learn from user corrections

🔧 Configuration Options

  • Strictness Levels: Adjustable validation sensitivity
  • Custom Personas: Support for additional persona types
  • Institution-Specific Rules: Customizable validation for different museums
  • Administrative Override: Manual approval for edge cases

Conclusion

The guard rails and input validation system represents a significant enhancement to the ArtSpark Chat Agent, ensuring that conversations remain educational, appropriate, and engaging. By implementing comprehensive content filtering and user guidance, the system maintains the high quality of interactions while providing a safe and welcoming environment for art exploration and learning.

The system strikes a balance between being permissive enough to allow natural conversation while being restrictive enough to maintain educational focus and content appropriateness. Through clear error messages and helpful suggestions, users are guided toward meaningful art discussions that align with the platform's educational mission.