PHP code example of dewaldhugo / laravel-ai-governor
1. Go to this page and download the library: Download dewaldhugo/laravel-ai-governor 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/ */
dewaldhugo / laravel-ai-governor example snippets
use AiGovernor\Prompts\PromptDefinition;
return new class extends PromptDefinition
{
public string $name = 'summarize';
public int $version = 1;
public string $model = 'gpt-4o-mini';
public float $temperature = 0.2;
public int $maxTokens = 800;
public function system(): string
{
return 'You are a precise summarizer. Return a maximum of three sentences.';
}
public function user(): string
{
return 'Summarize the following: {{text}}';
}
};
use AiGovernor\Traits\HasAiBudget;
class User extends Authenticatable
{
use HasAiBudget;
}
// 100,000 tokens per month — hard limit
$user->setAiBudget(limit: 100_000, period: 'monthly');
// 10,000 tokens per day — soft limit (warns, does not throw)
$user->setAiBudget(limit: 10_000, period: 'daily', hard: false);
// Scoped to a specific feature
$user->setAiBudget(limit: 50_000, period: 'monthly', scope: 'summarize');
// OpenAI (default)
'adapter' => \AiGovernor\Adapters\OpenAiAdapter::class,
// Anthropic Claude
'adapter' => \AiGovernor\Adapters\AnthropicAdapter::class,
// No-op for tests and local development
'adapter' => \AiGovernor\Adapters\NullAdapter::class,
use AiGovernor\Contracts\AiProviderAdapter;
use AiGovernor\Exceptions\ProviderException;
use AiGovernor\Models\PromptVersion;
use AiGovernor\Values\AdapterResult;
class MyCustomAdapter implements AiProviderAdapter
{
public function execute(PromptVersion $prompt, string $rendered): AdapterResult
{
// Call your provider and return a normalised AdapterResult.
// Throw ProviderException for provider-level semantic failures
// (e.g. invalid model, content policy rejection).
// Throw Illuminate\Http\Client\RequestException for HTTP/network errors.
return new AdapterResult(
content: $responseBody,
promptTokens: $usage['input'],
completionTokens: $usage['output'],
latencyMs: $latencyMs,
model: $prompt->model,
);
}
}
use AiGovernor\Adapters\NullAdapter;
use AiGovernor\Contracts\AiProviderAdapter;
// In your TestCase::setUp() or in an individual test:
$this->app->instance(
AiProviderAdapter::class,
NullAdapter::make(
content: 'This is the mocked AI response.',
promptTokens: 10,
completionTokens: 20,
),
);