PHP code example of distvan / dialclient

1. Go to this page and download the library: Download distvan/dialclient 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/ */

    

distvan / dialclient example snippets




use DialClient\Dial\DialChatCompletions;
use DialClient\Http\GuzzleClient;

$client = new GuzzleClient(
    baseUri: 'https://your-dial-host.example',
    apiKey: null, // (optional) sets Authorization: Bearer ...
);

$chat = new DialChatCompletions(
    client: $client,
    deploymentName: 'your-deployment',
    apiKeyHeaderValue: null, // (optional) sets `api-key: ...`
);

// Alternatively, omit deploymentName and pass it in the payload as `deployment_name` or `deployment`.

$message = $chat->create([
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $message->content;

$promise = $chat->createAsync(['model' => 'your-model', 'messages' => []]);
$promise->then(function (array $result): void {
    // handle result
});

foreach ($chat->streamText(['messages' => []]) as $delta) {
    echo $delta;
}

foreach ($chat->streamChunks(['messages' => []]) as $chunk) {
    // $chunk is the decoded JSON array for one SSE event
}

$message = $chat->stream(['messages' => []]);
echo $message->content;
bash
source ./scripts/dial-env.sh
php ./app.php
powershell
. .\scripts\dial-env.ps1
php .\app.php
powershell
composer install
. .\scripts\dial-env.ps1
php .\app.php

# With an explicit system prompt:
php .\app.php "You are a helpful assistant."

# Help:
php .\app.php --help
bash
composer install
source ./scripts/dial-env.sh
php ./app.php

# With an explicit system prompt:
php ./app.php "You are a helpful assistant."

# Help:
php ./app.php --help