Skip to main content
Use these extension points:
  1. Register a custom inference driver
  2. Register a custom embeddings driver
  3. Inject a custom HTTP client / middleware

Register a Custom Inference Driver

Register by class-string:
<?php
use Cognesy\Polyglot\Inference\Inference;
use Acme\Polyglot\Drivers\AcmeInferenceDriver;

Inference::registerDriver('acme', AcmeInferenceDriver::class);
// @doctest id="6695"
Or register by factory callback:
<?php
use Cognesy\Http\HttpClient;
use Cognesy\Polyglot\Inference\Config\LLMConfig;
use Cognesy\Polyglot\Inference\Contracts\CanProcessInferenceRequest;
use Cognesy\Polyglot\Inference\Drivers\OpenAI\OpenAIDriver;
use Cognesy\Polyglot\Inference\Inference;
use Psr\EventDispatcher\EventDispatcherInterface;

Inference::registerDriver(
    'openai-custom',
    function (
        LLMConfig $config,
        HttpClient $httpClient,
        EventDispatcherInterface $events
    ): CanProcessInferenceRequest {
        return new OpenAIDriver($config, $httpClient, $events);
    }
);
// @doctest id="8889"
Use it by setting driver in LLMConfig or by preset config. Cleanup helpers (important in tests/workers):
Inference::unregisterDriver('openai-custom');
Inference::resetDrivers();
// @doctest id="c86b"

Register a Custom Embeddings Driver

<?php
use Cognesy\Polyglot\Embeddings\Embeddings;
use Acme\Polyglot\Drivers\AcmeEmbeddingsDriver;

Embeddings::registerDriver('acme-embed', AcmeEmbeddingsDriver::class);
// @doctest id="cd5f"
Embeddings driver constructors are expected to follow factory wiring:
  • (EmbeddingsConfig $config, HttpClient $httpClient, EventDispatcherInterface $events)

Implementing Driver Contracts

Inference drivers implement:
  • CanProcessInferenceRequest
  • typically by extending BaseInferenceRequestDriver
Embeddings drivers implement:
  • CanHandleVectorization
  • typically by extending BaseEmbedDriver

Inject Custom HTTP Middleware

Polyglot uses Cognesy\Http\HttpClient. Add middleware there, then inject into runtime.
<?php
use Cognesy\Http\Creation\HttpClientBuilder;
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Polyglot\Inference\InferenceRuntime;

$httpClient = (new HttpClientBuilder())
    ->withPreset('guzzle')
    ->create()
    ->withMiddleware(new YourCustomMiddleware());

$inference = Inference::fromRuntime(
    InferenceRuntime::using(
        preset: 'openai',
        httpClient: $httpClient,
    )
);
// @doctest id="8181"
HttpClient is immutable. Always keep the returned instance from withMiddleware(...).