PHP code example of ldkafka / yii3-ai

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

    

ldkafka / yii3-ai example snippets


use Ldkafka\Ai\Client;
use Ldkafka\Ai\Http\CurlTransport;
use Ldkafka\Ai\Message\Message;
use Ldkafka\Ai\Model\ModelCatalog;
use Ldkafka\Ai\Pipeline\RetryMiddleware;
use Ldkafka\Ai\Provider\Anthropic\AnthropicProvider;
use Ldkafka\Ai\Provider\ProviderRegistry;
use Ldkafka\Ai\Request;

$params = (e::user('Summarise: ...')], model: 'claude-haiku-5-5'));
echo $answer->text(), sprintf(' ($%.5f)', $answer->cost?->total());

final class SupportController
{
    public function __construct(private Chat $chat)
    {
    }

    public function reply(string $ticketId, string $text): string
    {
        return $this->chat->send($ticketId, $text)->text();
    }
}

$runner = new Runner($client, new ToolRegistry([new BookingLookupTool($bookings)]), maxSteps: 6, maxCostUsd: 0.25);
$result = $runner->run(new Request([Message::user('Where is booking 1234?')], system: 'You are support.'));

echo $result->text(), ' ', $result->status->value, ' ', $result->steps;

use Ldkafka\Ai\Mcp\McpHttpClient;
use Ldkafka\Ai\Mcp\RemoteMcpToolSource;

$mcp = new McpHttpClient(
    'https://mcp.example.com/mcp',
    new CurlTransport(timeout: 60.0),
    (string) getenv('MCP_TOKEN'),          // sent as Authorization: Bearer
    userAgent: 'example-watch/1.0',        // set your own
);
$tools = (new RemoteMcpToolSource($mcp, allow: ['prod_check', 'app_logs']))->registry();
$result = (new Runner($client, $tools, maxSteps: 8))->run(new Request([Message::user('What changed since 06:00 UTC?')]));

$chat = new Chat($runner, new InMemoryConversationStore());
$result = $chat->send('ticket-42', 'Can I change the dates?', new Request(system: 'You are support.'));

// params.php
'yiisoft/db-migration' => [
    'sourceNamespaces' => ['Ldkafka\\Ai\\Migration'],
],