PHP code example of meirdick / laravel-cf-workersai
1. Go to this page and download the library: Download meirdick/laravel-cf-workersai 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/ */
foreach (agent('helper')->streamed('Tell me a story.', provider: 'workers-ai') as $event) {
if ($event instanceof \Laravel\Ai\Events\TextDelta) {
echo $event->text;
}
}
use Laravel\Ai\Attributes\Tool;
#[Tool(description: 'Look up the current weather.')]
function getWeather(string $city): string
{
return "Sunny in {$city}.";
}
agent('helper')->withTools([getWeather(...)])->prompt('Weather in Tokyo?', provider: 'workers-ai');
public function providerOptions(Lab|string $provider): array
{
// Custom drivers arrive as a plain string, not a Lab enum case.
return $provider === 'workers-ai' ? ['tool_choice' => '
use Laravel\Ai\Attributes\Timeout;
#[Timeout(120)]
class ExtractionAgent implements Agent { /* ... */ }
// or per call:
$agent->prompt('...', provider: 'workers-ai', timeout: 120);
use Laravel\Ai\Attributes\Strict;
#[Strict] // opt-in to strict JSON schema enforcement
final class TaskAgent extends \Laravel\Ai\Agent {}
use Laravel\Ai\Contracts\HasProviderOptions;
use Laravel\Ai\Enums\Lab;
class AnalysisAgent implements Agent, HasProviderOptions
{
public function providerOptions(Lab|string $provider): array
{
// Workers AI is a custom driver, so $provider arrives as the string.
return $provider === 'workers-ai'
? ['chat_template_kwargs' => ['thinking' => false]]
: [];
}
}