1. Go to this page and download the library: Download spatie/laravel-ai-evals 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/ */
spatie / laravel-ai-evals example snippets
evaluate('tells a customer where their order is')
->agent(fn () => new AgentDriver(SupportAgent::class, as: Customer::factory()->hasOrders(1)->create()))
->repeat(3)
->prompt('Where is my order?')
->toHaveToolCalls([LookupOrderTool::class])
->toHaveMaxWords(80)
->toSatisfy('Says when the order shipped.')
->prompt('Can you send it to my office instead?')
->toSatisfy('Explains that a shipped order can no longer change address.');
use App\Evals\ShopJudgeAgent;
use Spatie\LaravelAiEvals\AiEvals;
AiEvals::judgeUsing(ShopJudgeAgent::class);
AiEvals::repeat(3);
// Tests the model's reasoning. When it fails, there is nothing you can change.
->toSatisfy('Works out why the order is late.')
// Tests your agent. When it fails, you fix the tool, its description or the prompt.
->toHaveToolCalls([LookupOrderTool::class])
->toSatisfy('Bases its explanation on the shipment status that the order lookup tool returned.')
use App\Ai\Agents\SupportAgent;
use App\Ai\Tools\CancelOrderTool;
use App\Ai\Tools\LookupOrderTool;
use App\Models\Customer;
use App\Models\Order;
use Spatie\LaravelAiEvals\Drivers\AgentDriver;
evaluate('cancels an order after confirmation')
->agent(function () {
$customer = Customer::factory()->create();
Order::factory()->for($customer)->create(['number' => 'A-1001']);
return new AgentDriver(SupportAgent::class, as: $customer);
})
->repeat(3)
->prompt('Please cancel order A-1001.')
->toHaveToolCalls([LookupOrderTool::class])
->not->toHaveToolCalls([CancelOrderTool::class])
->toSatisfy('Asks the customer to confirm before cancelling.')
->prompt('Yes, cancel it.')
->approveAll()
->toHaveToolCalls([CancelOrderTool::class => ['order' => 'A-1001']])
->toHaveMaxWords(60)
->toSatisfy('Confirms that order A-1001 was cancelled.')
->toSatisfy('Does not claim a refund was issued.');
class ShopSeeder extends Seeder
{
public Customer $customer;
public Order $order;
public function run(): void
{
$this->customer = Customer::factory()->hasOrders(25)->create();
$this->order = $this->customer->orders->first();
}
}
class ShopDriver extends AgentDriver
{
public function __construct(
public readonly ShopSeeder $seeder,
) {
parent::__construct(SupportAgent::class, as: $seeder->customer);
}
public static function seeded(): self
{
$seeder = app(ShopSeeder::class);
$seeder->setContainer(app())->__invoke();
return new self($seeder);
}
}
evaluate('summarizes an order')
->agent(fn () => ShopDriver::seeded())
->prompt(fn (ShopDriver $driver) => "What is in order {$driver->seeder->order->number}?")
->toSatisfy('Lists the products of the order.');
evaluate('links to the order it describes')
->agent(fn () => ShopDriver::seeded())
->prompt(fn (ShopDriver $driver) => "Where is order {$driver->seeder->order->number}?")
->toContain(fn (ShopDriver $driver) => route('orders.show', $driver->seeder->order))
->toSatisfy(fn (ShopDriver $driver) => "Says that the order is {$driver->seeder->order->status}.");
evaluate('uses the current terminology')
->agent(fn () => ShopDriver::seeded())
->prompt('Which of my orders need attention?')
->not->toContain('shipment group', 'parcel set');
use App\Ai\Tools\LookupOrderTool;
use Spatie\LaravelAiEvals\Transcript\Transcript;
evaluate('cancels only the late orders')
->agent(fn () => ShopDriver::seeded())
->prompt('Cancel all my late orders.')
->approveAll()
->toPassCheck('Cancels exactly the late orders', function (ShopDriver $driver) {
$cancelled = $driver->seeder->customer
->orders()
->where('status', 'cancelled')
->pluck('number')
->all();
return $cancelled === ['A-1', 'A-3'] ?: 'Cancelled '.implode(', ', $cancelled).'.';
})
->toPassCheck('Looks up each order once', function (Transcript $transcript) {
$orders = array_column(array_column($transcript->callsTo(LookupOrderTool::class), 'arguments'), 'order');
return count($orders) === count(array_unique($orders));
});
new AgentDriver(SupportAgent::class, as: $customer);
new AgentDriver(fn () => new SupportAgent(locale: 'nl'), as: $customer);
use Laravel\Ai\Approvals\Decisions;
use Laravel\Ai\Responses\AgentResponse;
use Spatie\LaravelAiEvals\Drivers\Driver;
class ChatDriver implements Driver
{
protected Chat $chat;
public function __construct(
protected User $user,
) {}
public function startConversation(): void
{
$this->chat = app(StartChatAction::class)->execute($this->user);
}
public function prompt(string $message): AgentResponse
{
return app(SendChatMessageAction::class)->execute($this->chat, $message);
}
public function decide(array $decisions): AgentResponse
{
return app(SendChatMessageAction::class)->execute($this->chat, Decisions::from($decisions));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.