PHP code example of timkley / denk

1. Go to this page and download the library: Download timkley/denk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

timkley / denk example snippets


use Denk\Facades\Denk;

$text = Denk::text()->prompt('Once upon a time')->generate();

use Denk\DenkService;
use OpenAI;

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

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;
}

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

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();

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

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
bash
php artisan vendor:publish --tag=config --provider="Denk\DenkServiceProvider"