PHP code example of dskripchenko / laravel-llm

1. Go to this page and download the library: Download dskripchenko/laravel-llm 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/ */

    

dskripchenko / laravel-llm example snippets


use Dskripchenko\Llm\Models\Provider;
use Dskripchenko\Llm\Models\Route;

$gpu = Provider::create(['name' => 'gpu-box', 'base_url' => 'http://10.0.0.5:8000/v1', 'limits' => ['timeout' => 180]]);
$cpu = Provider::create(['name' => 'ollama',  'base_url' => 'http://127.0.0.1:11434/v1']);

$fallback = Route::create(['purpose' => 'summarize', 'provider_id' => $cpu->id, 'model' => 'llama3.2:3b']);
Route::create(['purpose' => 'summarize', 'provider_id' => $gpu->id, 'model' => 'qwen2.5:32b',
               'fallback_route_id' => $fallback->id, 'options' => ['temperature' => 0.2]]);

use Dskripchenko\Llm\Facades\Llm;

// From a preset (see docs/en/prompts-and-schemas.md)
$result = Llm::prompt(ClassifyArticle::class, ['body' => $article->description])
    ->about($article)              // logged as the call's subject
    ->run();
$result->data;                   // ['type' => 'guide', 'level' => 2], validated

// By hand
$result = Llm::request('summarize')
    ->system('Summarize the article in two sentences.')
    ->user('Article:')
    ->data('article', $article->description)     // untrusted text → a data block
    ->stream(function (string $delta): void { echo $delta; });

Llm::embed(['first text', 'second text'])->vectors;
Llm::rerank('query', $documents)->order();

Llm::fake(['article.classify' => ['type' => 'guide', 'level' => 2], 'summarize' => 'Short.']);

// … exercise your code …

Llm::assertPurpose('classify', 1);
Llm::assertSent(fn ($call) => $call->contains('To reset a password, open Settings', 'user'));