Drivers

The OpenAI Driver

Introduction

The OpenAI driver provides a simple way to access OpenAI APIs from within Kipchak.

It exposes a configured OpenAI client so you can create responses, generate embeddings, and run other OpenAI operations without wiring credentials in every call.

Installation

Install the driver via composer by running:

composer require kipchak/driver-openai

Initialise the Driver

Add the following line to your drivers/drivers.php file:

\Mamluk\Kipchak\Driver\OpenAI\OpenAI::iniitalise($container);

Configuration

The configuration in kipchak.openai.php can be used to configure one or more OpenAI connections:

<?php

use function Kipchak\Core\env;

return [
    'connections' => [
        'default' => [
            'api_key' => env('OPENAI_API_KEY', ''),
            'organization' => env('OPENAI_ORG', ''),
            'project' => env('OPENAI_PROJECT', ''),
            'base_uri' => env('OPENAI_BASE_URI', 'https://api.openai.com/v1'),
            'default_model' => env('OPENAI_DEFAULT_MODEL', 'gpt-4o-mini'),
            // Optional: add extra headers to every request
            // 'headers' => [
            //     'OpenAI-Beta' => 'responses=v1',
            // ],
            // Optional: customize the client factory or return a custom client
            // 'client_builder' => function (\OpenAI\Factory $factory, array $connection) {
            //     return $factory->withHttpHeader('X-App', 'kipchak')->make();
            // },
        ],
        'analysis' => [
            'api_key' => env('OPENAI_API_KEY', ''),
            'organization' => env('OPENAI_ORG', ''),
            'project' => env('OPENAI_PROJECT', ''),
            'base_uri' => env('OPENAI_BASE_URI', 'https://api.openai.com/v1'),
            'default_model' => env('OPENAI_ANALYSIS_MODEL', 'gpt-4o-mini'),
        ],
    ],
];

Usage

use Kipchak\Driver\OpenAI\OpenAI;

$client = OpenAI::get('default');

$response = $client->chat()->create([
    'messages' => [
        ['role' => 'system', 'content' => 'You are a concise assistant.'],
        ['role' => 'user', 'content' => 'Summarize Kipchak in one sentence.'],
    ],
]);

$text = $response->choices[0]->message->content;

Multiple connections are supported:

use Kipchak\Driver\OpenAI\OpenAI;

$default = OpenAI::get('default');
$analysis = OpenAI::get('analysis');

$response = $analysis->responses()->create([
    'input' => 'Explain caching strategies in one paragraph.',
]);

The driver wraps the OpenAI PHP client. If a default_model is configured, it is applied automatically to resources that accept a model parameter (chat, responses, embeddings, images, audio, and more). You can also pass headers for custom request headers or a client_builder callable to customize the factory/client per connection.

For more details on the client methods and response shapes, see the OpenAI PHP client documentation.

Git Repository

The source code for this driver is available on 1x.ax at https://1x.ax/mamluk/kipchak/drivers/openai.

Previous
Object Storage (S3)