timkley/denk

OpenAI helpers

Maintainers

Details

github.com/timkley/denk

Source

Issues

Installs: 73

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:project

0.0.3 2024-12-02 14:39 UTC

This package is auto-updated.

Last update: 2024-12-02 16:16:00 UTC


README

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.

php artisan vendor:publish --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();

// set a system prompt
$text = $denk->text()
    ->prompt('Once upon a time')
    ->systemPrompt('Write as a pirate')
    ->generate();
    
// Manually provide messages

use Denk\Messages\SystemMessage;
use Denk\Messages\UserMessage;

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

Available models

  • gpt-4o-mini
  • gpt-4o
  • gpt-4-turbo
  • gpt-4
  • gpt-3.5-turbo

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
  • gpt-4o-mini

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')
    ->quality('standard')
    ->generate();

Available image sizes

  • square
  • landscape
  • portrait

Available image qualities

  • standard
  • hd