Skip to main content

Quick Start

This guide will help you get up and running with LLM Magic quickly. You’ll learn the basics of asking questions, chatting with LLMs, and extracting structured data.

Basic Question Answering

The simplest way to interact with an LLM is using Magic::ask():
use Mateffy\Magic;

$answer = Magic::ask('What is the capital of France?');
// -> "The capital of France is Paris."

Customizing Responses

By default, Magic::ask() returns full sentence responses. You can control this behavior:
// Returns: "The capital of France is Paris."
$answer = Magic::ask('What is the capital of France?', sentence: true);

Advanced Chat Conversations

For more complex interactions, use Magic::chat() to build conversations:

Simple Chat

use Mateffy\Magic;

$response = Magic::chat()
    ->model('google/gemini-2.0-flash-lite')
    ->prompt('What is the capital of France?')
    ->stream()
    ->text();

Multi-Turn Conversations

Build conversations with multiple messages:
use Mateffy\Magic;
use Mateffy\Magic\Chat\Messages\Step;

$messages = Magic::chat()
    ->model('google/gemini-2.0-flash-lite')
    ->temperature(0.5)
    ->messages([
        Step::user([
            Step\Text::make('What is in this picture and where was it taken?'),
            Step\Image::make('https://example.com/eiffel-tower.jpg'),
        ]),
        Step::assistant([
            Step\Text::make('The picture shows the Eiffel Tower, which is located in Paris, France.'),
        ]),
        Step::user('How much is a flight to Paris?'),
    ]);
LLM Magic supports multi-modal inputs including text and images. Different models have different capabilities, so check your chosen model’s documentation.

Streaming Responses

Stream responses in real-time for better user experience:
$response = Magic::chat()
    ->model('google/gemini-2.0-flash-lite')
    ->prompt('Write a short poem about PHP')
    ->stream()
    ->text();

// The response will stream as it's generated
echo $response;

Function Calling with Tools

LLM Magic makes it easy to give LLMs access to custom functions:
use Mateffy\Magic;
use Mateffy\Magic\Chat\Messages\Step;
use Mateffy\Magic\Chat\Tool;

$messages = Magic::chat()
    ->model('google/gemini-2.0-flash-lite')
    ->messages([
        Step::user('How much is a flight to Paris?'),
    ])
    ->tools([
        Tool::make('search_flight')
            ->description('Search for flights to a given destination. Pass the departure airport code and the destination airport code in the ISO 3166-1 alpha-3 format.')
            ->callback(fn (string $from_airport_code, string $to_airport_code) {
                return app(FlightService::class)
                    ->search($from_airport_code, $to_airport_code)
                    ->toArray();
            }),
    ])
    ->send();
When you provide tools, the LLM can call them during the conversation to fetch data or perform actions. The results are automatically fed back into the conversation.

Structured Data Extraction

Extract structured, validated JSON data from various inputs:
use Mateffy\Magic;

$data = Magic::extract()
    ->schema([
        'type' => 'object',
        'properties' => [
            'name' => ['type' => 'string'],
            'age' => ['type' => 'integer'],
        ],
        'required' => ['name', 'age'],
    ])
    ->artifacts([$artifact])
    ->send();
The extraction API supports:
  • PDFs
  • Word documents
  • Images
  • Spreadsheets
  • Plain text

Learn More About Extraction

Explore advanced extraction strategies and techniques

Next Steps

Now that you know the basics, explore more advanced features:

Configuration

Learn about all configuration options and environment variables

Chat API

Dive deep into the chat API and all its options

Tools & Functions

Master function calling and custom tools

Data Extraction

Learn about extraction strategies and working with documents

Build docs developers (and LLMs) love