ai

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Overview

Package ai provides configuration and threshold management for AI-based content moderation.

Index

Constants

View Source
const (
	ActionNone      = "none"
	ActionFlag      = "flag"
	ActionHide      = "hide"
	ActionRemove    = "remove"
	ActionShadowBan = "shadow_ban"
	ActionReview    = "review"
)

ModerationAction types

View Source
const (
	SentimentPositive = "POSITIVE"
	SentimentNegative = "NEGATIVE"
	SentimentNeutral  = "NEUTRAL"
	SentimentMixed    = "MIXED"
)

Sentiment types

View Source
const (
	EntityPerson       = "PERSON"
	EntityLocation     = "LOCATION"
	EntityOrganization = "ORGANIZATION"
	EntityCommercial   = "COMMERCIAL_ITEM"
	EntityEvent        = "EVENT"
	EntityDate         = "DATE"
	EntityQuantity     = "QUANTITY"
	EntityTitle        = "TITLE"
	EntityOther        = "OTHER"
)

Entity types

View Source
const (
	PiiEmail         = "EMAIL"
	PiiPhone         = "PHONE"
	PiiAddress       = "ADDRESS"
	PiiSsn           = "SSN"
	PiiCreditCard    = "CREDIT_CARD"
	PiiBankAccount   = "BANK_ACCOUNT"
	PiiDriverLicense = "DRIVER_LICENSE"
	PiiPassport      = "PASSPORT"
	PiiName          = "NAME"
	PiiAge           = "AGE"
)

PII types

View Source
const (
	StatusPending    = "pending"
	StatusInProgress = "in_progress"
	StatusCompleted  = "completed"
	StatusFailed     = "failed"
)

Analysis request statuses

Variables

View Source
var (
	// ErrContentExtractionFailed is returned when content extraction from stream fails
	ErrContentExtractionFailed = errors.ProcessingFailed("content extraction", stdErrors.New("content extraction failed"))

	// ErrAnalysisFailed is returned when AI content analysis fails
	ErrAnalysisFailed = errors.ProcessingFailed("AI content analysis", stdErrors.New("AI content analysis failed"))

	// ErrAnalysisSaveFailed is returned when saving AI analysis fails
	ErrAnalysisSaveFailed = errors.FailedToSave("AI analysis", stdErrors.New("AI analysis save failed"))

	// ErrStreamUnmarshalFailed is returned when unmarshaling stream data fails
	ErrStreamUnmarshalFailed = errors.UnmarshalingFailed("stream image", stdErrors.New("stream image unmarshaling failed"))

	// ErrInvalidObjectPK is returned when object primary key is invalid
	ErrInvalidObjectPK = errors.IDInvalidFormat("object")

	// ErrNotAnalyzableType is returned when object type cannot be analyzed
	ErrNotAnalyzableType = errors.InvalidValue("object_type", []string{"image", "video", "audio", "text"}, "")

	// ErrNoJSONFoundInResponse is returned when no JSON is found in AI model response
	ErrNoJSONFoundInResponse = errors.ParsingFailed("JSON in AI response", stdErrors.New("JSON parsing failed"))

	// ErrLocalNetworkAccess is returned when attempting to access local networks
	ErrLocalNetworkAccess = errors.URLHostNotAllowed("", "local network")

	// ErrInvalidURLScheme is returned when URL has an invalid scheme
	ErrInvalidURLScheme = errors.URLSchemeNotAllowed("", "")

	// ErrImageDownloadHTTP is returned when image download fails with HTTP error
	ErrImageDownloadHTTP = errors.NetworkError("image download", stdErrors.New("image download failed"))

	// ErrInvalidEmbeddingResponse is returned when embedding response format is invalid
	ErrInvalidEmbeddingResponse = errors.ParsingFailed("embedding response", stdErrors.New("embedding response parsing failed"))

	// ErrGetAnalysisDeprecated is returned when deprecated GetAnalysis method is called
	ErrGetAnalysisDeprecated = errors.ProcessingFailed("GetAnalysis is deprecated", stdErrors.New("GetAnalysis is deprecated"))

	// ErrGetAnalysisStatsDeprecated is returned when deprecated GetAnalysisStats method is called
	ErrGetAnalysisStatsDeprecated = errors.ProcessingFailed("GetAnalysisStats is deprecated", stdErrors.New("GetAnalysisStats is deprecated"))
)

AI processing errors - consolidated to use centralized error system

View Source
var CostPerOperation = map[string]float64{
	"comprehend_sentiment":   0.0001,
	"comprehend_entities":    0.0001,
	"comprehend_pii":         0.0002,
	"comprehend_toxicity":    0.0005,
	"rekognition_moderation": 0.001,
	"rekognition_text":       0.001,
	"rekognition_celebrity":  0.001,
	"bedrock_claude":         0.002,
	"total_average":          0.003,
}

CostPerOperation defines AI service costs

View Source
var DefaultThresholds = map[string]ThresholdSet{
	"note": {
		AutoRemove: 0.95,
		AutoHide:   0.85,
		Flag:       0.70,
		WarnAuthor: 0.60,
	},
	"media": {
		AutoRemove: 0.90,
		AutoHide:   0.80,
		Flag:       0.65,
		WarnAuthor: 0.50,
	},
	"profile": {
		AutoRemove: 0.98,
		AutoHide:   0.90,
		Flag:       0.75,
		WarnAuthor: 0.65,
	},
	"comment": {
		AutoRemove: 0.93,
		AutoHide:   0.83,
		Flag:       0.68,
		WarnAuthor: 0.58,
	},
}

DefaultThresholds for different content types

View Source
var SignalWeights = map[string]float64{
	"toxicity":      0.3,
	"spam":          0.2,
	"ai_generated":  0.1,
	"nsfw":          0.3,
	"violence":      0.1,
	"pii":           0.2,
	"impersonation": 0.3,
	"harassment":    0.25,
}

SignalWeights for different AI detection signals

Functions

func GetSignalWeight

func GetSignalWeight(signalType string) float64

GetSignalWeight returns the weight for a specific signal type

Types

type AIAnalysis

type AIAnalysis struct {
	// Identity
	ID         string `json:"id" dynamodbav:"ID"`
	ObjectID   string `json:"object_id" dynamodbav:"ObjectID"`
	ObjectType string `json:"object_type" dynamodbav:"ObjectType"`

	// Text Analysis (AWS Comprehend)
	TextAnalysis *TextAnalysis `json:"text_analysis,omitempty" dynamodbav:"TextAnalysis,omitempty"`

	// Image Analysis (AWS Rekognition)
	ImageAnalysis *ImageAnalysis `json:"image_analysis,omitempty" dynamodbav:"ImageAnalysis,omitempty"`

	// AI Detection (AWS Bedrock)
	AIDetection *AIDetection `json:"ai_detection,omitempty" dynamodbav:"AIDetection,omitempty"`

	// Spam Detection (Custom)
	SpamAnalysis *SpamAnalysis `json:"spam_analysis,omitempty" dynamodbav:"SpamAnalysis,omitempty"`

	// Composite Scores
	OverallRisk      float64 `json:"overall_risk" dynamodbav:"OverallRisk"`
	ModerationAction string  `json:"moderation_action" dynamodbav:"ModerationAction"`
	Confidence       float64 `json:"confidence" dynamodbav:"Confidence"`

	// Metadata
	AnalyzedAt time.Time `json:"analyzed_at" dynamodbav:"AnalyzedAt"`
	Version    string    `json:"version" dynamodbav:"Version"`
	TTL        int64     `json:"-" dynamodbav:"TTL,omitempty"`
}

AIAnalysis represents comprehensive AI analysis of content

type AIConfig

type AIConfig struct {
	// Thresholds for auto-moderation
	NSFWThreshold      float64
	ToxicityThreshold  float64
	SpamThreshold      float64
	AIContentThreshold float64

	// Feature flags
	EnablePIIDetection  bool
	EnableAIDetection   bool
	EnableImageAnalysis bool

	// Model configurations
	BedrockModelID   string
	ToxicityModelARN string // Custom Comprehend classifier

	// S3 bucket for image analysis
	S3Bucket string

	// SQS queue URL for AI processing
	AIQueueURL string
}

AIConfig contains configuration for AI service features and thresholds

func DefaultAIConfig

func DefaultAIConfig() *AIConfig

DefaultAIConfig returns a sensible default configuration

type AIDetection

type AIDetection struct {
	// AI-Generated Content Detection
	AIGeneratedProbability float64 `json:"ai_generated_probability" dynamodbav:"AIGeneratedProbability"`
	GenerationModel        string  `json:"generation_model,omitempty" dynamodbav:"GenerationModel,omitempty"`

	// Pattern Analysis
	PatternConsistency float64 `json:"pattern_consistency" dynamodbav:"PatternConsistency"`
	StyleDeviation     float64 `json:"style_deviation" dynamodbav:"StyleDeviation"`

	// Semantic Coherence
	SemanticCoherence float64 `json:"semantic_coherence" dynamodbav:"SemanticCoherence"`
	TopicConsistency  float64 `json:"topic_consistency" dynamodbav:"TopicConsistency"`

	// Suspicious Patterns
	SuspiciousPatterns []string `json:"suspicious_patterns" dynamodbav:"SuspiciousPatterns"`
}

AIDetection using AWS Bedrock

type AIService

type AIService struct {
	// contains filtered or unexported fields
}

AIService provides AI-powered content moderation and analysis

func NewAIService

func NewAIService(cfg aws.Config, aiConfig *AIConfig) *AIService

NewAIService creates a new AI service instance

func NewAIServiceWithSQS

func NewAIServiceWithSQS(cfg aws.Config, aiConfig *AIConfig, sqsClient SQSClient) *AIService

NewAIServiceWithSQS creates a new AI service instance with custom SQS client

func (*AIService) AnalyzeContent

func (s *AIService) AnalyzeContent(ctx context.Context, content *Content) (*AIAnalysis, error)

AnalyzeContent performs comprehensive AI analysis

func (*AIService) GenerateEmbedding

func (s *AIService) GenerateEmbedding(ctx context.Context, text string) ([]float32, error)

GenerateEmbedding generates vector embeddings for text content using AWS Bedrock

func (*AIService) GetAnalysis

func (s *AIService) GetAnalysis(_ context.Context, _ string) (*AIAnalysis, error)

GetAnalysis is deprecated - use the service layer for retrieval

func (*AIService) GetAnalysisRequest

func (s *AIService) GetAnalysisRequest(_ context.Context, requestID string) (*AnalysisRequest, error)

GetAnalysisRequest retrieves the status of an analysis request

func (*AIService) GetAnalysisStats

func (s *AIService) GetAnalysisStats(_ context.Context, _ string) (*AIStats, error)

GetAnalysisStats is deprecated - use the service layer for statistics

func (*AIService) QueueAnalysisRequest

func (s *AIService) QueueAnalysisRequest(ctx context.Context, objectID string, objectType string, forceAnalysis bool) (string, error)

QueueAnalysisRequest queues an analysis request for async processing

type AIStats

type AIStats struct {
	Period            string         `json:"period"`
	TotalAnalyses     int            `json:"total_analyses"`
	ToxicContent      int            `json:"toxic_content"`
	SpamDetected      int            `json:"spam_detected"`
	AIGenerated       int            `json:"ai_generated"`
	NSFWContent       int            `json:"nsfw_content"`
	PIIDetected       int            `json:"pii_detected"`
	ToxicityRate      float64        `json:"toxicity_rate"`
	SpamRate          float64        `json:"spam_rate"`
	AIContentRate     float64        `json:"ai_content_rate"`
	NSFWRate          float64        `json:"nsfw_rate"`
	ModerationActions map[string]int `json:"moderation_actions"`
}

AIStats represents aggregated AI analysis statistics

type AnalysisRequest

type AnalysisRequest struct {
	ID            string     `json:"id" dynamodbav:"ID"`
	ObjectID      string     `json:"object_id" dynamodbav:"ObjectID"`
	ObjectType    string     `json:"object_type" dynamodbav:"ObjectType"`
	ForceAnalysis bool       `json:"force_analysis" dynamodbav:"ForceAnalysis"`
	RequestedAt   time.Time  `json:"requested_at" dynamodbav:"RequestedAt"`
	Status        string     `json:"status" dynamodbav:"Status"`
	CompletedAt   *time.Time `json:"completed_at,omitempty" dynamodbav:"CompletedAt,omitempty"`
	Error         *string    `json:"error,omitempty" dynamodbav:"Error,omitempty"`
	ResultID      *string    `json:"result_id,omitempty" dynamodbav:"ResultID,omitempty"`
}

AnalysisRequest represents a queued AI analysis request

type BedrockClient

type BedrockClient struct {
	// contains filtered or unexported fields
}

BedrockClient provides AI analysis using AWS Bedrock

func NewBedrockClient

func NewBedrockClient(ctx context.Context, logger *zap.Logger) (*BedrockClient, error)

NewBedrockClient creates a new AWS Bedrock client for AI analysis

func (*BedrockClient) AnalyzeReputation

AnalyzeReputation performs AI-powered reputation analysis using AWS Bedrock

type BedrockInvokeRequest

type BedrockInvokeRequest struct {
	Prompt      string  `json:"prompt"`
	MaxTokens   int     `json:"max_tokens"`
	Temperature float64 `json:"temperature"`
	TopP        float64 `json:"top_p"`
}

BedrockInvokeRequest represents the structure for Bedrock API calls

type BedrockInvokeResponse

type BedrockInvokeResponse struct {
	Completion string `json:"completion"`
	StopReason string `json:"stop_reason"`
}

BedrockInvokeResponse represents the response from Bedrock

type BedrockRuntimeClient

type BedrockRuntimeClient interface {
	InvokeModel(ctx context.Context, params *bedrockruntime.InvokeModelInput, optFns ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error)
}

BedrockRuntimeClient defines the interface for AWS Bedrock operations needed by AIService

type Celebrity

type Celebrity struct {
	Name       string   `json:"name" dynamodbav:"Name"`
	Confidence float64  `json:"confidence" dynamodbav:"Confidence"`
	URLs       []string `json:"urls,omitempty" dynamodbav:"URLs,omitempty"`
}

Celebrity represents a detected celebrity face in an image

type ComprehendClient

type ComprehendClient interface {
	DetectDominantLanguage(ctx context.Context, params *comprehend.DetectDominantLanguageInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectDominantLanguageOutput, error)
	DetectSentiment(ctx context.Context, params *comprehend.DetectSentimentInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectSentimentOutput, error)
	ClassifyDocument(ctx context.Context, params *comprehend.ClassifyDocumentInput, optFns ...func(*comprehend.Options)) (*comprehend.ClassifyDocumentOutput, error)
	DetectPiiEntities(ctx context.Context, params *comprehend.DetectPiiEntitiesInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectPiiEntitiesOutput, error)
	DetectEntities(ctx context.Context, params *comprehend.DetectEntitiesInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectEntitiesOutput, error)
	DetectKeyPhrases(ctx context.Context, params *comprehend.DetectKeyPhrasesInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectKeyPhrasesOutput, error)
}

ComprehendClient defines the interface for AWS Comprehend operations needed by AIService

type Content

type Content struct {
	ID           string     `json:"id"`
	Type         string     `json:"type"`
	Text         string     `json:"text"`
	MediaURLs    []string   `json:"media_urls"`
	AuthorID     string     `json:"author_id"`
	CreatedAt    time.Time  `json:"created_at"`
	LastAnalyzed *time.Time `json:"last_analyzed,omitempty"`
}

Content represents content to be analyzed

type ContentCategory

type ContentCategory struct {
	Name  string  `json:"name" dynamodbav:"Name"`
	Score float64 `json:"score" dynamodbav:"Score"`
}

ContentCategory represents a content classification category

type Entity

type Entity struct {
	Type  string  `json:"type" dynamodbav:"Type"`
	Text  string  `json:"text" dynamodbav:"Text"`
	Score float64 `json:"score" dynamodbav:"Score"`
}

Entity represents a named entity detected in text

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient defines the interface for HTTP operations needed by AIService

type ImageAnalysis

type ImageAnalysis struct {
	// Moderation
	ModerationLabels []ModerationLabel `json:"moderation_labels" dynamodbav:"ModerationLabels"`
	IsNSFW           bool              `json:"is_nsfw" dynamodbav:"IsNSFW"`
	NSFWConfidence   float64           `json:"nsfw_confidence" dynamodbav:"NSFWConfidence"`

	// Violence Detection
	ViolenceScore   float64 `json:"violence_score" dynamodbav:"ViolenceScore"`
	WeaponsDetected bool    `json:"weapons_detected" dynamodbav:"WeaponsDetected"`

	// Text in Images
	DetectedText []string `json:"detected_text" dynamodbav:"DetectedText"`
	TextToxicity float64  `json:"text_toxicity" dynamodbav:"TextToxicity"`

	// Other Detection
	CelebrityFaces []Celebrity `json:"celebrity_faces,omitempty" dynamodbav:"CelebrityFaces,omitempty"`
	Logos          []Logo      `json:"logos,omitempty" dynamodbav:"Logos,omitempty"`

	// Synthetic Media Detection
	DeepfakeScore float64 `json:"deepfake_score" dynamodbav:"DeepfakeScore"`
}

ImageAnalysis from AWS Rekognition

type Logo struct {
	Name       string  `json:"name" dynamodbav:"Name"`
	Confidence float64 `json:"confidence" dynamodbav:"Confidence"`
}

Logo represents a detected logo in an image

type ModerationLabel

type ModerationLabel struct {
	Name       string  `json:"name" dynamodbav:"Name"`
	Confidence float64 `json:"confidence" dynamodbav:"Confidence"`
	ParentName string  `json:"parent_name,omitempty" dynamodbav:"ParentName,omitempty"`
}

ModerationLabel represents a content moderation label from image analysis

type PIIEntity

type PIIEntity struct {
	Type        string  `json:"type" dynamodbav:"Type"`
	Text        string  `json:"text" dynamodbav:"Text"`
	Score       float64 `json:"score" dynamodbav:"Score"`
	BeginOffset int     `json:"begin_offset" dynamodbav:"BeginOffset"`
	EndOffset   int     `json:"end_offset" dynamodbav:"EndOffset"`
}

PIIEntity represents personally identifiable information detected in content

type RekognitionClient

type RekognitionClient interface {
	DetectModerationLabels(ctx context.Context, params *rekognition.DetectModerationLabelsInput, optFns ...func(*rekognition.Options)) (*rekognition.DetectModerationLabelsOutput, error)
	DetectText(ctx context.Context, params *rekognition.DetectTextInput, optFns ...func(*rekognition.Options)) (*rekognition.DetectTextOutput, error)
	RecognizeCelebrities(ctx context.Context, params *rekognition.RecognizeCelebritiesInput, optFns ...func(*rekognition.Options)) (*rekognition.RecognizeCelebritiesOutput, error)
}

RekognitionClient defines the interface for AWS Rekognition operations needed by AIService

type ReputationAnalysisRequest

type ReputationAnalysisRequest struct {
	Content           string   `json:"content"`
	Sources           []string `json:"sources"`
	ComplexityFactors []string `json:"complexity_factors"`
	AuthorMetadata    struct {
		AccountAge     int     `json:"account_age_days"`
		FollowerCount  int     `json:"follower_count"`
		PostHistory    int     `json:"post_count"`
		EngagementRate float64 `json:"engagement_rate"`
	} `json:"author_metadata"`
}

ReputationAnalysisRequest represents the input for reputation analysis

type ReputationAnalysisResponse

type ReputationAnalysisResponse struct {
	ReputationScore   float64 `json:"reputation_score"`
	ConfidenceLevel   float64 `json:"confidence_level"`
	QualityIndicators struct {
		SourceCredibility float64 `json:"source_credibility"`
		ContentCoherence  float64 `json:"content_coherence"`
		FactualAccuracy   float64 `json:"factual_accuracy"`
		LanguageQuality   float64 `json:"language_quality"`
	} `json:"quality_indicators"`
	RiskFactors []string `json:"risk_factors"`
	Reasoning   string   `json:"reasoning"`
}

ReputationAnalysisResponse represents the AI analysis result

type S3Client

type S3Client interface {
	PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
}

S3Client defines the interface for AWS S3 operations needed by AIService

type SQSClient

type SQSClient interface {
	SendMessage(ctx context.Context, params *sqs.SendMessageInput, optFns ...func(*sqs.Options)) (*sqs.SendMessageOutput, error)
}

SQSClient defines the interface for SQS operations needed by AIService

type SpamAnalysis

type SpamAnalysis struct {
	SpamScore      float64         `json:"spam_score" dynamodbav:"SpamScore"`
	SpamIndicators []SpamIndicator `json:"spam_indicators" dynamodbav:"SpamIndicators"`

	// Behavioral Analysis
	PostingVelocity float64 `json:"posting_velocity" dynamodbav:"PostingVelocity"`
	RepetitionScore float64 `json:"repetition_score" dynamodbav:"RepetitionScore"`
	LinkDensity     float64 `json:"link_density" dynamodbav:"LinkDensity"`

	// Network Analysis
	FollowerRatio   float64 `json:"follower_ratio" dynamodbav:"FollowerRatio"`
	InteractionRate float64 `json:"interaction_rate" dynamodbav:"InteractionRate"`
	AccountAge      int     `json:"account_age_days" dynamodbav:"AccountAgeDays"`
}

SpamAnalysis using custom heuristics

type SpamIndicator

type SpamIndicator struct {
	Type        string  `json:"type" dynamodbav:"Type"`
	Description string  `json:"description" dynamodbav:"Description"`
	Severity    float64 `json:"severity" dynamodbav:"Severity"`
}

SpamIndicator represents an indicator of spam content

type TextAnalysis

type TextAnalysis struct {
	// Sentiment
	Sentiment       string             `json:"sentiment" dynamodbav:"Sentiment"`
	SentimentScores map[string]float64 `json:"sentiment_scores" dynamodbav:"SentimentScores"`

	// Toxicity (using Comprehend custom classifier)
	ToxicityScore  float64  `json:"toxicity_score" dynamodbav:"ToxicityScore"`
	ToxicityLabels []string `json:"toxicity_labels" dynamodbav:"ToxicityLabels"`

	// Content Classification
	Categories []ContentCategory `json:"categories" dynamodbav:"Categories"`

	// PII Detection
	ContainsPII bool        `json:"contains_pii" dynamodbav:"ContainsPII"`
	PIIEntities []PIIEntity `json:"pii_entities,omitempty" dynamodbav:"PIIEntities,omitempty"`

	// Language
	DominantLanguage string             `json:"dominant_language" dynamodbav:"DominantLanguage"`
	LanguageScores   map[string]float64 `json:"language_scores" dynamodbav:"LanguageScores"`

	// Entities & Key Phrases
	Entities   []Entity `json:"entities" dynamodbav:"Entities"`
	KeyPhrases []string `json:"key_phrases" dynamodbav:"KeyPhrases"`
}

TextAnalysis from AWS Comprehend

type ThresholdSet

type ThresholdSet struct {
	AutoRemove float64 // Very high confidence required
	AutoHide   float64 // High confidence
	Flag       float64 // Moderate confidence
	WarnAuthor float64 // Low confidence
}

ThresholdSet defines action thresholds for different confidence levels

func GetThresholds

func GetThresholds(contentType string) ThresholdSet

GetThresholds returns the appropriate threshold set for a content type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL