How to Use the AI Connector in Custom Extensions

Available PHP Services

The Plumrocket AI Connector extension exposes two public PHP services that can be used in any Magento 2 module to interact with AI integrations:

  • \Plumrocket\AiConnector\Api\IntegrationProviderInterface – retrieves information about available AI integrations
  • \Plumrocket\AiConnector\Api\PromptProcessorInterface – sends a prompt to an AI integration and returns the generated text

Using the \Plumrocket\AiConnector\Api\IntegrationProviderInterface Service

This service allows you to retrieve all registered AI integrations or fetch a specific one by its code. Each returned object implements \Plumrocket\AiConnector\Api\Data\IntegrationInfoInterface and exposes the following methods:

MethodReturn typeDescription
getCode()stringUnique integration identifier (e.g. OpenAI, Anthropic, Google)
getLabel()stringHuman-readable name (e.g. OpenAI (GPT))
isEnabled()boolWhether the integration is enabled in the Admin Configuration
getSupportedModels()string[]List of available model codes for this integrationList of available model codes for this integration
getDefaultModel()string|nullDefault model configured in Admin, or null if not set

Example 1. Get all registered integrations and check their status

use Plumrocket\AiConnector\Api\IntegrationProviderInterface;

class YourClass
{
    private $integrationProvider;

    public function __construct(
        IntegrationProviderInterface $integrationProvider
    ) {
        $this->integrationProvider = $integrationProvider;
    }

    public function listIntegrations(): void
    {
        $integrations = $this->integrationProvider->getAllIntegrations();

        foreach ($integrations as $integration) {
            if ($integration->isEnabled()) {
                $models = $integration->getSupportedModels();
                $default = $integration->getDefaultModel();
                // use $models and $default as needed
            }
        }
    }
}

Example 2. Get a specific integration by code

use Magento\Framework\Exception\LocalizedException;
use Plumrocket\AiConnector\Api\IntegrationProviderInterface;

class YourClass
{
    private $integrationProvider;

    public function __construct(
        IntegrationProviderInterface $integrationProvider
    ) {
        $this->integrationProvider = $integrationProvider;
    }

    public function getOpenAiModels(): array
    {
        $integration = $this->integrationProvider->getIntegration('openai');

         if ($integration->isEnabled()) {
            return $integration->getSupportedModels();
         }
        
        return [];
    }
}

Using the \Plumrocket\AiConnector\Api\PromptProcessorInterface Service

This service processes a text prompt through the specified AI integration and returns the generated response as a plain string. It validates that both the module and the selected integration are enabled before making the API call, and logs the request result automatically.

Method signature:

public function processPrompt(string $integrationCode, string $prompt, ?string $model = null): string;

Parameters:

ParameterTypeRequiredDescription
$integrationCodestringYesIntegration to use: openai, anthropic, or google
$promptstringYesThe text prompt to send to the AI
$modelstring|nulNoSpecific model code to use. If null, uses the default model from Admin configuration

Example 1. Generate content using the default configured model

use Magento\Framework\Exception\LocalizedException;
use Plumrocket\AiConnector\Api\PromptProcessorInterface;

class YourClass
{
    private $promptProcessor;

    public function __construct(
        PromptProcessorInterface $promptProcessor
    ) {
        $this->promptProcessor = $promptProcessor;
    }

    public function generateProductDescription(string $productName): string
    {
        return $this->promptProcessor->processPrompt(
            'openai',
            'Write a short product description for: ' . $productName
        );
    }
}

Example 2. Generate content using a specific model

use Magento\Framework\Exception\LocalizedException;
use Plumrocket\AiConnector\Api\PromptProcessorInterface;

class YourClass
{
    private $promptProcessor;

    public function __construct(
        PromptProcessorInterface $promptProcessor
    ) {
        $this->promptProcessor = $promptProcessor;
    }

    public function generateSeoMetaDescription(string $pageContent): string
    {
        try {
            return $this->promptProcessor->processPrompt(
                'anthropic',
                'Write an SEO meta description (max 160 characters) for: ' . $pageContent,
                'claude-sonnet-4-5'
            );
        } catch (LocalizedException $e) {
            return '';
        }
    }
}
Was this article helpful?