Overview
You can provide your own LLM configuration data toInference object with DSN string.
This is useful for inline configuration or for building configuration from admin UI,
CLI arguments or environment variables.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Inference object with DSN string.
This is useful for inline configuration or for building configuration from admin UI,
CLI arguments or environment variables.
<?php
require 'examples/boot.php';
use Cognesy\Config\Dsn;
use Cognesy\Config\Env;
use Cognesy\Messages\Messages;
use Cognesy\Polyglot\Inference\Config\LLMConfig;
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Utils\Str;
$xaiApiKey = (string) Env::get('XAI_API_KEY', '');
$dsn = "driver=xai,apiUrl=https://api.x.ai/v1,endpoint=/chat/completions,apiKey={$xaiApiKey},model=grok-3";
$answer = Inference::fromConfig(LLMConfig::fromArray(Dsn::fromString($dsn)->toArray()))
->with(
messages: Messages::fromString('What is the capital of France'),
options: ['max_tokens' => 64]
)
->get();
echo "USER: What is capital of France\n";
echo "ASSISTANT: $answer\n";
assert(Str::contains($answer, 'Paris'));
?>