timkley/denk

OpenAI helpers
1,853 1
Install
composer require timkley/denk
Latest Version:0.5.2
PHP:^8.4|^8.5
Laravel:11 · 12
License:MIT
Last Updated:Jun 19, 2026
Links: GitHub  ·  Packagist
Maintainer: timkley

Denk – a small helper package to work with the OpenAI API

The package name is derived from the German word "denken" which means "to think".

Installation

composer require timkley/denk

How to use

Make sure that you have an OpenAI API key. You can get one here.

Initialization

Using Laravel

The package comes with a service provider that will automatically register the OpenAI client. You can use the Denk facade to access the service.

Make sure to publish the configuration file to set your API key and/or change the API url.

php artisan vendor:publish --tag=config --provider="Denk\DenkServiceProvider"

After that, you can use the Denk facade to access the service like this:

use Denk\Facades\Denk;

$text = Denk::text()->prompt('Once upon a time')->generate();
Manually or non-Laravel projects

To initialize the service, you need create an OpenAI client and pass it to the DenkService.

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

Generating text

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

// using only a simple prompt
$text = $denk->text()->prompt('Once upon a time')->generate();

// Manually provide messages
use Denk\ValueObjects\DeveloperMessage;
use Denk\ValueObjects\UserMessage;
use Denk\ValueObjects\AssistantMessage;

$text = $denk->text()
    ->messages([
        new DeveloperMessage('Write as a pirate'),
        new UserMessage('Once upon a time'),
    ])
    ->generate();

// Generate a streamed response
$stream = $denk->text()
    ->prompt('Write a long story about a pirate')
    ->generateStreamed();

// Iterate over the stream to get chunks of the response as they arrive
foreach ($stream as $response) {
    echo $response->choices[0]->delta->content;
}
Available models
  • gpt-4o-mini (default)
  • gpt-4o
  • gpt-4-turbo
  • gpt-4
  • gpt-3.5-turbo
Temperature

You can control the randomness of the response by setting the temperature:

$text = $denk->text()
    ->prompt('Once upon a time')
    ->temperature(0.5) // Between 0.0 and 2.0, default is 1.0
    ->generate();

Generating JSON

To generate Structured Output, you can use the json() method. Set the properties of the JSON object and provide a prompt, additionally you may also set a system prompt.

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

$json = $denk->json()
    ->properties([
        'title' => [
            'type' => 'string',
            'description' => 'The title of the story',
        ],
        'story' => [
            'type' => 'string',
            'description' => 'The story itself',
        ],
    ])
    ->prompt('Write a store about Chewbacca winning a game against Han Solo')
    ->generate();
Available models
  • gpt-4o-mini (default)
  • gpt-4o
Temperature

You can control the randomness of the response by setting the temperature:

$json = $denk->json()
    ->properties([...])
    ->prompt('...')
    ->temperature(0.5) // Between 0.0 and 2.0, default is 1.0
    ->generate();

Generating Images

To generate images, you can use the image() method. Set the properties of the image and provide a prompt.

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

$image = $denk->image()
    ->prompt('A beautiful sunset over the ocean')
    ->size('square') // or '1024x1024'
    ->quality('standard')
    ->model('dall-e-3') // optional, dall-e-3 is default
    ->generate();

// Returns the URL of the generated image
Available image sizes
  • 1024x1024 (square)
  • 1792x1024 (landscape)
  • 1024x1792 (portrait)

Or use the convenience aliases:

  • square
  • landscape
  • portrait
Available image qualities
  • standard
  • hd
Available image models
  • dall-e-3 (default)
  • dall-e-2

Related Packages

laravel/pint

An opinionated code formatter for PHP.

202,839,253 3,168
tightenco/duster

Automatic configuration for Laravel apps to apply Tighten's standard linting & c...

3,327,636 604
directorytree/ldaprecord-laravel

LDAP Authentication & Management for Laravel.

2,861,582 574
hemp/presenter

Easy Model Presenters in Laravel

649,449 247
juzaweb/cms

Juzaweb CMS is a Content Management System (CMS) developed based on Laravel Fram...

571,275 191

Version History

Version Released PHP Laravel License
0.5.2 ^8.4|^8.5 ^11.34|^12.0 MIT
0.5.1 ^8.3|^8.4 ^11.34|^12.0 MIT
0.5.0 ^8.3|^8.4 ^11.34|^12.0 MIT
0.4.0 ^8.3|^8.4 ^11.34|^12.0 MIT
0.3.1 ^8.3|^8.4 ^11.34|^12.0 MIT