1. Go to this page and download the library: Download ferdiunal/laravel-ai-router 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/ */
use Laravel\Ai\Attributes\Model;
use Laravel\Ai\Attributes\Provider;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Promptable;
#[Provider('laravel-ai-router')]
#[Model('auto')]
final class SupportAgent implements Agent
{
use Promptable;
public function instructions(): string
{
return 'Answer concisely and
$response = SupportAgent::make()->prompt('Summarize the order status.');
echo (string) $response;
$response = SupportAgent::make()->prompt(
'Summarize the order status.',
provider: 'laravel-ai-router',
model: 'qwen/qwen3-coder:free',
timeout: 20,
);
$response = SupportAgent::make()->prompt(
'Summarize the order status.',
provider: [
'laravel-ai-router' => 'auto',
'openai' => 'gpt-4o-mini',
],
timeout: 20,
);
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\JsonSchema\Types\Type;
use Laravel\Ai\Contracts\Tool;
use Laravel\Ai\Tools\Request;
final class LookupOrder implements Tool
{
public function name(): string
{
return 'lookup_order';
}
public function description(): string
{
return 'Look up an order total by order ID.';
}
public function handle(Request $request): string
{
$orderId = $request['order_id'];
// Replace this with your application lookup.
return "Order {$orderId} total is 42 TRY";
}
/** @return array<string, Type> */
public function schema(JsonSchema $schema): array
{
return [
'order_id' => $schema->string()->
$response = ai()
->using('laravel-ai-router', 'auto')
->instructions('Use tools when needed to answer order-related questions.')
->withTools([new LookupOrder])
->prompt('What is the total for order A-100?')
->response();
echo (string) $response;
// "The total for order A-100 is 42 TRY."
$response->toolCalls; // Collection of ToolCall objects
$response->toolResults; // Collection of ToolResult objects
$response->steps; // Collection of Step objects, including tool loop iterations
use Laravel\Ai\Attributes\Model;
use Laravel\Ai\Attributes\Provider;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasTools;
use Laravel\Ai\Contracts\Tool;
use Laravel\Ai\Promptable;
#[Provider('laravel-ai-router')]
#[Model('auto')]
final class OrderAgent implements Agent, HasTools
{
use Promptable;
public function instructions(): string
{
return 'Use tools when needed to answer order-related questions.';
}
/** @return iterable<int, Tool> */
public function tools(): iterable
{
return [new LookupOrder];
}
}
$response = OrderAgent::make()->prompt('What is the total for order A-100?');
echo (string) $response;
use Laravel\Ai\Streaming\Events\TextDelta;
$stream = ai()
->using('laravel-ai-router', 'auto')
->instructions('Answer with a short streaming response.')
->prompt('Explain the current order status.')
->stream();
foreach ($stream as $event) {
if ($event instanceof TextDelta) {
echo $event->delta;
}
}
$stream = SupportAgent::make()->stream(
'Explain the current order status.',
provider: 'laravel-ai-router',
model: 'auto',
);
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\JsonSchema\Types\Type;
use Laravel\Ai\Attributes\Model;
use Laravel\Ai\Attributes\Provider;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasStructuredOutput;
use Laravel\Ai\Promptable;
#[Provider('laravel-ai-router')]
#[Model('auto')]
final class TicketAnalyzer implements Agent, HasStructuredOutput
{
use Promptable;
public function instructions(): string
{
return 'Return a JSON response with the requested ticket analysis.';
}
/** @return array<string, Type> */
public function schema(JsonSchema $schema): array
{
return [
'summary' => $schema->string()->