Skip to main content

Overview

Example

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

use Cognesy\Config\Dsn;
use Cognesy\Config\Env;
use Cognesy\Http\HttpClient;
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Polyglot\Inference\Config\LLMConfig;
use Cognesy\Polyglot\Inference\InferenceRuntime;
use Cognesy\Utils\Str;

// check with default HTTP client facade
$httpClient = new HttpClient();

$openAiApiKey = (string) Env::get('OPENAI_API_KEY', '');
$dsn = "driver=openai,apiUrl=https://api.openai.com/v1,endpoint=/chat/completions,apiKey={$openAiApiKey},model=gpt-4.1-nano";

$answer = Inference::fromRuntime(InferenceRuntime::fromConfig(
        config: LLMConfig::fromArray(Dsn::fromString($dsn)->toArray()),
        httpClient: $httpClient,
    ))
    ->withMessages('What is the capital of France')
    ->withMaxTokens(64)
    ->get();

echo "USER: What is capital of France\n";
echo "ASSISTANT: $answer\n";

assert(Str::contains($answer, 'Paris'));
?>