Skip to main content

Overview

Inference class offers access to LLM APIs and convenient methods to execute model inference, incl. chat completions, tool calling or JSON output generation. LLM providers access details can be found and modified via LLMConfig objects.

Example

<?php
require 'examples/boot.php';

use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Utils\Str;

// EXAMPLE 1: use default runtime configuration for convenient ad-hoc calls
$answer = Inference::using('openai')
    ->with(messages: 'What is capital of Germany')
    ->get();

echo "USER: What is capital of Germany\n";
echo "ASSISTANT: $answer\n\n";
assert(Str::contains($answer, 'Berlin'));




// EXAMPLE 2: customize inference options using fluent API
$response = Inference::using('openai')
    ->withMessages([['role' => 'user', 'content' => 'What is capital of France']])
    ->withOptions(['max_tokens' => 64])
    ->create();

$answer = $response->get();
echo "USER: What is capital of France\n";
echo "ASSISTANT: $answer\n\n";
assert(Str::contains($answer, 'Paris'));




// EXAMPLE 3: streaming response
$stream = Inference::using('openai')
    ->withMessages([['role' => 'user', 'content' => 'Describe capital of Brasil']])
    ->withOptions(['max_tokens' => 128])
    ->withStreaming()
    ->stream()
    ->responses();

echo "USER: Describe capital of Brasil\n";
echo "ASSISTANT: ";
foreach ($stream as $partial) {
    echo $partial->contentDelta;
}
echo "\n";
?>