1. Go to this page and download the library: Download tetrixdev/laravel-ai-bridge 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/ */
tetrixdev / laravel-ai-bridge example snippets
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Tetrix\AiBridge\Facades\AiBridge;
class ChatController extends Controller
{
public function stream(Request $request)
{
// NOTE (UX-001): conversation_id must stay CONSTANT across all messages in the
// same conversation. Generating a new ID on every request (e.g. uniqid()) creates
// a brand-new conversation each time, so the AI has no memory of previous messages.
// Store the ID in the session and reuse it for follow-up messages.
$conversationId = $request->input('conversation_id')
?? $request->session()->get('ai_conversation_id')
?? 'conv-' . Str::uuid();
$request->session()->put('ai_conversation_id', $conversationId);
return AiBridge::streamToResponse(
conversationId: $conversationId,
message: $request->input('message'),
options: [
'system_prompt' => 'You are a helpful assistant.',
],
);
}
}
// In a service provider's boot() method
AiBridge::registerTool(
name: 'roll_dice',
description: 'Roll one or more dice',
parameters: [
'type' => 'object',
'properties' => [
'sides' => ['type' => 'integer', 'description' => 'Number of sides on each die'],
'count' => ['type' => 'integer', 'description' => 'Number of dice to roll'],
],
'
use Tetrix\AiBridge\Tools\AbstractTool;
use Tetrix\AiBridge\Tools\ToolParameter;
class LookupCharacterTool extends AbstractTool
{
public function name(): string
{
return 'lookup_character';
}
public function description(): string
{
return 'Look up a character in the database';
}
protected function defineParameters(): array
{
return [
new ToolParameter(
name: 'name',
type: 'string',
description: 'The full name of the character to look up',
),
new ToolParameter(
name: 'realm',
type: 'string',
description: 'Which realm to search in',
use Tetrix\AiBridge\Contracts\ToolHandler;
class LookupCharacterTool implements ToolHandler
{
public function name(): string { return 'lookup_character'; }
public function description(): string { return 'Look up a character in the database'; }
public function parameters(): array {
return [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string', 'description' => 'The full name of the character'],
],
'
$stream = AiBridge::stream('conv-1', 'Roll 2d6 for damage');
$stream->onToolCall(function (string $name, array $params, string $callId) {
// Tool execution happens automatically if registered.
// This callback is for UI updates / logging.
Log::info("AI called tool: {$name}", $params);
});
$stream->start();