Skip to main content

Overview

LLM Magic includes several prebuilt tools for common operations like extraction, mathematical calculations, and data manipulation.

Extract

The Extract tool is used to output structured data according to a defined JSON schema. This is the primary way to extract structured information from unstructured text.
use Mateffy\Magic\Tools\Prebuilt\Extract;

$schema = [
    'type' => 'object',
    'properties' => [
        'name' => ['type' => 'string'],
        'age' => ['type' => 'integer'],
        'email' => ['type' => 'string', 'format' => 'email'],
    ],
    'required' => ['name', 'email'],
];

Magic::chat()
    ->tools(new Extract($schema))
    ->prompt('Extract contact info: John Doe, 30 years old, [email protected]')
    ->ask();

Constructor

schema
array
required
The JSON schema that defines the structure of data to extract

Behavior

When invoked, the Extract tool ends the chat session and returns the extracted data. The tool validates the extracted data against the provided schema.

MagicReturnTool

A flexible tool for returning values of specific types. Useful when you want the LLM to return a simple value.
use Mateffy\Magic\Tools\Prebuilt\MagicReturnTool;

// Return a string
$result = Magic::chat()
    ->tools(new MagicReturnTool('string'))
    ->prompt('Summarize this in one sentence: [long text]')
    ->ask();

// Return a number
$result = Magic::chat()
    ->tools(new MagicReturnTool('number'))
    ->prompt('How many words are in this text?')
    ->ask();

// Return a boolean
$result = Magic::chat()
    ->tools(new MagicReturnTool('boolean'))
    ->prompt('Is this text positive in sentiment?')
    ->ask();

Constructor

type
string
required
The type of value to return: string, number, integer, boolean, array, or object
schema
array
Additional schema constraints for the returned value

Supported Types

  • string - Returns a string value
  • number - Returns a float value
  • integer - Returns an integer value
  • boolean - Returns a boolean value
  • array - Returns an array (can specify item schema)
  • object - Returns an object (provide full schema)
// Return an array of strings
new MagicReturnTool('array', ['type' => 'string'])

// Return an object
new MagicReturnTool('object', [
    'type' => 'object',
    'properties' => [
        'summary' => ['type' => 'string'],
        'wordCount' => ['type' => 'integer'],
    ],
])

Add

A simple tool for adding two numbers.
use Mateffy\Magic\Tools\Prebuilt\Add;

Magic::chat()
    ->tools(new Add())
    ->prompt('What is 15 plus 27?')
    ->ask();

Parameters

a
number
required
The first number to add
b
number
required
The second number to add

Returns

The sum of a and b.

Multiply

A simple tool for multiplying two numbers.
use Mateffy\Magic\Tools\Prebuilt\Multiply;

Magic::chat()
    ->tools(new Multiply())
    ->prompt('What is 6 times 7?')
    ->ask();

Parameters

a
number
required
The first number to multiply
b
number
required
The second number to multiply

Returns

The product of a and b.

Finish

Outputs a final text message and ends the conversation.
use Mateffy\Magic\Tools\Prebuilt\Finish;

Magic::chat()
    ->tools(new Finish())
    ->prompt('Generate a conclusion for this essay')
    ->ask();

Parameters

text
string
The final message to output

MergeData

Merges multiple data sources together according to a schema.
use Mateffy\Magic\Tools\Prebuilt\MergeData;

$schema = [
    'type' => 'object',
    'properties' => [
        'products' => [
            'type' => 'array',
            'items' => [
                'type' => 'object',
                'properties' => [
                    'name' => ['type' => 'string'],
                    'price' => ['type' => 'number'],
                ],
            ],
        ],
    ],
];

Magic::chat()
    ->tools(new MergeData($schema))
    ->prompt('Merge these product lists: [data1] and [data2]')
    ->ask();

Constructor

schema
array
required
The JSON schema defining the structure of merged data

ModifyData

Modifies data according to instructions.
use Mateffy\Magic\Tools\Prebuilt\ModifyData;

$schema = [
    'type' => 'object',
    'properties' => [
        'items' => [
            'type' => 'array',
            'items' => ['type' => 'object'],
        ],
    ],
];

Magic::chat()
    ->tools(new ModifyData($schema))
    ->prompt('Modify the data to add a "category" field to each item')
    ->ask();

Constructor

schema
array
required
The JSON schema defining the structure of modified data

RemoveDuplicates

Removes duplicate entries from arrays in a dataset.
use Mateffy\Magic\Tools\Prebuilt\RemoveDuplicates;

Magic::chat()
    ->tools(new RemoveDuplicates())
    ->prompt('Remove duplicates from the products list')
    ->ask();

Parameters

keys
array
required
Array of dot-notated keys indicating which arrays to deduplicate. Uses 0-based indices.Example: ["products.0", "categories.17"]

Returns

Returns the keys to deduplicate. The actual deduplication is handled by your application code.

OutputJsonSchema

Generates a JSON schema based on natural language instructions.
use Mateffy\Magic\Tools\Prebuilt\OutputJsonSchema;

Magic::chat()
    ->tools(new OutputJsonSchema())
    ->prompt('Create a schema for a user profile with name, email, age, and address')
    ->ask();

Returns

schema
object
required
The generated JSON schema. Must be a JSON object at the root level.Properties:
  • type (string) - Must be “object”
  • properties (object) - The schema properties
  • required (array) - Array of required property names

OutputStepLabels

Generates UI labels for a multi-step process.
use Mateffy\Magic\Tools\Prebuilt\OutputStepLabels;

Magic::chat()
    ->tools(new OutputStepLabels())
    ->prompt('Create UI labels for a product extraction wizard')
    ->ask();

Returns

Returns an object with labels for:
introduction_view_heading
string
required
Heading for the introduction view
introduction_view_description
string
required
Description for the introduction view
introduction_view_next_button_label
string
required
Label for the next button
bucket_view_heading
string
required
Heading for the bucket view
bucket_view_description
string
required
Description for the bucket view
bucket_view_back_button_label
string
required
Label for the back button
bucket_view_begin_button_label
string
required
Label for the begin button
bucket_view_continue_button_label
string
required
Label for the continue button
extraction_view_heading
string
required
Heading for the extraction view
extraction_view_description
string
required
Description for the extraction view
extraction_view_back_button_label
string
required
Label for the back button
extraction_view_continue_button_label
string
required
Label for the continue button
extraction_view_restart_button_label
string
required
Label for the restart button
results_view_heading
string
required
Heading for the results view
results_view_description
string
required
Description for the results view
results_view_back_button_label
string
required
Label for the back button
results_view_next_button_label
string
required
Label for the next button

Build docs developers (and LLMs) love