Skip to main content
The Magic facade provides the primary interface for interacting with LLM Magic. It offers convenient static methods for chat, extraction, embeddings, and quick question-answering operations.

Methods

ask

Quickly ask a question and get a structured or simple response.
Magic::ask(
    prompt: 'What is the capital of France?',
    schema: ['type' => 'string'],
    model: null,
    sentence: true
)
prompt
string
required
The question or prompt to ask the LLM
schema
array
default:"['type' => 'string']"
JSON schema defining the expected response type. Can be ‘string’, ‘number’, ‘boolean’, ‘array’, or ‘object’ with properties
model
string|LLM|null
default:"null"
The model to use. If null, uses the default configured model
sentence
bool
default:"true"
Whether to respond with a full sentence (true) or just the answer (false)
return
mixed
The extracted answer matching the specified schema type

chat

Create a new chat builder for conversational interactions.
Magic::chat()
return
ChatPreconfiguredModelBuilder
A new chat builder instance for configuring and executing chat operations

extract

Create a new extraction builder for extracting structured data from documents.
Magic::extract()
return
ExtractionLLMBuilder
A new extraction builder instance for configuring and executing extraction operations

embeddings

Create a new embeddings builder for generating text embeddings.
Magic::embeddings(
    input: null,
    model: null
)
input
Closure|string|null
default:"null"
The text input to generate embeddings for, or a closure that returns the input
model
OpenAIEmbeddings|null
default:"null"
The embedding model to use. If null, uses the default configured model
return
EmbeddingsBuilder
A new embeddings builder instance

function

Use the LLM as a function that returns a specific type of value.
Magic::function(
    description: 'Return the year when the song "Bohemian Rhapsody" was released',
    type: 'number',
    schema: null,
    llm: null
)
description
string
required
Description of what the function should return
type
'string'|'number'|'boolean'|'array'|'object'
required
The type of value to return
schema
array|null
default:"null"
Optional JSON schema for complex types (array or object)
llm
LLM|string|null
default:"null"
The model to use. If null, uses the default configured model
return
mixed
The value returned by the LLM function
throws
UnableToActAsFunction
Thrown when the LLM cannot complete the requested task
throws
UnknownInferenceException
Thrown when the LLM returns an invalid response

models

Get all registered models in a collection.
Magic::models()
return
Collection<string, string>
Collection of model keys and labels, sorted alphabetically by key

error

Create a tool call exception for use in tool implementations.
Magic::error(
    message: 'Invalid parameter value',
    code: null,
    previous: null
)
message
string
required
The error message
code
string|null
default:"null"
Optional error code
previous
Throwable|null
default:"null"
Previous exception for exception chaining
return
ToolCallException
A new tool call exception instance

end

Create an end conversation signal with output.
Magic::end($output)
output
mixed
required
The output value to return when ending the conversation
return
EndConversation
An end conversation signal object

Helper Methods

defaultModelName

Get the configured default model name.
Magic::defaultModelName()
return
string
The default model name from configuration

defaultModelLabel

Get the human-readable label for the default model.
Magic::defaultModelLabel()
return
string
The default model’s display label

defaultModel

Get the default model instance.
Magic::defaultModel()
return
LLM
The default LLM model instance

getExtractionStrategies

Get all registered extraction strategies.
Magic::getExtractionStrategies()
return
Collection<string, class-string<Strategy>>
Collection of strategy keys and class names

registerStrategy

Register a custom extraction strategy.
Magic::registerStrategy(
    key: 'my-custom-strategy',
    strategy: MyCustomStrategy::class
)
key
string
required
The unique identifier for the strategy
strategy
class-string<Strategy>
required
The fully qualified class name of the strategy implementation

Build docs developers (and LLMs) love