Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/core/llms.txt

Use this file to discover all available pages before exploring further.

Generate is the top-level static facade that ties the entire SDK together. Every text or image request begins here — it holds the configured runtime (Sdk), an optional default model, and exposes factory methods that return fluent builder objects you chain to shape each request before executing it.

Starting a text request

Generate::text() returns a PendingTextRequest. You can optionally pass a prompt string directly as a shortcut, or call ->prompt() on the returned builder.
use AiSdk\Generate;

// Shortcut: pass the prompt inline
$result = Generate::text('Explain the water cycle in two sentences.')->run();

// Builder style: chain methods before running
$result = Generate::text()
    ->prompt('Explain the water cycle in two sentences.')
    ->maxTokens(256)
    ->temperature(0.7)
    ->run();

echo $result->text;
PendingTextRequest accepts every generation parameter through a fluent interface before you call ->run() (blocking) or ->stream() (streaming generator).

Starting an image request

Generate::image() returns a PendingImageRequest. Like its text counterpart you can pass the prompt inline or set it via ->prompt().
use AiSdk\Generate;

$result = Generate::image('A serene Japanese garden at sunset, photorealistic.')
    ->size('1024x1024')
    ->count(2)
    ->run();

foreach ($result->images as $i => $image) {
    $image->save("output_{$i}.png");
}
PendingImageRequest does not share the HasMessages / ConfiguresGeneration traits used by text requests. Its builder surface covers prompt(), count(), size(), aspectRatio(), seed(), and providerOptions().

Setting a default model

The “default model” pattern lets you register a model once at application boot and omit it from every individual call. Call Generate::model() with any object that implements Model:
use AiSdk\Generate;

// In a service provider, bootstrap file, or container binding:
Generate::model($openAi->textModel('gpt-4o'));

// Later, anywhere in your application:
$result = Generate::text('Summarise this document.')->run();
// ↑ Uses gpt-4o automatically — no ->model() call required.
When a default model is set, PendingTextRequest and PendingImageRequest each check whether the stored model implements their required interface (TextModelInterface or ImageModelInterface). If it does not, the builder starts with null and the ModelResolver will throw at call time unless you supply a model explicitly. You can still override the default on a per-call basis:
$result = Generate::text('Draft a haiku.')
    ->model($openAi->textModel('gpt-4o-mini'))
    ->run();

Configuring the SDK runtime

For applications that already have a PSR-18 HTTP client and PSR-17 factories wired through a DI container, hand the fully-built Sdk object to Generate::configure(). This replaces the auto-discovered runtime with your own.
use AiSdk\Generate;
use AiSdk\Support\Sdk;

$sdk = new Sdk(
    httpClient: $container->get(ClientInterface::class),
    requestFactory: $container->get(RequestFactoryInterface::class),
    streamFactory: $container->get(StreamFactoryInterface::class),
    logger: $container->get(LoggerInterface::class),
);

Generate::configure($sdk);
Sdk is immutable. Generate::configure() stores the instance, and every subsequent call to Generate::sdk() returns it. You can retrieve the current runtime at any time:
$sdk = Generate::sdk(); // Returns the configured Sdk instance.

Resetting state (tests)

Generate::reset() clears both the stored Sdk and the runtime factory, returning the facade to its initial state. Call it in a tearDown or afterEach hook to prevent state leaking between tests.
use AiSdk\Generate;

afterEach(function () {
    Generate::reset();
});

Static method reference

MethodSignatureDescription
texttext(?string $prompt = null): PendingTextRequestBegin a text generation request.
imageimage(?string $prompt = null): PendingImageRequestBegin an image generation request.
modelmodel(Model $model): voidSet the application-wide default model.
configureconfigure(Sdk $sdk): voidReplace the runtime with a pre-built Sdk instance.
sdksdk(): SdkReturn the current (or auto-built) runtime.
resetreset(): voidClear all stored state (intended for tests).
When no Sdk has been configured, calling Generate::sdk() triggers SdkFactory::make(), which auto-discovers PSR-18 and PSR-17 implementations from your installed packages. If discovery fails an exception is thrown that lists what is missing.

Build docs developers (and LLMs) love