PHP code example of laraigent / larai-kit

1. Go to this page and download the library: Download laraigent/larai-kit 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/ */

    

laraigent / larai-kit example snippets


use LarAIgent\AiKit\Services\Ingestion\IngestionService;
use LarAIgent\AiKit\Services\Chat\ChatService;

app(IngestionService::class)->ingestText('Our return policy allows returns within 30 days of purchase.');

$reply = app(ChatService::class)->sendMessage('How long do I have to return a product?');
// $reply['reply']   => "You have 30 days from the date of purchase..."
// $reply['sources'] => [{ name, url, type }, ...]

use LarAIgent\AiKit\Services\Chat\ChatService;

$chat = app(ChatService::class);
$result = $chat->sendMessage('What is your return policy?');

$result['reply'];   // AI response grounded in your ingested documents
$result['sources']; // [{ name, url, type }, ...] for citation UI

use LarAIgent\AiKit\Services\Chat\ConversationManager;

$conversations = app(ConversationManager::class);
$chat = app(ChatService::class);

// Create a scoped conversation
$convo = $conversations->create(scope: ['chatbot_id' => 42], title: 'Support thread');

// Send messages — history auto-loaded and persisted
$result = $chat->sendMessage('What is the return policy?', conversationId: $convo->id);
$result = $chat->sendMessage('How long do I have?', conversationId: $convo->id);
// Both messages + replies are stored in ai_messages

// In a controller — return directly for automatic SSE response
public function stream(Request $request)
{
    $chat = app(ChatService::class);
    return $chat->streamMessage($request->input('message'))
        ->withOnComplete(function (string $fullText, mixed $usage, array $sources) {
            // Optional: persist assistant output, counters, timestamps, etc.
        });
    // Emits:  data: {"type":"text_delta","delta":"..."}  (per chunk)
    //         data: {"type":"sources","sources":[...]}    (at the end)
    //         data: {"type":"done"}
}

use LarAIgent\AiKit\Services\Ingestion\IngestionService;

$ingestion = app(IngestionService::class);

// Ingest plain text
$asset = $ingestion->ingestText('Our return policy allows returns within 30 days...');

// Ingest a file (PDF, DOCX, TXT)
$asset = $ingestion->ingestFile($request->file('document'));

// Ingest from an existing disk path (no UploadedFile 42]);

// app/Ai/Agents/ProductAgent.php
class ProductAgent implements Agent, HasTools
{
    use Promptable;

    public function instructions(): string
    {
        return 'You are a product expert. Use the knowledge base to answer questions.';
    }

    public function tools(): iterable
    {
        return [
            SimilaritySearch::usingModel(Document::class, 'embedding'),
        ];
    }
}

app(IngestionService::class)->ingestFile($request->file('document'));

$ingestion->ingestText($text, scope: ['chatbot_id' => 42]);
$chat->sendMessage($message, scope: ['chatbot_id' => 42]);

Event::listen(ChatCompleted::class, function ($event) {
    Log::info('AI usage', [
        'provider' => $event->provider,
        'model' => $event->model,
        'input' => $event->inputTokens,
        'output' => $event->outputTokens,
        'scope' => $event->scope,
    ]);
});
bash
php artisan make:larai-agent ProductAgent
bash
php artisan make:larai-tool CheckOrderTool
bash
php artisan larai:doctor        # config checks
php artisan larai:doctor --deep  # + live API probe (embedding test)
bash
php artisan larai:chat