Get up and running with Prism in minutes with this quick tutorial
This guide will walk you through creating your first AI-powered feature with Prism. We’ll build a simple example that generates text, handles multi-modal input, and uses tools.
Let’s start with the simplest possible example - generating text from a prompt:
use Prism\Prism\Facades\Prism;use Prism\Prism\Enums\Provider;$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt('Tell me a short story about a brave knight.') ->asText();echo $response->text;
System prompts help set the behavior and context for the AI:
use Prism\Prism\Facades\Prism;use Prism\Prism\Enums\Provider;$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withSystemPrompt('You are a helpful coding assistant who explains concepts clearly.') ->withPrompt('What is dependency injection?') ->asText();echo $response->text;
You can use Laravel views for complex prompts: ->withSystemPrompt(view('prompts.system'))
Prism makes it easy to analyze images with multi-modal models:
use Prism\Prism\Facades\Prism;use Prism\Prism\Enums\Provider;use Prism\Prism\ValueObjects\Media\Image;$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt( 'What objects do you see in this image?', [Image::fromLocalPath('/path/to/image.jpg')] ) ->asText();echo $response->text;
You can include multiple images and other media types:
use Prism\Prism\ValueObjects\Media\Image;use Prism\Prism\ValueObjects\Media\Document;$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt( 'Compare this image with the information in this document', [ Image::fromLocalPath('/path/to/chart.png'), Document::fromLocalPath('/path/to/report.pdf') ] ) ->asText();
Create interactive conversations by chaining messages:
use Prism\Prism\Facades\Prism;use Prism\Prism\Enums\Provider;use Prism\Prism\ValueObjects\Messages\UserMessage;use Prism\Prism\ValueObjects\Messages\AssistantMessage;$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withMessages([ new UserMessage('What is JSON?'), new AssistantMessage('JSON is a lightweight data format...'), new UserMessage('Can you show me an example?') ]) ->asText();echo $response->text;