Customizing AI models | laravel-url-ai-transformer | Spatie       

 SPATIE  

  Laravel URL AI Transformer 
=============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-url-ai-transformer](https://spatie.be/docs/laravel-url-ai-transformer/v2)  Advanced-usage  Customizing AI models

 Version   v2   v1      

 Other versions for crawler [v2](https://spatie.be/docs/laravel-url-ai-transformer/v2) [v1](https://spatie.be/docs/laravel-url-ai-transformer/v1) 

  Customizing AI models    
- [ Introduction ](https://spatie.be/docs/laravel-url-ai-transformer/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-url-ai-transformer/v2/support-us)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-url-ai-transformer/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-url-ai-transformer/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-url-ai-transformer/v2/changelog)
- [ About us ](https://spatie.be/docs/laravel-url-ai-transformer/v2/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-url-ai-transformer/v2/basic-usage/getting-started)
- [ Registering transformations ](https://spatie.be/docs/laravel-url-ai-transformer/v2/basic-usage/registering-transformations)
- [ Writing your own transformers ](https://spatie.be/docs/laravel-url-ai-transformer/v2/basic-usage/writing-your-own-transformers)

Advanced usage
--------------

- [ Structured output ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/structured-output)
- [ Customizing AI models ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/customizing-ai-models)
- [ Customizing the stored result ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/customizing-the-result)
- [ Conditional transformations ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/conditional-transformations)
- [ Crawling URLs ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/crawling-urls)
- [ Exploring command options ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/exploring-command-options)
- [ Regenerating results ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/regenerating-results)
- [ Handling failures ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/handling-failures)
- [ Listening for events ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/events)
- [ Using your own model ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/using-your-own-model)
- [ Customizing the job ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/customizing-the-job)
- [ Overriding actions ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/overriding-actions)
- [ Testing transformers ](https://spatie.be/docs/laravel-url-ai-transformer/v2/advanced-usage/testing-transformers)

 Customizing AI models
=====================

###  On this page 

1. [ Choosing a provider and model per transformer ](#content-choosing-a-provider-and-model-per-transformer)
2. [ A cheapest or smartest default model ](#content-a-cheapest-or-smartest-default-model)
3. [ Transformers without AI ](#content-transformers-without-ai)

By default, transformers use the provider and cheapest model configured in `config/url-ai-transformer.php`. You can override either one for a specific transformer, tune other AI options, or skip AI entirely.

This package uses the official [Laravel AI](https://github.com/laravel/ai) package under the hood for all AI interactions. Because every transformer is a Laravel AI agent, you can tune options like temperature and max tokens by adding attributes to the transformer class.

```
use Laravel\Ai\Attributes\MaxTokens;
use Laravel\Ai\Attributes\Temperature;
use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;
use Stringable;

#[MaxTokens(1000)]
#[Temperature(0.1)]
class PreciseTransformer extends Transformer
{
    public function instructions(): Stringable|string
    {
        return 'Extract the key facts as accurately as possible.';
    }
}

#[MaxTokens(2000)]
#[Temperature(0.8)]
class CreativeTransformer extends Transformer
{
    public function instructions(): Stringable|string
    {
        return 'Rewrite this webpage as an engaging summary.';
    }
}
```

Choosing a provider and model per transformer
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

By default, transformers use the `provider` and `model` from `config/url-ai-transformer.php`. A transformer can override these values with Laravel AI attributes or methods.

Use `#[UseCheapestModel]` or `#[UseSmartestModel]` to let the provider pick the right model without naming it. The configured provider is respected, only the model changes.

```
use Laravel\Ai\Attributes\UseSmartestModel;
use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;
use Stringable;

#[UseSmartestModel]
class ThoroughTransformer extends Transformer
{
    public function instructions(): Stringable|string
    {
        return 'Produce a detailed, high quality summary.';
    }
}
```

Use `#[Model]` to pin a specific model, or `#[Provider]` to use a different provider entirely (in which case its default model is used unless you also add `#[Model]`).

```
use Laravel\Ai\Attributes\Model;
use Laravel\Ai\Attributes\Provider;
use Laravel\Ai\Enums\Lab;
use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;
use Stringable;

#[Provider(Lab::Anthropic)]
#[Model('claude-haiku-4-5-20251001')]
class ClaudeTransformer extends Transformer
{
    public function instructions(): Stringable|string
    {
        return 'Summarize this webpage.';
    }
}
```

Methods are useful when the provider or model needs to be selected dynamically. Laravel AI checks these methods before its attributes:

```
use Laravel\Ai\Enums\Lab;
use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;
use Stringable;

class ClaudeTransformer extends Transformer
{
    public function provider(): Lab
    {
        return Lab::Anthropic;
    }

    public function model(): string
    {
        return 'claude-haiku-4-5-20251001';
    }

    public function instructions(): Stringable|string
    {
        return 'Summarize this webpage.';
    }
}
```

If you define `provider()` without `model()`, Laravel AI uses that provider's default model.

For detailed information about the available providers, models, and options, check out the [Laravel AI documentation](https://github.com/laravel/ai).

A cheapest or smartest default model
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

The `model` in your config may be a plain string, or one of the `Model` enum cases. `Model::Cheapest` and `Model::Smartest` let the configured provider pick the model for you, so you don't have to track model names.

```
// config/url-ai-transformer.php
use Laravel\Ai\Enums\Lab;
use Spatie\LaravelUrlAiTransformer\Enums\Model;

'ai' => [
    'provider' => Lab::OpenAI,
    'model' => Model::Cheapest,
],
```

Individual transformers can still override this with the attributes above.

Transformers without AI
-----------------------------------------------------------------------------------------------------------------------------

Not all transformers need to use AI. You can create transformers that process content using traditional methods:

```
use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;

class WordCountTransformer extends Transformer
{
    public function transform(): void
    {
        $wordCount = str_word_count(strip_tags($this->urlContent));

        $this->transformationResult->result = (string) $wordCount;
    }
}
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
