Download the PHP package tobiebenezer/php-ai without Composer
On this page you can find all versions of the php package tobiebenezer/php-ai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tobiebenezer/php-ai
More information about tobiebenezer/php-ai
Files in tobiebenezer/php-ai
Package php-ai
Short Description Laravel 8 compatible AI assistant scaffold.
License proprietary
Informations about the package php-ai
AI Package (ai)
This package provides a Laravel-compatible AI Assistant infrastructure designed to integrate LLMs (e.g., OpenRouter, Gemini) with analytical tools and guardrails.
Features
- Multi-Provider Support: Adapter system supporting OpenRouter and Google Gemini APIs.
- Dynamic Tool Discovery: Scans and loads analytical tools automatically.
- Guardrail Pipeline: Pre-execution system instructions (
InstructionGuardrail) and run-time safety check filters (RuntimeGuardrail). - Interactive Livewire Integration: Dynamic, user-friendly client-side chat interface.
Installation
Add the local repository to your host project's composer.json file:
Then install the package:
Configuration & Migrations
Publish the configuration file, migrations, and stubs:
Run the migrations to create the configuration table:
How to Write Custom AI Tools
The package discovers all classes implementing Tobiebenezer\Ai\Contracts\Tool inside the configured path (default: app/Ai/Tools/).
Extending AnalyticalTool
For database-backed, analytical querying tools, you should extend the abstract Tobiebenezer\Ai\Tools\AnalyticalTool class. This provides built-in query building, grouping, formatting, filter validation, and aggregation.
Here is an example tool that allows the AI to query a Sales table:
Implementing Tool directly
If you need a tool that does not query the database (e.g. calling an external API), implement Tobiebenezer\Ai\Contracts\Tool directly:
Guardrails
Guardrails monitor prompt context and run-time actions:
InstructionGuardrail: Prepends context-specific instructions to the LLM prompt before execution.RuntimeGuardrail: Triggers events during execution phases (BEFORE_PROVIDER_REQUEST,BEFORE_TOOL_CALL,AFTER_TOOL_RESULT,BEFORE_FINAL_RESPONSE) to allow sanitizing inputs or denying requests.
Custom Guardrail Example
Overriding Package Guardrails
If you want to customize or replace a default package guardrail (such as CapabilitiesGuardrail), you can override it in your application using one of the following two approaches:
Method 1: Container Binding (Recommended)
You can register an override in your application's AppServiceProvider so that the Laravel container resolves your custom guardrail class whenever the package asks for the default one.
Method 2: Configuration Replacement
Alternatively, publish the configuration file (config/ai.php) and swap the package guardrail class in the guardrails.global or guardrails.groups array with your custom class:
Your custom guardrail must implement the respective contract (Tobiebenezer\Ai\Contracts\InstructionGuardrail or Tobiebenezer\Ai\Contracts\RuntimeGuardrail):
🛠️ Advanced Features
1. Developer Artisan Generators
Automatically bootstrap your package assets using standard Artisan console commands:
- Create an AI Tool:
php artisan make:ai-tool {Name}(add--analyticalor-afor database-backed tools). - Create a Guardrail:
php artisan make:ai-guardrail {Name}(add--runtimeor-rfor runtime phase checking). - Create a Provider:
php artisan make:ai-provider {Name}.
2. Request & Tool Call Audit Logs
Run the migrations to create the request log tables:
The package automatically logs:
ai_request_logs: Prompts, final assistant content, token counts, and request-level execution latency.ai_tool_call_logs: Executed tools, input arguments, returned outputs, execution status, latencies, and exception logs.
3. Structured Outputs (JSON Schema)
Force the model to respond in structured JSON format conforming to a schema:
4. Monthly Token Budget
Prevent cost overruns by setting monthly limits in config/ai.php:
When exceeded, requests are blocked automatically and throw a GuardrailException.
5. Self-Healing Exception Recovery
If a tool execution fails (e.g., database query exceptions or column errors), the package intercepts the exception and feeds the error details back to the LLM as the tool result. The LLM can analyze the schema failure, correct its query arguments, and retry successfully.