Search by

openeuropa / gpt-at-ec-php-client

DIGIT-CORE

PHP API client to interact with European Commission GPT@EC.

Package info

github.com/openeuropa/gpt-at-ec-php-client

pkg:composer/openeuropa/gpt-at-ec-php-client

Statistics

Installs: 504

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0-beta3 2026-09-11 11:03 UTC

This package is auto-updated.

Last update: 2026-09-11 11:05:08 UTC


README

This is an API client written in PHP to interact with the GPT@EC API.

Important notes

The GPT@EC API is very similar to the Open AI API.
Therefore, in order to ease the development, this library reuses the openai-client/php where possible, or extends it if needed.
Since some parts of the openai-client/php are marked as internal, this library might become incompatible with future versions of it. Consider locking the openai-client/php version, and update it to the latest version reported as compatible in the test builds.

Install

Use Composer to require the library:

composer require openeuropa/gpt-at-ec-php-client

Make sure you have a PSR-18 client installed, or install one manually, e.g.:

composer require guzzlehttp/guzzle

The php-http/discovery plugin can also help installing and discovering a compatible PSR-18 client.

Usage

You can create a new API client using the factory class. All you need is a GPT@EC API key:

$api_key = getenv('KEY_AI_GPT_AT_EC');
$factory = new \Openeuropa\GptAtEcPhpClient\Factory();
$client = $factory->withApiKey($key)->make();

Now the client can be used to interact with the 4 available endpoints.

Chat

The Chat Completions API endpoint will generate a model response from a list of messages comprising a conversation.

Not all optional parameters are available for every model: GPT@EC applies a whitelist of forwarded parameters per model family, and parameters not in the whitelist are silently ignored. In particular response_format is not forwarded for any model, so structured output (JSON schema) is not available through this endpoint. Use the Responses endpoint instead, which does not apply the whitelist.

create method

Create a chat completion. The full model response will be returned at once by the API.

$response = $client->chat()->create([
    'model' => 'gpt-4o',
    'messages' => [
    [
        'role' => 'system',
        'content' => 'You are a helpful assistant.',
    ],
    [
        'role' => 'user',
        'content' => 'Hello!',
    ],
]);

echo $response->id; // "chatcmpl-0123456789abcdef0123456789ab"
echo $result->object; // "chat.completion"
echo $result->created; // 1755523764
echo $result->model; // "gpt-4o-2000-01-01"

echo $response->choices[0]->message->role; // "assistant"
echo $response->choices[0]->message->content; // "Hello! How can I assist you today?"
echo $response->choices[0]->index; // 0
echo $response->choices[0]->finishReason; // "stop"

echo $response->usage->promptTokens; // 9
echo $response->usage->completionTokens; // 10
echo $response->usage->totalTokens; // 19

createStreamed method

Create a chat completion. The response will be streamed back in parts as the model generates it.

$stream = $client->chat()->createStreamed([
    'model' => 'gpt-4o',
    'messages' => [
    [
        'role' => 'system',
        'content' => 'You are a helpful assistant.',
    ],
    [
        'role' => 'user',
        'content' => 'Hello!',
    ],
]);

foreach ($stream as $chunk) {
    echo $chunk->id; // "chatcmpl-0123456789abcdef0123456789ab"
    echo $chunk->object; // "chat.completion.chunk"
    echo $chunk->created; // 1755530359
    echo $chunk->model; // "meta-llama/Llama-3.3-70B-Instruct"

    echo $response->choices[0]->message->role; // "assistant"
    echo $response->choices[0]->message->content; // "Hello!", "How", "can", "I", "help", "you", "?"
}

Models

Lists the various models available in the API.
A single method is available, list.

$response = $client->models()->list();

foreach ($response->data as $model) {
    echo $model->id; // "llama-3.3-70b-instruct"
    echo $model->name; // "LLama 3.3 70b instruct"
    echo $model->description; // "Very powerful open-weights model on par with the capabilities of GPT-4o for many types of tasks."
    print_r($model->sensitivityLevel); // ["PA", "SNC", "CU"]
    print_r($model->defaultFor); // ["SNC"]
    echo $model->created; // 1744201138356
    echo $model->ownedBy; // "meta"
    echo $model->object; // "model"
}

Quota consumption

Provides the quota consumption details for a specific AI model. A single method is available, retrieve.

$response = $client->quotaConsumption()->retrieve('gpt-4o');

echo $response->consumedPromptTokens; // 1234
echo $response->consumedCompletionTokens; // 5678
echo $response->consumedTotalTokens; // 6912
echo $response->quota; // 100000

Responses

Creates a model response through the Responses API.
Two methods are available, create and createStreamed.

Unlike the Chat endpoint, the Responses endpoint does not apply a parameter whitelist: all parameters from the request schema are forwarded to the model. This makes it the endpoint to use for structured output, by passing a JSON schema as text.format.

Only some models support the Responses API. The API answers 400 Model '...' does not support the Responses API for the others.

create method

Create a model response. The full response will be returned at once by the API.

$response = $client->responses()->create([
    'model' => 'gpt-5.1',
    'input' => [
        [
            'role' => 'user',
            'content' => 'Give me a title and a body for a news item about the weather.',
        ],
    ],
    'text' => [
        'format' => [
            'type' => 'json_schema',
            'name' => 'news_item',
            'strict' => true,
            'schema' => [
                'type' => 'object',
                'properties' => [
                    'title' => ['type' => 'string'],
                    'body' => ['type' => 'string'],
                ],
                'required' => ['title', 'body'],
                'additionalProperties' => false,
            ],
        ],
    ],
]);

echo $response->status; // "completed"
echo $response->outputText; // '{"title":"...","body":"..."}'
echo $response->text->format->name; // "news_item"
echo $response->usage->totalTokens; // 104

createStreamed method

Create a model response. The events will be streamed back as the model generates them.

$stream = $client->responses()->createStreamed([
    'model' => 'gpt-5.1',
    'input' => 'Give me a title and a body for a news item about the weather.',
    'text' => [
        'format' => [
            'type' => 'json_schema',
            'name' => 'news_item',
            'strict' => true,
            'schema' => [
                'type' => 'object',
                'properties' => [
                    'title' => ['type' => 'string'],
                    'body' => ['type' => 'string'],
                ],
                'required' => ['title', 'body'],
                'additionalProperties' => false,
            ],
        ],
    ],
]);

foreach ($stream as $event) {
    echo $event->event; // "response.created", "response.output_text.delta", ..., "response.completed"

    if ($event->event === 'response.output_text.delta') {
        echo $event->response->delta; // "{\"", "title", "\":\"", ...
    }

    if ($event->event === 'response.completed') {
        echo $event->response->response->status; // "completed"
        echo $event->response->response->outputText; // '{"title":"...","body":"..."}'
    }
}

Tests

The library is full covered by unit tests using the PHPUnit framework. To run the tests, execute:

./vendor/bin/phpunit --coverage-html folder-for-coverage-results

Versioning

We use SemVer for versioning.