PHP code example of underwear / llm-wrapper

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

    

underwear / llm-wrapper example snippets


use Underwear\LlmWrapper\LlmClient;

$client = LlmClient::openai(['api_key' => 'your-key']);

$response = $client->chat()
    ->model('gpt-4')
    ->user('What is the capital of Germany?')
    ->send();

echo $response->text(); // Berlin

use Underwear\LlmWrapper\LlmClient;

$client = LlmClient::claude(['api_key' => 'your-key']);

$response = $client->chat()
    ->user('Explain photosynthesis in simple terms.')
    ->send();

echo $response->text();

$response = $client->chat()
    ->user('Who wrote "1984"?')
    ->send();

echo $response->text(); // George Orwell


$chat = $client->chat()
    ->system('You are a helpful historian.')
    ->user('Who discovered penicillin?')
    ->assistant('Alexander Fleming discovered penicillin in 1928.')
    ->user('Tell me more about his discovery.');

$response = $chat->send();

echo $response->text();

$response = $client->chat()
    ->model('gpt-4.1-mini')
    ->temperature(0.9)
    ->user('Write a short poem about the sea.')
    ->send();

echo $response->text();

use Underwear\LlmWrapper\ChatBuilder\FunctionCallMode;

$response = $client->chat()
    ->system('You are generator of random profiles')
    ->function('createProfile', function ($f) {
        $f->stringParam('firstName')->tion('createProfile')->get('firstName');
echo "Generated name: {$firstName}";

use Underwear\LlmWrapper\ChatBuilder\FunctionBuilder;
use Underwear\LlmWrapper\ChatBuilder\FunctionCallMode;
use Underwear\LlmWrapper\ChatBuilder\FunctionParam;
use Underwear\LlmWrapper\ChatBuilder\ObjectProp;

$response = $client->chat()
    ->system('You are an assistant capable of creating users and sending notifications.')
    ->user('Now we need to create a user')
    ->function('create_user', function (FunctionBuilder $f) {
        $f->description('Creates a new user account.');
        $f->param(FunctionParam::string('username')->functionCall(FunctionCallMode::auto())
    ->send();

if ($response->hasFunctionCalls()) {
    if ($response->called('create_user')) {
        $args = $response->function('create_user')->getArguments();
        // do something here...
    }
    
    if ($response->called('send_notification')) {
        $args = $response->function('send_notification')->getArguments();
        // do something here...
    }
}

use Underwear\LlmWrapper\ChatBuilder\FunctionBuilder;
use Underwear\LlmWrapper\ChatBuilder\FunctionParam;

$response = $client->chat()
    ->system('You are a shopping assistant that helps organize grocery lists.')
    ->user('Create a grocery list with 5 fruits and their estimated prices')
    ->function('create_grocery_list', function (FunctionBuilder $f) {
        $f->description('Creates a structured grocery list');
        
        // Array of strings - using convenience method
        $f->param(FunctionParam::stringArray('fruits')->minItems(3)->maxItems(10));
        
        // Array of numbers - using arrayOf method
        $f->param(FunctionParam::array('prices')
            ->arrayOf(FunctionParam::float('price'))
            ->minItems(3)
            ->maxItems(10)
        );
        
        // Array of integers
        $f->param(FunctionParam::intArray('quantities')->