PHP code example of tivins / llm-lib

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

    

tivins / llm-lib example snippets




use Tivins\LlmLib\Agent;
use Tivins\LlmLib\ChatCompletionOptions;
use Tivins\LlmLib\Conversation;
use Tivins\LlmLib\LLM;
use Tivins\LlmLib\Message;
use Tivins\LlmLib\Role;
use Tivins\LlmLib\ToolRegistry;

$llm = new LLM(
    endpoint: 'http://localhost:11434',  // Ollama example
    defaultModel: 'llama3',
);

$tools = new ToolRegistry();
$agent = new Agent($llm, $tools);

$conversation = new Conversation([
    Message::withCreatedAt(Role::User, 'Hello!'),
]);

$result = $agent->runTurn($conversation, new ChatCompletionOptions(temperature: 0.3));

if ($result->success) {
    echo $result->message->content;
} else {
    echo 'Error: ' . $result->error;
}

use Tivins\LlmLib\Tool;
use Tivins\LlmLib\ToolSchema;

$tools = new ToolRegistry(
    new Tool(
        new ToolSchema(
            name: 'get_weather',
            description: 'Get current weather for a city',
            parameters: [
                'type' => 'object',
                'properties' => [
                    'city' => ['type' => 'string'],
                ],
                '

use Tivins\LlmLib\EmbeddingOptions;
use Tivins\LlmLib\LLM;

$llm = new LLM(
    endpoint: 'http://127.0.0.1:8081',
    defaultModel: 'bge-m3-Q8_0.gguf',
);

$response = $llm->embeddings(
    ['The cat sits on the mat.', 'A feline rests on a rug.'],
    new EmbeddingOptions(encodingFormat: 'float'),
);

foreach ($response->embeddings as $embedding) {
    echo count($embedding->vector) . " dimensions\n";
}

use Tivins\LlmLib\LLM;
use Tivins\LlmLib\RerankOptions;

$llm = new LLM(
    endpoint: 'http://127.0.0.1:8082',
    defaultModel: 'Qwen3-Reranker-4B-Q4_K_M.gguf',
);

$documents = [
    'The giant panda is a bear endemic to China.',
    'Stock markets rallied after earnings.',
];

$response = $llm->rerank('What is a panda?', $documents, new RerankOptions(topN: 2));

foreach ($response->rankedDocuments($documents) as $item) {
    echo $item['relevanceScore'] . ' — ' . $item['document'] . "\n";
}

use Tivins\LlmLib\Logger;

$conversation = new Conversation(
    messages: [],
    logger: new Logger('/var/log/conversations/session-1.json'),
);
// Each addMessage() (including those done by Agent) triggers a file write.

new AgentTurnResult(
    message: ?Message,   // final assistant message when success
    success: bool,
    error: ?string,
    toolRounds: int,       // number of tool execution rounds in this turn
);

$hooks = new AgentHooks();
$hooks->beforeToolCall(function (BeforeToolCallEvent $event): void {
    // Skip execution with a standard user-rejection payload:
    // $event->replacement = ToolCallRejection::userRejected($event->call);

    // Or return any canned tool message without calling the handler:
    // $event->replacement = new Message(Role::Tool, '{"mock":true}', toolCallId: $event->call->id);
});

$agent = new Agent($llm, $tools, hooks: $hooks);
bash
php examples/ex120-embeddings.php   # uires rerank server (e.g. port 8082)