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.
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.
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 generatedecho $response;
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.