Writing your own transformers | 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/v1)  Basic-usage  Writing your own transformers

 Version   v1

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

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

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

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

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

- [ Testing transformers ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/testing-transformers)
- [ Conditional transformations ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/conditional-transformations)
- [ Crawling URLs ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/crawling-urls)
- [ Customizing AI models ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/customizing-ai-models)
- [ Exploring command options ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/exploring-command-options)
- [ Overriding actions ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/overriding-actions)
- [ Regenerating results ](https://spatie.be/docs/laravel-url-ai-transformer/v1/advanced-usage/regenerating-results)

 Writing your own transformers
=============================

###  On this page

1. [ Creating a basic transformer ](#content-creating-a-basic-transformer)
2. [ Custom transformer types ](#content-custom-transformer-types)

The real power of this package comes from writing your own transformers. Let's explore how to create custom transformers that fit your specific needs.

Creating a basic transformer
--------------------------------------------------------------------------------------------------------------------------------------------

All transformers extend the `Transformer` base class and implement two required methods `transform` and `getPrompt`.

```
// app/Transformers/SummaryTransformer.php
namespace App\Transformers;

use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;
use Prism\Prism\Prism;
use Spatie\LaravelUrlAiTransformer\Support\Config;

class SummaryTransformer extends Transformer
{
    public function transform(): void
    {
        $response = Prism::text()
            ->using(Config::aiProvider(), Config::aiModel())
            ->withPrompt($this->getPrompt())
            ->asText();

        // you should set the result property on the transformation result model to store the result
        $this->transformationResult->result = $response->text;
    }

    public function getPrompt(): string
    {
        return "Summarize this webpage content in 3 concise bullet points: \n\n"
            . $this->urlContent;
    }
}
```

You can now use your transformer:

```
Transform::urls('https://example.com/article')
    ->usingTransformers(new SummaryTransformer);
```

When a transformer runs, it has access to three properties:

- `$this->url` - The URL being transformed
- `$this->urlContent` - The fetched content from the URL
- `$this->transformationResult` - The database model where you store results

Custom transformer types
--------------------------------------------------------------------------------------------------------------------------------

By default, the transformer type is derived from the class name. You can override this:

```
class MyCustomTransformer extends Transformer
{
    public function type(): string
    {
        return 'customType';
    }

    // Other methods...
}
```

You can use your custom type when retrieving a transformation result:

```
$ldJsonData = TransformationResult::forUrl('https://spatie.be/blog', 'customType');

```
