定制 freema/perspective-api-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

freema/perspective-api-bundle

Composer 安装命令:

composer require freema/perspective-api-bundle

包简介

Symfony bundle for Google Perspective API - AI-powered content moderation with flexible threshold management, caching support, and multiple usage patterns. Modern alternative to abandoned wrappers.

README 文档

README

Latest Stable Version License PHP Version Require Symfony Version

A powerful Symfony bundle that integrates Google's Perspective API for content moderation and toxicity detection in your applications. Built with flexibility in mind, it supports multiple threshold management strategies, caching, and can be used as a standalone service or Symfony validator.

🎯 Key Features

  • 🔧 Flexible Threshold Management - Static config, database-driven, or runtime dynamic thresholds
  • 💾 Built-in Caching Support - Reduce API calls and improve performance
  • 🎨 Multiple Usage Patterns - Use as a service, validator constraint, or command-line tool
  • 🌍 Multi-language Support - Analyze content in multiple languages with automatic detection
  • 📊 Rich Analysis Results - Detailed scoring across multiple toxicity dimensions
  • ⚡ Production Ready - Battle-tested with proper error handling and logging
  • 🔄 Easy Migration - Simple migration path from deprecated libraries like bredmor/comment-analyzer

🚀 Why This Bundle?

This bundle was created as a modern, maintained alternative to abandoned Perspective API wrappers. It provides a clean, Symfony-native integration that follows best practices and offers the flexibility needed for real-world applications.

Unlike simple API wrappers, this bundle understands that different parts of your application might need different moderation thresholds - a children's section needs stricter controls than a general discussion forum. It allows you to implement context-aware content moderation without sacrificing simplicity.

⚠️ Important Note About Google's Gemini LLM Integration

Google has integrated their Gemini LLM into the Perspective API, which represents a shift from traditional deterministic models to AI-powered analysis. While the API remains fully functional and is actively supported by Google, please be aware that:

  • Consistency: The same text might receive slightly different scores when analyzed at different times
  • Predictability: Results may be less deterministic compared to traditional machine learning models
  • Recommendation: Consider implementing caching to ensure consistent moderation decisions for the same content

This change reflects Google's commitment to improving the API with cutting-edge AI technology, but it's important to understand these characteristics when implementing content moderation.

📋 Requirements

  • PHP 8.1 or higher
  • Symfony 5.4, 6.x, or 7.x
  • Google Perspective API key (Get one here)

🔧 Advanced Configuration

HTTP Client Options

The bundle supports custom HTTP client configuration for environments that require proxy servers or specific timeouts:

# config/packages/perspective_api.yaml
perspective_api:
    api_key: '%env(PERSPECTIVE_API_KEY)%'
    
    # Configure HTTP client options
    http_client_options:
        # Use proxy server
        proxy: '%env(HTTP_PROXY)%'
        
        # Request timeout in seconds
        timeout: 30
        
        # Maximum number of redirects
        max_redirects: 5
        
        # Custom headers
        headers:
            'User-Agent': 'MyApp/1.0'
        
        # Any other Symfony HTTP client option
        verify_peer: true
        verify_host: true

This is particularly useful for:

  • Corporate environments with proxy requirements
  • Applications with specific network configurations
  • Custom timeout requirements for slow networks
  • Adding custom headers for monitoring or tracking

🛠️ Use Cases

This bundle is perfect for:

  • Comment Systems - Automatically moderate user comments before publication
  • Forums & Communities - Maintain healthy discussion environments
  • Social Platforms - Filter toxic content in real-time
  • Content Management - Review and flag potentially problematic content
  • Customer Support - Filter inappropriate messages in support tickets
  • Educational Platforms - Ensure safe learning environments

📦 Installation

Install the bundle using Composer:

composer require freema/perspective-api-bundle

⚙️ Configuration

Basic Configuration

# config/packages/perspective_api.yaml
perspective_api:
    api_key: '%env(PERSPECTIVE_API_KEY)%'
    
    # Default thresholds for all attributes
    thresholds:
        TOXICITY: 0.5
        SEVERE_TOXICITY: 0.3
        IDENTITY_ATTACK: 0.5
        INSULT: 0.5
        PROFANITY: 0.5
        THREAT: 0.5
    
    # Which attributes to analyze
    analyze_attributes:
        - TOXICITY
        - SEVERE_TOXICITY
        - IDENTITY_ATTACK
        - INSULT
        - PROFANITY
        - THREAT
    
    # Default language for analysis
    default_language: 'en'
    
    # Allow runtime threshold override
    allow_runtime_override: true
    
    # Optional: Custom threshold provider service
    # threshold_provider: 'my.custom.threshold.provider'
    
    # Optional: HTTP client options (proxy, timeout, etc.)
    # http_client_options:
    #     proxy: 'http://proxy.example.com:8080'
    #     timeout: 30
    #     max_redirects: 5

Environment Variables

# .env.local
PERSPECTIVE_API_KEY=your_api_key_here

# Optional: HTTP proxy configuration
HTTP_PROXY=http://proxy.example.com:8080
HTTPS_PROXY=http://proxy.example.com:8080

📖 Usage

Basic Service Usage

use Freema\PerspectiveApiBundle\Service\PerspectiveApiService;

class CommentController
{
    public function __construct(
        private readonly PerspectiveApiService $perspectiveApi
    ) {}

    public function submitComment(Request $request): Response
    {
        $comment = $request->get('comment');
        
        // Analyze the comment
        $result = $this->perspectiveApi->analyzeText($comment);
        
        // Check if content is safe
        if (!$result->isSafe()) {
            // Get specific violations
            $violations = $result->getViolations();
            
            return $this->json([
                'error' => 'Your comment contains inappropriate content',
                'violations' => $violations
            ], 400);
        }
        
        // Save the comment...
    }
}

Using as a Validator

use Freema\PerspectiveApiBundle\Validator\PerspectiveContent;
use Symfony\Component\Validator\Constraints as Assert;

class Comment
{
    #[Assert\NotBlank]
    #[PerspectiveContent(
        thresholds: [
            'TOXICITY' => 0.5,
            'PROFANITY' => 0.3
        ],
        message: 'Your comment contains inappropriate content.'
    )]
    private string $content;
}

Using in Symfony Forms

The bundle provides a PerspectiveTextType for easy integration into Symfony forms:

use Freema\PerspectiveApiBundle\Form\Type\PerspectiveTextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class CommentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('author', TextType::class)
            ->add('content', PerspectiveTextType::class, [
                'perspective_thresholds' => [
                    'TOXICITY' => 0.7,
                    'PROFANITY' => 0.5,
                ],
                'perspective_language' => 'en',
                'perspective_message' => 'Please keep your comments respectful.',
                'attr' => [
                    'rows' => 5,
                    'placeholder' => 'Write your comment...'
                ]
            ])
            ->add('submit', SubmitType::class);
    }
}

Form Type Options

  • perspective_validation (bool, default: true) - Enable/disable Perspective validation
  • perspective_thresholds (array, default: []) - Custom thresholds for validation
  • perspective_language (string|null, default: null) - Language for analysis
  • perspective_message (string|null, default: null) - Custom validation error message
// Disable validation for specific form
$builder->add('content', PerspectiveTextType::class, [
    'perspective_validation' => false
]);

// Use form with custom validation message
$builder->add('message', PerspectiveTextType::class, [
    'perspective_thresholds' => ['TOXICITY' => 0.8],
    'perspective_message' => 'This content violates our community guidelines.'
]);

Custom Threshold Provider

Implement dynamic thresholds based on context:

use Freema\PerspectiveApiBundle\Contract\ThresholdProviderInterface;

class ContextualThresholdProvider implements ThresholdProviderInterface
{
    public function getThresholds(): array
    {
        // Return different thresholds based on current context
        if ($this->isChildrenSection()) {
            return [
                'TOXICITY' => 0.3,
                'PROFANITY' => 0.1,
                'THREAT' => 0.1,
            ];
        }
        
        return [
            'TOXICITY' => 0.7,
            'PROFANITY' => 0.5,
            'THREAT' => 0.5,
        ];
    }
}

Using Threshold Resolver

// Set a custom resolver function
$perspectiveApi->setThresholdResolver(function (string $attribute, array $context) {
    // Custom logic based on attribute and context
    if ($attribute === 'PROFANITY' && $context['strict_mode']) {
        return 0.1;
    }
    return null; // Use default
});

// Use with context
$result = $perspectiveApi->analyzeText(
    $text,
    null,
    null,
    ['strict_mode' => true]
);

Batch Analysis

$texts = [
    'This is a normal comment',
    'This might be problematic',
    'Another text to analyze'
];

$results = $this->perspectiveApi->analyzeBatch($texts);

foreach ($results as $index => $result) {
    echo "Text {$index}: " . ($result->isSafe() ? 'Safe' : 'Unsafe') . PHP_EOL;
}

Analyzing with Specific Attributes

// Analyze only specific attributes
$result = $this->perspectiveApi->analyzeWithAttributes(
    $text,
    ['TOXICITY', 'THREAT'],
    'en'
);

🧪 Development

For development setup, testing, and contribution guidelines, see:

🔄 Migration from bredmor/comment-analyzer

If you're migrating from the deprecated bredmor/comment-analyzer library:

// Old way with bredmor/comment-analyzer
$analyzer = new CommentAnalyzer($apiKey);
$response = $analyzer->analyze($text);
$toxicity = $response->toxicity();

// New way with this bundle
$result = $perspectiveApi->analyzeText($text);
$toxicity = $result->getScore('TOXICITY');
$isSafe = $result->isSafe();

📊 Available Attributes

  • TOXICITY - A rude, disrespectful, or unreasonable comment
  • SEVERE_TOXICITY - A very hateful, aggressive, disrespectful comment
  • IDENTITY_ATTACK - Negative or hateful comments targeting identity
  • INSULT - Insulting, inflammatory, or negative comment
  • PROFANITY - Swear words, curse words, or profane language
  • THREAT - Describes an intention to inflict pain or violence

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This bundle is released under the MIT License. See the bundled LICENSE file for details.

🙏 Credits

  • Created and maintained by Freema
  • Inspired by the need for a modern, maintained Perspective API integration
  • Thanks to all contributors

📚 Resources

freema/perspective-api-bundle 适用场景与选型建议

freema/perspective-api-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 165 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「google」 「moderation」 「ai」 「content-moderation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 freema/perspective-api-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 freema/perspective-api-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 freema/perspective-api-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 165
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-08